From 9b6590b972590672d465b1823c050b56fb4c288e Mon Sep 17 00:00:00 2001 From: burbschat Date: Sat, 14 Jun 2025 22:30:46 +0900 Subject: [PATCH 1/2] Add CSS classes for sway workspace indicators depending on associated output Allows for different styles of workspace indicators (buttons) depending on the output the workspace is located at. Using this one can for example color the indicators depending on the output making it easy to infer on which output a workspace is located. Example config: "sway/workspaces": { "all-outputs": true, "format": "{name}", "output-classes": { "DP-1": "output1", "LVDS-2": "output2" } } Example CSS: #workspaces button.output1{ background-color: green; } #workspaces button.output2{ background-color: blue; } --- man/waybar-sway-workspaces.5.scd | 7 +++++++ src/modules/sway/workspaces.cpp | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/man/waybar-sway-workspaces.5.scd b/man/waybar-sway-workspaces.5.scd index 1c4360e9..e4041f5a 100644 --- a/man/waybar-sway-workspaces.5.scd +++ b/man/waybar-sway-workspaces.5.scd @@ -105,6 +105,12 @@ warp-on-scroll: ++ default: false ++ Enables this module to consume all left over space dynamically. +*output-classes*: ++ + typeof: array ++ + Specify additional CSS classes to be added to workspace indicators based on the output on which the associated workspace is located. + Keys are output names and values are class names like *${output}: {output-class}*. + Assignment in config is used to keep the stylesheet independent of the available outputs. + # FORMAT REPLACEMENTS @@ -193,3 +199,4 @@ n.b.: the list of outputs can be obtained from command line using *swaymsg -t ge - *#workspaces button.empty* - *#workspaces button.current_output* - *#workspaces button#sway-workspace-${name}* +- *#workspaces button.${output-class}* diff --git a/src/modules/sway/workspaces.cpp b/src/modules/sway/workspaces.cpp index b8ed73d4..fc122a03 100644 --- a/src/modules/sway/workspaces.cpp +++ b/src/modules/sway/workspaces.cpp @@ -321,6 +321,17 @@ auto Workspaces::update() -> void { button.get_style_context()->remove_class("empty"); } if ((*it)["output"].isString()) { + // Simply attempt to remove all output classes every time to reset output classes. This works + // even if a class has not been previously added to the style context. + for (const auto & oclass : config_["output-classes"]) { + button.get_style_context()->remove_class(oclass.asString()); + } + // If output-classes contains a class for output associated with current workspace button, add + // the class to its style context. + std::string output_name = (*it)["output"].asString(); + if (config_["output-classes"].isMember(output_name) && config_["output-classes"][output_name].isString()){ + button.get_style_context()->add_class(config_["output-classes"][output_name].asString()); + } if (((*it)["output"].asString()) == bar_.output->name) { button.get_style_context()->add_class("current_output"); } else { From d6f9dc911a9fd549893778e3e605cbea89ea3a98 Mon Sep 17 00:00:00 2001 From: burbschat Date: Sat, 14 Jun 2025 22:59:32 +0900 Subject: [PATCH 2/2] Apply format according to .clang-format --- src/modules/sway/workspaces.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/sway/workspaces.cpp b/src/modules/sway/workspaces.cpp index fc122a03..3b319623 100644 --- a/src/modules/sway/workspaces.cpp +++ b/src/modules/sway/workspaces.cpp @@ -323,13 +323,14 @@ auto Workspaces::update() -> void { if ((*it)["output"].isString()) { // Simply attempt to remove all output classes every time to reset output classes. This works // even if a class has not been previously added to the style context. - for (const auto & oclass : config_["output-classes"]) { + for (const auto &oclass : config_["output-classes"]) { button.get_style_context()->remove_class(oclass.asString()); } // If output-classes contains a class for output associated with current workspace button, add // the class to its style context. std::string output_name = (*it)["output"].asString(); - if (config_["output-classes"].isMember(output_name) && config_["output-classes"][output_name].isString()){ + if (config_["output-classes"].isMember(output_name) && + config_["output-classes"][output_name].isString()) { button.get_style_context()->add_class(config_["output-classes"][output_name].asString()); } if (((*it)["output"].asString()) == bar_.output->name) {