From 40e6360722acc32ea1b266f361666a4cc64d9608 Mon Sep 17 00:00:00 2001 From: Grant Moyer Date: Sun, 7 Feb 2021 18:57:12 -0500 Subject: [PATCH] Update css class when locked/unlocked --- src/modules/keyboard_state.cpp | 35 +++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/modules/keyboard_state.cpp b/src/modules/keyboard_state.cpp index b51617f..516502f 100644 --- a/src/modules/keyboard_state.cpp +++ b/src/modules/keyboard_state.cpp @@ -99,19 +99,28 @@ auto waybar::modules::KeyboardState::update() -> void { int capsl = libevdev_get_event_value(dev_, EV_LED, LED_CAPSL); int scrolll = libevdev_get_event_value(dev_, EV_LED, LED_SCROLLL); - std::string text; - text = fmt::format(numlock_format_, - fmt::arg("icon", numl ? icon_locked_ : icon_unlocked_), - fmt::arg("name", "Num")); - numlock_label_.set_markup(text); - text = fmt::format(capslock_format_, - fmt::arg("icon", capsl ? icon_locked_ : icon_unlocked_), - fmt::arg("name", "Caps")); - capslock_label_.set_markup(text); - text = fmt::format(scrolllock_format_, - fmt::arg("icon", scrolll ? icon_locked_ : icon_unlocked_), - fmt::arg("name", "Scroll")); - scrolllock_label_.set_markup(text); + struct { + bool state; + Gtk::Label& label; + const std::string& format; + const char* name; + } label_states[] = { + {(bool) numl, numlock_label_, numlock_format_, "Num"}, + {(bool) capsl, capslock_label_, capslock_format_, "Caps"}, + {(bool) scrolll, scrolllock_label_, scrolllock_format_, "Scroll"}, + }; + for (auto& label_state : label_states) { + std::string text; + text = fmt::format(label_state.format, + fmt::arg("icon", label_state.state ? icon_locked_ : icon_unlocked_), + fmt::arg("name", label_state.name)); + label_state.label.set_markup(text); + if (label_state.state) { + label_state.label.get_style_context()->add_class("locked"); + } else { + label_state.label.get_style_context()->remove_class("locked"); + } + } AModule::update(); }