feat(Window): handle closed window

pull/46/head
Alexis 2018-09-18 21:16:35 +02:00
parent d914429194
commit d5d620e72d
No known key found for this signature in database
GPG Key ID: 20B1EC2EBBA96BD9
2 changed files with 21 additions and 9 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <fmt/format.h> #include <fmt/format.h>
#include <tuple>
#include "bar.hpp" #include "bar.hpp"
#include "client.hpp" #include "client.hpp"
#include "util/chrono.hpp" #include "util/chrono.hpp"
@ -16,7 +17,7 @@ class Window : public ALabel {
auto update() -> void; auto update() -> void;
private: private:
void worker(); void worker();
std::string getFocusedNode(Json::Value nodes); std::tuple<int, std::string> getFocusedNode(Json::Value nodes);
void getFocusedWindow(); void getFocusedWindow();
Bar& bar_; Bar& bar_;
@ -24,6 +25,7 @@ class Window : public ALabel {
util::JsonParser parser_; util::JsonParser parser_;
Ipc ipc_; Ipc ipc_;
std::string window_; std::string window_;
int windowId_;
}; };
} }

View File

@ -1,7 +1,7 @@
#include "modules/sway/window.hpp" #include "modules/sway/window.hpp"
waybar::modules::sway::Window::Window(Bar &bar, const Json::Value& config) waybar::modules::sway::Window::Window(Bar &bar, const Json::Value& config)
: ALabel(config, "{}"), bar_(bar) : ALabel(config, "{}"), bar_(bar), windowId_(-1)
{ {
label_.set_name("window"); label_.set_name("window");
ipc_.connect(); ipc_.connect();
@ -20,6 +20,13 @@ void waybar::modules::sway::Window::worker()
if ((parsed["change"] == "focus" || parsed["change"] == "title") if ((parsed["change"] == "focus" || parsed["change"] == "title")
&& parsed["container"]["focused"].asBool()) { && parsed["container"]["focused"].asBool()) {
window_ = parsed["container"]["name"].asString(); window_ = parsed["container"]["name"].asString();
windowId_ = parsed["container"]["id"].asInt();
dp.emit();
} else if (parsed["change"] == "close"
&& parsed["container"]["focused"].asBool()
&& windowId_ == parsed["container"]["id"].asInt()) {
window_.clear();
windowId_ = -1;
dp.emit(); dp.emit();
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
@ -34,18 +41,19 @@ auto waybar::modules::sway::Window::update() -> void
label_.set_tooltip_text(window_); label_.set_tooltip_text(window_);
} }
std::string waybar::modules::sway::Window::getFocusedNode(Json::Value nodes) std::tuple<int, std::string> waybar::modules::sway::Window::getFocusedNode(
Json::Value nodes)
{ {
for (auto &node : nodes) { for (auto &node : nodes) {
if (node["focused"].asBool() && node["type"] == "con") { if (node["focused"].asBool() && node["type"] == "con") {
return node["name"].asString(); return { node["id"].asInt(), node["name"].asString() };
} }
auto res = getFocusedNode(node["nodes"]); auto [id, name] = getFocusedNode(node["nodes"]);
if (!res.empty()) { if (id > -1 && !name.empty()) {
return res; return { id, name };
} }
} }
return std::string(); return { -1, std::string() };
} }
void waybar::modules::sway::Window::getFocusedWindow() void waybar::modules::sway::Window::getFocusedWindow()
@ -53,7 +61,9 @@ void waybar::modules::sway::Window::getFocusedWindow()
try { try {
auto res = ipc_.sendCmd(IPC_GET_TREE); auto res = ipc_.sendCmd(IPC_GET_TREE);
auto parsed = parser_.parse(res.payload); auto parsed = parser_.parse(res.payload);
window_ = getFocusedNode(parsed["nodes"]); auto [id, name] = getFocusedNode(parsed["nodes"]);
windowId_ = id;
window_ = name;
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update)); Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update));
} catch (const std::exception &e) { } catch (const std::exception &e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;