label: add `$LAYOUT` variable (#211)

* label: add `$LAYOUT` variable

* add exclamation mark as wildcard to hide layout
This commit is contained in:
bvr-yr 2024-03-21 18:19:12 +03:00 committed by GitHub
parent 54da0cae0f
commit f77e17e4d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 0 deletions

View File

@ -675,6 +675,11 @@ static void handleKeyboardModifiers(void* data, wl_keyboard* wl_keyboard, uint s
if (!g_pHyprlock->m_pXKBState)
return;
if (group != g_pHyprlock->m_uiActiveLayout) {
g_pHyprlock->m_uiActiveLayout = group;
forceUpdateTimers();
}
xkb_state_update_mask(g_pHyprlock->m_pXKBState, mods_depressed, mods_latched, mods_locked, 0, 0, group);
}

View File

@ -79,6 +79,8 @@ class CHyprlock {
xkb_keymap* m_pXKBKeymap = nullptr;
xkb_state* m_pXKBState = nullptr;
xkb_layout_index_t m_uiActiveLayout = 0;
bool m_bTerminate = false;
bool m_bLocked = false;

View File

@ -72,6 +72,27 @@ static void replaceAllAttempts(std::string& str) {
}
}
static void replaceAllLayout(std::string& str) {
const auto LAYOUTIDX = g_pHyprlock->m_uiActiveLayout;
const auto LAYOUTNAME = xkb_keymap_layout_get_name(g_pHyprlock->m_pXKBKeymap, LAYOUTIDX);
const std::string STR = LAYOUTNAME ? LAYOUTNAME : "error";
size_t pos = 0;
while ((pos = str.find("$LAYOUT", pos)) != std::string::npos) {
if (str.substr(pos, 8).ends_with('[') && str.substr(pos).contains(']')) {
const std::string REPL = str.substr(pos + 8, str.find_first_of(']', pos) - 8 - pos);
const CVarList LANGS(REPL);
const std::string LANG = LANGS[LAYOUTIDX].empty() ? STR : LANGS[LAYOUTIDX] == "!" ? "" : LANGS[LAYOUTIDX];
str.replace(pos, 9 + REPL.length(), LANG);
pos += LANG.length();
} else {
str.replace(pos, 7, STR);
pos += STR.length();
}
}
}
static std::string getTime() {
const auto current_zone = std::chrono::current_zone();
const auto HHMMSS = std::chrono::hh_mm_ss{current_zone->to_local(std::chrono::system_clock::now()) -
@ -109,6 +130,11 @@ IWidget::SFormatResult IWidget::formatString(std::string in) {
result.allowForceUpdate = true;
}
if (in.contains("$LAYOUT")) {
replaceAllLayout(in);
result.allowForceUpdate = true;
}
if (in.starts_with("cmd[") && in.contains("]")) {
// this is a command
CVarList vars(in.substr(4, in.find_first_of(']') - 4));