fix urgent window icons

This commit is contained in:
2025-12-17 23:34:12 +01:00
parent 11ccd55a52
commit 9b0a036925
11 changed files with 172 additions and 194 deletions

View File

@@ -1,2 +1,3 @@
IndentWidth: 4
ColumnLimit: 0
AlignConsecutiveAssignments: true

View File

@@ -10,6 +10,9 @@
class HyprlandService {
public:
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;
@@ -20,7 +23,7 @@ class HyprlandService {
int monitorId = -1;
bool active = false;
bool focused = false;
std::vector<int> urgentWindows;
std::vector<std::string> urgentWindows;
std::string label;
};
@@ -38,7 +41,6 @@ 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;
@@ -46,9 +48,7 @@ class HyprlandService {
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;
void switchToWorkspace(int workspaceId);
std::map<int, WorkspaceState *> getAllWorkspaces() const {
@@ -65,5 +65,6 @@ class HyprlandService {
void parse_message(const std::string &line);
void refresh_monitors();
void refresh_workspaces();
void handle_urgent_window(std::string windowAddress);
void onUrgentEvent(std::string windowAddress);
void onActiveWindowEvent(std::string windowAddress);
};

View File

@@ -22,6 +22,10 @@ window {
border-radius: 5px;
}
.workspace-pill:hover {
background-color: rgba(255, 255, 255, 0.1);
}
.workspace-pill-focused {
background-color: #ffffff;
color: #1e1e1e;
@@ -42,10 +46,6 @@ window {
margin-right: 0;
}
.workspace-pill:hover {
background-color: rgba(255, 255, 255, 0.1);
}
.minimized {
background-color: rgba(50, 50, 50, 0.5);
}

View File

@@ -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() {
@@ -39,13 +29,19 @@ void HyprlandService::on_hyprland_event(std::string event, std::string data) {
}
if (event == "urgent") {
handle_urgent_window(data);
onUrgentEvent(data);
}
if (event == "workspace" || event == "focusedmon" || event == "monitoradded" ||
event == "monitorremoved" || event == "movewindow" || event == "activewindow") {
if (event == "workspace" || event == "movewindow") {
refresh_workspaces();
}
// use for
// event == "focusedmon"
if (event == "activewindowv2") {
onActiveWindowEvent(data);
}
}
void HyprlandService::start() {
@@ -145,8 +141,6 @@ void HyprlandService::refresh_monitors() {
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &monitorJson : monitorsJson) {
assert(monitorJson.is_object());
Monitor monitor;
monitor.id = monitorJson.value("id", -1);
monitor.name = monitorJson.value("name", "");
@@ -194,14 +188,11 @@ void HyprlandService::refresh_workspaces() {
auto monitorsJson = nlohmann::json::parse(output, nullptr, false);
for (const auto &monitorJson : monitorsJson) {
const int monitorId = monitorJson.value("id", -1);
const int focusedWorkspaceId = monitorJson["activeWorkspace"].value("id", -1);
auto monitorIt = this->monitors.find(monitorId);
monitorIt->second.focusedWorkspaceId = focusedWorkspaceId;
auto monitor = this->monitors[monitorId];
monitor.focusedWorkspaceId = focusedWorkspaceId;
}
for (const auto &workspaceJson : workspacesJson) {
@@ -209,50 +200,13 @@ void HyprlandService::refresh_workspaces() {
std::map<int, WorkspaceState *>::iterator workspaceStateIt = this->workspaces.find(workspaceId);
WorkspaceState *workspaceState = workspaceStateIt->second;
workspaceState->focused = monitors
.at(workspaceState->monitorId)
.focusedWorkspaceId ==
workspaceId;
workspaceState->focused = monitors[workspaceState->monitorId].focusedWorkspaceId == workspaceId;
workspaceState->active = true;
this->workspaces[workspaceId] = workspaceState;
}
workspaceStateChanged.emit();
}
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;
}
void HyprlandService::switchToWorkspace(int workspaceId) {
std::string cmd =
"hyprctl dispatch workspace " + std::to_string(workspaceId);
@@ -265,38 +219,53 @@ void HyprlandService::switchToWorkspace(int workspaceId) {
}
}
void HyprlandService::handle_urgent_window(std::string windowAddress) {
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) {
if (!clientJson.is_object()) {
continue;
}
const std::string addr = clientJson.value("address", "");
if (addr != windowAddress) {
continue;
}
const int workspaceId = clientJson.value("workspace", -1);
if (workspaceId < 0) {
continue;
}
if (addr == "0x" + windowAddress) {
int workspaceId = clientJson["workspace"].value("id", -1);
auto workspaceIt = this->workspaces.find(workspaceId);
if (workspaceIt == this->workspaces.end()) {
continue;
}
WorkspaceState *ws = this->workspaces[workspaceId];
WorkspaceState *wsState = workspaceIt->second;
wsState->urgentWindows.push_back(1);
// todo: maybee better method of access
if (ws) {
ws->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"];
WorkspaceState *ws = this->workspaces[workspaceId];
if (ws) {
auto it = std::find(ws->urgentWindows.begin(), ws->urgentWindows.end(), windowAddress);
if (it != ws->urgentWindows.end()) {
ws->urgentWindows.erase(it);
workspaceStateChanged.emit();
}
break;
}
}
}
}
void HyprlandService::printMonitor(const Monitor &mon) const {
std::cout << "=== Monitor Info ===\n";
std::cout << "Name: " << mon.name << " (ID: " << mon.id << ")\n";
@@ -335,3 +304,24 @@ void HyprlandService::printMonitor(const Monitor &mon) const {
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));
}
auto it = monitors.begin();
std::advance(it, static_cast<long>(index));
return &it->second;
}

View File

@@ -82,7 +82,6 @@ void WorkspaceIndicator::rebuild() {
HyprlandService::Monitor *mon = service.getMonitorById(this->monitorId);
for (auto [id, workspaceState] : mon->workspaceStates) {
assert(id > 0);
Gtk::Label *label = workspaceLabels[id];
this->refreshLabel(label, *workspaceState);
}

View File

@@ -1,13 +0,0 @@
#include <gio/gdbusmenumodel.h>
#include <gio/gio.h>
#include <giomm/menumodel.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;
}