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

@@ -13,6 +13,7 @@ TrayIconWidget::TrayIconWidget(TrayService &service, std::string id)
set_focusable(false);
set_valign(Gtk::Align::CENTER);
set_halign(Gtk::Align::CENTER);
add_css_class("tray-icon");
picture.set_halign(Gtk::Align::CENTER);
picture.set_valign(Gtk::Align::CENTER);

View File

@@ -17,15 +17,10 @@ VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
append(label);
// Click toggles mute using wpctl
click = Gtk::GestureClick::create();
click->set_button(GDK_BUTTON_PRIMARY);
// signal_released provides (int, double, double) — use lambda to ignore
// args
click->signal_released().connect([this](int /*n_press*/, double /*x*/,
double /*y*/) {
click->signal_released().connect([this](int, double, double) {
try {
// Toggle mute then refresh
(void)SystemHelper::get_command_output(
"wpctl set-mute @DEFAULT_SINK@ toggle");
} catch (const std::exception &ex) {
@@ -34,19 +29,17 @@ VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
}
this->update();
});
add_controller(click);
// Initial read
add_controller(click);
update();
// Start polling every 1 second to keep the display up to date
timeoutConn = Glib::signal_timeout().connect(
this->timeoutConn = Glib::signal_timeout().connect(
sigc::mem_fun(*this, &VolumeWidget::on_timeout), 100);
}
VolumeWidget::~VolumeWidget() {
if (timeoutConn.connected())
timeoutConn.disconnect();
if (this->timeoutConn.connected())
this->timeoutConn.disconnect();
}
void VolumeWidget::update() {
@@ -54,7 +47,6 @@ void VolumeWidget::update() {
const std::string out =
SystemHelper::get_command_output("wpctl get-volume @DEFAULT_SINK@");
// Attempt to parse a number (percentage or fraction)
std::smatch m;
std::regex r_percent(R"((\d+(?:\.\d+)?)%)");
std::regex r_number(R"((\d+(?:\.\d+)?))");
@@ -65,7 +57,6 @@ void VolumeWidget::update() {
if (std::regex_search(text, m, r_percent)) {
percent = static_cast<int>(std::round(std::stod(m[1].str())));
} else if (std::regex_search(text, m, r_number)) {
// If number looks like 0.8 treat as fraction
const double v = std::stod(m[1].str());
if (v <= 1.0)
percent = static_cast<int>(std::round(v * 100.0));
@@ -76,7 +67,6 @@ void VolumeWidget::update() {
if (percent >= 0) {
label.set_text(std::to_string(percent) + "%");
} else {
// Fallback to raw output (trimmed)
auto pos = text.find_first_not_of(" \t\n\r");
if (pos != std::string::npos) {
auto end = text.find_last_not_of(" \t\n\r");
@@ -94,5 +84,6 @@ void VolumeWidget::update() {
bool VolumeWidget::on_timeout() {
update();
return true; // keep timeout active
}

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);