Compare commits
4 Commits
f1c68321a7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c245fa7277 | |||
| 9b0a036925 | |||
| 11ccd55a52 | |||
| a912cb9687 |
@@ -1 +1,3 @@
|
||||
IndentWidth: 4
|
||||
ColumnLimit: 0
|
||||
AlignConsecutiveAssignments: true
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <gtk4-layer-shell/gtk4-layer-shell.h>
|
||||
#include <gtkmm.h>
|
||||
|
||||
#include "icons.hpp"
|
||||
#include "services/hyprland.hpp"
|
||||
#include "services/tray.hpp"
|
||||
#include "widgets/clock.hpp"
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "widgets/tray.hpp"
|
||||
#include "widgets/webWidget.hpp"
|
||||
#include "widgets/workspaceIndicator.hpp"
|
||||
#include "icons.hpp"
|
||||
|
||||
class Bar : public Gtk::Window {
|
||||
public:
|
||||
@@ -26,7 +25,7 @@ class Bar : public Gtk::Window {
|
||||
private:
|
||||
Clock clock;
|
||||
Date date;
|
||||
WebWidget homeAssistant {ICON_HOME, "Home Assistant",
|
||||
WebWidget homeAssistant{ICON_HOME, "Home Assistant",
|
||||
"https://home.rivercry.com"};
|
||||
TrayService &trayService;
|
||||
HyprlandService &hyprlandService;
|
||||
|
||||
@@ -2,26 +2,33 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <glibmm.h>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <sigc++/sigc++.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class HyprlandService {
|
||||
public:
|
||||
static constexpr int kWorkspaceSlotCount = 5;
|
||||
static constexpr int kWorkspaceSlotCount = 7;
|
||||
const char *kMonitorCommand = "hyprctl monitors -j";
|
||||
const char *kWorkspaceCommand = "hyprctl workspaces -j";
|
||||
const char *kClientsCommand = "hyprctl clients -j";
|
||||
|
||||
struct WindowState {
|
||||
int hyprId = -1;
|
||||
};
|
||||
|
||||
struct WorkspaceState {
|
||||
int id = -1;
|
||||
int hyprId = -1;
|
||||
int monitorId = -1;
|
||||
bool active = false;
|
||||
bool focused = false;
|
||||
bool urgent = false;
|
||||
std::vector<std::string> urgentWindows;
|
||||
std::string label;
|
||||
};
|
||||
|
||||
struct Monitor {
|
||||
std::map<int, WorkspaceState> workspaceStates;
|
||||
std::map<int, WorkspaceState *> workspaceStates;
|
||||
std::string name;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
@@ -34,69 +41,32 @@ class HyprlandService {
|
||||
|
||||
void start();
|
||||
void on_hyprland_event(std::string event, std::string data);
|
||||
|
||||
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);
|
||||
const Monitor *getMonitorById(int id) const;
|
||||
Monitor *getMonitorByIndex(std::size_t index);
|
||||
const Monitor *getMonitorByIndex(std::size_t index) const;
|
||||
// Switch to a workspace slot on a given monitor. If the workspace has an
|
||||
// associated Hyprland workspace id (hyprId >= 0) that id will be used.
|
||||
// Otherwise the slot and monitor name will be used to request creation
|
||||
// / activation via `hyprctl`.
|
||||
void switchToWorkspace(int workspaceId);
|
||||
|
||||
std::map<int, WorkspaceState *> getAllWorkspaces() const {
|
||||
return this->workspaces;
|
||||
}
|
||||
|
||||
private:
|
||||
int fd = -1;
|
||||
std::string buffer;
|
||||
std::map<int, Monitor> monitors;
|
||||
std::map<int, WorkspaceState *> workspaces;
|
||||
// persistent buffer for socket reads to handle partial messages
|
||||
std::string socket_buffer;
|
||||
|
||||
std::string get_socket_path();
|
||||
bool on_socket_read(Glib::IOCondition condition);
|
||||
void parse_message(const std::string &line);
|
||||
std::string get_socket_path();
|
||||
void refresh_monitors();
|
||||
void refresh_workspaces();
|
||||
void handle_urgent_window(std::string windowAddress);
|
||||
void onUrgentEvent(std::string windowAddress);
|
||||
void onActiveWindowEvent(std::string windowAddress);
|
||||
};
|
||||
|
||||
inline void HyprlandService::printMonitor(const Monitor &mon) const {
|
||||
std::cout << "=== Monitor Info ===\n";
|
||||
std::cout << "Name: " << mon.name << " (ID: " << mon.id << ")\n";
|
||||
std::cout << "Position: (" << mon.x << ", " << mon.y << ")\n";
|
||||
std::cout << "Focused Workspace ID: " << mon.focusedWorkspaceId << "\n";
|
||||
|
||||
std::cout << "Workspaces:\n";
|
||||
|
||||
if (mon.workspaceStates.empty()) {
|
||||
std::cout << " (None)\n";
|
||||
} else {
|
||||
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount;
|
||||
++slot) {
|
||||
const auto it = mon.workspaceStates.find(slot);
|
||||
if (it == mon.workspaceStates.end()) {
|
||||
std::cout << " - [Slot: " << slot << " | HyprID: n/a]"
|
||||
<< " Label: <none> | Active: No | Focused: No | "
|
||||
"Urgent: No\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
const WorkspaceState &ws = it->second;
|
||||
std::cout << " - [Slot: " << ws.id << " | HyprID: "
|
||||
<< (ws.hyprId >= 0 ? std::to_string(ws.hyprId)
|
||||
: std::string("n/a"))
|
||||
<< "] "
|
||||
<< "Label: " << (ws.label.empty() ? "<none>" : ws.label)
|
||||
<< " | "
|
||||
<< "Active: " << (ws.active ? "Yes" : "No") << " | "
|
||||
<< "Focused: " << (ws.focused ? "Yes" : "No") << " | "
|
||||
<< "Urgent: " << (ws.urgent ? "Yes" : "No") << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "====================\n";
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <gtkmm/box.h>
|
||||
#include <gtkmm/label.h>
|
||||
#include <gtkmm/gestureclick.h>
|
||||
#include <sigc++/connection.h>
|
||||
|
||||
#include "services/hyprland.hpp"
|
||||
@@ -16,9 +17,11 @@ class WorkspaceIndicator : public Gtk::Box {
|
||||
int monitorId;
|
||||
sigc::connection workspaceConnection;
|
||||
sigc::connection monitorConnection;
|
||||
std::map<int, Gtk::Label *> workspaceLabels;
|
||||
std::map<int, Glib::RefPtr<Gtk::GestureClick>> workspaceGestures;
|
||||
|
||||
void rebuild();
|
||||
void on_workspace_update(int monitorId);
|
||||
void on_workspace_update();
|
||||
void on_monitor_update();
|
||||
void clear_children();
|
||||
void refreshLabel(Gtk::Label *label, const HyprlandService::WorkspaceState &state);
|
||||
};
|
||||
|
||||
@@ -22,11 +22,19 @@ window {
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
.workspace-pill:hover {
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.workspace-pill-focused {
|
||||
background-color: #ffffff;
|
||||
color: #1e1e1e;
|
||||
font-weight: bold;
|
||||
border-bottom: #89b4fa 2px;
|
||||
box-shadow: 0 0 6px rgba(255, 255, 255, 0.8);
|
||||
}
|
||||
|
||||
.workspace-pill-focused:hover {
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
.workspace-pill-active {
|
||||
@@ -36,16 +44,30 @@ window {
|
||||
.workspace-pill-urgent {
|
||||
background-color: #ff5555;
|
||||
color: #fff;
|
||||
|
||||
/* base glow (will be animated) */
|
||||
animation: workspace-blink 1s linear infinite;
|
||||
}
|
||||
|
||||
/* blinking animation for urgent workspaces */
|
||||
@keyframes workspace-blink {
|
||||
0% {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
50% {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
100% {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.workspace-pill:last-child {
|
||||
margin-right: 0;
|
||||
}
|
||||
|
||||
.workspace-pill:hover {
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.minimized {
|
||||
background-color: rgba(50, 50, 50, 0.5);
|
||||
}
|
||||
@@ -86,6 +108,6 @@ tooltip {
|
||||
}
|
||||
|
||||
.icon-label {
|
||||
font-family: "Material Icons, Hack Nerd Font Mono";
|
||||
font-family: "Material Icons, Font Awesome 7 Brands, Hack Nerd Font Mono";
|
||||
font-size: 19px;
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "bar/bar.hpp"
|
||||
|
||||
#include "gtk/gtk.h"
|
||||
|
||||
#include "widgets/date.hpp"
|
||||
#include "widgets/spacer.hpp"
|
||||
#include "widgets/volumeWidget.hpp"
|
||||
@@ -30,12 +32,12 @@ Bar::Bar(GdkMonitor *monitor, HyprlandService &hyprlandService,
|
||||
gtk_layer_set_anchor(this->gobj(), GTK_LAYER_SHELL_EDGE_TOP, true);
|
||||
gtk_layer_set_anchor(this->gobj(), GTK_LAYER_SHELL_EDGE_LEFT, true);
|
||||
gtk_layer_set_anchor(this->gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, true);
|
||||
gtk_layer_set_layer(this->gobj(), GTK_LAYER_SHELL_LAYER_TOP);
|
||||
|
||||
gtk_layer_auto_exclusive_zone_enable(this->gobj());
|
||||
|
||||
set_child(main_box);
|
||||
|
||||
|
||||
this->volumeWidget = Gtk::make_managed<VolumeWidget>();
|
||||
|
||||
load_css();
|
||||
@@ -46,13 +48,9 @@ Bar::Bar(GdkMonitor *monitor, HyprlandService &hyprlandService,
|
||||
Glib::signal_timeout().connect(sigc::mem_fun(clock, &Clock::onUpdate),
|
||||
1000);
|
||||
|
||||
|
||||
date.onUpdate();
|
||||
|
||||
Glib::signal_timeout().connect(sigc::mem_fun(date, &Date::onUpdate),
|
||||
1000);
|
||||
|
||||
|
||||
Glib::signal_timeout().connect(sigc::mem_fun(date, &Date::onUpdate), 1000);
|
||||
}
|
||||
|
||||
void Bar::setup_ui() {
|
||||
@@ -65,15 +63,14 @@ void Bar::setup_ui() {
|
||||
|
||||
left_box.set_valign(Gtk::Align::CENTER);
|
||||
|
||||
// Don't expand the center box — keep it centered by alignment
|
||||
center_box.set_hexpand(false);
|
||||
center_box.set_valign(Gtk::Align::CENTER);
|
||||
center_box.set_halign(Gtk::Align::CENTER);
|
||||
|
||||
right_box.set_valign(Gtk::Align::CENTER);
|
||||
|
||||
workspaceIndicator =
|
||||
Gtk::make_managed<WorkspaceIndicator>(hyprlandService, monitorId);
|
||||
workspaceIndicator = Gtk::make_managed<WorkspaceIndicator>(hyprlandService, monitorId);
|
||||
|
||||
left_box.append(*workspaceIndicator);
|
||||
|
||||
clock.set_name("clock-label");
|
||||
@@ -83,7 +80,6 @@ void Bar::setup_ui() {
|
||||
center_box.append(*(new Spacer()));
|
||||
center_box.append(*this->volumeWidget);
|
||||
|
||||
|
||||
trayWidget = Gtk::make_managed<TrayWidget>(trayService);
|
||||
right_box.append(*trayWidget);
|
||||
right_box.append(homeAssistant);
|
||||
@@ -93,16 +89,16 @@ void Bar::load_css() {
|
||||
auto css_provider = Gtk::CssProvider::create();
|
||||
|
||||
std::string css_path = "resources/bar.css";
|
||||
const char* home = std::getenv("HOME");
|
||||
const char *home = std::getenv("HOME");
|
||||
if (home) {
|
||||
std::filesystem::path config_path = std::filesystem::path(home) / ".config/bar/bar.css";
|
||||
std::filesystem::path config_path =
|
||||
std::filesystem::path(home) / ".config/bar/bar.css";
|
||||
if (std::filesystem::exists(config_path)) {
|
||||
css_path = config_path.string();
|
||||
}
|
||||
}
|
||||
|
||||
const std::string css =
|
||||
SystemHelper::read_file_to_string(css_path);
|
||||
const std::string css = SystemHelper::read_file_to_string(css_path);
|
||||
css_provider->load_from_data(css);
|
||||
|
||||
Gtk::StyleContext::add_provider_for_display(
|
||||
|
||||
@@ -14,16 +14,6 @@
|
||||
|
||||
#include "helpers/systemHelper.hpp"
|
||||
|
||||
namespace {
|
||||
const char *kMonitorCommand = "hyprctl monitors -j";
|
||||
const char *kWorkspaceCommand = "hyprctl workspaces -j";
|
||||
const char *kClientsCommand = "hyprctl clients -j";
|
||||
|
||||
bool is_workspace_event(const std::string &event) {
|
||||
return event.find("workspace") != std::string::npos;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
HyprlandService::HyprlandService() = default;
|
||||
|
||||
HyprlandService::~HyprlandService() {
|
||||
@@ -31,18 +21,35 @@ HyprlandService::~HyprlandService() {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
// free allocated workspace pointers
|
||||
for (auto &p : this->workspaces) {
|
||||
delete p.second;
|
||||
}
|
||||
this->workspaces.clear();
|
||||
this->monitors.clear();
|
||||
}
|
||||
|
||||
void HyprlandService::on_hyprland_event(std::string event, std::string data) {
|
||||
|
||||
if (event == "urgent") {
|
||||
handle_urgent_window(data);
|
||||
onUrgentEvent(data);
|
||||
}
|
||||
|
||||
if (is_workspace_event(event) || event == "focusedmon" ||
|
||||
event == "monitoradded" || event == "monitorremoved") {
|
||||
refresh_monitors();
|
||||
if (event == "activewindowv2") {
|
||||
onActiveWindowEvent(data);
|
||||
}
|
||||
|
||||
if (event == "workspace" || event == "movewindow") {
|
||||
refresh_workspaces();
|
||||
}
|
||||
|
||||
// use for
|
||||
// event == "focusedmon"
|
||||
|
||||
if (event == "monitoradded" || event == "monitorremoved") {
|
||||
refresh_monitors();
|
||||
}
|
||||
}
|
||||
|
||||
void HyprlandService::start() {
|
||||
@@ -79,34 +86,41 @@ void HyprlandService::start() {
|
||||
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;
|
||||
const auto error_mask = Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR;
|
||||
|
||||
if (static_cast<int>(condition & error_mask) != 0) {
|
||||
std::cerr << "[Hyprland] Socket disconnected." << std::endl;
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[4096];
|
||||
const ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
|
||||
char temp_buffer[4096];
|
||||
const ssize_t bytes_read = read(fd, temp_buffer, sizeof(temp_buffer));
|
||||
|
||||
if (bytes_read > 0) {
|
||||
buffer[bytes_read] = '\0';
|
||||
this->buffer.append(buffer);
|
||||
if (bytes_read <= 0) {
|
||||
// peer closed or error
|
||||
std::cerr << "[Hyprland] Socket read returned " << bytes_read << std::endl;
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// append exactly bytes_read bytes (may contain embedded nulls)
|
||||
this->socket_buffer.append(temp_buffer, static_cast<size_t>(bytes_read));
|
||||
|
||||
size_t pos = 0;
|
||||
|
||||
while ((pos = this->buffer.find('\n')) != std::string::npos) {
|
||||
const std::string line = this->buffer.substr(0, pos);
|
||||
while ((pos = this->socket_buffer.find('\n')) != std::string::npos) {
|
||||
const std::string line = this->socket_buffer.substr(0, pos);
|
||||
parse_message(line);
|
||||
this->buffer.erase(0, pos + 1);
|
||||
}
|
||||
this->socket_buffer.erase(0, pos + 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -135,177 +149,94 @@ std::string HyprlandService::get_socket_path() {
|
||||
}
|
||||
|
||||
void HyprlandService::refresh_monitors() {
|
||||
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;
|
||||
// free any previously allocated WorkspaceState objects before rebuilding
|
||||
for (auto &p : this->workspaces) {
|
||||
delete p.second;
|
||||
}
|
||||
this->workspaces.clear();
|
||||
this->monitors.clear();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
std::map<int, Monitor> updated;
|
||||
|
||||
for (const auto &monitorJson : monitorsJson) {
|
||||
if (!monitorJson.is_object()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Monitor monitor;
|
||||
monitor.id = monitorJson.value("id", -1);
|
||||
monitor.name = monitorJson.value("name", "");
|
||||
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) {
|
||||
updated.emplace(monitor.id, std::move(monitor));
|
||||
this->monitors[monitor.id] = monitor;
|
||||
}
|
||||
|
||||
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount; ++slot) {
|
||||
WorkspaceState wsState;
|
||||
wsState.focused = false;
|
||||
wsState.active = false;
|
||||
wsState.label = std::to_string(slot);
|
||||
wsState.monitorId = monitor.id;
|
||||
|
||||
int id = slot + monitor.id * HyprlandService::kWorkspaceSlotCount;
|
||||
wsState.hyprId = id;
|
||||
this->workspaces[id] = new WorkspaceState(wsState);
|
||||
if (monitor.id >= 0) {
|
||||
this->monitors[monitor.id].workspaceStates[slot] = this->workspaces[id];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
monitors.swap(updated);
|
||||
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;
|
||||
}
|
||||
|
||||
for (auto &pair : monitors) {
|
||||
auto &monitor = pair.second;
|
||||
monitor.workspaceStates.clear();
|
||||
output = SystemHelper::get_command_output(kMonitorCommand);
|
||||
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
|
||||
|
||||
int focusedSlot = -1;
|
||||
if (monitor.focusedWorkspaceId > 0) {
|
||||
focusedSlot = ((monitor.focusedWorkspaceId - 1) %
|
||||
HyprlandService::kWorkspaceSlotCount) +
|
||||
1;
|
||||
}
|
||||
for (const auto &monitorJson : monitorsJson) {
|
||||
const int monitorId = monitorJson.value("id", -1);
|
||||
const int focusedWorkspaceId = monitorJson["activeWorkspace"].value("id", -1);
|
||||
|
||||
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount;
|
||||
++slot) {
|
||||
WorkspaceState state;
|
||||
state.id = slot;
|
||||
state.hyprId = -1;
|
||||
state.label = std::to_string(slot);
|
||||
state.active = (slot == focusedSlot);
|
||||
state.focused = state.focused;
|
||||
state.urgent = false;
|
||||
monitor.workspaceStates.emplace(slot, state);
|
||||
// write into the stored monitor (use reference)
|
||||
auto it = this->monitors.find(monitorId);
|
||||
if (it != this->monitors.end()) {
|
||||
it->second.focusedWorkspaceId = focusedWorkspaceId;
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &workspaceJson : workspacesJson) {
|
||||
if (!workspaceJson.is_object()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const int monitorId = workspaceJson.value("monitorID", -1);
|
||||
const int workspaceId = workspaceJson.value("id", -1);
|
||||
|
||||
auto monitorIt = monitors.find(monitorId);
|
||||
if (monitorIt == monitors.end() || workspaceId < 0) {
|
||||
auto workspaceStateIt = this->workspaces.find(workspaceId);
|
||||
if (workspaceStateIt == this->workspaces.end()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto &monitor = monitorIt->second;
|
||||
const int slot =
|
||||
((workspaceId - 1) % HyprlandService::kWorkspaceSlotCount) + 1;
|
||||
auto &workspaceState = monitor.workspaceStates[slot];
|
||||
|
||||
workspaceState.id = slot;
|
||||
workspaceState.hyprId = workspaceId;
|
||||
workspaceState.focused = (monitor.focusedWorkspaceId == workspaceId);
|
||||
workspaceState.active =
|
||||
workspaceState.focused || workspaceJson.value("windows", 0) > 0;
|
||||
workspaceState.urgent = false;
|
||||
|
||||
std::string labelCandidate;
|
||||
if (workspaceJson.contains("name") &&
|
||||
workspaceJson["name"].is_string()) {
|
||||
labelCandidate = workspaceJson["name"].get<std::string>();
|
||||
}
|
||||
|
||||
if (labelCandidate.empty() ||
|
||||
labelCandidate == std::to_string(workspaceId)) {
|
||||
workspaceState.label = std::to_string(slot);
|
||||
WorkspaceState *workspaceState = workspaceStateIt->second;
|
||||
auto mit = this->monitors.find(workspaceState->monitorId);
|
||||
if (mit != this->monitors.end()) {
|
||||
workspaceState->focused = mit->second.focusedWorkspaceId == workspaceId;
|
||||
} else {
|
||||
workspaceState.label = labelCandidate;
|
||||
workspaceState->focused = false;
|
||||
}
|
||||
workspaceState->active = true;
|
||||
}
|
||||
|
||||
if (workspaceJson.contains("urgent") &&
|
||||
workspaceJson["urgent"].is_boolean()) {
|
||||
workspaceState.urgent = workspaceJson["urgent"].get<bool>();
|
||||
} else if (workspaceJson.contains("hasurgent") &&
|
||||
workspaceJson["hasurgent"].is_boolean()) {
|
||||
workspaceState.urgent = workspaceJson["hasurgent"].get<bool>();
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &pair : monitors) {
|
||||
workspaceStateChanged.emit(pair.first);
|
||||
}
|
||||
}
|
||||
|
||||
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 = 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 >= monitors.size()) {
|
||||
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;
|
||||
workspaceStateChanged.emit();
|
||||
}
|
||||
|
||||
void HyprlandService::switchToWorkspace(int workspaceId) {
|
||||
@@ -320,68 +251,113 @@ void HyprlandService::switchToWorkspace(int workspaceId) {
|
||||
}
|
||||
}
|
||||
|
||||
const HyprlandService::Monitor *
|
||||
HyprlandService::getMonitorByIndex(std::size_t index) const {
|
||||
void HyprlandService::onUrgentEvent(std::string windowAddress) {
|
||||
std::string output = SystemHelper::get_command_output(kClientsCommand);
|
||||
auto clientsJson = nlohmann::json::parse(output, nullptr, false);
|
||||
|
||||
for (const auto &clientJson : clientsJson) {
|
||||
const std::string addr = clientJson.value("address", "");
|
||||
|
||||
if (addr == "0x" + windowAddress) {
|
||||
int workspaceId = clientJson["workspace"].value("id", -1);
|
||||
|
||||
auto it = this->workspaces.find(workspaceId);
|
||||
if (it != this->workspaces.end() && it->second) {
|
||||
|
||||
if (std::find(it->second->urgentWindows.begin(),
|
||||
it->second->urgentWindows.end(), windowAddress) ==
|
||||
it->second->urgentWindows.end()) {
|
||||
it->second->urgentWindows.push_back(windowAddress);
|
||||
workspaceStateChanged.emit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HyprlandService::onActiveWindowEvent(std::string windowAddress) {
|
||||
std::string output = SystemHelper::get_command_output(kClientsCommand);
|
||||
auto clientsJson = nlohmann::json::parse(output, nullptr, false);
|
||||
|
||||
for (const auto &clientJson : clientsJson) {
|
||||
const std::string addr = clientJson.value("address", "");
|
||||
|
||||
if (addr == "0x" + windowAddress) {
|
||||
int workspaceId = clientJson["workspace"]["id"];
|
||||
auto it = this->workspaces.find(workspaceId);
|
||||
if (it != this->workspaces.end() && it->second) {
|
||||
WorkspaceState *ws = it->second;
|
||||
auto uit = std::find(ws->urgentWindows.begin(), ws->urgentWindows.end(), windowAddress);
|
||||
if (uit != ws->urgentWindows.end()) {
|
||||
ws->urgentWindows.erase(uit);
|
||||
workspaceStateChanged.emit();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HyprlandService::printMonitor(const Monitor &mon) const {
|
||||
std::cout << "=== Monitor Info ===\n";
|
||||
std::cout << "Name: " << mon.name << " (ID: " << mon.id << ")\n";
|
||||
std::cout << "Position: (" << mon.x << ", " << mon.y << ")\n";
|
||||
std::cout << "Focused Workspace ID: " << mon.focusedWorkspaceId << "\n";
|
||||
|
||||
std::cout << "Workspaces:\n";
|
||||
|
||||
if (mon.workspaceStates.empty()) {
|
||||
std::cout << " (None)\n";
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (int slot = 1; slot <= HyprlandService::kWorkspaceSlotCount; ++slot) {
|
||||
const auto it = mon.workspaceStates.find(slot);
|
||||
if (it == mon.workspaceStates.end()) {
|
||||
std::cout << " - [Slot: " << slot << " | HyprID: n/a]"
|
||||
<< " Label: <none> | Active: No | Focused: No | "
|
||||
"Urgent: No\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
const WorkspaceState *ws = it->second;
|
||||
std::cout << " - [HyprID: "
|
||||
<< (ws->hyprId >= 0 ? std::to_string(ws->hyprId)
|
||||
: std::string("n/a"))
|
||||
<< "] "
|
||||
<< "Label: " << (ws->label.empty() ? "<none>" : ws->label)
|
||||
<< " | "
|
||||
<< "Active: " << (ws->active ? "Yes" : "No") << " | "
|
||||
<< "Focused: " << (ws->focused ? "Yes" : "No") << " | "
|
||||
<< "Urgent: " << (ws->urgentWindows.size() > 0 ? "Yes" : "No")
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
std::cout << "====================\n";
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void HyprlandService::handle_urgent_window(std::string windowAddress) {
|
||||
std::string output;
|
||||
try {
|
||||
output = SystemHelper::get_command_output(kClientsCommand);
|
||||
} catch (const std::exception &ex) {
|
||||
std::cerr << "[Hyprland] Failed to query clients: " << ex.what()
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
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())
|
||||
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);
|
||||
}
|
||||
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.urgent) {
|
||||
wsPair.second.urgent = true;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
workspaceStateChanged.emit(monitor.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "widgets/workspaceIndicator.hpp"
|
||||
#include "services/hyprland.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <cassert>
|
||||
#include <gdk/gdk.h>
|
||||
#include <gtkmm/gestureclick.h>
|
||||
#include <gtkmm/widget.h>
|
||||
@@ -17,6 +18,25 @@ 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");
|
||||
|
||||
// create and store a gesture controller once to avoid duplicate handlers
|
||||
auto gesture = Gtk::GestureClick::create();
|
||||
gesture->set_button(GDK_BUTTON_PRIMARY);
|
||||
gesture->signal_released().connect(
|
||||
[this, i](int, double, double) {
|
||||
this->service.switchToWorkspace(
|
||||
i + this->monitorId * HyprlandService::kWorkspaceSlotCount);
|
||||
});
|
||||
label->add_controller(gesture);
|
||||
workspaceGestures[i] = gesture;
|
||||
|
||||
workspaceLabels[i] = label;
|
||||
append(*label);
|
||||
}
|
||||
|
||||
rebuild();
|
||||
}
|
||||
|
||||
@@ -30,77 +50,34 @@ 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() {
|
||||
clear_children();
|
||||
void WorkspaceIndicator::refreshLabel(Gtk::Label *label, const HyprlandService::WorkspaceState &state) {
|
||||
label->remove_css_class("workspace-pill-active");
|
||||
label->remove_css_class("workspace-pill-focused");
|
||||
label->remove_css_class("workspace-pill-urgent");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
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);
|
||||
});
|
||||
label->add_controller(gesture);
|
||||
|
||||
if (state != nullptr) {
|
||||
if (state->urgent != true) {
|
||||
if (state->focused) {
|
||||
// controller created once in constructor and reused
|
||||
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 (state->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) {
|
||||
Gtk::Label *label = workspaceLabels[id];
|
||||
this->refreshLabel(label, *workspaceState);
|
||||
}
|
||||
}
|
||||
|
||||
10
tmp_test.cpp
10
tmp_test.cpp
@@ -1,10 +0,0 @@
|
||||
#include <giomm/menumodel.h>
|
||||
#include <gio/gio.h>
|
||||
#include <gio/gdbusmenumodel.h>
|
||||
|
||||
int main(){
|
||||
GDBusMenuModel *dbusModel = g_dbus_menu_model_get_for_bus_sync(G_BUS_TYPE_SESSION, G_DBUS_MENU_MODEL_FLAGS_NONE, "org.freedesktop.Notifications", "/Menu", nullptr, nullptr);
|
||||
if(!dbusModel) return 0;
|
||||
Glib::RefPtr<Gio::MenuModel> model = Glib::wrap(G_MENU_MODEL(dbusModel));
|
||||
return model ? 0 : 1;
|
||||
}
|
||||
Reference in New Issue
Block a user