From bb8ff5a99fbc67126aba3b4b75729009da97664c Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 25 Apr 2019 16:47:51 +0200 Subject: [PATCH] feat(Bar): add class depend of window in the workspace --- include/modules/sway/window.hpp | 11 ++++--- resources/style.css | 19 ++++++++++++ src/modules/sway/window.cpp | 55 ++++++++++++++++++++++++++------- 3 files changed, 68 insertions(+), 17 deletions(-) diff --git a/include/modules/sway/window.hpp b/include/modules/sway/window.hpp index 1752793..a5b0e7c 100644 --- a/include/modules/sway/window.hpp +++ b/include/modules/sway/window.hpp @@ -17,17 +17,18 @@ class Window : public ALabel { auto update() -> void; private: - void onEvent(const struct Ipc::ipc_response&); - void onCmd(const struct Ipc::ipc_response&); - void worker(); - std::tuple getFocusedNode(const Json::Value& nodes); - void getFocusedWindow(); + void onEvent(const struct Ipc::ipc_response&); + void onCmd(const struct Ipc::ipc_response&); + void worker(); + std::tuple getFocusedNode(const Json::Value& nodes); + void getTree(); const Bar& bar_; waybar::util::SleeperThread thread_; Ipc ipc_; std::string window_; int windowId_; + std::string app_id_; }; } // namespace waybar::modules::sway diff --git a/resources/style.css b/resources/style.css index a528f85..a6cff59 100644 --- a/resources/style.css +++ b/resources/style.css @@ -16,6 +16,24 @@ window#waybar.hidded { opacity: 0.2; } +/* +window#waybar.empty { + background: transparent; +} +window#waybar.solo { + background: #FFFFFF; +} +*/ + +window#waybar.termite { + background-color: #3F3F3F; +} + +window#waybar.chromium { + background-color: #DEE1E6; + color: #000000; +} + /* https://github.com/Alexays/Waybar/wiki/FAQ#the-workspace-buttons-have-a-strange-hover-effect */ #workspaces button { padding: 0 5px; @@ -41,6 +59,7 @@ window#waybar.hidded { #clock, #battery, #cpu, #memory, #temperature, #backlight, #network, #pulseaudio, #custom-media, #tray, #mode, #idle_inhibitor { padding: 0 10px; margin: 0 5px; + color: #ffffff; } #clock { diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index 740fd44..34e2573 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -15,7 +15,8 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config) ipc_.subscribe(R"(["window","workspace"])"); ipc_.signal_event.connect(sigc::mem_fun(*this, &Window::onEvent)); ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Window::onCmd)); - getFocusedWindow(); + // Get Initial focused window + getTree(); // Launch worker worker(); } @@ -28,6 +29,7 @@ void Window::onEvent(const struct Ipc::ipc_response& res) { window_ = Glib::Markup::escape_text(data["container"]["name"].asString()); windowId_ = data["container"]["id"].asInt(); dp.emit(); + getTree(); } else if ((data["change"] == "close" && data["container"]["focused"].asBool() && windowId_ == data["container"]["id"].asInt()) || (data["change"] == "focus" && data["current"]["focus"].isArray() && @@ -35,14 +37,34 @@ void Window::onEvent(const struct Ipc::ipc_response& res) { window_.clear(); windowId_ = -1; dp.emit(); + getTree(); } } void Window::onCmd(const struct Ipc::ipc_response& res) { - auto [id, name] = getFocusedNode(res.payload["nodes"]); - windowId_ = id; - window_ = name; - dp.emit(); + auto [nb, id, name, app_id] = getFocusedNode(res.payload); + if (nb == 0) { + bar_.window.get_style_context()->add_class("empty"); + } else { + bar_.window.get_style_context()->remove_class("empty"); + } + if (!app_id_.empty()) { + bar_.window.get_style_context()->remove_class(app_id_); + } + if (nb == 1) { + bar_.window.get_style_context()->add_class("solo"); + if (!app_id.empty()) { + bar_.window.get_style_context()->add_class(app_id); + } + } else { + bar_.window.get_style_context()->remove_class("solo"); + } + app_id_ = app_id; + if (windowId_ != id || window_ != name) { + windowId_ = id; + window_ = name; + dp.emit(); + } } void Window::worker() { @@ -62,20 +84,29 @@ auto Window::update() -> void { } } -std::tuple Window::getFocusedNode(const Json::Value& nodes) { - for (auto const& node : nodes) { +std::tuple Window::getFocusedNode( + const Json::Value& nodes) { + for (auto const& node : nodes["nodes"]) { if (node["focused"].asBool() && node["type"] == "con") { - return {node["id"].asInt(), node["name"].asString()}; + if ((!config_["all-outputs"].asBool() && nodes["output"] == bar_.output->name) || + config_["all-outputs"].asBool()) { + auto app_id = node["app_id"].isString() ? node["app_id"].asString() + : node["window_properties"]["instance"].asString(); + return {nodes["nodes"].size(), + node["id"].asInt(), + Glib::Markup::escape_text(node["name"].asString()), + app_id}; + } } - auto [id, name] = getFocusedNode(node["nodes"]); + auto [nb, id, name, app_id] = getFocusedNode(node); if (id > -1 && !name.empty()) { - return {id, name}; + return {nb, id, name, app_id}; } } - return {-1, std::string()}; + return {0, -1, "", ""}; } -void Window::getFocusedWindow() { +void Window::getTree() { try { ipc_.sendCmd(IPC_GET_TREE); } catch (const std::exception& e) {