fix workspace interactivity

This commit is contained in:
2025-12-17 22:45:30 +01:00
parent a912cb9687
commit 11ccd55a52
5 changed files with 125 additions and 176 deletions

View File

@@ -2,7 +2,6 @@
#include <cstddef>
#include <glibmm.h>
#include <iostream>
#include <map>
#include <sigc++/sigc++.h>
#include <string>
@@ -43,7 +42,7 @@ class HyprlandService {
void printMonitor(const Monitor &mon) const;
sigc::signal<void(std::string, std::string)> socketEventSignal;
sigc::signal<void(int)> workspaceStateChanged;
sigc::signal<void()> workspaceStateChanged;
sigc::signal<void()> monitorStateChanged;
Monitor *getMonitorById(int id);
@@ -52,14 +51,14 @@ class HyprlandService {
const Monitor *getMonitorByIndex(std::size_t index) const;
void switchToWorkspace(int workspaceId);
std::map<int, WorkspaceState> getAllWorkspaces() const {
std::map<int, WorkspaceState*> getAllWorkspaces() const {
return this->workspaces;
}
private:
int fd = -1;
std::map<int, Monitor> monitors;
std::map<int, WorkspaceState> workspaces;
std::map<int, WorkspaceState*> workspaces;
std::string get_socket_path();
bool on_socket_read(Glib::IOCondition condition);

View File

@@ -16,9 +16,10 @@ class WorkspaceIndicator : public Gtk::Box {
int monitorId;
sigc::connection workspaceConnection;
sigc::connection monitorConnection;
std::map<int, Gtk::Label *> workspaceLabels;
void rebuild();
void on_workspace_update(int monitorId);
void on_workspace_update();
void on_monitor_update();
void clear_children();
void refreshLabel(Gtk::Label *label, HyprlandService::WorkspaceState state);
};

View File

@@ -82,7 +82,7 @@ void Bar::setup_ui() {
trayWidget = Gtk::make_managed<TrayWidget>(trayService);
right_box.append(*trayWidget);
right_box.append(homeAssistant);
// right_box.append(homeAssistant);
}
void Bar::load_css() {

View File

@@ -1,6 +1,5 @@
#include "services/hyprland.hpp"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iterator>
@@ -35,13 +34,16 @@ HyprlandService::~HyprlandService() {
}
void HyprlandService::on_hyprland_event(std::string event, std::string data) {
if (event == "monitoradded" || event == "monitorremoved") {
refresh_monitors();
}
if (event == "urgent") {
handle_urgent_window(data);
}
if (is_workspace_event(event) || event == "focusedmon" ||
event == "monitoradded" || event == "monitorremoved") {
refresh_monitors();
if (event == "workspace" || event == "focusedmon" || event == "monitoradded" ||
event == "monitorremoved" || event == "movewindow" || event == "activewindow") {
refresh_workspaces();
}
}
@@ -80,7 +82,6 @@ void HyprlandService::start() {
Glib::IOCondition::IO_ERR);
refresh_monitors();
refresh_workspaces();
}
bool HyprlandService::on_socket_read(Glib::IOCondition condition) {
@@ -140,26 +141,11 @@ void HyprlandService::refresh_monitors() {
this->monitors.clear();
this->workspaces.clear();
std::string output;
try {
output = SystemHelper::get_command_output(kMonitorCommand);
} catch (const std::exception &ex) {
std::cerr << "[Hyprland] Failed to query monitors: " << ex.what()
<< std::endl;
return;
}
std::string output = SystemHelper::get_command_output(kMonitorCommand);
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
if (!monitorsJson.is_array()) {
std::cerr << "[Hyprland] Unexpected monitor payload" << std::endl;
return;
}
for (const auto &monitorJson : monitorsJson) {
if (!monitorJson.is_object()) {
continue;
}
assert(monitorJson.is_object());
Monitor monitor;
monitor.id = monitorJson.value("id", -1);
@@ -167,11 +153,7 @@ void HyprlandService::refresh_monitors() {
monitor.x = monitorJson.value("x", 0);
monitor.y = monitorJson.value("y", 0);
if (monitorJson.contains("activeWorkspace") &&
monitorJson["activeWorkspace"].is_object()) {
monitor.focusedWorkspaceId =
monitorJson["activeWorkspace"].value("id", -1);
}
monitor.focusedWorkspaceId = monitorJson["activeWorkspace"].value("id", -1);
if (monitor.id >= 0) {
this->monitors[monitor.id] = monitor;
@@ -181,78 +163,63 @@ void HyprlandService::refresh_monitors() {
WorkspaceState wsState;
wsState.focused = false;
wsState.active = false;
wsState.urgentWindows.clear();
wsState.label = std::to_string(slot);
wsState.monitorId = monitor.id;
int id = slot + monitor.id * HyprlandService::kWorkspaceSlotCount;
wsState.hyprId = id;
this->workspaces[id] = wsState;
this->monitors[monitor.id].workspaceStates[slot] = &this->workspaces[id];
this->workspaces[id] = new WorkspaceState(wsState);
this->monitors[monitor.id].workspaceStates[slot] = this->workspaces[id];
}
}
this->refresh_workspaces();
monitorStateChanged.emit();
}
/**
* Called every time when workspace changes have been detected.
* Used to Update the internal state for the Workspaces,
*/
void HyprlandService::refresh_workspaces() {
if (monitors.empty()) {
return;
}
std::string output;
try {
output = SystemHelper::get_command_output(kWorkspaceCommand);
} catch (const std::exception &ex) {
std::cerr << "[Hyprland] Failed to query workspaces: " << ex.what()
<< std::endl;
return;
}
std::string output = SystemHelper::get_command_output(kWorkspaceCommand);
auto workspacesJson = nlohmann::json::parse(output, nullptr, false);
if (!workspacesJson.is_array()) {
std::cerr << "[Hyprland] Unexpected workspace payload" << std::endl;
return;
for (auto &[id, ws] : this->workspaces) {
ws->focused = false;
ws->active = false;
}
output = SystemHelper::get_command_output(kMonitorCommand);
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &monitorJson : monitorsJson) {
const int monitorId = monitorJson.value("id", -1);
const int focusedWorkspaceId = monitorJson["activeWorkspace"].value("id", -1);
auto monitorIt = this->monitors.find(monitorId);
monitorIt->second.focusedWorkspaceId = focusedWorkspaceId;
}
for (const auto &workspaceJson : workspacesJson) {
if (!workspaceJson.is_object()) {
continue;
}
const int workspaceId = workspaceJson.value("id", -1);
std::map<int, WorkspaceState *>::iterator workspaceStateIt = this->workspaces.find(workspaceId);
WorkspaceState *workspaceState = workspaceStateIt->second;
std::cout << "Workspace ID: " << workspaceId << std::endl;
assert(workspaceId != -1);
workspaceState->focused = monitors
.at(workspaceState->monitorId)
.focusedWorkspaceId ==
workspaceId;
workspaceState->active = true;
std::map<int, WorkspaceState>::iterator workspaceStateIt = this->workspaces.find(workspaceId);
if (workspaceStateIt == this->workspaces.end()) {
assert(false); // should not happen
this->workspaces[workspaceId] = workspaceState;
}
WorkspaceState *workspaceState = &workspaceStateIt->second;
workspaceState->hyprId = workspaceId;
workspaceState->focused = (this->monitors[workspaceState->monitorId].focusedWorkspaceId == workspaceId);
workspaceState->active =
workspaceState->focused || workspaceJson.value("windows", 0) > 0;
workspaceState->urgentWindows.clear();
if (workspaceJson.contains("urgent") &&
workspaceJson["urgent"].is_boolean()) {
if (workspaceJson["urgent"].get<bool>()) {
workspaceState->urgentWindows.push_back(workspaceId);
}
}
this->workspaces[workspaceId] = *workspaceState;
}
for (const auto &pair : monitors) {
workspaceStateChanged.emit(pair.first);
}
workspaceStateChanged.emit();
}
HyprlandService::Monitor *HyprlandService::getMonitorById(int id) {
@@ -275,15 +242,14 @@ const HyprlandService::Monitor *HyprlandService::getMonitorById(int id) const {
return &it->second;
}
HyprlandService::Monitor *
HyprlandService::getMonitorByIndex(std::size_t index) {
HyprlandService::Monitor *HyprlandService::getMonitorByIndex(std::size_t index) {
if (index >= monitors.size()) {
throw std::runtime_error("Monitor index out of bounds: " +
std::to_string(index));
throw std::runtime_error("Monitor index out of bounds: " + std::to_string(index));
}
auto it = monitors.begin();
std::advance(it, static_cast<long>(index));
return &it->second;
}
@@ -300,60 +266,35 @@ void HyprlandService::switchToWorkspace(int workspaceId) {
}
void HyprlandService::handle_urgent_window(std::string windowAddress) {
std::string output;
// TODO: fix bugs first
return;
try {
output = SystemHelper::get_command_output(kClientsCommand);
} catch (const std::exception &ex) {
std::cerr << "[Hyprland] Failed to query clients: " << ex.what()
<< std::endl;
return;
}
std::string output = SystemHelper::get_command_output(kClientsCommand);
auto clientsJson = nlohmann::json::parse(output, nullptr, false);
if (!clientsJson.is_array()) {
return;
}
int workspaceId = -1;
for (const auto &client : clientsJson) {
if (!client.is_object())
for (const auto &clientJson : clientsJson) {
if (!clientJson.is_object()) {
continue;
std::string addr = client.value("address", "");
if (addr == "0x" + windowAddress) {
if (client.contains("workspace") &&
client["workspace"].is_object()) {
workspaceId = client["workspace"].value("id", -1);
}
const std::string addr = clientJson.value("address", "");
if (addr != windowAddress) {
continue;
}
const int workspaceId = clientJson.value("workspace", -1);
if (workspaceId < 0) {
continue;
}
auto workspaceIt = this->workspaces.find(workspaceId);
if (workspaceIt == this->workspaces.end()) {
continue;
}
WorkspaceState *wsState = workspaceIt->second;
wsState->urgentWindows.push_back(1);
break;
}
}
if (workspaceId == -1) {
return;
}
for (auto &pair : monitors) {
auto &monitor = pair.second;
bool changed = false;
for (auto &wsPair : monitor.workspaceStates) {
if (wsPair.second->hyprId == workspaceId) {
if (wsPair.second->urgentWindows.empty()) {
wsPair.second->urgentWindows.push_back(workspaceId);
changed = true;
}
}
}
if (changed) {
this->workspaceStateChanged.emit(monitor.id);
}
}
workspaceStateChanged.emit();
}
void HyprlandService::printMonitor(const Monitor &mon) const {

View File

@@ -1,5 +1,7 @@
#include "widgets/workspaceIndicator.hpp"
#include "services/hyprland.hpp"
#include <cassert>
#include <gdk/gdk.h>
#include <gtkmm/gestureclick.h>
#include <gtkmm/widget.h>
@@ -16,6 +18,23 @@ WorkspaceIndicator::WorkspaceIndicator(HyprlandService &service, int monitorId)
monitorConnection = service.monitorStateChanged.connect(
sigc::mem_fun(*this, &WorkspaceIndicator::on_monitor_update));
for (int i = 1; i <= HyprlandService::kWorkspaceSlotCount; ++i) {
auto label = Gtk::make_managed<Gtk::Label>(std::to_string(i));
label->add_css_class("workspace-pill");
auto gesture = Gtk::GestureClick::create();
gesture->set_button(GDK_BUTTON_PRIMARY);
gesture->signal_released().connect(
[this, i, &service](int, double, double) {
service.switchToWorkspace(
i + this->monitorId * HyprlandService::kWorkspaceSlotCount);
});
label->add_controller(gesture);
workspaceLabels[i] = label;
append(*label);
}
rebuild();
}
@@ -29,53 +48,42 @@ WorkspaceIndicator::~WorkspaceIndicator() {
}
}
void WorkspaceIndicator::on_workspace_update(int monitorId) {
if (this->monitorId != monitorId && monitorId != -1) {
return;
}
void WorkspaceIndicator::on_workspace_update() {
rebuild();
}
void WorkspaceIndicator::on_monitor_update() { rebuild(); }
void WorkspaceIndicator::rebuild() {
HyprlandService hypr = this->service;
int i = 0;
for (auto &[id, workspaceState] : hypr.getAllWorkspaces()) {
if (workspaceState.monitorId != this->monitorId) {
continue;
}
auto label = Gtk::make_managed<Gtk::Label>(workspaceState.label);
label->add_css_class("workspace-pill");
void WorkspaceIndicator::refreshLabel(Gtk::Label *label, HyprlandService::WorkspaceState state) {
label->remove_css_class("workspace-pill-active");
label->remove_css_class("workspace-pill-focused");
label->remove_css_class("workspace-pill-urgent");
auto gesture = Gtk::GestureClick::create();
gesture->set_button(GDK_BUTTON_PRIMARY);
gesture->signal_released().connect(
[this, id](int, double, double) { service.switchToWorkspace(id); });
[this, state](int, double, double) {
service.switchToWorkspace(state.hyprId);
});
label->add_controller(gesture);
if (workspaceState.urgentWindows.empty()) {
if (workspaceState.focused) {
if (state.urgentWindows.size() > 0) {
label->add_css_class("workspace-pill-urgent");
} else {
if (state.focused) {
label->add_css_class("workspace-pill-focused");
} else if (workspaceState.active) {
} else if (state.active) {
label->add_css_class("workspace-pill-active");
}
} else {
label->add_css_class("workspace-pill-urgent");
}
append(*label);
}
}
void WorkspaceIndicator::clear_children() {
Gtk::Widget *child = get_first_child();
while (child != nullptr) {
Gtk::Widget *next = child->get_next_sibling();
remove(*child);
child = next;
void WorkspaceIndicator::rebuild() {
HyprlandService::Monitor *mon = service.getMonitorById(this->monitorId);
for (auto [id, workspaceState] : mon->workspaceStates) {
assert(id > 0);
Gtk::Label *label = workspaceLabels[id];
this->refreshLabel(label, *workspaceState);
}
}