fix code style

This commit is contained in:
2025-12-10 17:58:01 +01:00
parent 4d61a80a7c
commit 0b3a6c4696
19 changed files with 1016 additions and 1366 deletions

View File

@@ -8,7 +8,7 @@ App::App() {
this->setupServices();
this->app = Gtk::Application::create("org.example.mybar");
app->signal_activate().connect([&]() {
auto display = Gdk::Display::get_default();
auto monitors = display->get_monitors();
@@ -17,16 +17,19 @@ App::App() {
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(
monitors->get_object(i));
if (monitor) {
HyprlandService::Monitor* hyprlandMonitor = nullptr;
HyprlandService::Monitor *hyprlandMonitor = nullptr;
try {
hyprlandMonitor = this->hyprlandService.getMonitorByIndex(i);
hyprlandMonitor =
this->hyprlandService.getMonitorByIndex(i);
} catch (const std::exception &ex) {
std::cerr << "[App] Failed to fetch Hyprland monitor: " << ex.what() << std::endl;
std::cerr << "[App] Failed to fetch Hyprland monitor: "
<< ex.what() << std::endl;
continue;
}
auto bar = new Bar(monitor->gobj(), this->hyprlandService, this->trayService, hyprlandMonitor->id);
auto bar = new Bar(monitor->gobj(), this->hyprlandService,
this->trayService, hyprlandMonitor->id);
bar->set_application(app);
bar->show();
@@ -53,6 +56,4 @@ void App::setupServices() {
this->trayService.start();
}
int App::run() {
return this->app->run();
}
int App::run() { return this->app->run(); }

View File

@@ -1,8 +1,8 @@
#include "bar/bar.hpp"
#include "gtk/gtk.h"
#include "widgets/spacer.hpp"
#include "widgets/workspaceIndicator.hpp"
#include "widgets/volumeWidget.hpp"
#include "widgets/workspaceIndicator.hpp"
#include "helpers/systemHelper.hpp"
@@ -13,16 +13,15 @@
#include "glibmm/main.h"
#include "sigc++/functors/mem_fun.h"
Bar::Bar(GdkMonitor *monitor, HyprlandService &hyprlandService, TrayService &trayService, int monitorId)
: m_hyprlandService(hyprlandService), m_trayService(trayService), m_monitorId(monitorId)
{
// Name the window so CSS can be scoped specifically to this bar.
Bar::Bar(GdkMonitor *monitor, HyprlandService &hyprlandService,
TrayService &trayService, int monitorId)
: hyprlandService(hyprlandService), trayService(trayService),
monitorId(monitorId) {
set_name("bar-window");
gtk_layer_init_for_window(this->gobj());
if (monitor)
{
if (monitor) {
gtk_layer_set_monitor(this->gobj(), monitor);
}
@@ -39,15 +38,11 @@ Bar::Bar(GdkMonitor *monitor, HyprlandService &hyprlandService, TrayService &tra
clock.onUpdate();
Glib::signal_timeout().connect(
sigc::mem_fun(
clock,
&Clock::onUpdate),
1000);
Glib::signal_timeout().connect(sigc::mem_fun(clock, &Clock::onUpdate),
1000);
}
void Bar::setup_ui()
{
void Bar::setup_ui() {
main_box.set_hexpand(true);
main_box.set_start_widget(left_box);
@@ -64,8 +59,9 @@ void Bar::setup_ui()
right_box.set_valign(Gtk::Align::CENTER);
m_workspaceIndicator = Gtk::make_managed<WorkspaceIndicator>(m_hyprlandService, m_monitorId);
left_box.append(*m_workspaceIndicator);
workspaceIndicator =
Gtk::make_managed<WorkspaceIndicator>(hyprlandService, monitorId);
left_box.append(*workspaceIndicator);
clock.set_name("clock-label");
clock.set_hexpand(false);
@@ -74,24 +70,21 @@ void Bar::setup_ui()
center_box.append(clock);
center_box.append(*(new Spacer()));
// Volume widget placed after spacer
m_volumeWidget = Gtk::make_managed<VolumeWidget>();
center_box.append(*m_volumeWidget);
volumeWidget = Gtk::make_managed<VolumeWidget>();
center_box.append(*volumeWidget);
m_trayWidget = Gtk::make_managed<TrayWidget>(m_trayService);
right_box.append(*m_trayWidget);
trayWidget = Gtk::make_managed<TrayWidget>(trayService);
right_box.append(*trayWidget);
}
void Bar::load_css()
{
void Bar::load_css() {
auto css_provider = Gtk::CssProvider::create();
// Load CSS from external resource file. Fall back to embedded CSS on error.
const std::string css = SystemHelper::read_file_to_string("resources/bar.css");
const std::string css =
SystemHelper::read_file_to_string("resources/bar.css");
css_provider->load_from_data(css);
Gtk::StyleContext::add_provider_for_display(
Gdk::Display::get_default(),
css_provider,
Gdk::Display::get_default(), css_provider,
GTK_STYLE_PROVIDER_PRIORITY_USER + 1);
}

View File

@@ -14,48 +14,41 @@
#include "helpers/systemHelper.hpp"
namespace
{
const char *kMonitorCommand = "hyprctl monitors -j";
namespace {
const char *kMonitorCommand = "hyprctl monitors -j";
const char *kWorkspaceCommand = "hyprctl workspaces -j";
bool is_workspace_event(const std::string &event)
{
bool is_workspace_event(const std::string &event) {
return event.find("workspace") != std::string::npos;
}
}
} // namespace
HyprlandService::HyprlandService() = default;
HyprlandService::~HyprlandService()
{
if (m_fd != -1)
{
close(m_fd);
m_fd = -1;
HyprlandService::~HyprlandService() {
if (fd != -1) {
close(fd);
fd = -1;
}
}
void HyprlandService::on_hyprland_event(std::string event, std::string /*data*/)
{
if (is_workspace_event(event) || event == "focusedmon" || event == "monitoradded" || event == "monitorremoved")
{
void HyprlandService::on_hyprland_event(std::string event,
std::string /*data*/) {
if (is_workspace_event(event) || event == "focusedmon" ||
event == "monitoradded" || event == "monitorremoved") {
refresh_monitors();
refresh_workspaces();
}
}
void HyprlandService::start()
{
void HyprlandService::start() {
const std::string socket_path = get_socket_path();
if (socket_path.empty())
{
if (socket_path.empty()) {
return;
}
m_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (m_fd == -1)
{
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
std::cerr << "[Hyprland] Failed to create socket" << std::endl;
return;
}
@@ -65,63 +58,59 @@ void HyprlandService::start()
addr.sun_family = AF_UNIX;
std::strncpy(addr.sun_path, socket_path.c_str(), sizeof(addr.sun_path) - 1);
if (connect(m_fd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)) == -1)
{
std::cerr << "[Hyprland] Failed to connect to " << socket_path << std::endl;
close(m_fd);
m_fd = -1;
if (connect(fd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)) ==
-1) {
std::cerr << "[Hyprland] Failed to connect to " << socket_path
<< std::endl;
close(fd);
fd = -1;
return;
}
std::cout << "[Hyprland] Connected to event socket." << std::endl;
Glib::signal_io().connect(
sigc::mem_fun(*this, &HyprlandService::on_socket_read),
m_fd,
Glib::IOCondition::IO_IN | Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR);
sigc::mem_fun(*this, &HyprlandService::on_socket_read), fd,
Glib::IOCondition::IO_IN | Glib::IOCondition::IO_HUP |
Glib::IOCondition::IO_ERR);
refresh_monitors();
refresh_workspaces();
}
bool HyprlandService::on_socket_read(Glib::IOCondition condition)
{
const auto error_mask = Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR;
bool HyprlandService::on_socket_read(Glib::IOCondition condition) {
const auto error_mask =
Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR;
if (static_cast<int>(condition & error_mask) != 0)
{
if (static_cast<int>(condition & error_mask) != 0) {
std::cerr << "[Hyprland] Socket disconnected." << std::endl;
close(m_fd);
m_fd = -1;
close(fd);
fd = -1;
return false;
}
char buffer[4096];
const ssize_t bytes_read = read(m_fd, buffer, sizeof(buffer) - 1);
const ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
if (bytes_read > 0)
{
if (bytes_read > 0) {
buffer[bytes_read] = '\0';
m_buffer.append(buffer);
this->buffer.append(buffer);
size_t pos = 0;
while ((pos = m_buffer.find('\n')) != std::string::npos)
{
const std::string line = m_buffer.substr(0, pos);
while ((pos = this->buffer.find('\n')) != std::string::npos) {
const std::string line = this->buffer.substr(0, pos);
parse_message(line);
m_buffer.erase(0, pos + 1);
this->buffer.erase(0, pos + 1);
}
}
return true;
}
void HyprlandService::parse_message(const std::string &line)
{
void HyprlandService::parse_message(const std::string &line) {
const size_t split = line.find(">>");
if (split != std::string::npos)
{
if (split != std::string::npos) {
const std::string event_name = line.substr(0, split);
const std::string event_data = line.substr(split + 2);
@@ -129,13 +118,11 @@ void HyprlandService::parse_message(const std::string &line)
}
}
std::string HyprlandService::get_socket_path()
{
const char *sig = std::getenv("HYPRLAND_INSTANCE_SIGNATURE");
std::string HyprlandService::get_socket_path() {
const char *sig = std::getenv("HYPRLAND_INSTANCE_SIGNATURE");
const char *runtime = std::getenv("XDG_RUNTIME_DIR");
if (!sig || !runtime)
{
if (!sig || !runtime) {
std::cerr << "[Hyprland] Environment variables missing!" << std::endl;
return "";
}
@@ -143,232 +130,200 @@ std::string HyprlandService::get_socket_path()
return std::string(runtime) + "/hypr/" + sig + "/.socket2.sock";
}
void HyprlandService::refresh_monitors()
{
void HyprlandService::refresh_monitors() {
std::string output;
try
{
try {
output = SystemHelper::get_command_output(kMonitorCommand);
}
catch (const std::exception &ex)
{
std::cerr << "[Hyprland] Failed to query monitors: " << ex.what() << std::endl;
} catch (const std::exception &ex) {
std::cerr << "[Hyprland] Failed to query monitors: " << ex.what()
<< std::endl;
return;
}
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
if (!monitorsJson.is_array())
{
if (!monitorsJson.is_array()) {
std::cerr << "[Hyprland] Unexpected monitor payload" << std::endl;
return;
}
std::map<int, Monitor> updated;
for (const auto &monitorJson : monitorsJson)
{
if (!monitorJson.is_object())
{
for (const auto &monitorJson : monitorsJson) {
if (!monitorJson.is_object()) {
continue;
}
Monitor monitor;
monitor.id = monitorJson.value("id", -1);
monitor.id = monitorJson.value("id", -1);
monitor.name = monitorJson.value("name", "");
monitor.x = monitorJson.value("x", 0);
monitor.y = monitorJson.value("y", 0);
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);
if (monitorJson.contains("activeWorkspace") &&
monitorJson["activeWorkspace"].is_object()) {
monitor.focusedWorkspaceId =
monitorJson["activeWorkspace"].value("id", -1);
}
if (monitor.id >= 0)
{
if (monitor.id >= 0) {
updated.emplace(monitor.id, std::move(monitor));
}
}
m_monitors.swap(updated);
monitors.swap(updated);
monitorStateChanged.emit();
}
void HyprlandService::refresh_workspaces()
{
if (m_monitors.empty())
{
void HyprlandService::refresh_workspaces() {
if (monitors.empty()) {
return;
}
std::string output;
try
{
try {
output = SystemHelper::get_command_output(kWorkspaceCommand);
}
catch (const std::exception &ex)
{
std::cerr << "[Hyprland] Failed to query workspaces: " << ex.what() << std::endl;
} catch (const std::exception &ex) {
std::cerr << "[Hyprland] Failed to query workspaces: " << ex.what()
<< std::endl;
return;
}
auto workspacesJson = nlohmann::json::parse(output, nullptr, false);
if (!workspacesJson.is_array())
{
if (!workspacesJson.is_array()) {
std::cerr << "[Hyprland] Unexpected workspace payload" << std::endl;
return;
}
for (auto &pair : m_monitors)
{
for (auto &pair : monitors) {
auto &monitor = pair.second;
monitor.workspaceStates.clear();
int focusedSlot = -1;
if (monitor.focusedWorkspaceId > 0)
{
focusedSlot = ((monitor.focusedWorkspaceId - 1) % HyprlandService::kWorkspaceSlotCount) + 1;
if (monitor.focusedWorkspaceId > 0) {
focusedSlot = ((monitor.focusedWorkspaceId - 1) %
HyprlandService::kWorkspaceSlotCount) +
1;
}
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount; ++slot)
{
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount;
++slot) {
WorkspaceState state;
state.id = slot;
state.hyprId = -1;
state.label = std::to_string(slot);
state.id = slot;
state.hyprId = -1;
state.label = std::to_string(slot);
state.focused = (slot == focusedSlot);
state.active = state.focused;
state.urgent = false;
state.active = state.focused;
state.urgent = false;
monitor.workspaceStates.emplace(slot, state);
}
}
for (const auto &workspaceJson : workspacesJson)
{
if (!workspaceJson.is_object())
{
for (const auto &workspaceJson : workspacesJson) {
if (!workspaceJson.is_object()) {
continue;
}
const int monitorId = workspaceJson.value("monitorID", -1);
const int monitorId = workspaceJson.value("monitorID", -1);
const int workspaceId = workspaceJson.value("id", -1);
auto monitorIt = m_monitors.find(monitorId);
if (monitorIt == m_monitors.end() || workspaceId < 0)
{
auto monitorIt = monitors.find(monitorId);
if (monitorIt == monitors.end() || workspaceId < 0) {
continue;
}
auto &monitor = monitorIt->second;
const int slot = ((workspaceId - 1) % HyprlandService::kWorkspaceSlotCount) + 1;
const int slot =
((workspaceId - 1) % HyprlandService::kWorkspaceSlotCount) + 1;
auto &workspaceState = monitor.workspaceStates[slot];
workspaceState.id = slot;
workspaceState.hyprId = workspaceId;
workspaceState.id = slot;
workspaceState.hyprId = workspaceId;
workspaceState.focused = (monitor.focusedWorkspaceId == workspaceId);
workspaceState.active = workspaceState.focused || workspaceJson.value("windows", 0) > 0;
workspaceState.urgent = false;
workspaceState.active =
workspaceState.focused || workspaceJson.value("windows", 0) > 0;
workspaceState.urgent = false;
std::string labelCandidate;
if (workspaceJson.contains("name") && workspaceJson["name"].is_string())
{
if (workspaceJson.contains("name") &&
workspaceJson["name"].is_string()) {
labelCandidate = workspaceJson["name"].get<std::string>();
}
if (labelCandidate.empty() || labelCandidate == std::to_string(workspaceId))
{
if (labelCandidate.empty() ||
labelCandidate == std::to_string(workspaceId)) {
workspaceState.label = std::to_string(slot);
}
else
{
} else {
workspaceState.label = labelCandidate;
}
if (workspaceJson.contains("urgent") && workspaceJson["urgent"].is_boolean())
{
if (workspaceJson.contains("urgent") &&
workspaceJson["urgent"].is_boolean()) {
workspaceState.urgent = workspaceJson["urgent"].get<bool>();
}
else if (workspaceJson.contains("hasurgent") && workspaceJson["hasurgent"].is_boolean())
{
} else if (workspaceJson.contains("hasurgent") &&
workspaceJson["hasurgent"].is_boolean()) {
workspaceState.urgent = workspaceJson["hasurgent"].get<bool>();
}
}
for (const auto &pair : m_monitors)
{
for (const auto &pair : monitors) {
workspaceStateChanged.emit(pair.first);
}
}
HyprlandService::Monitor *HyprlandService::getMonitorById(int id)
{
auto it = m_monitors.find(id);
if (it == m_monitors.end())
{
throw std::runtime_error("Monitor with ID " + std::to_string(id) + " not found.");
HyprlandService::Monitor *HyprlandService::getMonitorById(int id) {
auto it = monitors.find(id);
if (it == monitors.end()) {
throw std::runtime_error("Monitor with ID " + std::to_string(id) +
" not found.");
}
return &it->second;
}
const HyprlandService::Monitor *HyprlandService::getMonitorById(int id) const
{
auto it = m_monitors.find(id);
if (it == m_monitors.end())
{
throw std::runtime_error("Monitor with ID " + std::to_string(id) + " not found.");
const HyprlandService::Monitor *HyprlandService::getMonitorById(int id) const {
auto it = monitors.find(id);
if (it == monitors.end()) {
throw std::runtime_error("Monitor with ID " + std::to_string(id) +
" not found.");
}
return &it->second;
}
HyprlandService::Monitor *HyprlandService::getMonitorByIndex(std::size_t index)
{
if (index >= m_monitors.size())
{
throw std::runtime_error("Monitor index out of bounds: " + std::to_string(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));
}
auto it = m_monitors.begin();
auto it = monitors.begin();
std::advance(it, static_cast<long>(index));
return &it->second;
}
void HyprlandService::switchToWorkspace(int monitorId, int slot)
{
auto it = m_monitors.find(monitorId);
if (it == m_monitors.end())
{
std::cerr << "[Hyprland] switchToWorkspace: monitor " << monitorId << " not found\n";
return;
}
void HyprlandService::switchToWorkspace(int workspaceId) {
std::string cmd =
"hyprctl dispatch workspace " + std::to_string(workspaceId);
const auto &monitor = it->second;
auto wsIt = monitor.workspaceStates.find(slot);
std::string cmd;
// Use the Hyprland workspace id if available
cmd = "hyprctl dispatch workspace " + std::to_string(wsIt->second.hyprId);
try
{
try {
(void)SystemHelper::get_command_output(cmd.c_str());
}
catch (const std::exception &ex)
{
std::cerr << "[Hyprland] Failed to dispatch workspace command: " << ex.what() << " cmd=" << cmd << std::endl;
} catch (const std::exception &ex) {
std::cerr << "[Hyprland] Failed to dispatch workspace command: "
<< ex.what() << " cmd=" << cmd << std::endl;
}
}
const HyprlandService::Monitor *HyprlandService::getMonitorByIndex(std::size_t index) const
{
if (index >= m_monitors.size())
{
throw std::runtime_error("Monitor index out of bounds: " + std::to_string(index));
const HyprlandService::Monitor *
HyprlandService::getMonitorByIndex(std::size_t index) const {
if (index >= monitors.size()) {
throw std::runtime_error("Monitor index out of bounds: " +
std::to_string(index));
}
auto it = m_monitors.begin();
auto it = monitors.begin();
std::advance(it, static_cast<long>(index));
return &it->second;
}

File diff suppressed because it is too large Load Diff

View File

@@ -3,9 +3,9 @@
#include <chrono>
#include <iomanip>
bool Clock::onUpdate()
{
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
bool Clock::onUpdate() {
auto now =
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::stringstream ss;
ss << std::put_time(std::localtime(&now), "%H:%M:%S");

View File

@@ -1,363 +1,326 @@
#include "widgets/tray.hpp"
#include <gtk/gtk.h>
#include <gdkmm/rectangle.h>
#include <gio/gmenu.h>
#include <utility>
#include <gtk/gtk.h>
#include <iostream>
#include <utility>
TrayIconWidget::TrayIconWidget(TrayService &service, std::string id)
: m_service(service), m_id(std::move(id)), m_container(Gtk::Orientation::HORIZONTAL)
{
: service(service), id(std::move(id)),
container(Gtk::Orientation::HORIZONTAL) {
set_has_frame(false);
set_focusable(false);
set_valign(Gtk::Align::CENTER);
set_halign(Gtk::Align::CENTER);
m_picture.set_halign(Gtk::Align::CENTER);
m_picture.set_valign(Gtk::Align::CENTER);
m_picture.set_can_shrink(true);
m_picture.set_size_request(20, 20);
picture.set_halign(Gtk::Align::CENTER);
picture.set_valign(Gtk::Align::CENTER);
picture.set_can_shrink(true);
picture.set_size_request(20, 20);
m_image.set_pixel_size(20);
m_image.set_halign(Gtk::Align::CENTER);
m_image.set_valign(Gtk::Align::CENTER);
image.set_pixel_size(20);
image.set_halign(Gtk::Align::CENTER);
image.set_valign(Gtk::Align::CENTER);
m_container.set_halign(Gtk::Align::CENTER);
m_container.set_valign(Gtk::Align::CENTER);
m_container.append(m_picture);
m_container.append(m_image);
container.set_halign(Gtk::Align::CENTER);
container.set_valign(Gtk::Align::CENTER);
container.append(picture);
container.append(image);
m_picture.set_visible(false);
m_image.set_visible(true);
set_child(m_container);
picture.set_visible(false);
image.set_visible(true);
set_child(container);
m_primaryGesture = Gtk::GestureClick::create();
m_primaryGesture->set_button(GDK_BUTTON_PRIMARY);
m_primaryGesture->signal_released().connect(sigc::mem_fun(*this, &TrayIconWidget::on_primary_released));
add_controller(m_primaryGesture);
primaryGesture = Gtk::GestureClick::create();
primaryGesture->set_button(GDK_BUTTON_PRIMARY);
primaryGesture->signal_released().connect(
sigc::mem_fun(*this, &TrayIconWidget::on_primary_released));
add_controller(primaryGesture);
m_secondaryGesture = Gtk::GestureClick::create();
m_secondaryGesture->set_button(GDK_BUTTON_SECONDARY);
m_secondaryGesture->signal_released().connect(sigc::mem_fun(*this, &TrayIconWidget::on_secondary_released));
add_controller(m_secondaryGesture);
secondaryGesture = Gtk::GestureClick::create();
secondaryGesture->set_button(GDK_BUTTON_SECONDARY);
secondaryGesture->signal_released().connect(
sigc::mem_fun(*this, &TrayIconWidget::on_secondary_released));
add_controller(secondaryGesture);
}
void TrayIconWidget::update(const TrayService::Item &item)
{
if (!item.menuAvailable)
{
m_menuModel.reset();
m_menuActions.reset();
m_menuPopupPending = false;
if (m_menuChangedConnection.connected())
{
m_menuChangedConnection.disconnect();
void TrayIconWidget::update(const TrayService::Item &item) {
if (!item.menuAvailable) {
menuModel.reset();
menuActions.reset();
menuPopupPending = false;
if (menuChangedConnection.connected()) {
menuChangedConnection.disconnect();
}
if (m_menuPopover)
{
m_menuPopover->insert_action_group("dbusmenu", Glib::RefPtr<Gio::ActionGroup>());
m_menuPopover->set_menu_model({});
m_menuPopover->unparent();
m_menuPopover.reset();
if (menuPopover) {
menuPopover->insert_action_group("dbusmenu",
Glib::RefPtr<Gio::ActionGroup>());
menuPopover->set_menu_model({});
menuPopover->unparent();
menuPopover.reset();
}
}
if (item.iconPaintable)
{
m_picture.set_paintable(item.iconPaintable);
m_picture.set_visible(true);
m_image.set_visible(false);
}
else if (!item.iconName.empty())
{
m_image.set_from_icon_name(item.iconName);
m_image.set_pixel_size(20);
m_image.set_visible(true);
m_picture.set_visible(false);
}
else
{
m_picture.set_paintable({});
m_image.set_visible(false);
m_picture.set_visible(false);
if (item.iconPaintable) {
picture.set_paintable(item.iconPaintable);
picture.set_visible(true);
image.set_visible(false);
} else if (!item.iconName.empty()) {
image.set_from_icon_name(item.iconName);
image.set_pixel_size(20);
image.set_visible(true);
picture.set_visible(false);
} else {
picture.set_paintable({});
image.set_visible(false);
picture.set_visible(false);
}
if (!item.title.empty())
{
if (!item.title.empty()) {
set_tooltip_text(item.title);
}
else
{
} else {
set_tooltip_text("");
}
set_sensitive(item.status != "Passive");
}
void TrayIconWidget::on_primary_released(int /*n_press*/, double x, double y)
{
m_service.activate(m_id, -1, -1);
void TrayIconWidget::on_primary_released(int /*n_press*/, double x, double y) {
service.activate(id, -1, -1);
}
void TrayIconWidget::on_secondary_released(int /*n_press*/, double x, double y)
{
m_service.contextMenu(m_id, -1, -1);
void TrayIconWidget::on_secondary_released(int /*n_press*/, double x,
double y) {
service.contextMenu(id, -1, -1);
if (!ensure_menu())
{
if (!ensure_menu()) {
return;
}
m_pendingX = x;
m_pendingY = y;
m_menuPopupPending = true;
pendingX = x;
pendingY = y;
menuPopupPending = true;
try_popup();
}
bool TrayIconWidget::ensure_menu()
{
auto layoutOpt = m_service.get_menu_layout(m_id);
if (!layoutOpt)
{
m_menuModel.reset();
m_menuActions.reset();
m_menuPopupPending = false;
if (m_menuChangedConnection.connected())
{
m_menuChangedConnection.disconnect();
bool TrayIconWidget::ensure_menu() {
auto layoutOpt = service.get_menu_layout(id);
if (!layoutOpt) {
menuModel.reset();
menuActions.reset();
menuPopupPending = false;
if (menuChangedConnection.connected()) {
menuChangedConnection.disconnect();
}
if (m_menuPopover)
{
if (menuPopover) {
remove_action_group("dbusmenu");
m_menuPopover->set_menu_model({});
m_menuPopover->unparent();
m_menuPopover.reset();
menuPopover->set_menu_model({});
menuPopover->unparent();
menuPopover.reset();
}
return false;
}
const auto &layout = *layoutOpt;
auto menu = Gio::Menu::create();
auto actions = Gio::SimpleActionGroup::create();
populate_menu_items(layout.children, menu, actions);
const auto itemCount = menu->get_n_items();
std::cout << "[TrayIconWidget] menu update for " << m_id << ", items: " << itemCount << std::endl;
if (itemCount == 0)
{
m_service.debug_dump_menu_layout(m_id);
std::cout << "[TrayIconWidget] menu update for " << id
<< ", items: " << itemCount << std::endl;
if (itemCount == 0) {
service.debug_dump_menu_layout(id);
return false;
}
m_menuModel = menu;
m_menuActions = actions;
menuModel = menu;
menuActions = actions;
if (!m_menuPopover)
{
if (!menuPopover) {
auto *rawPopover = Gtk::make_managed<Gtk::PopoverMenu>();
m_menuPopover = Glib::make_refptr_for_instance<Gtk::PopoverMenu>(rawPopover);
if (!m_menuPopover)
{
menuPopover =
Glib::make_refptr_for_instance<Gtk::PopoverMenu>(rawPopover);
if (!menuPopover) {
return false;
}
m_menuPopover->set_has_arrow(false);
m_menuPopover->set_autohide(true);
m_menuPopover->set_parent(*this);
menuPopover->set_has_arrow(false);
menuPopover->set_autohide(true);
menuPopover->set_parent(*this);
}
m_menuPopover->remove_action_group("dbusmenu");
m_menuPopover->insert_action_group("dbusmenu", m_menuActions);
menuPopover->remove_action_group("dbusmenu");
menuPopover->insert_action_group("dbusmenu", menuActions);
if (m_menuChangedConnection.connected())
{
m_menuChangedConnection.disconnect();
if (menuChangedConnection.connected()) {
menuChangedConnection.disconnect();
}
m_menuChangedConnection = m_menuModel->signal_items_changed().connect(sigc::mem_fun(*this, &TrayIconWidget::on_menu_items_changed));
menuChangedConnection = menuModel->signal_items_changed().connect(
sigc::mem_fun(*this, &TrayIconWidget::on_menu_items_changed));
m_menuPopover->set_menu_model(m_menuModel);
menuPopover->set_menu_model(menuModel);
return true;
}
void TrayIconWidget::on_menu_items_changed(guint /*position*/, guint /*removed*/, guint /*added*/)
{
if (!m_menuModel)
{
void TrayIconWidget::on_menu_items_changed(guint /*position*/,
guint /*removed*/, guint /*added*/) {
if (!menuModel) {
return;
}
const auto count = m_menuModel->get_n_items();
std::cout << "[TrayIconWidget] items changed for " << m_id << ": " << count << " entries" << std::endl;
const auto count = menuModel->get_n_items();
std::cout << "[TrayIconWidget] items changed for " << id << ": " << count
<< " entries" << std::endl;
try_popup();
}
void TrayIconWidget::try_popup()
{
if (!m_menuPopupPending || !m_menuPopover || !m_menuModel)
{
void TrayIconWidget::try_popup() {
if (!menuPopupPending || !menuPopover || !menuModel) {
return;
}
if (m_menuModel->get_n_items() == 0)
{
if (menuModel->get_n_items() == 0) {
return;
}
Gdk::Rectangle rect(static_cast<int>(m_pendingX), static_cast<int>(m_pendingY), 1, 1);
m_menuPopover->set_pointing_to(rect);
m_menuPopover->popup();
m_menuPopupPending = false;
Gdk::Rectangle rect(static_cast<int>(pendingX), static_cast<int>(pendingY),
1, 1);
menuPopover->set_pointing_to(rect);
menuPopover->popup();
menuPopupPending = false;
}
void TrayIconWidget::populate_menu_items(const std::vector<TrayService::MenuNode> &nodes,
const Glib::RefPtr<Gio::Menu> &menu,
const Glib::RefPtr<Gio::SimpleActionGroup> &actions)
{
for (const auto &node : nodes)
{
if (!node.visible)
{
void TrayIconWidget::populate_menu_items(
const std::vector<TrayService::MenuNode> &nodes,
const Glib::RefPtr<Gio::Menu> &menu,
const Glib::RefPtr<Gio::SimpleActionGroup> &actions) {
for (const auto &node : nodes) {
if (!node.visible) {
continue;
}
if (node.separator)
{
if (node.separator) {
auto section = Gio::Menu::create();
menu->append_section("", section);
continue;
}
if (!node.children.empty())
{
if (!node.children.empty()) {
auto submenu = Gio::Menu::create();
populate_menu_items(node.children, submenu, actions);
auto submenuItem = Gio::MenuItem::create(node.label, Glib::ustring());
auto submenuItem =
Gio::MenuItem::create(node.label, Glib::ustring());
submenuItem->set_submenu(submenu);
if (!node.enabled)
{
submenuItem->set_attribute_value("enabled", Glib::Variant<bool>::create(false));
if (!node.enabled) {
submenuItem->set_attribute_value(
"enabled", Glib::Variant<bool>::create(false));
}
menu->append_item(submenuItem);
continue;
}
const std::string actionName = "item" + std::to_string(node.id);
auto menuItem = Gio::MenuItem::create(node.label, "dbusmenu." + actionName);
if (!node.enabled)
{
menuItem->set_attribute_value("enabled", Glib::Variant<bool>::create(false));
auto menuItem =
Gio::MenuItem::create(node.label, "dbusmenu." + actionName);
if (!node.enabled) {
menuItem->set_attribute_value("enabled",
Glib::Variant<bool>::create(false));
}
auto action = Gio::SimpleAction::create(actionName);
action->set_enabled(node.enabled);
action->signal_activate().connect(sigc::bind(sigc::mem_fun(*this, &TrayIconWidget::on_menu_action), node.id));
action->signal_activate().connect(sigc::bind(
sigc::mem_fun(*this, &TrayIconWidget::on_menu_action), node.id));
actions->add_action(action);
menu->append_item(menuItem);
}
}
void TrayIconWidget::on_menu_action(const Glib::VariantBase & /*parameter*/, int itemId)
{
m_service.activate_menu_item(m_id, itemId);
if (m_menuPopover)
{
m_menuPopover->popdown();
void TrayIconWidget::on_menu_action(const Glib::VariantBase & /*parameter*/,
int itemId) {
service.activate_menu_item(id, itemId);
if (menuPopover) {
menuPopover->popdown();
}
}
TrayWidget::TrayWidget(TrayService &service)
: Gtk::Box(Gtk::Orientation::HORIZONTAL), m_service(service)
{
: Gtk::Box(Gtk::Orientation::HORIZONTAL), service(service) {
set_valign(Gtk::Align::CENTER);
set_halign(Gtk::Align::CENTER);
set_visible(false);
m_addConnection = m_service.signal_item_added().connect(sigc::mem_fun(*this, &TrayWidget::on_item_added));
m_removeConnection = m_service.signal_item_removed().connect(sigc::mem_fun(*this, &TrayWidget::on_item_removed));
m_updateConnection = m_service.signal_item_updated().connect(sigc::mem_fun(*this, &TrayWidget::on_item_updated));
addConnection = service.signal_item_added().connect(
sigc::mem_fun(*this, &TrayWidget::on_item_added));
removeConnection = service.signal_item_removed().connect(
sigc::mem_fun(*this, &TrayWidget::on_item_removed));
updateConnection = service.signal_item_updated().connect(
sigc::mem_fun(*this, &TrayWidget::on_item_updated));
rebuild_existing();
}
TrayWidget::~TrayWidget()
{
if (m_addConnection.connected())
{
m_addConnection.disconnect();
TrayWidget::~TrayWidget() {
if (addConnection.connected()) {
addConnection.disconnect();
}
if (m_removeConnection.connected())
{
m_removeConnection.disconnect();
if (removeConnection.connected()) {
removeConnection.disconnect();
}
if (m_updateConnection.connected())
{
m_updateConnection.disconnect();
if (updateConnection.connected()) {
updateConnection.disconnect();
}
}
void TrayWidget::rebuild_existing()
{
auto items = m_service.snapshotItems();
for (const auto &item : items)
{
void TrayWidget::rebuild_existing() {
auto items = service.snapshotItems();
for (const auto &item : items) {
on_item_added(item);
}
set_visible(!m_icons.empty());
set_visible(!icons.empty());
}
void TrayWidget::on_item_added(const TrayService::Item &item)
{
auto it = m_icons.find(item.id);
if (it != m_icons.end())
{
void TrayWidget::on_item_added(const TrayService::Item &item) {
auto it = icons.find(item.id);
if (it != icons.end()) {
it->second->update(item);
return;
}
auto icon = std::make_unique<TrayIconWidget>(m_service, item.id);
auto icon = std::make_unique<TrayIconWidget>(service, item.id);
icon->update(item);
auto *raw = icon.get();
append(*raw);
m_icons.emplace(item.id, std::move(icon));
icons.emplace(item.id, std::move(icon));
set_visible(true);
}
void TrayWidget::on_item_removed(const std::string &id)
{
auto it = m_icons.find(id);
if (it == m_icons.end())
{
void TrayWidget::on_item_removed(const std::string &id) {
auto it = icons.find(id);
if (it == icons.end()) {
return;
}
remove(*it->second);
it->second->unparent();
m_icons.erase(it);
icons.erase(it);
if (m_icons.empty())
{
if (icons.empty()) {
set_visible(false);
}
}
void TrayWidget::on_item_updated(const TrayService::Item &item)
{
auto it = m_icons.find(item.id);
if (it == m_icons.end())
{
void TrayWidget::on_item_updated(const TrayService::Item &item) {
auto it = icons.find(item.id);
if (it == icons.end()) {
on_item_added(item);
return;
}

View File

@@ -2,60 +2,57 @@
#include "helpers/systemHelper.hpp"
#include <sigc++/functors/mem_fun.h>
#include <regex>
#include <iostream>
#include <cmath>
#include <iostream>
#include <regex>
#include <sigc++/functors/mem_fun.h>
VolumeWidget::VolumeWidget()
: Gtk::Box(Gtk::Orientation::HORIZONTAL)
{
VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
set_valign(Gtk::Align::CENTER);
set_halign(Gtk::Align::CENTER);
m_label.set_halign(Gtk::Align::CENTER);
m_label.set_valign(Gtk::Align::CENTER);
m_label.set_text("Vol");
label.set_halign(Gtk::Align::CENTER);
label.set_valign(Gtk::Align::CENTER);
label.set_text("Vol");
append(m_label);
append(label);
// Click toggles mute using wpctl
m_click = Gtk::GestureClick::create();
m_click->set_button(GDK_BUTTON_PRIMARY);
// signal_released provides (int, double, double) — use lambda to ignore args
m_click->signal_released().connect([this](int /*n_press*/, double /*x*/, double /*y*/)
{
try
{
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*/) {
try {
// Toggle mute then refresh
(void)SystemHelper::get_command_output("wpctl set-mute @DEFAULT_SINK@ toggle");
}
catch (const std::exception &ex)
{
std::cerr << "[VolumeWidget] failed to toggle mute: " << ex.what() << std::endl;
(void)SystemHelper::get_command_output(
"wpctl set-mute @DEFAULT_SINK@ toggle");
} catch (const std::exception &ex) {
std::cerr << "[VolumeWidget] failed to toggle mute: " << ex.what()
<< std::endl;
}
this->update();
});
add_controller(m_click);
add_controller(click);
// Initial read
update();
// Start polling every 1 second to keep the display up to date
m_timeoutConn = Glib::signal_timeout().connect(sigc::mem_fun(*this, &VolumeWidget::on_timeout), 100);
timeoutConn = Glib::signal_timeout().connect(
sigc::mem_fun(*this, &VolumeWidget::on_timeout), 100);
}
VolumeWidget::~VolumeWidget()
{
if (m_timeoutConn.connected())
m_timeoutConn.disconnect();
VolumeWidget::~VolumeWidget() {
if (timeoutConn.connected())
timeoutConn.disconnect();
}
void VolumeWidget::update()
{
try
{
const std::string out = SystemHelper::get_command_output("wpctl get-volume @DEFAULT_SINK@");
void VolumeWidget::update() {
try {
const std::string out =
SystemHelper::get_command_output("wpctl get-volume @DEFAULT_SINK@");
// Attempt to parse a number (percentage or fraction)
std::smatch m;
@@ -65,12 +62,9 @@ void VolumeWidget::update()
std::string text = out;
int percent = -1;
if (std::regex_search(text, m, r_percent))
{
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))
{
} 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)
@@ -79,34 +73,26 @@ void VolumeWidget::update()
percent = static_cast<int>(std::round(v));
}
if (percent >= 0)
{
m_label.set_text(std::to_string(percent) + "%");
}
else
{
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)
{
if (pos != std::string::npos) {
auto end = text.find_last_not_of(" \t\n\r");
m_label.set_text(text.substr(pos, end - pos + 1));
}
else
{
m_label.set_text("?");
label.set_text(text.substr(pos, end - pos + 1));
} else {
label.set_text("?");
}
}
}
catch (const std::exception &ex)
{
std::cerr << "[VolumeWidget] failed to read volume: " << ex.what() << std::endl;
m_label.set_text("N/A");
} catch (const std::exception &ex) {
std::cerr << "[VolumeWidget] failed to read volume: " << ex.what()
<< std::endl;
label.set_text("N/A");
}
}
bool VolumeWidget::on_timeout()
{
bool VolumeWidget::on_timeout() {
update();
return true; // keep timeout active
}

View File

@@ -1,130 +1,104 @@
#include "widgets/workspaceIndicator.hpp"
#include <exception>
#include <gtkmm/widget.h>
#include <gtkmm/gestureclick.h>
#include <gdk/gdk.h>
#include <gtkmm/gestureclick.h>
#include <gtkmm/widget.h>
#include <sigc++/functors/mem_fun.h>
WorkspaceIndicator::WorkspaceIndicator(HyprlandService &service, int monitorId)
: Gtk::Box(Gtk::Orientation::HORIZONTAL), m_service(service), m_monitorId(monitorId)
{
: Gtk::Box(Gtk::Orientation::HORIZONTAL), service(service),
monitorId(monitorId) {
set_margin_top(2);
set_margin_bottom(2);
m_workspaceConnection = m_service.workspaceStateChanged.connect(
workspaceConnection = service.workspaceStateChanged.connect(
sigc::mem_fun(*this, &WorkspaceIndicator::on_workspace_update));
m_monitorConnection = m_service.monitorStateChanged.connect(
monitorConnection = service.monitorStateChanged.connect(
sigc::mem_fun(*this, &WorkspaceIndicator::on_monitor_update));
rebuild();
}
WorkspaceIndicator::~WorkspaceIndicator()
{
if (m_workspaceConnection.connected())
{
m_workspaceConnection.disconnect();
WorkspaceIndicator::~WorkspaceIndicator() {
if (workspaceConnection.connected()) {
workspaceConnection.disconnect();
}
if (m_monitorConnection.connected())
{
m_monitorConnection.disconnect();
if (monitorConnection.connected()) {
monitorConnection.disconnect();
}
}
void WorkspaceIndicator::on_workspace_update(int monitorId)
{
if (monitorId != m_monitorId && monitorId != -1)
{
void WorkspaceIndicator::on_workspace_update(int monitorId) {
if (monitorId != monitorId && monitorId != -1) {
return;
}
rebuild();
}
void WorkspaceIndicator::on_monitor_update()
{
rebuild();
}
void WorkspaceIndicator::on_monitor_update() { rebuild(); }
void WorkspaceIndicator::rebuild()
{
void WorkspaceIndicator::rebuild() {
clear_children();
HyprlandService::Monitor *monitor = nullptr;
try
{
monitor = m_service.getMonitorById(m_monitorId);
}
catch (const std::exception &)
{
try {
monitor = service.getMonitorById(monitorId);
} catch (const std::exception &) {
return;
}
if (monitor == nullptr)
{
if (monitor == nullptr) {
return;
}
for (int workspaceId = 1; workspaceId <= HyprlandService::kWorkspaceSlotCount; ++workspaceId)
{
for (int workspaceId = 1;
workspaceId <= HyprlandService::kWorkspaceSlotCount; ++workspaceId) {
const HyprlandService::WorkspaceState *state = nullptr;
auto it = monitor->workspaceStates.find(workspaceId);
if (it != monitor->workspaceStates.end())
{
if (it != monitor->workspaceStates.end()) {
state = &it->second;
}
const std::string display = (state && !state->label.empty()) ? state->label : std::to_string(workspaceId);
const std::string display = (state && !state->label.empty())
? state->label
: std::to_string(workspaceId);
auto label = Gtk::make_managed<Gtk::Label>(display);
label->add_css_class("workspace-pill");
// Make the label clickable using a gesture click (primary button)
auto gesture = Gtk::GestureClick::create();
gesture->set_button(GDK_BUTTON_PRIMARY);
// Use a lambda to capture the workspace slot id
gesture->signal_released().connect([this, workspaceId](int /*n_press*/, double /*x*/, double /*y*/)
{
try
{
m_service.switchToWorkspace(m_monitorId, workspaceId);
}
catch (const std::exception &ex)
{
std::cerr << "[WorkspaceIndicator] switchToWorkspace failed: " << ex.what() << std::endl;
}
});
gesture->signal_released().connect(
[this, workspaceId](int /*n_press*/, double /*x*/, double /*y*/) {
int realWorkspaceId = workspaceId + 5 * (monitorId);
service.switchToWorkspace(realWorkspaceId);
});
label->add_controller(gesture);
if (state != nullptr)
{
if (state->focused)
{
if (state != nullptr) {
if (state->focused) {
label->add_css_class("workspace-pill-focused");
}
else if (state->active)
{
} else if (state->active) {
label->add_css_class("workspace-pill-active");
}
if (state->urgent)
{
if (state->urgent) {
label->add_css_class("workspace-pill-urgent");
}
}
append(*label);
}
}
void WorkspaceIndicator::clear_children()
{
void WorkspaceIndicator::clear_children() {
Gtk::Widget *child = get_first_child();
while (child != nullptr)
{
while (child != nullptr) {
Gtk::Widget *next = child->get_next_sibling();
remove(*child);
child = next;