From c4bace504ce058ab784c44c9357fd5bd27aedffb Mon Sep 17 00:00:00 2001 From: MisterPine Date: Sat, 8 Jul 2023 21:42:28 +0200 Subject: [PATCH 1/6] Separate query and struct --- include/modules/hyprland/window.hpp | 14 ++++++++++++ src/modules/hyprland/window.cpp | 35 ++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/include/modules/hyprland/window.hpp b/include/modules/hyprland/window.hpp index 54e7e2b7..bd438044 100644 --- a/include/modules/hyprland/window.hpp +++ b/include/modules/hyprland/window.hpp @@ -24,8 +24,20 @@ class Window : public waybar::ALabel, public EventHandler { static auto parse(const Json::Value&) -> Workspace; }; + struct WindowData { + bool floating; + int monitor; + std::string class_name; + std::string initial_class_name; + std::string title; + std::string initial_title; + + static auto parse(const Json::Value&) -> WindowData; + }; + auto getActiveWorkspace(const std::string&) -> Workspace; auto getActiveWorkspace() -> Workspace; + auto getWindowData(const std::string& window_id) -> WindowData; void onEvent(const std::string&) override; void queryActiveWorkspace(); void setClass(const std::string&, bool enable); @@ -34,7 +46,9 @@ class Window : public waybar::ALabel, public EventHandler { std::mutex mutex_; const Bar& bar_; util::JsonParser parser_; + std::string last_window_address_; std::string last_title_; + WindowData window_data_; Workspace workspace_; std::string solo_class_; std::string last_solo_class_; diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index 74a2a432..d6a226cc 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -14,7 +14,7 @@ namespace waybar::modules::hyprland { Window::Window(const std::string& id, const Bar& bar, const Json::Value& config) - : ALabel(config, "window", id, "{}", 0, true), bar_(bar) { + : ALabel(config, "window", id, "{title}", 0, true), bar_(bar) { modulesReady = true; separate_outputs = config["separate-outputs"].asBool(); @@ -44,6 +44,7 @@ auto Window::update() -> void { std::lock_guard lg(mutex_); std::string window_name = waybar::util::sanitize_string(workspace_.last_window_title); + std::string window_address = workspace_.last_window; if (window_name != last_title_) { if (window_name.empty()) { @@ -54,10 +55,19 @@ auto Window::update() -> void { last_title_ = window_name; } + if (window_address != last_window_address_) { + last_window_address_ = window_address; + window_data_ = getWindowData(window_address); + } + if (!format_.empty()) { label_.show(); - label_.set_markup(fmt::format(fmt::runtime(format_), - waybar::util::rewriteString(window_name, config_["rewrite"]))); + label_.set_markup(waybar::util::rewriteString( + fmt::format(fmt::runtime(format_), fmt::arg("title", window_name), + fmt::arg("initialTitle", window_data_.initial_title), + fmt::arg("class", window_data_.class_name), + fmt::arg("initialClass", window_data_.initial_class_name)), + config_["rewrite"])); } else { label_.hide(); } @@ -117,6 +127,25 @@ auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace { value["lastwindowtitle"].asString()}; } +auto Window::getWindowData(const std::string& window_address) -> WindowData { + const auto clients = gIPC->getSocket1JsonReply("clients"); + assert(clients.isArray()); + auto window = std::find_if(clients.begin(), clients.end(), [&](Json::Value window) { + return window["address"] == window_address; + }); + if (window == std::end(clients)) { + spdlog::warn("No client with address {}", window_address); + return WindowData{false, -1, "", "", "", ""}; + } + return WindowData::parse(*window); +} + +auto Window::WindowData::parse(const Json::Value& value) -> Window::WindowData { + return WindowData{value["floating"].asBool(), value["monitor"].asInt(), + value["class"].asString(), value["initialClass"].asString(), + value["title"].asString(), value["initialTitle"].asString()}; +} + void Window::queryActiveWorkspace() { std::lock_guard lg(mutex_); From c5f1771375fbc3fcdeb8b6434d9acd731eb9fe64 Mon Sep 17 00:00:00 2001 From: MisterPine Date: Sat, 8 Jul 2023 22:05:15 +0200 Subject: [PATCH 2/6] Use already existing `queryActiveWorkspace()` --- include/modules/hyprland/window.hpp | 4 +--- src/modules/hyprland/window.cpp | 13 +++++-------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/include/modules/hyprland/window.hpp b/include/modules/hyprland/window.hpp index bd438044..3f9b89bf 100644 --- a/include/modules/hyprland/window.hpp +++ b/include/modules/hyprland/window.hpp @@ -26,7 +26,7 @@ class Window : public waybar::ALabel, public EventHandler { struct WindowData { bool floating; - int monitor; + int monitor = -1; std::string class_name; std::string initial_class_name; std::string title; @@ -46,8 +46,6 @@ class Window : public waybar::ALabel, public EventHandler { std::mutex mutex_; const Bar& bar_; util::JsonParser parser_; - std::string last_window_address_; - std::string last_title_; WindowData window_data_; Workspace workspace_; std::string solo_class_; diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index d6a226cc..01f672a4 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -46,18 +46,13 @@ auto Window::update() -> void { std::string window_name = waybar::util::sanitize_string(workspace_.last_window_title); std::string window_address = workspace_.last_window; - if (window_name != last_title_) { + if (window_name != window_data_.title) { if (window_name.empty()) { label_.get_style_context()->add_class("empty"); } else { label_.get_style_context()->remove_class("empty"); } - last_title_ = window_name; - } - - if (window_address != last_window_address_) { - last_window_address_ = window_address; - window_data_ = getWindowData(window_address); + window_data_.title = window_name; } if (!format_.empty()) { @@ -165,6 +160,7 @@ void Window::queryActiveWorkspace() { return; } + window_data_ = WindowData::parse(*active_window); std::vector workspace_windows; std::copy_if(clients.begin(), clients.end(), std::back_inserter(workspace_windows), [&](Json::Value window) { @@ -187,11 +183,12 @@ void Window::queryActiveWorkspace() { } if (solo_) { - solo_class_ = (*active_window)["class"].asString(); + solo_class_ = window_data_.class_name; } else { solo_class_ = ""; } } else { + window_data_ = WindowData{}; all_floating_ = false; hidden_ = false; fullscreen_ = false; From 2ae13c40921d19b9a78d8f8adfd404a80e659bd5 Mon Sep 17 00:00:00 2001 From: MisterPine Date: Sat, 8 Jul 2023 22:27:25 +0200 Subject: [PATCH 3/6] consitent naming --- src/modules/hyprland/window.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index 01f672a4..fea0d310 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -59,9 +59,9 @@ auto Window::update() -> void { label_.show(); label_.set_markup(waybar::util::rewriteString( fmt::format(fmt::runtime(format_), fmt::arg("title", window_name), - fmt::arg("initialTitle", window_data_.initial_title), + fmt::arg("initial-title", window_data_.initial_title), fmt::arg("class", window_data_.class_name), - fmt::arg("initialClass", window_data_.initial_class_name)), + fmt::arg("initial-class", window_data_.initial_class_name)), config_["rewrite"])); } else { label_.hide(); From 1887512ba1c388855c58ea9ffc99e4933dd0e3a7 Mon Sep 17 00:00:00 2001 From: MisterPine Date: Sat, 8 Jul 2023 22:29:30 +0200 Subject: [PATCH 4/6] Update scd --- man/waybar-hyprland-window.5.scd | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/man/waybar-hyprland-window.5.scd b/man/waybar-hyprland-window.5.scd index 58227092..53ee22a1 100644 --- a/man/waybar-hyprland-window.5.scd +++ b/man/waybar-hyprland-window.5.scd @@ -14,7 +14,7 @@ Addressed by *hyprland/window* *format*: ++ typeof: string ++ - default: {} ++ + default: {title} ++ The format, how information should be displayed. On {} the current window title is displayed. *rewrite*: ++ @@ -25,6 +25,17 @@ Addressed by *hyprland/window* typeof: bool ++ Show the active window of the monitor the bar belongs to, instead of the focused window. +# FORMAT REPLACEMENTS +See the output of "hyprctl clients" for examples + +*{title}*: The current title of the focused window. + +*{initial-title}*: The initial title of the focused window. + +*{class}*: The current class of the focused window. + +*{initial-class}*: The initial class of the focused window. + # REWRITE RULES *rewrite* is an object where keys are regular expressions and values are From 9ee883ee1b61aeb978bedff31451501286127e70 Mon Sep 17 00:00:00 2001 From: MisterPine Date: Sat, 8 Jul 2023 23:11:11 +0200 Subject: [PATCH 5/6] No dashes is format arg name --- man/waybar-hyprland-window.5.scd | 4 ++-- src/modules/hyprland/window.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/man/waybar-hyprland-window.5.scd b/man/waybar-hyprland-window.5.scd index 53ee22a1..2a3f62f1 100644 --- a/man/waybar-hyprland-window.5.scd +++ b/man/waybar-hyprland-window.5.scd @@ -30,11 +30,11 @@ See the output of "hyprctl clients" for examples *{title}*: The current title of the focused window. -*{initial-title}*: The initial title of the focused window. +*{initialTitle}*: The initial title of the focused window. *{class}*: The current class of the focused window. -*{initial-class}*: The initial class of the focused window. +*{initialClass}*: The initial class of the focused window. # REWRITE RULES diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index fea0d310..01f672a4 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -59,9 +59,9 @@ auto Window::update() -> void { label_.show(); label_.set_markup(waybar::util::rewriteString( fmt::format(fmt::runtime(format_), fmt::arg("title", window_name), - fmt::arg("initial-title", window_data_.initial_title), + fmt::arg("initialTitle", window_data_.initial_title), fmt::arg("class", window_data_.class_name), - fmt::arg("initial-class", window_data_.initial_class_name)), + fmt::arg("initialClass", window_data_.initial_class_name)), config_["rewrite"])); } else { label_.hide(); From f97c1c7136528798f17f5f1b621e41ae46d66aaf Mon Sep 17 00:00:00 2001 From: MisterPine Date: Sat, 8 Jul 2023 23:22:29 +0200 Subject: [PATCH 6/6] remove getWindowData --- include/modules/hyprland/window.hpp | 1 - src/modules/hyprland/window.cpp | 13 ------------- 2 files changed, 14 deletions(-) diff --git a/include/modules/hyprland/window.hpp b/include/modules/hyprland/window.hpp index 3f9b89bf..44253e1f 100644 --- a/include/modules/hyprland/window.hpp +++ b/include/modules/hyprland/window.hpp @@ -37,7 +37,6 @@ class Window : public waybar::ALabel, public EventHandler { auto getActiveWorkspace(const std::string&) -> Workspace; auto getActiveWorkspace() -> Workspace; - auto getWindowData(const std::string& window_id) -> WindowData; void onEvent(const std::string&) override; void queryActiveWorkspace(); void setClass(const std::string&, bool enable); diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index 01f672a4..7fc7fe28 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -122,19 +122,6 @@ auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace { value["lastwindowtitle"].asString()}; } -auto Window::getWindowData(const std::string& window_address) -> WindowData { - const auto clients = gIPC->getSocket1JsonReply("clients"); - assert(clients.isArray()); - auto window = std::find_if(clients.begin(), clients.end(), [&](Json::Value window) { - return window["address"] == window_address; - }); - if (window == std::end(clients)) { - spdlog::warn("No client with address {}", window_address); - return WindowData{false, -1, "", "", "", ""}; - } - return WindowData::parse(*window); -} - auto Window::WindowData::parse(const Json::Value& value) -> Window::WindowData { return WindowData{value["floating"].asBool(), value["monitor"].asInt(), value["class"].asString(), value["initialClass"].asString(),