feat(Bar): add class depend of window in the workspace

This commit is contained in:
Alex 2019-04-25 16:47:51 +02:00
parent 79a5e9ecee
commit bb8ff5a99f
3 changed files with 68 additions and 17 deletions

View File

@ -17,17 +17,18 @@ class Window : public ALabel {
auto update() -> void; auto update() -> void;
private: private:
void onEvent(const struct Ipc::ipc_response&); void onEvent(const struct Ipc::ipc_response&);
void onCmd(const struct Ipc::ipc_response&); void onCmd(const struct Ipc::ipc_response&);
void worker(); void worker();
std::tuple<int, std::string> getFocusedNode(const Json::Value& nodes); std::tuple<std::size_t, int, std::string, std::string> getFocusedNode(const Json::Value& nodes);
void getFocusedWindow(); void getTree();
const Bar& bar_; const Bar& bar_;
waybar::util::SleeperThread thread_; waybar::util::SleeperThread thread_;
Ipc ipc_; Ipc ipc_;
std::string window_; std::string window_;
int windowId_; int windowId_;
std::string app_id_;
}; };
} // namespace waybar::modules::sway } // namespace waybar::modules::sway

View File

@ -16,6 +16,24 @@ window#waybar.hidded {
opacity: 0.2; 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 */ /* https://github.com/Alexays/Waybar/wiki/FAQ#the-workspace-buttons-have-a-strange-hover-effect */
#workspaces button { #workspaces button {
padding: 0 5px; padding: 0 5px;
@ -41,6 +59,7 @@ window#waybar.hidded {
#clock, #battery, #cpu, #memory, #temperature, #backlight, #network, #pulseaudio, #custom-media, #tray, #mode, #idle_inhibitor { #clock, #battery, #cpu, #memory, #temperature, #backlight, #network, #pulseaudio, #custom-media, #tray, #mode, #idle_inhibitor {
padding: 0 10px; padding: 0 10px;
margin: 0 5px; margin: 0 5px;
color: #ffffff;
} }
#clock { #clock {

View File

@ -15,7 +15,8 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
ipc_.subscribe(R"(["window","workspace"])"); ipc_.subscribe(R"(["window","workspace"])");
ipc_.signal_event.connect(sigc::mem_fun(*this, &Window::onEvent)); ipc_.signal_event.connect(sigc::mem_fun(*this, &Window::onEvent));
ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Window::onCmd)); ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Window::onCmd));
getFocusedWindow(); // Get Initial focused window
getTree();
// Launch worker // Launch worker
worker(); worker();
} }
@ -28,6 +29,7 @@ void Window::onEvent(const struct Ipc::ipc_response& res) {
window_ = Glib::Markup::escape_text(data["container"]["name"].asString()); window_ = Glib::Markup::escape_text(data["container"]["name"].asString());
windowId_ = data["container"]["id"].asInt(); windowId_ = data["container"]["id"].asInt();
dp.emit(); dp.emit();
getTree();
} else if ((data["change"] == "close" && data["container"]["focused"].asBool() && } else if ((data["change"] == "close" && data["container"]["focused"].asBool() &&
windowId_ == data["container"]["id"].asInt()) || windowId_ == data["container"]["id"].asInt()) ||
(data["change"] == "focus" && data["current"]["focus"].isArray() && (data["change"] == "focus" && data["current"]["focus"].isArray() &&
@ -35,14 +37,34 @@ void Window::onEvent(const struct Ipc::ipc_response& res) {
window_.clear(); window_.clear();
windowId_ = -1; windowId_ = -1;
dp.emit(); dp.emit();
getTree();
} }
} }
void Window::onCmd(const struct Ipc::ipc_response& res) { void Window::onCmd(const struct Ipc::ipc_response& res) {
auto [id, name] = getFocusedNode(res.payload["nodes"]); auto [nb, id, name, app_id] = getFocusedNode(res.payload);
windowId_ = id; if (nb == 0) {
window_ = name; bar_.window.get_style_context()->add_class("empty");
dp.emit(); } 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() { void Window::worker() {
@ -62,20 +84,29 @@ auto Window::update() -> void {
} }
} }
std::tuple<int, std::string> Window::getFocusedNode(const Json::Value& nodes) { std::tuple<std::size_t, int, std::string, std::string> Window::getFocusedNode(
for (auto const& node : nodes) { const Json::Value& nodes) {
for (auto const& node : nodes["nodes"]) {
if (node["focused"].asBool() && node["type"] == "con") { 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()) { 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 { try {
ipc_.sendCmd(IPC_GET_TREE); ipc_.sendCmd(IPC_GET_TREE);
} catch (const std::exception& e) { } catch (const std::exception& e) {