From 0ead42e52b9839a12364aa35c06b40b6c5309357 Mon Sep 17 00:00:00 2001 From: Azazel Date: Sun, 25 Feb 2024 22:55:30 +0000 Subject: [PATCH 1/8] feat: improve search of .desktop files --- src/AAppIconLabel.cpp | 49 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/src/AAppIconLabel.cpp b/src/AAppIconLabel.cpp index a238143b..1ceed73b 100644 --- a/src/AAppIconLabel.cpp +++ b/src/AAppIconLabel.cpp @@ -24,18 +24,57 @@ AAppIconLabel::AAppIconLabel(const Json::Value& config, const std::string& name, image_.set_pixel_size(app_icon_size_); } +std::string to_lowercase(const std::string& input) { + std::string result = input; + std::transform(result.begin(), result.end(), result.begin(), + [](unsigned char c) { return std::tolower(c); }); + return result; +} + +std::optional getFileBySuffix(const std::string& dir, const std::string& suffix, + bool check_lower_case) { + if (!std::filesystem::exists(dir)) { + return {}; + } + for (const auto& entry : std::filesystem::recursive_directory_iterator(dir)) { + if (entry.is_regular_file()) { + std::string filename = entry.path().filename().string(); + if (filename.size() < suffix.size()) { + continue; + } + if ((filename.compare(filename.size() - suffix.size(), suffix.size(), suffix) == 0) || + (check_lower_case && filename.compare(filename.size() - suffix.size(), suffix.size(), + to_lowercase(suffix)) == 0)) { + return entry.path().string(); + } + } + } + + return {}; +} + +std::optional getFileBySuffix(const std::string& dir, const std::string& suffix) { + return getFileBySuffix(dir, suffix, false); +} + std::optional getDesktopFilePath(const std::string& app_identifier, const std::string& alternative_app_identifier) { const auto data_dirs = Glib::get_system_data_dirs(); for (const auto& data_dir : data_dirs) { - const auto data_app_dir = data_dir + "applications/"; - auto desktop_file_path = data_app_dir + app_identifier + ".desktop"; - if (std::filesystem::exists(desktop_file_path)) { + const auto data_app_dir = data_dir + "/applications/"; + auto desktop_file_suffix = app_identifier + ".desktop"; + // searching for file by suffix catches cases like terminal emulator "foot" where class is + // "footclient" and desktop file is named "org.codeberg.dnkl.footclient.desktop" + auto desktop_file_path = getFileBySuffix(data_app_dir, desktop_file_suffix, true); + // "true" argument allows checking for lowercase - this catches cases where class name is + // "LibreWolf" and desktop file is named "librewolf.desktop" + if (desktop_file_path.has_value()) { return desktop_file_path; } if (!alternative_app_identifier.empty()) { - desktop_file_path = data_app_dir + alternative_app_identifier + ".desktop"; - if (std::filesystem::exists(desktop_file_path)) { + desktop_file_suffix = alternative_app_identifier + ".desktop"; + desktop_file_path = getFileBySuffix(data_app_dir, desktop_file_suffix, true); + if (desktop_file_path.has_value()) { return desktop_file_path; } } From 3a5aa5ee832a8f201f0ffe721ab3ee1774f8c143 Mon Sep 17 00:00:00 2001 From: Azazel Date: Sun, 25 Feb 2024 22:56:52 +0000 Subject: [PATCH 2/8] feat: improve default spacing and add to config --- src/AIconLabel.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/AIconLabel.cpp b/src/AIconLabel.cpp index a7e2380a..ef379ff3 100644 --- a/src/AIconLabel.cpp +++ b/src/AIconLabel.cpp @@ -10,7 +10,14 @@ AIconLabel::AIconLabel(const Json::Value &config, const std::string &name, const : ALabel(config, name, id, format, interval, ellipsize, enable_click, enable_scroll) { event_box_.remove(); box_.set_orientation(Gtk::Orientation::ORIENTATION_HORIZONTAL); - box_.set_spacing(8); + + // set aesthetic default spacing + int spacing = config_["icon-spacing"].isInt() ? config_["icon-spacing"].asInt() : -5; + box_.set_spacing(spacing); + + int margin_top = config_["margin-top"].isInt() ? config_["margin-top"].asInt() : 6; + box_.set_margin_top(margin_top); + box_.add(image_); box_.add(label_); event_box_.add(box_); From 16aced7f9ffcac1200473192712575afaa4e6513 Mon Sep 17 00:00:00 2001 From: Azazel Date: Mon, 26 Feb 2024 04:07:03 +0000 Subject: [PATCH 3/8] feat: move name and classes from label_ to box_ --- src/AIconLabel.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/AIconLabel.cpp b/src/AIconLabel.cpp index ef379ff3..051654df 100644 --- a/src/AIconLabel.cpp +++ b/src/AIconLabel.cpp @@ -9,17 +9,20 @@ AIconLabel::AIconLabel(const Json::Value &config, const std::string &name, const bool enable_click, bool enable_scroll) : ALabel(config, name, id, format, interval, ellipsize, enable_click, enable_scroll) { event_box_.remove(); + label_.unset_name(); + label_.get_style_context()->remove_class(MODULE_CLASS); + box_.get_style_context()->add_class(MODULE_CLASS); + if (!id.empty()) { + label_.get_style_context()->remove_class(id); + box_.get_style_context()->add_class(id); + } + box_.set_orientation(Gtk::Orientation::ORIENTATION_HORIZONTAL); - - // set aesthetic default spacing - int spacing = config_["icon-spacing"].isInt() ? config_["icon-spacing"].asInt() : -5; - box_.set_spacing(spacing); - - int margin_top = config_["margin-top"].isInt() ? config_["margin-top"].asInt() : 6; - box_.set_margin_top(margin_top); + box_.set_name(name); box_.add(image_); box_.add(label_); + event_box_.add(box_); } From 695c7863544dbb1d6e7c009bed71078f33350377 Mon Sep 17 00:00:00 2001 From: Azazel Date: Mon, 26 Feb 2024 04:17:45 +0000 Subject: [PATCH 4/8] refactor: reuse toLowerCase function --- src/AAppIconLabel.cpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/AAppIconLabel.cpp b/src/AAppIconLabel.cpp index 1ceed73b..0dd87425 100644 --- a/src/AAppIconLabel.cpp +++ b/src/AAppIconLabel.cpp @@ -24,7 +24,7 @@ AAppIconLabel::AAppIconLabel(const Json::Value& config, const std::string& name, image_.set_pixel_size(app_icon_size_); } -std::string to_lowercase(const std::string& input) { +std::string toLowerCase(const std::string& input) { std::string result = input; std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::tolower(c); }); @@ -44,7 +44,7 @@ std::optional getFileBySuffix(const std::string& dir, const std::st } if ((filename.compare(filename.size() - suffix.size(), suffix.size(), suffix) == 0) || (check_lower_case && filename.compare(filename.size() - suffix.size(), suffix.size(), - to_lowercase(suffix)) == 0)) { + toLowerCase(suffix)) == 0)) { return entry.path().string(); } } @@ -97,16 +97,9 @@ std::optional getIconName(const std::string& app_identifier, return app_identifier_desktop; } - const auto to_lower = [](const std::string& str) { - auto str_cpy = str; - std::transform(str_cpy.begin(), str_cpy.end(), str_cpy.begin(), - [](unsigned char c) { return std::tolower(c); }); - return str; - }; - const auto first_space = app_identifier.find_first_of(' '); if (first_space != std::string::npos) { - const auto first_word = to_lower(app_identifier.substr(0, first_space)); + const auto first_word = toLowerCase(app_identifier.substr(0, first_space)); if (DefaultGtkIconThemeWrapper::has_icon(first_word)) { return first_word; } @@ -114,7 +107,7 @@ std::optional getIconName(const std::string& app_identifier, const auto first_dash = app_identifier.find_first_of('-'); if (first_dash != std::string::npos) { - const auto first_word = to_lower(app_identifier.substr(0, first_dash)); + const auto first_word = toLowerCase(app_identifier.substr(0, first_dash)); if (DefaultGtkIconThemeWrapper::has_icon(first_word)) { return first_word; } From a7d8b1bacf08b717cb447c54e2c902bdffb24166 Mon Sep 17 00:00:00 2001 From: Azazel Date: Mon, 26 Feb 2024 20:58:38 +0000 Subject: [PATCH 5/8] feat: re-add default and configurable icon spacing --- src/AIconLabel.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/AIconLabel.cpp b/src/AIconLabel.cpp index 051654df..ee68a22e 100644 --- a/src/AIconLabel.cpp +++ b/src/AIconLabel.cpp @@ -20,6 +20,9 @@ AIconLabel::AIconLabel(const Json::Value &config, const std::string &name, const box_.set_orientation(Gtk::Orientation::ORIENTATION_HORIZONTAL); box_.set_name(name); + int spacing = config_["icon-spacing"].isInt() ? config_["icon-spacing"].asInt() : 6; + box_.set_spacing(spacing); + box_.add(image_); box_.add(label_); From c59bb509bd4585af8941db66e357b0bf9b08b2de Mon Sep 17 00:00:00 2001 From: Azazel Date: Mon, 26 Feb 2024 21:00:16 +0000 Subject: [PATCH 6/8] fix: hide icon if window is unfocused --- include/modules/hyprland/window.hpp | 1 + src/modules/hyprland/window.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/include/modules/hyprland/window.hpp b/include/modules/hyprland/window.hpp index 593e3422..f2c266bd 100644 --- a/include/modules/hyprland/window.hpp +++ b/include/modules/hyprland/window.hpp @@ -59,6 +59,7 @@ class Window : public waybar::AAppIconLabel, public EventHandler { bool allFloating_; bool swallowing_; bool fullscreen_; + bool focused_; }; } // namespace waybar::modules::hyprland diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index c7d287e5..ec151a7b 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -62,6 +62,12 @@ auto Window::update() -> void { label_.hide(); } + if (focused_) { + image_.show(); + } else { + image_.hide(); + } + setClass("empty", workspace_.windows == 0); setClass("solo", solo_); setClass("floating", allFloating_); @@ -137,13 +143,16 @@ void Window::queryActiveWorkspace() { workspace_ = getActiveWorkspace(); } + focused_ = true; if (workspace_.windows > 0) { const auto clients = gIPC->getSocket1JsonReply("clients"); assert(clients.isArray()); auto activeWindow = std::find_if(clients.begin(), clients.end(), [&](Json::Value window) { return window["address"] == workspace_.last_window; }); + if (activeWindow == std::end(clients)) { + focused_ = false; return; } @@ -185,6 +194,7 @@ void Window::queryActiveWorkspace() { soloClass_ = ""; } } else { + focused_ = false; windowData_ = WindowData{}; allFloating_ = false; swallowing_ = false; From 615c9050e7f76537dab6286764337913298cdf0b Mon Sep 17 00:00:00 2001 From: Azazel Date: Mon, 26 Feb 2024 22:52:28 +0000 Subject: [PATCH 7/8] fix: prevent icon showing when app_identifier is empty --- src/AAppIconLabel.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/AAppIconLabel.cpp b/src/AAppIconLabel.cpp index 0dd87425..e64e6daa 100644 --- a/src/AAppIconLabel.cpp +++ b/src/AAppIconLabel.cpp @@ -59,6 +59,10 @@ std::optional getFileBySuffix(const std::string& dir, const std::st std::optional getDesktopFilePath(const std::string& app_identifier, const std::string& alternative_app_identifier) { + if (app_identifier.empty()) { + return {}; + } + const auto data_dirs = Glib::get_system_data_dirs(); for (const auto& data_dir : data_dirs) { const auto data_app_dir = data_dir + "/applications/"; From ba48d26dd4d528032f89e285ee3838a2da280383 Mon Sep 17 00:00:00 2001 From: Azazel Date: Wed, 28 Feb 2024 00:24:58 +0000 Subject: [PATCH 8/8] chore: amend default icon spacing --- src/AIconLabel.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AIconLabel.cpp b/src/AIconLabel.cpp index ee68a22e..d7ee666e 100644 --- a/src/AIconLabel.cpp +++ b/src/AIconLabel.cpp @@ -20,7 +20,7 @@ AIconLabel::AIconLabel(const Json::Value &config, const std::string &name, const box_.set_orientation(Gtk::Orientation::ORIENTATION_HORIZONTAL); box_.set_name(name); - int spacing = config_["icon-spacing"].isInt() ? config_["icon-spacing"].asInt() : 6; + int spacing = config_["icon-spacing"].isInt() ? config_["icon-spacing"].asInt() : 8; box_.set_spacing(spacing); box_.add(image_);