hyprland/window: add .floating and .fullscreen CSS classes

This commit is contained in:
gardenapple 2023-06-20 00:42:19 +03:00
parent e233022d1a
commit 4f14ce3285
No known key found for this signature in database
GPG Key ID: 30A642B65B1529FE
2 changed files with 41 additions and 25 deletions

View File

@ -16,6 +16,7 @@ class Window : public waybar::ALabel, public EventHandler {
private: private:
struct Workspace { struct Workspace {
int id;
int windows; int windows;
std::string last_window; std::string last_window;
std::string last_window_title; std::string last_window_title;
@ -25,7 +26,6 @@ class Window : public waybar::ALabel, public EventHandler {
auto getActiveWorkspace(const std::string&) -> Workspace; auto getActiveWorkspace(const std::string&) -> Workspace;
auto getActiveWorkspace() -> Workspace; auto getActiveWorkspace() -> Workspace;
auto getWindowClass(const std::string&) -> std::string;
void onEvent(const std::string&) override; void onEvent(const std::string&) override;
void queryActiveWorkspace(); void queryActiveWorkspace();
void setClass(const std::string&, bool enable); void setClass(const std::string&, bool enable);
@ -38,6 +38,8 @@ class Window : public waybar::ALabel, public EventHandler {
Workspace workspace_; Workspace workspace_;
std::string solo_class_; std::string solo_class_;
std::string last_solo_class_; std::string last_solo_class_;
bool fullscreen_;
bool all_floating_;
}; };
} // namespace waybar::modules::hyprland } // namespace waybar::modules::hyprland

View File

@ -2,19 +2,20 @@
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <algorithm>
#include <regex> #include <regex>
#include <util/sanitize_str.hpp>
#include "modules/hyprland/backend.hpp" #include "modules/hyprland/backend.hpp"
#include "util/json.hpp" #include "util/json.hpp"
#include "util/rewrite_string.hpp" #include "util/rewrite_string.hpp"
#include <util/sanitize_str.hpp>
namespace waybar::modules::hyprland { namespace waybar::modules::hyprland {
Window::Window(const std::string& id, const Bar& bar, const Json::Value& config) Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
: ALabel(config, "window", id, "{}", 0, true), bar_(bar) { : ALabel(config, "window", id, "{}", 0, true), bar_(bar) {
modulesReady = true; modulesReady = true;
separate_outputs = config["separate-outputs"].as<bool>(); separate_outputs = config["separate-outputs"].asBool();
if (!gIPC.get()) { if (!gIPC.get()) {
gIPC = std::make_unique<IPC>(); gIPC = std::make_unique<IPC>();
@ -27,6 +28,8 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
gIPC->registerForIPC("activewindow", this); gIPC->registerForIPC("activewindow", this);
gIPC->registerForIPC("closewindow", this); gIPC->registerForIPC("closewindow", this);
gIPC->registerForIPC("movewindow", this); gIPC->registerForIPC("movewindow", this);
gIPC->registerForIPC("changefloatingmode", this);
gIPC->registerForIPC("fullscreen", this);
} }
Window::~Window() { Window::~Window() {
@ -62,6 +65,8 @@ auto Window::update() -> void {
setClass("empty", workspace_.windows == 0); setClass("empty", workspace_.windows == 0);
setClass("solo", workspace_.windows == 1); setClass("solo", workspace_.windows == 1);
setClass("fullscreen", fullscreen_);
setClass("floating", all_floating_);
if (!last_solo_class_.empty() && solo_class_ != last_solo_class_) { if (!last_solo_class_.empty() && solo_class_ != last_solo_class_) {
if (bar_.window.get_style_context()->has_class(last_solo_class_)) { if (bar_.window.get_style_context()->has_class(last_solo_class_)) {
@ -71,10 +76,10 @@ auto Window::update() -> void {
} }
if (!solo_class_.empty() && solo_class_ != last_solo_class_) { if (!solo_class_.empty() && solo_class_ != last_solo_class_) {
last_solo_class_ = solo_class_;
bar_.window.get_style_context()->add_class(solo_class_); bar_.window.get_style_context()->add_class(solo_class_);
spdlog::trace("Adding solo class: {}", solo_class_); spdlog::trace("Adding solo class: {}", solo_class_);
} }
last_solo_class_ = solo_class_;
ALabel::update(); ALabel::update();
} }
@ -94,9 +99,9 @@ auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace {
[&](Json::Value monitor) { return monitor["name"] == monitorName; }); [&](Json::Value monitor) { return monitor["name"] == monitorName; });
if (monitor == std::end(json)) { if (monitor == std::end(json)) {
spdlog::warn("Monitor not found: {}", monitorName); spdlog::warn("Monitor not found: {}", monitorName);
return Workspace{0, "", ""}; return Workspace{-1, 0, "", ""};
} }
const int id = (*monitor)["activeWorkspace"]["id"].as<int>(); const int id = (*monitor)["activeWorkspace"]["id"].asInt();
const auto workspaces = gIPC->getSocket1Reply("j/workspaces"); const auto workspaces = gIPC->getSocket1Reply("j/workspaces");
json = parser_.parse(workspaces); json = parser_.parse(workspaces);
@ -105,31 +110,20 @@ auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace {
[&](Json::Value workspace) { return workspace["id"] == id; }); [&](Json::Value workspace) { return workspace["id"] == id; });
if (workspace == std::end(json)) { if (workspace == std::end(json)) {
spdlog::warn("No workspace with id {}", id); spdlog::warn("No workspace with id {}", id);
return Workspace{0, "", ""}; return Workspace{-1, 0, "", ""};
} }
return Workspace::parse(*workspace); return Workspace::parse(*workspace);
} }
auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace { auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace {
return Workspace{ return Workspace{
value["windows"].as<int>(), value["id"].asInt(),
value["lastwindow"].as<std::string>(), value["windows"].asInt(),
value["lastwindowtitle"].as<std::string>() value["lastwindow"].asString(),
value["lastwindowtitle"].asString()
}; };
} }
auto Window::getWindowClass(const std::string& address) -> std::string {
const auto clients = gIPC->getSocket1Reply("j/clients");
Json::Value json = parser_.parse(clients);
assert(json.isArray());
auto client = std::find_if(json.begin(), json.end(),
[&](Json::Value window) { return window["address"] == address; });
if (client == std::end(json)) {
return "";
}
return (*client)["class"].as<std::string>();
}
void Window::queryActiveWorkspace() { void Window::queryActiveWorkspace() {
std::lock_guard<std::mutex> lg(mutex_); std::lock_guard<std::mutex> lg(mutex_);
@ -139,11 +133,31 @@ void Window::queryActiveWorkspace() {
workspace_ = getActiveWorkspace(); workspace_ = getActiveWorkspace();
} }
if (workspace_.windows == 1) {
solo_class_ = getWindowClass(workspace_.last_window); if (workspace_.windows > 0) {
const auto clients = gIPC->getSocket1Reply("j/clients");
Json::Value json = parser_.parse(clients);
assert(json.isArray());
auto active_window = std::find_if(json.begin(), json.end(),
[&](Json::Value window) { return window["address"] == workspace_.last_window; });
if (active_window == std::end(json)) {
return;
}
if (workspace_.windows == 1 && !(*active_window)["floating"].asBool()) {
solo_class_ = (*active_window)["class"].asString();
} else { } else {
solo_class_ = ""; solo_class_ = "";
} }
all_floating_ = std::all_of(json.begin(), json.end(),
[&](Json::Value window) { return window["floating"].asBool() ||
window["workspace"]["id"] != workspace_.id; });
fullscreen_ = (*active_window)["fullscreen"].asBool();
} else {
solo_class_ = "";
all_floating_ = false;
fullscreen_ = false;
}
} }
void Window::onEvent(const std::string& ev) { void Window::onEvent(const std::string& ev) {