optimized the code, added some bugs :)
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
IndentWidth: 4
|
IndentWidth: 4
|
||||||
|
ColumnLimit: 0
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include <gtk4-layer-shell/gtk4-layer-shell.h>
|
#include <gtk4-layer-shell/gtk4-layer-shell.h>
|
||||||
#include <gtkmm.h>
|
#include <gtkmm.h>
|
||||||
|
#include "icons.hpp"
|
||||||
#include "services/hyprland.hpp"
|
#include "services/hyprland.hpp"
|
||||||
#include "services/tray.hpp"
|
#include "services/tray.hpp"
|
||||||
#include "widgets/clock.hpp"
|
#include "widgets/clock.hpp"
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
#include "widgets/tray.hpp"
|
#include "widgets/tray.hpp"
|
||||||
#include "widgets/webWidget.hpp"
|
#include "widgets/webWidget.hpp"
|
||||||
#include "widgets/workspaceIndicator.hpp"
|
#include "widgets/workspaceIndicator.hpp"
|
||||||
#include "icons.hpp"
|
|
||||||
|
|
||||||
class Bar : public Gtk::Window {
|
class Bar : public Gtk::Window {
|
||||||
public:
|
public:
|
||||||
@@ -26,8 +25,8 @@ class Bar : public Gtk::Window {
|
|||||||
private:
|
private:
|
||||||
Clock clock;
|
Clock clock;
|
||||||
Date date;
|
Date date;
|
||||||
WebWidget homeAssistant {ICON_HOME, "Home Assistant",
|
WebWidget homeAssistant{ICON_HOME, "Home Assistant",
|
||||||
"https://home.rivercry.com"};
|
"https://home.rivercry.com"};
|
||||||
TrayService &trayService;
|
TrayService &trayService;
|
||||||
HyprlandService &hyprlandService;
|
HyprlandService &hyprlandService;
|
||||||
int monitorId;
|
int monitorId;
|
||||||
|
|||||||
@@ -6,22 +6,27 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <sigc++/sigc++.h>
|
#include <sigc++/sigc++.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class HyprlandService {
|
class HyprlandService {
|
||||||
public:
|
public:
|
||||||
static constexpr int kWorkspaceSlotCount = 5;
|
static constexpr int kWorkspaceSlotCount = 7;
|
||||||
|
|
||||||
|
struct WindowState {
|
||||||
|
int hyprId = -1;
|
||||||
|
};
|
||||||
|
|
||||||
struct WorkspaceState {
|
struct WorkspaceState {
|
||||||
int id = -1;
|
|
||||||
int hyprId = -1;
|
int hyprId = -1;
|
||||||
|
int monitorId = -1;
|
||||||
bool active = false;
|
bool active = false;
|
||||||
bool focused = false;
|
bool focused = false;
|
||||||
bool urgent = false;
|
std::vector<int> urgentWindows;
|
||||||
std::string label;
|
std::string label;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Monitor {
|
struct Monitor {
|
||||||
std::map<int, WorkspaceState> workspaceStates;
|
std::map<int, WorkspaceState*> workspaceStates;
|
||||||
std::string name;
|
std::string name;
|
||||||
int x = 0;
|
int x = 0;
|
||||||
int y = 0;
|
int y = 0;
|
||||||
@@ -45,58 +50,21 @@ class HyprlandService {
|
|||||||
const Monitor *getMonitorById(int id) const;
|
const Monitor *getMonitorById(int id) const;
|
||||||
Monitor *getMonitorByIndex(std::size_t index);
|
Monitor *getMonitorByIndex(std::size_t index);
|
||||||
const Monitor *getMonitorByIndex(std::size_t index) const;
|
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);
|
void switchToWorkspace(int workspaceId);
|
||||||
|
|
||||||
|
std::map<int, WorkspaceState> getAllWorkspaces() const {
|
||||||
|
return this->workspaces;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
std::string buffer;
|
|
||||||
std::map<int, Monitor> monitors;
|
std::map<int, Monitor> monitors;
|
||||||
|
std::map<int, WorkspaceState> workspaces;
|
||||||
|
|
||||||
|
std::string get_socket_path();
|
||||||
bool on_socket_read(Glib::IOCondition condition);
|
bool on_socket_read(Glib::IOCondition condition);
|
||||||
void parse_message(const std::string &line);
|
void parse_message(const std::string &line);
|
||||||
std::string get_socket_path();
|
|
||||||
void refresh_monitors();
|
void refresh_monitors();
|
||||||
void refresh_workspaces();
|
void refresh_workspaces();
|
||||||
void handle_urgent_window(std::string windowAddress);
|
void handle_urgent_window(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";
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -86,6 +86,6 @@ tooltip {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.icon-label {
|
.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;
|
font-size: 19px;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "bar/bar.hpp"
|
#include "bar/bar.hpp"
|
||||||
|
|
||||||
#include "gtk/gtk.h"
|
#include "gtk/gtk.h"
|
||||||
|
|
||||||
#include "widgets/date.hpp"
|
#include "widgets/date.hpp"
|
||||||
#include "widgets/spacer.hpp"
|
#include "widgets/spacer.hpp"
|
||||||
#include "widgets/volumeWidget.hpp"
|
#include "widgets/volumeWidget.hpp"
|
||||||
@@ -35,7 +37,6 @@ Bar::Bar(GdkMonitor *monitor, HyprlandService &hyprlandService,
|
|||||||
|
|
||||||
set_child(main_box);
|
set_child(main_box);
|
||||||
|
|
||||||
|
|
||||||
this->volumeWidget = Gtk::make_managed<VolumeWidget>();
|
this->volumeWidget = Gtk::make_managed<VolumeWidget>();
|
||||||
|
|
||||||
load_css();
|
load_css();
|
||||||
@@ -46,13 +47,9 @@ Bar::Bar(GdkMonitor *monitor, HyprlandService &hyprlandService,
|
|||||||
Glib::signal_timeout().connect(sigc::mem_fun(clock, &Clock::onUpdate),
|
Glib::signal_timeout().connect(sigc::mem_fun(clock, &Clock::onUpdate),
|
||||||
1000);
|
1000);
|
||||||
|
|
||||||
|
|
||||||
date.onUpdate();
|
date.onUpdate();
|
||||||
|
|
||||||
Glib::signal_timeout().connect(sigc::mem_fun(date, &Date::onUpdate),
|
Glib::signal_timeout().connect(sigc::mem_fun(date, &Date::onUpdate), 1000);
|
||||||
1000);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bar::setup_ui() {
|
void Bar::setup_ui() {
|
||||||
@@ -83,7 +80,6 @@ void Bar::setup_ui() {
|
|||||||
center_box.append(*(new Spacer()));
|
center_box.append(*(new Spacer()));
|
||||||
center_box.append(*this->volumeWidget);
|
center_box.append(*this->volumeWidget);
|
||||||
|
|
||||||
|
|
||||||
trayWidget = Gtk::make_managed<TrayWidget>(trayService);
|
trayWidget = Gtk::make_managed<TrayWidget>(trayService);
|
||||||
right_box.append(*trayWidget);
|
right_box.append(*trayWidget);
|
||||||
right_box.append(homeAssistant);
|
right_box.append(homeAssistant);
|
||||||
@@ -93,16 +89,16 @@ void Bar::load_css() {
|
|||||||
auto css_provider = Gtk::CssProvider::create();
|
auto css_provider = Gtk::CssProvider::create();
|
||||||
|
|
||||||
std::string css_path = "resources/bar.css";
|
std::string css_path = "resources/bar.css";
|
||||||
const char* home = std::getenv("HOME");
|
const char *home = std::getenv("HOME");
|
||||||
if (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)) {
|
if (std::filesystem::exists(config_path)) {
|
||||||
css_path = config_path.string();
|
css_path = config_path.string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string css =
|
const std::string css = SystemHelper::read_file_to_string(css_path);
|
||||||
SystemHelper::read_file_to_string(css_path);
|
|
||||||
css_provider->load_from_data(css);
|
css_provider->load_from_data(css);
|
||||||
|
|
||||||
Gtk::StyleContext::add_provider_for_display(
|
Gtk::StyleContext::add_provider_for_display(
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "services/hyprland.hpp"
|
#include "services/hyprland.hpp"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
@@ -93,19 +94,20 @@ bool HyprlandService::on_socket_read(Glib::IOCondition condition) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buffer[4096];
|
std::string buffer;
|
||||||
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) - 1);
|
||||||
|
|
||||||
if (bytes_read > 0) {
|
if (bytes_read > 0) {
|
||||||
buffer[bytes_read] = '\0';
|
temp_buffer[bytes_read] = '\0';
|
||||||
this->buffer.append(buffer);
|
buffer.append(temp_buffer);
|
||||||
|
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
|
|
||||||
while ((pos = this->buffer.find('\n')) != std::string::npos) {
|
while ((pos = buffer.find('\n')) != std::string::npos) {
|
||||||
const std::string line = this->buffer.substr(0, pos);
|
const std::string line = buffer.substr(0, pos);
|
||||||
parse_message(line);
|
parse_message(line);
|
||||||
this->buffer.erase(0, pos + 1);
|
buffer.erase(0, pos + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,6 +137,9 @@ std::string HyprlandService::get_socket_path() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HyprlandService::refresh_monitors() {
|
void HyprlandService::refresh_monitors() {
|
||||||
|
this->monitors.clear();
|
||||||
|
this->workspaces.clear();
|
||||||
|
|
||||||
std::string output;
|
std::string output;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -151,8 +156,6 @@ void HyprlandService::refresh_monitors() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<int, Monitor> updated;
|
|
||||||
|
|
||||||
for (const auto &monitorJson : monitorsJson) {
|
for (const auto &monitorJson : monitorsJson) {
|
||||||
if (!monitorJson.is_object()) {
|
if (!monitorJson.is_object()) {
|
||||||
continue;
|
continue;
|
||||||
@@ -171,11 +174,24 @@ void HyprlandService::refresh_monitors() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (monitor.id >= 0) {
|
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.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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
monitors.swap(updated);
|
|
||||||
monitorStateChanged.emit();
|
monitorStateChanged.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,75 +216,38 @@ void HyprlandService::refresh_workspaces() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto &workspaceJson : workspacesJson) {
|
for (const auto &workspaceJson : workspacesJson) {
|
||||||
if (!workspaceJson.is_object()) {
|
if (!workspaceJson.is_object()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int monitorId = workspaceJson.value("monitorID", -1);
|
|
||||||
const int workspaceId = workspaceJson.value("id", -1);
|
const int workspaceId = workspaceJson.value("id", -1);
|
||||||
|
|
||||||
auto monitorIt = monitors.find(monitorId);
|
std::cout << "Workspace ID: " << workspaceId << std::endl;
|
||||||
if (monitorIt == monitors.end() || workspaceId < 0) {
|
|
||||||
continue;
|
assert(workspaceId != -1);
|
||||||
|
|
||||||
|
std::map<int, WorkspaceState>::iterator workspaceStateIt = this->workspaces.find(workspaceId);
|
||||||
|
|
||||||
|
if (workspaceStateIt == this->workspaces.end()) {
|
||||||
|
assert(false); // should not happen
|
||||||
}
|
}
|
||||||
|
|
||||||
auto &monitor = monitorIt->second;
|
WorkspaceState *workspaceState = &workspaceStateIt->second;
|
||||||
const int slot =
|
workspaceState->hyprId = workspaceId;
|
||||||
((workspaceId - 1) % HyprlandService::kWorkspaceSlotCount) + 1;
|
workspaceState->focused = (this->monitors[workspaceState->monitorId].focusedWorkspaceId == workspaceId);
|
||||||
auto &workspaceState = monitor.workspaceStates[slot];
|
workspaceState->active =
|
||||||
|
workspaceState->focused || workspaceJson.value("windows", 0) > 0;
|
||||||
workspaceState.id = slot;
|
workspaceState->urgentWindows.clear();
|
||||||
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);
|
|
||||||
} else {
|
|
||||||
workspaceState.label = labelCandidate;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (workspaceJson.contains("urgent") &&
|
if (workspaceJson.contains("urgent") &&
|
||||||
workspaceJson["urgent"].is_boolean()) {
|
workspaceJson["urgent"].is_boolean()) {
|
||||||
workspaceState.urgent = workspaceJson["urgent"].get<bool>();
|
if (workspaceJson["urgent"].get<bool>()) {
|
||||||
} else if (workspaceJson.contains("hasurgent") &&
|
workspaceState->urgentWindows.push_back(workspaceId);
|
||||||
workspaceJson["hasurgent"].is_boolean()) {
|
}
|
||||||
workspaceState.urgent = workspaceJson["hasurgent"].get<bool>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this->workspaces[workspaceId] = *workspaceState;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto &pair : monitors) {
|
for (const auto &pair : monitors) {
|
||||||
@@ -320,20 +299,11 @@ void HyprlandService::switchToWorkspace(int workspaceId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = monitors.begin();
|
|
||||||
std::advance(it, static_cast<long>(index));
|
|
||||||
return &it->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
void HyprlandService::handle_urgent_window(std::string windowAddress) {
|
void HyprlandService::handle_urgent_window(std::string windowAddress) {
|
||||||
std::string output;
|
std::string output;
|
||||||
|
// TODO: fix bugs first
|
||||||
|
return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
output = SystemHelper::get_command_output(kClientsCommand);
|
output = SystemHelper::get_command_output(kClientsCommand);
|
||||||
} catch (const std::exception &ex) {
|
} catch (const std::exception &ex) {
|
||||||
@@ -372,16 +342,55 @@ void HyprlandService::handle_urgent_window(std::string windowAddress) {
|
|||||||
bool changed = false;
|
bool changed = false;
|
||||||
|
|
||||||
for (auto &wsPair : monitor.workspaceStates) {
|
for (auto &wsPair : monitor.workspaceStates) {
|
||||||
if (wsPair.second.hyprId == workspaceId) {
|
if (wsPair.second->hyprId == workspaceId) {
|
||||||
if (!wsPair.second.urgent) {
|
if (wsPair.second->urgentWindows.empty()) {
|
||||||
wsPair.second.urgent = true;
|
wsPair.second->urgentWindows.push_back(workspaceId);
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (changed) {
|
if (changed) {
|
||||||
workspaceStateChanged.emit(monitor.id);
|
this->workspaceStateChanged.emit(monitor.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ TrayIconWidget::TrayIconWidget(TrayService &service, std::string id)
|
|||||||
set_focusable(false);
|
set_focusable(false);
|
||||||
set_valign(Gtk::Align::CENTER);
|
set_valign(Gtk::Align::CENTER);
|
||||||
set_halign(Gtk::Align::CENTER);
|
set_halign(Gtk::Align::CENTER);
|
||||||
|
add_css_class("tray-icon");
|
||||||
|
|
||||||
picture.set_halign(Gtk::Align::CENTER);
|
picture.set_halign(Gtk::Align::CENTER);
|
||||||
picture.set_valign(Gtk::Align::CENTER);
|
picture.set_valign(Gtk::Align::CENTER);
|
||||||
|
|||||||
@@ -17,15 +17,10 @@ VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
|
|||||||
|
|
||||||
append(label);
|
append(label);
|
||||||
|
|
||||||
// Click toggles mute using wpctl
|
|
||||||
click = Gtk::GestureClick::create();
|
click = Gtk::GestureClick::create();
|
||||||
click->set_button(GDK_BUTTON_PRIMARY);
|
click->set_button(GDK_BUTTON_PRIMARY);
|
||||||
// signal_released provides (int, double, double) — use lambda to ignore
|
click->signal_released().connect([this](int, double, double) {
|
||||||
// args
|
|
||||||
click->signal_released().connect([this](int /*n_press*/, double /*x*/,
|
|
||||||
double /*y*/) {
|
|
||||||
try {
|
try {
|
||||||
// Toggle mute then refresh
|
|
||||||
(void)SystemHelper::get_command_output(
|
(void)SystemHelper::get_command_output(
|
||||||
"wpctl set-mute @DEFAULT_SINK@ toggle");
|
"wpctl set-mute @DEFAULT_SINK@ toggle");
|
||||||
} catch (const std::exception &ex) {
|
} catch (const std::exception &ex) {
|
||||||
@@ -34,19 +29,17 @@ VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
|
|||||||
}
|
}
|
||||||
this->update();
|
this->update();
|
||||||
});
|
});
|
||||||
add_controller(click);
|
|
||||||
|
|
||||||
// Initial read
|
add_controller(click);
|
||||||
update();
|
update();
|
||||||
|
|
||||||
// Start polling every 1 second to keep the display up to date
|
this->timeoutConn = Glib::signal_timeout().connect(
|
||||||
timeoutConn = Glib::signal_timeout().connect(
|
|
||||||
sigc::mem_fun(*this, &VolumeWidget::on_timeout), 100);
|
sigc::mem_fun(*this, &VolumeWidget::on_timeout), 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
VolumeWidget::~VolumeWidget() {
|
VolumeWidget::~VolumeWidget() {
|
||||||
if (timeoutConn.connected())
|
if (this->timeoutConn.connected())
|
||||||
timeoutConn.disconnect();
|
this->timeoutConn.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VolumeWidget::update() {
|
void VolumeWidget::update() {
|
||||||
@@ -54,7 +47,6 @@ void VolumeWidget::update() {
|
|||||||
const std::string out =
|
const std::string out =
|
||||||
SystemHelper::get_command_output("wpctl get-volume @DEFAULT_SINK@");
|
SystemHelper::get_command_output("wpctl get-volume @DEFAULT_SINK@");
|
||||||
|
|
||||||
// Attempt to parse a number (percentage or fraction)
|
|
||||||
std::smatch m;
|
std::smatch m;
|
||||||
std::regex r_percent(R"((\d+(?:\.\d+)?)%)");
|
std::regex r_percent(R"((\d+(?:\.\d+)?)%)");
|
||||||
std::regex r_number(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)) {
|
if (std::regex_search(text, m, r_percent)) {
|
||||||
percent = static_cast<int>(std::round(std::stod(m[1].str())));
|
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());
|
const double v = std::stod(m[1].str());
|
||||||
if (v <= 1.0)
|
if (v <= 1.0)
|
||||||
percent = static_cast<int>(std::round(v * 100.0));
|
percent = static_cast<int>(std::round(v * 100.0));
|
||||||
@@ -76,7 +67,6 @@ void VolumeWidget::update() {
|
|||||||
if (percent >= 0) {
|
if (percent >= 0) {
|
||||||
label.set_text(std::to_string(percent) + "%");
|
label.set_text(std::to_string(percent) + "%");
|
||||||
} else {
|
} else {
|
||||||
// Fallback to raw output (trimmed)
|
|
||||||
auto pos = text.find_first_not_of(" \t\n\r");
|
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");
|
auto end = text.find_last_not_of(" \t\n\r");
|
||||||
@@ -94,5 +84,6 @@ void VolumeWidget::update() {
|
|||||||
|
|
||||||
bool VolumeWidget::on_timeout() {
|
bool VolumeWidget::on_timeout() {
|
||||||
update();
|
update();
|
||||||
|
|
||||||
return true; // keep timeout active
|
return true; // keep timeout active
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include "widgets/workspaceIndicator.hpp"
|
#include "widgets/workspaceIndicator.hpp"
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include <gdk/gdk.h>
|
#include <gdk/gdk.h>
|
||||||
#include <gtkmm/gestureclick.h>
|
#include <gtkmm/gestureclick.h>
|
||||||
#include <gtkmm/widget.h>
|
#include <gtkmm/widget.h>
|
||||||
@@ -41,55 +40,31 @@ void WorkspaceIndicator::on_workspace_update(int monitorId) {
|
|||||||
void WorkspaceIndicator::on_monitor_update() { rebuild(); }
|
void WorkspaceIndicator::on_monitor_update() { rebuild(); }
|
||||||
|
|
||||||
void WorkspaceIndicator::rebuild() {
|
void WorkspaceIndicator::rebuild() {
|
||||||
clear_children();
|
HyprlandService hypr = this->service;
|
||||||
|
int i = 0;
|
||||||
HyprlandService::Monitor *monitor = nullptr;
|
for (auto &[id, workspaceState] : hypr.getAllWorkspaces()) {
|
||||||
try {
|
if (workspaceState.monitorId != this->monitorId) {
|
||||||
monitor = service.getMonitorById(monitorId);
|
continue;
|
||||||
} 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);
|
auto label = Gtk::make_managed<Gtk::Label>(workspaceState.label);
|
||||||
label->add_css_class("workspace-pill");
|
label->add_css_class("workspace-pill");
|
||||||
|
|
||||||
auto gesture = Gtk::GestureClick::create();
|
auto gesture = Gtk::GestureClick::create();
|
||||||
gesture->set_button(GDK_BUTTON_PRIMARY);
|
gesture->set_button(GDK_BUTTON_PRIMARY);
|
||||||
gesture->signal_released().connect(
|
gesture->signal_released().connect(
|
||||||
[this, workspaceId](int /*n_press*/, double /*x*/, double /*y*/) {
|
[this, id](int, double, double) { service.switchToWorkspace(id); });
|
||||||
int realWorkspaceId = workspaceId + 5 * (monitorId);
|
|
||||||
|
|
||||||
service.switchToWorkspace(realWorkspaceId);
|
|
||||||
});
|
|
||||||
label->add_controller(gesture);
|
label->add_controller(gesture);
|
||||||
|
|
||||||
if (state != nullptr) {
|
if (workspaceState.urgentWindows.empty()) {
|
||||||
if (state->urgent != true) {
|
if (workspaceState.focused) {
|
||||||
if (state->focused) {
|
label->add_css_class("workspace-pill-focused");
|
||||||
label->add_css_class("workspace-pill-focused");
|
} else if (workspaceState.active) {
|
||||||
} else if (state->active) {
|
label->add_css_class("workspace-pill-active");
|
||||||
label->add_css_class("workspace-pill-active");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
label->add_css_class("workspace-pill-urgent");
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
label->add_css_class("workspace-pill-urgent");
|
||||||
}
|
}
|
||||||
|
|
||||||
append(*label);
|
append(*label);
|
||||||
|
|||||||
13
tmp_test.cpp
13
tmp_test.cpp
@@ -1,10 +1,13 @@
|
|||||||
#include <giomm/menumodel.h>
|
|
||||||
#include <gio/gio.h>
|
|
||||||
#include <gio/gdbusmenumodel.h>
|
#include <gio/gdbusmenumodel.h>
|
||||||
|
#include <gio/gio.h>
|
||||||
|
#include <giomm/menumodel.h>
|
||||||
|
|
||||||
int main(){
|
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);
|
GDBusMenuModel *dbusModel = g_dbus_menu_model_get_for_bus_sync(
|
||||||
if(!dbusModel) return 0;
|
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));
|
Glib::RefPtr<Gio::MenuModel> model = Glib::wrap(G_MENU_MODEL(dbusModel));
|
||||||
return model ? 0 : 1;
|
return model ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user