optimized the code, added some bugs :)

This commit is contained in:
2025-12-17 16:12:15 +01:00
parent f1c68321a7
commit a912cb9687
10 changed files with 148 additions and 205 deletions

View File

@@ -1,6 +1,5 @@
#include "widgets/workspaceIndicator.hpp"
#include <exception>
#include <gdk/gdk.h>
#include <gtkmm/gestureclick.h>
#include <gtkmm/widget.h>
@@ -41,55 +40,31 @@ void WorkspaceIndicator::on_workspace_update(int monitorId) {
void WorkspaceIndicator::on_monitor_update() { rebuild(); }
void WorkspaceIndicator::rebuild() {
clear_children();
HyprlandService::Monitor *monitor = nullptr;
try {
monitor = service.getMonitorById(monitorId);
} catch (const std::exception &) {
return;
}
if (monitor == nullptr) {
return;
}
for (int workspaceId = 1;
workspaceId <= HyprlandService::kWorkspaceSlotCount; ++workspaceId) {
const HyprlandService::WorkspaceState *state = nullptr;
auto it = monitor->workspaceStates.find(workspaceId);
if (it != monitor->workspaceStates.end()) {
state = &it->second;
HyprlandService hypr = this->service;
int i = 0;
for (auto &[id, workspaceState] : hypr.getAllWorkspaces()) {
if (workspaceState.monitorId != this->monitorId) {
continue;
}
const std::string display = (state && !state->label.empty())
? state->label
: std::to_string(workspaceId);
auto label = Gtk::make_managed<Gtk::Label>(display);
auto label = Gtk::make_managed<Gtk::Label>(workspaceState.label);
label->add_css_class("workspace-pill");
auto gesture = Gtk::GestureClick::create();
gesture->set_button(GDK_BUTTON_PRIMARY);
gesture->signal_released().connect(
[this, workspaceId](int /*n_press*/, double /*x*/, double /*y*/) {
int realWorkspaceId = workspaceId + 5 * (monitorId);
service.switchToWorkspace(realWorkspaceId);
});
[this, id](int, double, double) { service.switchToWorkspace(id); });
label->add_controller(gesture);
if (state != nullptr) {
if (state->urgent != true) {
if (state->focused) {
label->add_css_class("workspace-pill-focused");
} else if (state->active) {
label->add_css_class("workspace-pill-active");
}
} else {
label->add_css_class("workspace-pill-urgent");
if (workspaceState.urgentWindows.empty()) {
if (workspaceState.focused) {
label->add_css_class("workspace-pill-focused");
} else if (workspaceState.active) {
label->add_css_class("workspace-pill-active");
}
} else {
label->add_css_class("workspace-pill-urgent");
}
append(*label);