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.
pull/3017/head
Marc Meier 2024-03-20 15:54:52 +01:00
parent 01dce4609c
commit fe1d49518b
3 changed files with 77 additions and 40 deletions

View File

@ -62,7 +62,7 @@ class Workspace {
public: public:
explicit Workspace(const Json::Value& workspace_data, Workspaces& workspace_manager, explicit Workspace(const Json::Value& workspace_data, Workspaces& workspace_manager,
const Json::Value& clients_data = Json::Value::nullRef); const Json::Value& clients_data = Json::Value::nullRef);
std::string& selectIcon(std::map<std::string, std::string>& icons_map); std::string& selectString(std::map<std::string, std::string>& string_map);
Gtk::Button& button() { return m_button; }; Gtk::Button& button() { return m_button; };
int id() const { return m_id; }; int id() const { return m_id; };
@ -199,7 +199,9 @@ class Workspaces : public AModule, public EventHandler {
{"DEFAULT", SortMethod::DEFAULT}}; {"DEFAULT", SortMethod::DEFAULT}};
std::string m_format; std::string m_format;
std::string m_tooltipFormat;
std::map<std::string, std::string> m_tooltipMap;
bool m_withTooltip;
std::map<std::string, std::string> m_iconsMap; std::map<std::string, std::string> m_iconsMap;
util::RegexCollection m_windowRewriteRules; util::RegexCollection m_windowRewriteRules;

View File

@ -70,10 +70,9 @@ Addressed by *hyprland/workspaces*
default: true ++ default: true ++
Option to disable tooltip on hover. Option to disable tooltip on hover.
*tooltip-format*: ++ *tooltips*: ++
typeof: string ++ typeof: array ++
default: "{name}" ++ 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*.
The tooltip format.
# FORMAT REPLACEMENTS # FORMAT REPLACEMENTS
@ -163,7 +162,33 @@ Additional to workspace name matching, the following *format-icons* can be set.
"format": "{icon}", "format": "{icon}",
"format-window-separator": ", ", "format-window-separator": ", ",
"tooltip": true, "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": { "format-icons": {
"1": "", "1": "",
"2": "", "2": "",

View File

@ -60,11 +60,14 @@ auto Workspaces::parseConfig(const Json::Value &config) -> void {
m_format = configFormat.isString() ? configFormat.asString() : "{name}"; m_format = configFormat.isString() ? configFormat.asString() : "{name}";
m_withIcon = m_format.find("{icon}") != std::string::npos; m_withIcon = m_format.find("{icon}") != std::string::npos;
if (tooltipEnabled()) { m_withTooltip = tooltipEnabled();
const Json::Value &configTooltipFormat = config["tooltip-format"];
m_tooltipFormat = configTooltipFormat.isString() ? configTooltipFormat.asString() : "{name}"; if (m_withTooltip && m_tooltipMap.empty()) {
} else { Json::Value tooltipFormats = config["tooltips"];
m_tooltipFormat = ""; for (std::string &name : tooltipFormats.getMemberNames()) {
m_tooltipMap.emplace(name, tooltipFormats[name].asString());
}
m_tooltipMap.emplace("", "");
} }
if (m_withIcon && m_iconsMap.empty()) { if (m_withIcon && m_iconsMap.empty()) {
@ -232,7 +235,15 @@ void Workspaces::doUpdate() {
// set workspace icon // set workspace icon
std::string &workspaceIcon = m_iconsMap[""]; std::string &workspaceIcon = m_iconsMap[""];
if (m_withIcon) { 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 // update m_output
@ -247,7 +258,7 @@ void Workspaces::doUpdate() {
workspace->setOutput((*updated_workspace)["monitor"].asString()); workspace->setOutput((*updated_workspace)["monitor"].asString());
} }
workspace->update(m_format, workspaceIcon, m_tooltipFormat); workspace->update(m_format, workspaceIcon, workspaceTooltip);
} }
spdlog::trace("Updating window count"); spdlog::trace("Updating window count");
@ -984,58 +995,57 @@ void Workspaces::sortWorkspaces() {
} }
} }
std::string &Workspace::selectIcon(std::map<std::string, std::string> &icons_map) { std::string &Workspace::selectString(std::map<std::string, std::string> &string_map) {
spdlog::trace("Selecting icon for workspace {}", name());
if (isUrgent()) { if (isUrgent()) {
auto urgentIconIt = icons_map.find("urgent"); auto urgentStringIt = string_map.find("urgent");
if (urgentIconIt != icons_map.end()) { if (urgentStringIt != string_map.end()) {
return urgentIconIt->second; return urgentStringIt->second;
} }
} }
if (isActive()) { if (isActive()) {
auto activeIconIt = icons_map.find("active"); auto activeStringIt = string_map.find("active");
if (activeIconIt != icons_map.end()) { if (activeStringIt != string_map.end()) {
return activeIconIt->second; return activeStringIt->second;
} }
} }
if (isSpecial()) { if (isSpecial()) {
auto specialIconIt = icons_map.find("special"); auto specialStringIt = string_map.find("special");
if (specialIconIt != icons_map.end()) { if (specialStringIt != string_map.end()) {
return specialIconIt->second; return specialStringIt->second;
} }
} }
auto namedIconIt = icons_map.find(name()); auto namedStringIt = string_map.find(name());
if (namedIconIt != icons_map.end()) { if (namedStringIt != string_map.end()) {
return namedIconIt->second; return namedStringIt->second;
} }
if (isVisible()) { if (isVisible()) {
auto visibleIconIt = icons_map.find("visible"); auto visibleStringIt = string_map.find("visible");
if (visibleIconIt != icons_map.end()) { if (visibleStringIt != string_map.end()) {
return visibleIconIt->second; return visibleStringIt->second;
} }
} }
if (isEmpty()) { if (isEmpty()) {
auto emptyIconIt = icons_map.find("empty"); auto emptyStringIt = string_map.find("empty");
if (emptyIconIt != icons_map.end()) { if (emptyStringIt != string_map.end()) {
return emptyIconIt->second; return emptyStringIt->second;
} }
} }
if (isPersistent()) { if (isPersistent()) {
auto persistentIconIt = icons_map.find("persistent"); auto persistentStringIt = string_map.find("persistent");
if (persistentIconIt != icons_map.end()) { if (persistentStringIt != string_map.end()) {
return persistentIconIt->second; return persistentStringIt->second;
} }
} }
auto defaultIconIt = icons_map.find("default"); auto defaultStringIt = string_map.find("default");
if (defaultIconIt != icons_map.end()) { if (defaultStringIt != string_map.end()) {
return defaultIconIt->second; return defaultStringIt->second;
} }
return m_name; return m_name;