From fe1d49518bd63425a03f21cb585f93c013e183a8 Mon Sep 17 00:00:00 2001 From: Marc Meier Date: Wed, 20 Mar 2024 15:54:52 +0100 Subject: [PATCH] Add tooltip selection based on name and state One can set different tooltips for different workspaces. Selection works the same as it does for Icons. Also refactored the `selectIcon` function to a more general `selectString` function. Man page is updated too. --- include/modules/hyprland/workspaces.hpp | 6 +- man/waybar-hyprland-workspaces.5.scd | 35 ++++++++++-- src/modules/hyprland/workspaces.cpp | 76 ++++++++++++++----------- 3 files changed, 77 insertions(+), 40 deletions(-) diff --git a/include/modules/hyprland/workspaces.hpp b/include/modules/hyprland/workspaces.hpp index 1a8fe872..52b0850c 100644 --- a/include/modules/hyprland/workspaces.hpp +++ b/include/modules/hyprland/workspaces.hpp @@ -62,7 +62,7 @@ class Workspace { public: explicit Workspace(const Json::Value& workspace_data, Workspaces& workspace_manager, const Json::Value& clients_data = Json::Value::nullRef); - std::string& selectIcon(std::map& icons_map); + std::string& selectString(std::map& string_map); Gtk::Button& button() { return m_button; }; int id() const { return m_id; }; @@ -199,7 +199,9 @@ class Workspaces : public AModule, public EventHandler { {"DEFAULT", SortMethod::DEFAULT}}; std::string m_format; - std::string m_tooltipFormat; + + std::map m_tooltipMap; + bool m_withTooltip; std::map m_iconsMap; util::RegexCollection m_windowRewriteRules; diff --git a/man/waybar-hyprland-workspaces.5.scd b/man/waybar-hyprland-workspaces.5.scd index db306091..f564c03f 100644 --- a/man/waybar-hyprland-workspaces.5.scd +++ b/man/waybar-hyprland-workspaces.5.scd @@ -70,10 +70,9 @@ Addressed by *hyprland/workspaces* default: true ++ Option to disable tooltip on hover. -*tooltip-format*: ++ - typeof: string ++ - default: "{name}" ++ - The tooltip format. +*tooltips*: ++ + typeof: array ++ + Based on the workspace ID and state, the corresponding tooltip gets selected. Selection works the same as *format-icons* do. Format replacements are supported. See *icons*. # FORMAT REPLACEMENTS @@ -163,7 +162,33 @@ Additional to workspace name matching, the following *format-icons* can be set. "format": "{icon}", "format-window-separator": ", ", "tooltip": true, - "tooltip-format": "{name}: {windows}", + "tooltips": { + "default": "{name}: {windows}", + "empty": "" // Will result in no tooltip + } + "format-icons": { + "1": "", + "2": "", + "3": "", + "4": "", + "5": "", + "active": "", + "default": "" + }, + // Window rewrites omitted for brevity +} +``` + +``` +"hyprland/workspaces": { + "format": "{icon}", + "format-window-separator": ", ", + "tooltip": true, + "tooltips": { + "1": "This is the first workspace", + "2": "This is the second", + "2": "And this is the third" + } "format-icons": { "1": "", "2": "", diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index 8c543fd3..d7eb1f3b 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -60,11 +60,14 @@ auto Workspaces::parseConfig(const Json::Value &config) -> void { m_format = configFormat.isString() ? configFormat.asString() : "{name}"; m_withIcon = m_format.find("{icon}") != std::string::npos; - if (tooltipEnabled()) { - const Json::Value &configTooltipFormat = config["tooltip-format"]; - m_tooltipFormat = configTooltipFormat.isString() ? configTooltipFormat.asString() : "{name}"; - } else { - m_tooltipFormat = ""; + m_withTooltip = tooltipEnabled(); + + if (m_withTooltip && m_tooltipMap.empty()) { + Json::Value tooltipFormats = config["tooltips"]; + for (std::string &name : tooltipFormats.getMemberNames()) { + m_tooltipMap.emplace(name, tooltipFormats[name].asString()); + } + m_tooltipMap.emplace("", ""); } if (m_withIcon && m_iconsMap.empty()) { @@ -232,7 +235,15 @@ void Workspaces::doUpdate() { // set workspace icon std::string &workspaceIcon = m_iconsMap[""]; if (m_withIcon) { - workspaceIcon = workspace->selectIcon(m_iconsMap); + spdlog::trace("Selecting icon for workspace {}", workspace->name()); + workspaceIcon = workspace->selectString(m_iconsMap); + } + + // set tooltip + std::string &workspaceTooltip = m_tooltipMap[""]; + if (m_withTooltip) { + spdlog::trace("Selecting tooltip for workspace {}", workspace->name()); + workspaceTooltip = workspace->selectString(m_tooltipMap); } // update m_output @@ -247,7 +258,7 @@ void Workspaces::doUpdate() { workspace->setOutput((*updated_workspace)["monitor"].asString()); } - workspace->update(m_format, workspaceIcon, m_tooltipFormat); + workspace->update(m_format, workspaceIcon, workspaceTooltip); } spdlog::trace("Updating window count"); @@ -984,58 +995,57 @@ void Workspaces::sortWorkspaces() { } } -std::string &Workspace::selectIcon(std::map &icons_map) { - spdlog::trace("Selecting icon for workspace {}", name()); +std::string &Workspace::selectString(std::map &string_map) { if (isUrgent()) { - auto urgentIconIt = icons_map.find("urgent"); - if (urgentIconIt != icons_map.end()) { - return urgentIconIt->second; + auto urgentStringIt = string_map.find("urgent"); + if (urgentStringIt != string_map.end()) { + return urgentStringIt->second; } } if (isActive()) { - auto activeIconIt = icons_map.find("active"); - if (activeIconIt != icons_map.end()) { - return activeIconIt->second; + auto activeStringIt = string_map.find("active"); + if (activeStringIt != string_map.end()) { + return activeStringIt->second; } } if (isSpecial()) { - auto specialIconIt = icons_map.find("special"); - if (specialIconIt != icons_map.end()) { - return specialIconIt->second; + auto specialStringIt = string_map.find("special"); + if (specialStringIt != string_map.end()) { + return specialStringIt->second; } } - auto namedIconIt = icons_map.find(name()); - if (namedIconIt != icons_map.end()) { - return namedIconIt->second; + auto namedStringIt = string_map.find(name()); + if (namedStringIt != string_map.end()) { + return namedStringIt->second; } if (isVisible()) { - auto visibleIconIt = icons_map.find("visible"); - if (visibleIconIt != icons_map.end()) { - return visibleIconIt->second; + auto visibleStringIt = string_map.find("visible"); + if (visibleStringIt != string_map.end()) { + return visibleStringIt->second; } } if (isEmpty()) { - auto emptyIconIt = icons_map.find("empty"); - if (emptyIconIt != icons_map.end()) { - return emptyIconIt->second; + auto emptyStringIt = string_map.find("empty"); + if (emptyStringIt != string_map.end()) { + return emptyStringIt->second; } } if (isPersistent()) { - auto persistentIconIt = icons_map.find("persistent"); - if (persistentIconIt != icons_map.end()) { - return persistentIconIt->second; + auto persistentStringIt = string_map.find("persistent"); + if (persistentStringIt != string_map.end()) { + return persistentStringIt->second; } } - auto defaultIconIt = icons_map.find("default"); - if (defaultIconIt != icons_map.end()) { - return defaultIconIt->second; + auto defaultStringIt = string_map.find("default"); + if (defaultStringIt != string_map.end()) { + return defaultStringIt->second; } return m_name;