From f77e17e4d2699e3d257cb0b603a0b4c1f0fde344 Mon Sep 17 00:00:00 2001 From: bvr-yr <130279855+bvr-yr@users.noreply.github.com> Date: Thu, 21 Mar 2024 18:19:12 +0300 Subject: [PATCH] label: add `$LAYOUT` variable (#211) * label: add `$LAYOUT` variable * add exclamation mark as wildcard to hide layout --- src/core/hyprlock.cpp | 5 +++++ src/core/hyprlock.hpp | 2 ++ src/renderer/widgets/IWidget.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/core/hyprlock.cpp b/src/core/hyprlock.cpp index 26b9421..4203350 100644 --- a/src/core/hyprlock.cpp +++ b/src/core/hyprlock.cpp @@ -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); } diff --git a/src/core/hyprlock.hpp b/src/core/hyprlock.hpp index cb25712..f474220 100644 --- a/src/core/hyprlock.hpp +++ b/src/core/hyprlock.hpp @@ -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; diff --git a/src/renderer/widgets/IWidget.cpp b/src/renderer/widgets/IWidget.cpp index d9519de..07bdea1 100644 --- a/src/renderer/widgets/IWidget.cpp +++ b/src/renderer/widgets/IWidget.cpp @@ -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));