From 53ca5a48830a3cbe4b15fd1b3f5992a40384bd2f Mon Sep 17 00:00:00 2001 From: Pol Rivero <65060696+pol-rivero@users.noreply.github.com> Date: Thu, 2 Jan 2025 07:41:24 +0100 Subject: [PATCH] workspace taskbars: Display windows in a consistent order Use a vector instead of a map for for storing the workspace windows. This orders the windows by the time they were added to the workspace, instead of sorting by address (which is effectively a random order). The new ordering seems to match the wlr/taskbar module --- .../hyprland/windowcreationpayload.hpp | 1 + include/modules/hyprland/workspace.hpp | 8 +++-- .../hyprland/windowcreationpayload.cpp | 3 +- src/modules/hyprland/workspace.cpp | 29 ++++++++++++------- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/include/modules/hyprland/windowcreationpayload.hpp b/include/modules/hyprland/windowcreationpayload.hpp index 06af3074..dca7ba9e 100644 --- a/include/modules/hyprland/windowcreationpayload.hpp +++ b/include/modules/hyprland/windowcreationpayload.hpp @@ -27,6 +27,7 @@ namespace waybar::modules::hyprland { class Workspaces; struct WindowRepr { + std::string address; std::string window_class; std::string window_title; std::string repr_rewrite; diff --git a/include/modules/hyprland/workspace.hpp b/include/modules/hyprland/workspace.hpp index 71529363..2dc9239b 100644 --- a/include/modules/hyprland/workspace.hpp +++ b/include/modules/hyprland/workspace.hpp @@ -54,9 +54,11 @@ class Workspace { void setWindows(uint value) { m_windows = value; }; void setName(std::string const& value) { m_name = value; }; void setOutput(std::string const& value) { m_output = value; }; - bool containsWindow(WindowAddress const& addr) const { return m_windowMap.contains(addr); } + bool containsWindow(WindowAddress const& addr) const { + return std::ranges::any_of(m_windowMap, + [&addr](const auto& window) { return window.address == addr; }); + }; void insertWindow(WindowCreationPayload create_window_paylod); - WindowRepr removeWindow(WindowAddress const& addr); void initializeWindowMap(const Json::Value& clients_data); bool onWindowOpened(WindowCreationPayload const& create_window_paylod); @@ -78,7 +80,7 @@ class Workspace { bool m_isUrgent = false; bool m_isVisible = false; - std::map> m_windowMap; + std::vector m_windowMap; Gtk::Button m_button; Gtk::Box m_content; diff --git a/src/modules/hyprland/windowcreationpayload.cpp b/src/modules/hyprland/windowcreationpayload.cpp index d8e3bdd5..5e587d51 100644 --- a/src/modules/hyprland/windowcreationpayload.cpp +++ b/src/modules/hyprland/windowcreationpayload.cpp @@ -98,7 +98,8 @@ WindowRepr WindowCreationPayload::repr(Workspaces &workspace_manager) { } if (std::holds_alternative(m_window)) { auto [window_class, window_title] = std::get(m_window); - return {window_class, window_title, workspace_manager.getRewrite(window_class, window_title)}; + return {m_windowAddress, window_class, window_title, + workspace_manager.getRewrite(window_class, window_title)}; } // Unreachable spdlog::error("WorkspaceWindow::repr: Unreachable"); diff --git a/src/modules/hyprland/workspace.cpp b/src/modules/hyprland/workspace.cpp index aa12f33c..0baa67a8 100644 --- a/src/modules/hyprland/workspace.cpp +++ b/src/modules/hyprland/workspace.cpp @@ -53,8 +53,13 @@ void addOrRemoveClass(const Glib::RefPtr &context, bool condi } std::optional Workspace::closeWindow(WindowAddress const &addr) { - if (m_windowMap.contains(addr)) { - return removeWindow(addr); + auto it = std::ranges::find_if(m_windowMap, + [&addr](const auto &window) { return window.address == addr; }); + // If the vector contains the address, remove it and return the window representation + if (it != m_windowMap.end()) { + WindowRepr windowRepr = *it; + m_windowMap.erase(it); + return windowRepr; } return std::nullopt; } @@ -101,7 +106,15 @@ void Workspace::insertWindow(WindowCreationPayload create_window_paylod) { auto repr = create_window_paylod.repr(m_workspaceManager); if (!repr.empty()) { - m_windowMap[create_window_paylod.getAddress()] = repr; + auto addr = create_window_paylod.getAddress(); + auto it = std::ranges::find_if( + m_windowMap, [&addr](const auto &window) { return window.address == addr; }); + // If the vector contains the address, update the window representation, otherwise insert it + if (it != m_windowMap.end()) { + *it = repr; + } else { + m_windowMap.emplace_back(repr); + } } } }; @@ -114,12 +127,6 @@ bool Workspace::onWindowOpened(WindowCreationPayload const &create_window_paylod return false; } -WindowRepr Workspace::removeWindow(WindowAddress const &addr) { - WindowRepr windowRepr = m_windowMap[addr]; - m_windowMap.erase(addr); - return windowRepr; -} - std::string &Workspace::selectIcon(std::map &icons_map) { spdlog::trace("Selecting icon for workspace {}", name()); if (isUrgent()) { @@ -212,7 +219,7 @@ void Workspace::update(const std::string &workspace_icon) { bool isNotFirst = false; - for (const auto &[_pid, window_repr] : m_windowMap) { + for (const auto &window_repr : m_windowMap) { if (isNotFirst) { windows.append(windowSeparator); } @@ -238,7 +245,7 @@ void Workspace::updateTaskbar(const std::string &workspace_icon) { } } - for (const auto &[_addr, window_repr] : m_windowMap) { + for (const auto &window_repr : m_windowMap) { auto window_box = Gtk::make_managed(Gtk::ORIENTATION_HORIZONTAL); window_box->set_tooltip_text(window_repr.window_title);