Compare commits
3 Commits
2d5b492da8
...
laptop
| Author | SHA1 | Date | |
|---|---|---|---|
| e3bb3fdf31 | |||
| 0e613141da | |||
| ab7b3b3092 |
@@ -28,6 +28,7 @@ target_sources(bar_lib
|
||||
src/widgets/workspaceIndicator.cpp
|
||||
src/widgets/volumeWidget.cpp
|
||||
src/widgets/webWidget.cpp
|
||||
src/widgets/battery.cpp
|
||||
|
||||
src/services/todo.cpp
|
||||
src/services/sqliteTodoAdapter.cpp
|
||||
|
||||
@@ -4,13 +4,14 @@
|
||||
#include <gtkmm.h>
|
||||
|
||||
#include "icons.hpp"
|
||||
#include "widgets/battery.hpp"
|
||||
#include "widgets/clock.hpp"
|
||||
#include "widgets/controlCenter.hpp"
|
||||
#include "widgets/date.hpp"
|
||||
#include "widgets/tray.hpp"
|
||||
#include "widgets/volumeWidget.hpp"
|
||||
#include "widgets/webWidget.hpp"
|
||||
#include "widgets/workspaceIndicator.hpp"
|
||||
#include "widgets/controlCenter.hpp"
|
||||
|
||||
class Bar : public Gtk::Window {
|
||||
public:
|
||||
@@ -32,6 +33,7 @@ class Bar : public Gtk::Window {
|
||||
WorkspaceIndicator *workspaceIndicator = nullptr;
|
||||
TrayWidget *trayWidget = nullptr;
|
||||
VolumeWidget *volumeWidget = nullptr;
|
||||
BatteryWidget *batteryWidget = nullptr;
|
||||
|
||||
|
||||
void setup_ui();
|
||||
|
||||
@@ -50,8 +50,11 @@ class TrayService {
|
||||
bool separator = false;
|
||||
std::vector<MenuNode> children;
|
||||
};
|
||||
std::optional<MenuNode> get_menu_layout(const std::string &id);
|
||||
bool activate_menu_item(const std::string &id, int itemId);
|
||||
using MenuLayoutCallback = sigc::slot<void(std::optional<MenuNode>)>;
|
||||
void request_menu_layout(const std::string &id, MenuLayoutCallback callback);
|
||||
bool activate_menu_item(const std::string &id, int itemId, int32_t x = -1,
|
||||
int32_t y = -1, uint32_t button = 1,
|
||||
uint32_t timestampMs = 0);
|
||||
|
||||
sigc::signal<void(const Item &)> &signal_item_added();
|
||||
sigc::signal<void(const std::string &)> &signal_item_removed();
|
||||
@@ -75,6 +78,11 @@ class TrayService {
|
||||
guint ownerWatchId = 0;
|
||||
Glib::RefPtr<Gio::MenuModel> menuModel;
|
||||
Glib::RefPtr<Gio::ActionGroup> menuActions;
|
||||
|
||||
guint refreshSourceId = 0;
|
||||
bool refreshInFlight = false;
|
||||
bool refreshQueued = false;
|
||||
bool addSignalPending = false;
|
||||
};
|
||||
|
||||
Glib::RefPtr<Gio::DBus::Connection> connection;
|
||||
@@ -124,7 +132,11 @@ class TrayService {
|
||||
void register_item(const Glib::ustring &sender, const std::string &service);
|
||||
void unregister_item(const std::string &id);
|
||||
|
||||
void refresh_item(TrackedItem &item);
|
||||
void schedule_refresh(const std::string &id);
|
||||
void begin_refresh(const std::string &id);
|
||||
static gboolean refresh_timeout_cb(gpointer user_data);
|
||||
static void on_refresh_finished_static(GObject *source, GAsyncResult *res,
|
||||
gpointer user_data);
|
||||
void emit_registered_items_changed();
|
||||
|
||||
Glib::Variant<std::vector<Glib::ustring>>
|
||||
|
||||
26
include/widgets/battery.hpp
Normal file
26
include/widgets/battery.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <gtkmm/box.h>
|
||||
#include <gtkmm/label.h>
|
||||
#include <sigc++/connection.h>
|
||||
|
||||
class BatteryWidget : public Gtk::Box {
|
||||
public:
|
||||
BatteryWidget();
|
||||
~BatteryWidget();
|
||||
|
||||
private:
|
||||
Gtk::Label iconLabel;
|
||||
Gtk::Label label;
|
||||
sigc::connection timeoutConn;
|
||||
std::filesystem::path batteryPath;
|
||||
std::string currentStateClass;
|
||||
|
||||
void update();
|
||||
bool on_timeout();
|
||||
void find_battery_path();
|
||||
void set_state_class(const std::string &stateClass);
|
||||
std::string build_icon(int capacity, bool hasBattery, bool charging, bool full) const;
|
||||
std::string build_text(int capacity, const std::string &status, bool hasBattery, bool charging, bool full) const;
|
||||
};
|
||||
@@ -13,7 +13,9 @@
|
||||
#include <gtkmm/popovermenu.h>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "services/tray.hpp"
|
||||
#include "components/base/button.hpp"
|
||||
@@ -21,6 +23,7 @@
|
||||
class TrayIconWidget : public Button {
|
||||
public:
|
||||
TrayIconWidget(std::string id);
|
||||
~TrayIconWidget() override;
|
||||
|
||||
void update(const TrayService::Item &item);
|
||||
|
||||
@@ -31,25 +34,28 @@ class TrayIconWidget : public Button {
|
||||
Gtk::Picture picture;
|
||||
Gtk::Image image;
|
||||
Glib::RefPtr<Gtk::GestureClick> primaryGesture;
|
||||
Glib::RefPtr<Gtk::GestureClick> middleGesture;
|
||||
Glib::RefPtr<Gtk::GestureClick> secondaryGesture;
|
||||
Glib::RefPtr<Gtk::PopoverMenu> menuPopover;
|
||||
Glib::RefPtr<Gio::SimpleActionGroup> menuActions;
|
||||
Glib::RefPtr<Gio::MenuModel> menuModel;
|
||||
sigc::connection menuChangedConnection;
|
||||
bool menuPopupPending = false;
|
||||
bool menuRequestInFlight = false;
|
||||
bool hasRemoteMenu = false;
|
||||
std::shared_ptr<bool> aliveFlag;
|
||||
double pendingX = 0.0;
|
||||
double pendingY = 0.0;
|
||||
|
||||
void on_primary_released(int n_press, double x, double y);
|
||||
void on_middle_released(int n_press, double x, double y);
|
||||
void on_secondary_released(int n_press, double x, double y);
|
||||
bool ensure_menu();
|
||||
void on_menu_items_changed(guint position, guint removed, guint added);
|
||||
void try_popup();
|
||||
void on_menu_layout_ready(std::optional<TrayService::MenuNode> layout);
|
||||
void
|
||||
populate_menu_items(const std::vector<TrayService::MenuNode> &nodes,
|
||||
const Glib::RefPtr<Gio::Menu> &menu,
|
||||
const Glib::RefPtr<Gio::SimpleActionGroup> &actions);
|
||||
void on_menu_action(const Glib::VariantBase ¶meter, int itemId);
|
||||
bool try_get_pending_coords(int32_t &outX, int32_t &outY) const;
|
||||
};
|
||||
|
||||
class TrayWidget : public Gtk::Box {
|
||||
|
||||
@@ -136,3 +136,93 @@ button {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.battery-widget {
|
||||
padding: 2px 6px;
|
||||
border-radius: 6px;
|
||||
background-color: rgba(255, 255, 255, 0.04);
|
||||
}
|
||||
|
||||
.battery-widget-icon {
|
||||
min-width: 7ch;
|
||||
text-align: center;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.08ch;
|
||||
}
|
||||
|
||||
.battery-widget-text {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.battery-widget-normal {
|
||||
background-color: rgba(120, 120, 120, 0.18);
|
||||
}
|
||||
|
||||
.battery-widget-full {
|
||||
background-color: rgba(76, 129, 76, 0.24);
|
||||
}
|
||||
|
||||
.battery-widget-full .battery-widget-icon {
|
||||
color: #bdf5bd;
|
||||
}
|
||||
|
||||
.battery-widget-charging {
|
||||
background-color: rgba(76, 129, 76, 0.28);
|
||||
}
|
||||
|
||||
.battery-widget-charging .battery-widget-icon {
|
||||
color: #9df19d;
|
||||
animation: battery-charge-glow 1.4s ease-in-out infinite;
|
||||
}
|
||||
|
||||
.battery-widget-low {
|
||||
background-color: rgba(148, 61, 61, 0.28);
|
||||
}
|
||||
|
||||
.battery-widget-low .battery-widget-icon {
|
||||
color: #ff8585;
|
||||
animation: battery-low-blink 1s steps(2, start) infinite;
|
||||
}
|
||||
|
||||
.battery-widget-external {
|
||||
background-color: rgba(100, 100, 100, 0.18);
|
||||
}
|
||||
|
||||
.battery-widget-external .battery-widget-icon {
|
||||
color: #d7d7d7;
|
||||
}
|
||||
|
||||
@keyframes battery-charge-glow {
|
||||
0% {
|
||||
opacity: 0.7;
|
||||
transform: translateX(0);
|
||||
}
|
||||
|
||||
50% {
|
||||
opacity: 1;
|
||||
transform: translateX(1px);
|
||||
}
|
||||
|
||||
100% {
|
||||
opacity: 0.7;
|
||||
transform: translateX(0);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes battery-low-blink {
|
||||
0% {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
49% {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
50% {
|
||||
opacity: 0.4;
|
||||
}
|
||||
|
||||
100% {
|
||||
opacity: 0.4;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ Bar::Bar(GdkMonitor *monitor, int monitorId)
|
||||
this->volumeWidget = Gtk::make_managed<VolumeWidget>();
|
||||
this->workspaceIndicator = Gtk::make_managed<WorkspaceIndicator>(monitorId);
|
||||
this->trayWidget = Gtk::make_managed<TrayWidget>();
|
||||
this->batteryWidget = Gtk::make_managed<BatteryWidget>();
|
||||
|
||||
load_css();
|
||||
setup_ui();
|
||||
@@ -77,6 +78,7 @@ void Bar::setup_center_box() {
|
||||
|
||||
void Bar::setup_right_box() {
|
||||
right_box.append(*this->trayWidget);
|
||||
right_box.append(*this->batteryWidget);
|
||||
right_box.append(this->homeAssistant);
|
||||
right_box.append(this->controlCenter);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "services/hyprland.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
@@ -9,6 +10,7 @@
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unordered_set>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "helpers/systemHelper.hpp"
|
||||
@@ -213,6 +215,22 @@ void HyprlandService::refresh_workspaces() {
|
||||
}
|
||||
}
|
||||
|
||||
std::string clientsOutput = SystemHelper::get_command_output(kClientsCommand);
|
||||
auto clientsJson = nlohmann::json::parse(clientsOutput, nullptr, false);
|
||||
std::unordered_set<std::string> liveClientAddresses;
|
||||
for (const auto &clientJson : clientsJson) {
|
||||
const std::string addr = clientJson.value("address", "");
|
||||
if (addr.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (addr.rfind("0x", 0) == 0) {
|
||||
liveClientAddresses.insert(addr.substr(2));
|
||||
} else {
|
||||
liveClientAddresses.insert(addr);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &workspaceJson : workspacesJson) {
|
||||
const int workspaceId = workspaceJson.value("id", -1);
|
||||
auto workspaceStateIt = this->workspaces.find(workspaceId);
|
||||
@@ -230,6 +248,17 @@ void HyprlandService::refresh_workspaces() {
|
||||
workspaceState->active = true;
|
||||
}
|
||||
|
||||
// drop urgent flags for windows no longer reported by hyprctl clients
|
||||
for (auto &[id, ws] : this->workspaces) {
|
||||
auto &urgent = ws->urgentWindows;
|
||||
auto newEnd = std::remove_if(urgent.begin(), urgent.end(), [&](const std::string &addr) {
|
||||
return liveClientAddresses.find(addr) == liveClientAddresses.end();
|
||||
});
|
||||
if (newEnd != urgent.end()) {
|
||||
urgent.erase(newEnd, urgent.end());
|
||||
}
|
||||
}
|
||||
|
||||
workspaceStateChanged.emit();
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <giomm/dbusownname.h>
|
||||
#include <giomm/menumodel.h>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
@@ -21,6 +22,11 @@ constexpr const char *kItemInterface = "org.kde.StatusNotifierItem";
|
||||
constexpr const char *kDBusPropertiesIface = "org.freedesktop.DBus.Properties";
|
||||
constexpr const char *kDBusMenuInterface = "com.canonical.dbusmenu";
|
||||
|
||||
constexpr int kDBusTimeoutMs = 1500;
|
||||
constexpr int kDBusMenuTimeoutMs = 2000;
|
||||
constexpr int kRefreshDebounceMs = 50;
|
||||
constexpr int kAboutToShowTimeoutMs = 800;
|
||||
|
||||
const char *kWatcherIntrospection =
|
||||
R"(<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-Bus Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
|
||||
<node>
|
||||
@@ -105,53 +111,43 @@ void call_about_to_show(const Glib::RefPtr<Gio::DBus::Connection> &connection,
|
||||
return;
|
||||
}
|
||||
|
||||
GError *error = nullptr;
|
||||
GVariant *result = g_dbus_connection_call_sync(
|
||||
connection->gobj(), busName.c_str(), menuPath.c_str(),
|
||||
kDBusMenuInterface, "AboutToShow", g_variant_new("(i)", id), nullptr,
|
||||
G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
|
||||
if (result) {
|
||||
g_variant_unref(result);
|
||||
}
|
||||
if (error) {
|
||||
std::cerr << "[TrayService] AboutToShow failed for " << busName
|
||||
<< menuPath << " (" << id << "): " << error->message
|
||||
<< std::endl;
|
||||
g_error_free(error);
|
||||
}
|
||||
g_dbus_connection_call(connection->gobj(), busName.c_str(), menuPath.c_str(),
|
||||
kDBusMenuInterface, "AboutToShow",
|
||||
g_variant_new("(i)", id), nullptr,
|
||||
G_DBUS_CALL_FLAGS_NONE, kAboutToShowTimeoutMs,
|
||||
nullptr, nullptr, nullptr);
|
||||
}
|
||||
|
||||
GVariant *call_get_layout(const Glib::RefPtr<Gio::DBus::Connection> &connection,
|
||||
const std::string &busName,
|
||||
const std::string &menuPath) {
|
||||
if (!connection) {
|
||||
return nullptr;
|
||||
}
|
||||
struct SimpleCallData {
|
||||
std::string debugLabel;
|
||||
bool ignoreUnknownMethod = false;
|
||||
};
|
||||
|
||||
GVariant *properties = create_property_list_variant();
|
||||
if (!properties) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
GVariant *params = g_variant_new("(ii@as)", 0, -1, properties);
|
||||
g_variant_ref_sink(properties);
|
||||
void on_simple_call_finished(GObject *source, GAsyncResult *res,
|
||||
gpointer user_data) {
|
||||
std::unique_ptr<SimpleCallData> data(
|
||||
static_cast<SimpleCallData *>(user_data));
|
||||
|
||||
GError *error = nullptr;
|
||||
GVariant *result = g_dbus_connection_call_sync(
|
||||
connection->gobj(), busName.c_str(), menuPath.c_str(),
|
||||
kDBusMenuInterface, "GetLayout", params, nullptr,
|
||||
G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
|
||||
GVariant *reply =
|
||||
g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error);
|
||||
|
||||
g_variant_unref(properties);
|
||||
|
||||
if (error) {
|
||||
std::cerr << "[TrayService] GetLayout failed for " << busName
|
||||
<< menuPath << ": " << error->message << std::endl;
|
||||
g_error_free(error);
|
||||
return nullptr;
|
||||
if (reply) {
|
||||
g_variant_unref(reply);
|
||||
}
|
||||
|
||||
return result;
|
||||
if (!error) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bool isUnknownMethod =
|
||||
(error->domain == G_DBUS_ERROR && error->code == G_DBUS_ERROR_UNKNOWN_METHOD);
|
||||
if (!(data && data->ignoreUnknownMethod && isUnknownMethod)) {
|
||||
std::cerr << "[TrayService] "
|
||||
<< (data ? data->debugLabel : std::string("D-Bus call"))
|
||||
<< " failed: " << error->message << std::endl;
|
||||
}
|
||||
g_error_free(error);
|
||||
}
|
||||
|
||||
void parse_menu_node(GVariant *tuple, TrayService::MenuNode &outNode) {
|
||||
@@ -308,6 +304,10 @@ void TrayService::start() {
|
||||
void TrayService::stop() {
|
||||
if (connection) {
|
||||
for (auto &pair : items) {
|
||||
if (pair.second->refreshSourceId != 0) {
|
||||
g_source_remove(pair.second->refreshSourceId);
|
||||
pair.second->refreshSourceId = 0;
|
||||
}
|
||||
if (pair.second->signalSubscriptionId != 0) {
|
||||
g_dbus_connection_signal_unsubscribe(
|
||||
connection->gobj(), pair.second->signalSubscriptionId);
|
||||
@@ -361,22 +361,14 @@ void TrayService::activate(const std::string &id, int32_t x, int32_t y) {
|
||||
return;
|
||||
}
|
||||
|
||||
GError *error = nullptr;
|
||||
GVariant *result = g_dbus_connection_call_sync(
|
||||
auto data = new SimpleCallData();
|
||||
data->debugLabel = "Activate(" + id + ")";
|
||||
data->ignoreUnknownMethod = false;
|
||||
g_dbus_connection_call(
|
||||
connection->gobj(), it->second->publicData.busName.c_str(),
|
||||
it->second->publicData.objectPath.c_str(), kItemInterface, "Activate",
|
||||
g_variant_new("(ii)", x, y), nullptr, G_DBUS_CALL_FLAGS_NONE, -1,
|
||||
nullptr, &error);
|
||||
|
||||
if (result) {
|
||||
g_variant_unref(result);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
std::cerr << "[TrayService] Activate failed for " << id << ": "
|
||||
<< error->message << std::endl;
|
||||
g_error_free(error);
|
||||
}
|
||||
g_variant_new("(ii)", x, y), nullptr, G_DBUS_CALL_FLAGS_NONE,
|
||||
kDBusTimeoutMs, nullptr, &on_simple_call_finished, data);
|
||||
}
|
||||
|
||||
void TrayService::secondaryActivate(const std::string &id, int32_t x,
|
||||
@@ -386,22 +378,15 @@ void TrayService::secondaryActivate(const std::string &id, int32_t x,
|
||||
return;
|
||||
}
|
||||
|
||||
GError *error = nullptr;
|
||||
GVariant *result = g_dbus_connection_call_sync(
|
||||
auto data = new SimpleCallData();
|
||||
data->debugLabel = "SecondaryActivate(" + id + ")";
|
||||
data->ignoreUnknownMethod = false;
|
||||
g_dbus_connection_call(
|
||||
connection->gobj(), it->second->publicData.busName.c_str(),
|
||||
it->second->publicData.objectPath.c_str(), kItemInterface,
|
||||
"SecondaryActivate", g_variant_new("(ii)", x, y), nullptr,
|
||||
G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
|
||||
|
||||
if (result) {
|
||||
g_variant_unref(result);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
std::cerr << "[TrayService] SecondaryActivate failed for " << id << ": "
|
||||
<< error->message << std::endl;
|
||||
g_error_free(error);
|
||||
}
|
||||
G_DBUS_CALL_FLAGS_NONE, kDBusTimeoutMs, nullptr,
|
||||
&on_simple_call_finished, data);
|
||||
}
|
||||
|
||||
void TrayService::contextMenu(const std::string &id, int32_t x, int32_t y) {
|
||||
@@ -410,25 +395,15 @@ void TrayService::contextMenu(const std::string &id, int32_t x, int32_t y) {
|
||||
return;
|
||||
}
|
||||
|
||||
GError *error = nullptr;
|
||||
GVariant *result = g_dbus_connection_call_sync(
|
||||
auto data = new SimpleCallData();
|
||||
data->debugLabel = "ContextMenu(" + id + ")";
|
||||
data->ignoreUnknownMethod = true;
|
||||
g_dbus_connection_call(
|
||||
connection->gobj(), it->second->publicData.busName.c_str(),
|
||||
it->second->publicData.objectPath.c_str(), kItemInterface,
|
||||
"ContextMenu", g_variant_new("(ii)", x, y), nullptr,
|
||||
G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
|
||||
|
||||
if (result) {
|
||||
g_variant_unref(result);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
if (!(error->domain == G_DBUS_ERROR &&
|
||||
error->code == G_DBUS_ERROR_UNKNOWN_METHOD)) {
|
||||
std::cerr << "[TrayService] ContextMenu failed for " << id << ": "
|
||||
<< error->message << std::endl;
|
||||
}
|
||||
g_error_free(error);
|
||||
}
|
||||
G_DBUS_CALL_FLAGS_NONE, kDBusTimeoutMs, nullptr,
|
||||
&on_simple_call_finished, data);
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gio::MenuModel>
|
||||
@@ -485,42 +460,110 @@ TrayService::get_menu_action_group(const std::string &id) {
|
||||
return item.menuActions;
|
||||
}
|
||||
|
||||
std::optional<TrayService::MenuNode>
|
||||
TrayService::get_menu_layout(const std::string &id) {
|
||||
struct MenuLayoutCallData {
|
||||
TrayService *self = nullptr;
|
||||
std::string id;
|
||||
std::string busName;
|
||||
std::string menuPath;
|
||||
TrayService::MenuLayoutCallback callback;
|
||||
};
|
||||
|
||||
void on_menu_layout_finished(GObject *source, GAsyncResult *res,
|
||||
gpointer user_data) {
|
||||
std::unique_ptr<MenuLayoutCallData> data(
|
||||
static_cast<MenuLayoutCallData *>(user_data));
|
||||
if (!data || !data->self) {
|
||||
return;
|
||||
}
|
||||
|
||||
GError *error = nullptr;
|
||||
GVariant *reply =
|
||||
g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error);
|
||||
|
||||
if (error) {
|
||||
if (data->callback) {
|
||||
data->callback(std::nullopt);
|
||||
}
|
||||
g_error_free(error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!reply) {
|
||||
if (data->callback) {
|
||||
data->callback(std::nullopt);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
GVariant *rootTuple = g_variant_get_child_value(reply, 1);
|
||||
g_variant_unref(reply);
|
||||
|
||||
if (!rootTuple) {
|
||||
if (data->callback) {
|
||||
data->callback(std::nullopt);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
TrayService::MenuNode rootNode;
|
||||
parse_menu_node(rootTuple, rootNode);
|
||||
g_variant_unref(rootTuple);
|
||||
|
||||
if (data->callback) {
|
||||
data->callback(std::make_optional(std::move(rootNode)));
|
||||
}
|
||||
}
|
||||
|
||||
void TrayService::request_menu_layout(const std::string &id,
|
||||
MenuLayoutCallback callback) {
|
||||
auto it = items.find(id);
|
||||
if (it == items.end() || !connection) {
|
||||
return std::nullopt;
|
||||
if (callback) {
|
||||
callback(std::nullopt);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
auto &item = *it->second;
|
||||
if (!item.publicData.menuAvailable || item.publicData.menuPath.empty()) {
|
||||
return std::nullopt;
|
||||
if (callback) {
|
||||
callback(std::nullopt);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
call_about_to_show(connection, item.publicData.busName,
|
||||
item.publicData.menuPath, 0);
|
||||
|
||||
GVariant *result = call_get_layout(connection, item.publicData.busName,
|
||||
item.publicData.menuPath);
|
||||
if (!result) {
|
||||
return std::nullopt;
|
||||
auto data = new MenuLayoutCallData();
|
||||
data->self = this;
|
||||
data->id = id;
|
||||
data->busName = item.publicData.busName;
|
||||
data->menuPath = item.publicData.menuPath;
|
||||
data->callback = std::move(callback);
|
||||
|
||||
GVariant *properties = create_property_list_variant();
|
||||
if (!properties) {
|
||||
if (data->callback) {
|
||||
data->callback(std::nullopt);
|
||||
}
|
||||
delete data;
|
||||
return;
|
||||
}
|
||||
|
||||
GVariant *rootTuple = g_variant_get_child_value(result, 1);
|
||||
g_variant_unref(result);
|
||||
// g_variant_new consumes the floating reference for '@as'.
|
||||
GVariant *params = g_variant_new("(ii@as)", 0, -1, properties);
|
||||
|
||||
if (!rootTuple) {
|
||||
return std::nullopt;
|
||||
g_dbus_connection_call(connection->gobj(), data->busName.c_str(),
|
||||
data->menuPath.c_str(), kDBusMenuInterface,
|
||||
"GetLayout", params, nullptr,
|
||||
G_DBUS_CALL_FLAGS_NONE, kDBusMenuTimeoutMs, nullptr,
|
||||
&on_menu_layout_finished, data);
|
||||
}
|
||||
|
||||
MenuNode rootNode;
|
||||
parse_menu_node(rootTuple, rootNode);
|
||||
g_variant_unref(rootTuple);
|
||||
|
||||
return rootNode;
|
||||
}
|
||||
|
||||
bool TrayService::activate_menu_item(const std::string &id, int itemId) {
|
||||
bool TrayService::activate_menu_item(const std::string &id, int itemId,
|
||||
int32_t x, int32_t y, uint32_t button,
|
||||
uint32_t timestampMs) {
|
||||
auto it = items.find(id);
|
||||
if (it == items.end() || !connection) {
|
||||
return false;
|
||||
@@ -531,28 +574,44 @@ bool TrayService::activate_menu_item(const std::string &id, int itemId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GVariant *emptyData =
|
||||
g_variant_new_array(G_VARIANT_TYPE("{sv}"), nullptr, 0);
|
||||
GVariant *params = g_variant_new(
|
||||
"(isvu)", itemId, "clicked", g_variant_new_variant(emptyData),
|
||||
static_cast<guint32>(g_get_monotonic_time() / 1000));
|
||||
const guint32 nowMs = static_cast<guint32>(g_get_real_time() / 1000);
|
||||
const guint32 ts = timestampMs ? timestampMs : nowMs;
|
||||
|
||||
GError *error = nullptr;
|
||||
GVariant *result = g_dbus_connection_call_sync(
|
||||
connection->gobj(), item.publicData.busName.c_str(),
|
||||
item.publicData.menuPath.c_str(), kDBusMenuInterface, "Event", params,
|
||||
nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
|
||||
std::cerr << "[TrayService] MenuEvent id=" << id << " item=" << itemId
|
||||
<< " x=" << x << " y=" << y << " button=" << button
|
||||
<< " tsMs=" << ts << std::endl;
|
||||
|
||||
if (result) {
|
||||
g_variant_unref(result);
|
||||
// dbusmenu Event signature: (i s v u)
|
||||
// Some handlers (e.g., media players) look for both "timestamp" and
|
||||
// "time" keys; send both alongside coords/button when available.
|
||||
GVariantBuilder dict;
|
||||
g_variant_builder_init(&dict, G_VARIANT_TYPE("a{sv}"));
|
||||
g_variant_builder_add(&dict, "{sv}", "timestamp",
|
||||
g_variant_new_uint32(ts));
|
||||
g_variant_builder_add(&dict, "{sv}", "time", g_variant_new_uint32(ts));
|
||||
|
||||
if (x != -1 && y != -1) {
|
||||
g_variant_builder_add(&dict, "{sv}", "x", g_variant_new_int32(x));
|
||||
g_variant_builder_add(&dict, "{sv}", "y", g_variant_new_int32(y));
|
||||
}
|
||||
if (button > 0) {
|
||||
g_variant_builder_add(
|
||||
&dict, "{sv}", "button",
|
||||
g_variant_new_int32(static_cast<int32_t>(button)));
|
||||
}
|
||||
|
||||
if (error) {
|
||||
std::cerr << "[TrayService] Event failed for " << id << " (" << itemId
|
||||
<< "): " << error->message << std::endl;
|
||||
g_error_free(error);
|
||||
return false;
|
||||
}
|
||||
GVariant *payloadDict = g_variant_builder_end(&dict);
|
||||
GVariant *payload = g_variant_new_variant(payloadDict);
|
||||
GVariant *params = g_variant_new("(isvu)", itemId, "clicked",
|
||||
payload, ts);
|
||||
|
||||
auto data = new SimpleCallData();
|
||||
data->debugLabel = "MenuEvent(" + id + "," + std::to_string(itemId) + ")";
|
||||
g_dbus_connection_call(connection->gobj(), item.publicData.busName.c_str(),
|
||||
item.publicData.menuPath.c_str(), kDBusMenuInterface,
|
||||
"Event", params, nullptr, G_DBUS_CALL_FLAGS_NONE,
|
||||
kDBusMenuTimeoutMs, nullptr,
|
||||
&on_simple_call_finished, data);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -696,8 +755,7 @@ void TrayService::register_item(const Glib::ustring &sender,
|
||||
const std::string id = parsed.busName + parsed.objectPath;
|
||||
auto existing = items.find(id);
|
||||
if (existing != items.end()) {
|
||||
refresh_item(*existing->second);
|
||||
itemUpdatedSignal.emit(existing->second->publicData);
|
||||
schedule_refresh(id);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -706,7 +764,7 @@ void TrayService::register_item(const Glib::ustring &sender,
|
||||
item->publicData.busName = parsed.busName;
|
||||
item->publicData.objectPath = parsed.objectPath;
|
||||
|
||||
refresh_item(*item);
|
||||
item->addSignalPending = true;
|
||||
|
||||
item->signalSubscriptionId = g_dbus_connection_signal_subscribe(
|
||||
connection->gobj(), item->publicData.busName.c_str(), nullptr, nullptr,
|
||||
@@ -726,7 +784,7 @@ void TrayService::register_item(const Glib::ustring &sender,
|
||||
std::make_tuple(Glib::ustring(id)));
|
||||
emit_watcher_signal("StatusNotifierItemRegistered", params);
|
||||
|
||||
itemAddedSignal.emit(items.at(id)->publicData);
|
||||
schedule_refresh(id);
|
||||
}
|
||||
|
||||
void TrayService::unregister_item(const std::string &id) {
|
||||
@@ -735,6 +793,11 @@ void TrayService::unregister_item(const std::string &id) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (it->second->refreshSourceId != 0) {
|
||||
g_source_remove(it->second->refreshSourceId);
|
||||
it->second->refreshSourceId = 0;
|
||||
}
|
||||
|
||||
if (connection && it->second->signalSubscriptionId != 0) {
|
||||
g_dbus_connection_signal_unsubscribe(connection->gobj(),
|
||||
it->second->signalSubscriptionId);
|
||||
@@ -755,31 +818,58 @@ void TrayService::unregister_item(const std::string &id) {
|
||||
itemRemovedSignal.emit(id);
|
||||
}
|
||||
|
||||
void TrayService::refresh_item(TrackedItem &item) {
|
||||
if (!connection) {
|
||||
struct RefreshCallData {
|
||||
TrayService *self = nullptr;
|
||||
std::string id;
|
||||
std::string busName;
|
||||
std::string objectPath;
|
||||
};
|
||||
|
||||
void TrayService::on_refresh_finished_static(GObject *source, GAsyncResult *res,
|
||||
gpointer user_data) {
|
||||
std::unique_ptr<RefreshCallData> data(
|
||||
static_cast<RefreshCallData *>(user_data));
|
||||
if (!data || !data->self) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto it = data->self->items.find(data->id);
|
||||
if (it == data->self->items.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto &tracked = *it->second;
|
||||
|
||||
GError *error = nullptr;
|
||||
GVariant *reply = g_dbus_connection_call_sync(
|
||||
connection->gobj(), item.publicData.busName.c_str(),
|
||||
item.publicData.objectPath.c_str(), kDBusPropertiesIface, "GetAll",
|
||||
g_variant_new("(s)", kItemInterface), G_VARIANT_TYPE("(a{sv})"),
|
||||
G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
|
||||
GVariant *reply =
|
||||
g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error);
|
||||
if (!reply) {
|
||||
if (error) {
|
||||
std::cerr << "[TrayService] Failed to query properties for "
|
||||
<< item.publicData.id << ": " << error->message
|
||||
<< std::endl;
|
||||
<< data->id << ": " << error->message << std::endl;
|
||||
g_error_free(error);
|
||||
}
|
||||
|
||||
tracked.refreshInFlight = false;
|
||||
if (tracked.addSignalPending) {
|
||||
tracked.addSignalPending = false;
|
||||
data->self->itemAddedSignal.emit(tracked.publicData);
|
||||
}
|
||||
if (tracked.refreshQueued) {
|
||||
tracked.refreshQueued = false;
|
||||
data->self->schedule_refresh(data->id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
GVariant *dictVariant = g_variant_get_child_value(reply, 0);
|
||||
g_variant_unref(reply);
|
||||
|
||||
if (!dictVariant) {
|
||||
tracked.refreshInFlight = false;
|
||||
if (tracked.refreshQueued) {
|
||||
tracked.refreshQueued = false;
|
||||
data->self->schedule_refresh(data->id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -812,13 +902,8 @@ void TrayService::refresh_item(TrackedItem &item) {
|
||||
const gchar *str = g_variant_get_string(value, nullptr);
|
||||
status = str ? str : "";
|
||||
} else if (std::strcmp(key, "Menu") == 0) {
|
||||
if (g_variant_is_of_type(value, G_VARIANT_TYPE_OBJECT_PATH)) {
|
||||
const gchar *str = g_variant_get_string(value, nullptr);
|
||||
menuPath = str ? str : "";
|
||||
} else {
|
||||
const gchar *str = g_variant_get_string(value, nullptr);
|
||||
menuPath = str ? str : "";
|
||||
}
|
||||
} else if (std::strcmp(key, "IconName") == 0) {
|
||||
const gchar *str = g_variant_get_string(value, nullptr);
|
||||
iconName = str ? str : "";
|
||||
@@ -826,39 +911,127 @@ void TrayService::refresh_item(TrackedItem &item) {
|
||||
const gchar *str = g_variant_get_string(value, nullptr);
|
||||
attentionIconName = str ? str : "";
|
||||
} else if (std::strcmp(key, "IconPixmap") == 0) {
|
||||
iconTexture = parse_icon_pixmap(value);
|
||||
iconTexture = TrayService::parse_icon_pixmap(value);
|
||||
} else if (std::strcmp(key, "AttentionIconPixmap") == 0) {
|
||||
attentionTexture = parse_icon_pixmap(value);
|
||||
attentionTexture = TrayService::parse_icon_pixmap(value);
|
||||
}
|
||||
|
||||
g_variant_unref(value);
|
||||
}
|
||||
|
||||
g_variant_unref(dictVariant);
|
||||
const bool menuPathChanged = (item.publicData.menuPath != menuPath);
|
||||
item.publicData.title = title;
|
||||
item.publicData.status = status;
|
||||
item.publicData.menuPath = menuPath;
|
||||
item.publicData.menuAvailable = !menuPath.empty();
|
||||
|
||||
if (menuPathChanged || !item.publicData.menuAvailable) {
|
||||
item.menuModel.reset();
|
||||
item.menuActions.reset();
|
||||
const bool menuPathChanged = (tracked.publicData.menuPath != menuPath);
|
||||
tracked.publicData.title = title;
|
||||
tracked.publicData.status = status;
|
||||
tracked.publicData.menuPath = menuPath;
|
||||
tracked.publicData.menuAvailable = !menuPath.empty();
|
||||
|
||||
if (menuPathChanged || !tracked.publicData.menuAvailable) {
|
||||
tracked.menuModel.reset();
|
||||
tracked.menuActions.reset();
|
||||
}
|
||||
item.publicData.iconName =
|
||||
|
||||
tracked.publicData.iconName =
|
||||
(status == "NeedsAttention" && !attentionIconName.empty())
|
||||
? attentionIconName
|
||||
: iconName;
|
||||
|
||||
if (status == "NeedsAttention" && attentionTexture) {
|
||||
item.publicData.iconPaintable = attentionTexture;
|
||||
tracked.publicData.iconPaintable = attentionTexture;
|
||||
} else {
|
||||
item.publicData.iconPaintable = iconTexture;
|
||||
tracked.publicData.iconPaintable = iconTexture;
|
||||
}
|
||||
|
||||
if (!item.publicData.iconPaintable && iconTexture) {
|
||||
item.publicData.iconPaintable = iconTexture;
|
||||
if (!tracked.publicData.iconPaintable && iconTexture) {
|
||||
tracked.publicData.iconPaintable = iconTexture;
|
||||
}
|
||||
|
||||
tracked.refreshInFlight = false;
|
||||
|
||||
if (tracked.addSignalPending) {
|
||||
tracked.addSignalPending = false;
|
||||
data->self->itemAddedSignal.emit(tracked.publicData);
|
||||
} else {
|
||||
data->self->itemUpdatedSignal.emit(tracked.publicData);
|
||||
}
|
||||
|
||||
if (tracked.refreshQueued) {
|
||||
tracked.refreshQueued = false;
|
||||
data->self->schedule_refresh(data->id);
|
||||
}
|
||||
}
|
||||
|
||||
struct RefreshTimeoutData {
|
||||
TrayService *self = nullptr;
|
||||
std::string id;
|
||||
};
|
||||
|
||||
gboolean TrayService::refresh_timeout_cb(gpointer user_data) {
|
||||
std::unique_ptr<RefreshTimeoutData> data(
|
||||
static_cast<RefreshTimeoutData *>(user_data));
|
||||
if (!data || !data->self) {
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
auto it = data->self->items.find(data->id);
|
||||
if (it == data->self->items.end()) {
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
it->second->refreshSourceId = 0;
|
||||
data->self->begin_refresh(data->id);
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
void TrayService::schedule_refresh(const std::string &id) {
|
||||
auto it = items.find(id);
|
||||
if (it == items.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto &tracked = *it->second;
|
||||
if (tracked.refreshSourceId != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto *data = new RefreshTimeoutData();
|
||||
data->self = this;
|
||||
data->id = id;
|
||||
tracked.refreshSourceId =
|
||||
g_timeout_add(kRefreshDebounceMs, &TrayService::refresh_timeout_cb, data);
|
||||
}
|
||||
|
||||
void TrayService::begin_refresh(const std::string &id) {
|
||||
if (!connection) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto it = items.find(id);
|
||||
if (it == items.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto &tracked = *it->second;
|
||||
if (tracked.refreshInFlight) {
|
||||
tracked.refreshQueued = true;
|
||||
return;
|
||||
}
|
||||
|
||||
tracked.refreshInFlight = true;
|
||||
|
||||
auto data = new RefreshCallData();
|
||||
data->self = this;
|
||||
data->id = id;
|
||||
data->busName = tracked.publicData.busName;
|
||||
data->objectPath = tracked.publicData.objectPath;
|
||||
|
||||
g_dbus_connection_call(connection->gobj(), data->busName.c_str(),
|
||||
data->objectPath.c_str(), kDBusPropertiesIface,
|
||||
"GetAll", g_variant_new("(s)", kItemInterface),
|
||||
G_VARIANT_TYPE("(a{sv})"), G_DBUS_CALL_FLAGS_NONE,
|
||||
kDBusTimeoutMs, nullptr,
|
||||
&TrayService::on_refresh_finished_static, data);
|
||||
}
|
||||
|
||||
void TrayService::emit_registered_items_changed() {
|
||||
@@ -956,12 +1129,10 @@ void TrayService::on_dbus_signal(const gchar *sender_name,
|
||||
std::strcmp(signal_name, "NewAttentionIcon") == 0 ||
|
||||
std::strcmp(signal_name, "NewToolTip") == 0 ||
|
||||
std::strcmp(signal_name, "NewMenu") == 0) {
|
||||
refresh_item(*it->second);
|
||||
itemUpdatedSignal.emit(it->second->publicData);
|
||||
schedule_refresh(it->first);
|
||||
}
|
||||
} else if (isPropertiesSignal) {
|
||||
refresh_item(*it->second);
|
||||
itemUpdatedSignal.emit(it->second->publicData);
|
||||
schedule_refresh(it->first);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
219
src/widgets/battery.cpp
Normal file
219
src/widgets/battery.cpp
Normal file
@@ -0,0 +1,219 @@
|
||||
#include "widgets/battery.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#include <glibmm/main.h>
|
||||
#include <sigc++/functors/mem_fun.h>
|
||||
|
||||
namespace {
|
||||
std::string trim(const std::string &value) {
|
||||
const auto start = value.find_first_not_of(" \t\n\r");
|
||||
if (start == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const auto end = value.find_last_not_of(" \t\n\r");
|
||||
return value.substr(start, end - start + 1);
|
||||
}
|
||||
}
|
||||
|
||||
BatteryWidget::BatteryWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
|
||||
set_valign(Gtk::Align::CENTER);
|
||||
set_halign(Gtk::Align::CENTER);
|
||||
set_spacing(6);
|
||||
add_css_class("battery-widget");
|
||||
|
||||
iconLabel.set_halign(Gtk::Align::CENTER);
|
||||
iconLabel.set_valign(Gtk::Align::CENTER);
|
||||
iconLabel.set_width_chars(7);
|
||||
iconLabel.set_text("[ ]");
|
||||
iconLabel.add_css_class("battery-widget-icon");
|
||||
|
||||
append(iconLabel);
|
||||
|
||||
label.set_halign(Gtk::Align::CENTER);
|
||||
label.set_valign(Gtk::Align::CENTER);
|
||||
label.set_width_chars(5);
|
||||
label.set_text("--%");
|
||||
label.add_css_class("battery-widget-text");
|
||||
|
||||
append(label);
|
||||
|
||||
update();
|
||||
|
||||
timeoutConn = Glib::signal_timeout().connect(
|
||||
sigc::mem_fun(*this, &BatteryWidget::on_timeout), 100);
|
||||
}
|
||||
|
||||
BatteryWidget::~BatteryWidget() {
|
||||
if (timeoutConn.connected()) {
|
||||
timeoutConn.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
void BatteryWidget::find_battery_path() {
|
||||
constexpr auto base = "/sys/class/power_supply";
|
||||
|
||||
batteryPath.clear();
|
||||
|
||||
if (!std::filesystem::exists(base)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto &entry : std::filesystem::directory_iterator(base)) {
|
||||
if (!entry.is_directory()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::ifstream typeFile(entry.path() / "type");
|
||||
if (!typeFile) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string type;
|
||||
std::getline(typeFile, type);
|
||||
type = trim(type);
|
||||
std::transform(type.begin(), type.end(), type.begin(), [](unsigned char ch) {
|
||||
return static_cast<char>(std::toupper(ch));
|
||||
});
|
||||
|
||||
if (type == "BATTERY") {
|
||||
batteryPath = entry.path();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BatteryWidget::update() {
|
||||
try {
|
||||
if (batteryPath.empty() || !std::filesystem::exists(batteryPath)) {
|
||||
find_battery_path();
|
||||
}
|
||||
|
||||
const bool hasBattery = !batteryPath.empty();
|
||||
|
||||
int capacity = -1;
|
||||
std::string status;
|
||||
if (hasBattery) {
|
||||
std::ifstream capacityFile(batteryPath / "capacity");
|
||||
if (capacityFile) {
|
||||
capacityFile >> capacity;
|
||||
}
|
||||
|
||||
std::ifstream statusFile(batteryPath / "status");
|
||||
if (statusFile) {
|
||||
std::getline(statusFile, status);
|
||||
}
|
||||
}
|
||||
|
||||
status = trim(status);
|
||||
|
||||
const bool charging = status == "Charging";
|
||||
const bool full = hasBattery && (status == "Full" || (capacity >= 95));
|
||||
const bool low = (capacity >= 0 && capacity < 20 && !charging);
|
||||
|
||||
std::string stateClass;
|
||||
if (!hasBattery) {
|
||||
stateClass = "battery-widget-external";
|
||||
} else if (charging) {
|
||||
stateClass = "battery-widget-charging";
|
||||
} else if (low) {
|
||||
stateClass = "battery-widget-low";
|
||||
} else if (full) {
|
||||
stateClass = "battery-widget-full";
|
||||
} else {
|
||||
stateClass = "battery-widget-normal";
|
||||
}
|
||||
|
||||
set_state_class(stateClass);
|
||||
|
||||
iconLabel.set_text(build_icon(capacity, hasBattery, charging, full));
|
||||
label.set_text(build_text(capacity, status, hasBattery, charging, full));
|
||||
} catch (...) {
|
||||
set_state_class("battery-widget-normal");
|
||||
iconLabel.set_text("[???]");
|
||||
label.set_text("??%");
|
||||
}
|
||||
}
|
||||
|
||||
bool BatteryWidget::on_timeout() {
|
||||
update();
|
||||
return true;
|
||||
}
|
||||
|
||||
void BatteryWidget::set_state_class(const std::string &stateClass) {
|
||||
if (currentStateClass == stateClass) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!currentStateClass.empty()) {
|
||||
remove_css_class(currentStateClass);
|
||||
}
|
||||
|
||||
currentStateClass = stateClass;
|
||||
|
||||
if (!currentStateClass.empty()) {
|
||||
add_css_class(currentStateClass);
|
||||
}
|
||||
}
|
||||
|
||||
std::string BatteryWidget::build_icon(int capacity, bool hasBattery, bool charging, bool full) const {
|
||||
if (!hasBattery) {
|
||||
return "<AC>";
|
||||
}
|
||||
|
||||
static constexpr int segments = 5;
|
||||
const int clampedCapacity = std::clamp(capacity, 0, 100);
|
||||
int filledSegments = (clampedCapacity * segments + 99) / 100;
|
||||
|
||||
if (full) {
|
||||
filledSegments = segments;
|
||||
}
|
||||
|
||||
std::string icon = "[";
|
||||
for (int i = 0; i < segments; ++i) {
|
||||
icon += (i < filledSegments) ? '=' : ' ';
|
||||
}
|
||||
icon += ']';
|
||||
|
||||
if (charging) {
|
||||
icon += '>';
|
||||
} else if (clampedCapacity <= 5) {
|
||||
icon += '!';
|
||||
} else {
|
||||
icon += ' ';
|
||||
}
|
||||
|
||||
return icon;
|
||||
}
|
||||
|
||||
std::string BatteryWidget::build_text(int capacity, const std::string &status, bool hasBattery, bool charging, bool full) const {
|
||||
if (!hasBattery) {
|
||||
return "AC";
|
||||
}
|
||||
|
||||
if (capacity >= 0) {
|
||||
std::string text;
|
||||
if (full && capacity >= 99) {
|
||||
text = "100%";
|
||||
} else {
|
||||
text = std::to_string(std::clamp(capacity, 0, 100)) + "%";
|
||||
}
|
||||
|
||||
if (charging && capacity < 100) {
|
||||
text.insert(text.begin(), '+');
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
if (!status.empty()) {
|
||||
return status;
|
||||
}
|
||||
|
||||
return "?%";
|
||||
}
|
||||
@@ -3,13 +3,187 @@
|
||||
#include <gdkmm/rectangle.h>
|
||||
#include <gio/gmenu.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <graphene.h>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
#include "components/base/button.hpp"
|
||||
|
||||
namespace {
|
||||
bool is_wayland_display(GtkWidget *widget) {
|
||||
if (!widget) {
|
||||
return true;
|
||||
}
|
||||
|
||||
GtkNative *native = gtk_widget_get_native(widget);
|
||||
if (!native) {
|
||||
return true;
|
||||
}
|
||||
|
||||
GdkSurface *surface = gtk_native_get_surface(native);
|
||||
if (!surface) {
|
||||
return true;
|
||||
}
|
||||
|
||||
GdkDisplay *display = gdk_surface_get_display(surface);
|
||||
if (!display) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const char *typeName = G_OBJECT_TYPE_NAME(display);
|
||||
if (!typeName) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return std::string(typeName).find("Wayland") != std::string::npos;
|
||||
}
|
||||
|
||||
bool try_get_monitor_geometry(GtkWidget *widget, GdkRectangle &outGeom) {
|
||||
if (!widget) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GtkNative *native = gtk_widget_get_native(widget);
|
||||
if (!native) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GdkSurface *surface = gtk_native_get_surface(native);
|
||||
if (!surface) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GdkDisplay *display = gdk_surface_get_display(surface);
|
||||
if (!display) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GdkMonitor *monitor = gdk_display_get_monitor_at_surface(display, surface);
|
||||
if (!monitor) {
|
||||
return false;
|
||||
}
|
||||
|
||||
gdk_monitor_get_geometry(monitor, &outGeom);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool try_get_global_click_coords(GtkWidget *widget, double x, double y,
|
||||
int32_t &outX, int32_t &outY) {
|
||||
if (!widget) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GtkNative *native = gtk_widget_get_native(widget);
|
||||
if (!native) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GtkWidget *nativeWidget = GTK_WIDGET(native);
|
||||
graphene_point_t src{static_cast<float>(x), static_cast<float>(y)};
|
||||
graphene_point_t dst{0.0f, 0.0f};
|
||||
if (!gtk_widget_compute_point(widget, nativeWidget, &src, &dst)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GdkRectangle geom;
|
||||
if (!try_get_monitor_geometry(widget, geom)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outX = static_cast<int32_t>(geom.x + std::lround(dst.x));
|
||||
outY = static_cast<int32_t>(geom.y + std::lround(dst.y));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool try_get_global_pointer_coords(GtkWidget *widget, int32_t &outX,
|
||||
int32_t &outY) {
|
||||
if (!widget) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GtkNative *native = gtk_widget_get_native(widget);
|
||||
if (!native) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GdkSurface *surface = gtk_native_get_surface(native);
|
||||
if (!surface) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GdkDisplay *display = gdk_surface_get_display(surface);
|
||||
if (!display) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GdkSeat *seat = gdk_display_get_default_seat(display);
|
||||
if (!seat) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GdkDevice *pointer = gdk_seat_get_pointer(seat);
|
||||
if (!pointer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
double sx = 0.0;
|
||||
double sy = 0.0;
|
||||
if (!gdk_surface_get_device_position(surface, pointer, &sx, &sy, nullptr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GdkRectangle geom;
|
||||
if (!try_get_monitor_geometry(widget, geom)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outX = static_cast<int32_t>(geom.x + std::lround(sx));
|
||||
outY = static_cast<int32_t>(geom.y + std::lround(sy));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool has_popup_surface(GtkWidget *widget) {
|
||||
if (!widget) {
|
||||
return false;
|
||||
}
|
||||
GtkRoot *root = gtk_widget_get_root(widget);
|
||||
if (!root) {
|
||||
return false;
|
||||
}
|
||||
GtkNative *native = gtk_widget_get_native(widget);
|
||||
if (!native) {
|
||||
return false;
|
||||
}
|
||||
GdkSurface *surface = gtk_native_get_surface(native);
|
||||
if (!surface) {
|
||||
return false;
|
||||
}
|
||||
return gdk_surface_get_mapped(surface);
|
||||
}
|
||||
|
||||
void log_menu_tree(const std::vector<TrayService::MenuNode> &nodes,
|
||||
int depth = 0) {
|
||||
const std::string indent(static_cast<std::size_t>(depth) * 2, ' ');
|
||||
for (const auto &node : nodes) {
|
||||
if (!node.visible) {
|
||||
continue;
|
||||
}
|
||||
std::cerr << "[TrayIconWidget] menu node id=" << node.id
|
||||
<< " label='" << node.label << "' enabled="
|
||||
<< (node.enabled ? "1" : "0") << " sep="
|
||||
<< (node.separator ? "1" : "0") << " depth=" << depth
|
||||
<< std::endl;
|
||||
if (!node.children.empty()) {
|
||||
log_menu_tree(node.children, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TrayIconWidget::TrayIconWidget( std::string id)
|
||||
: Button(id), id(std::move(id)),
|
||||
container(Gtk::Orientation::HORIZONTAL) {
|
||||
aliveFlag = std::make_shared<bool>(true);
|
||||
set_has_frame(false);
|
||||
set_focusable(false);
|
||||
set_valign(Gtk::Align::CENTER);
|
||||
@@ -40,6 +214,12 @@ TrayIconWidget::TrayIconWidget( std::string id)
|
||||
sigc::mem_fun(*this, &TrayIconWidget::on_primary_released));
|
||||
add_controller(primaryGesture);
|
||||
|
||||
middleGesture = Gtk::GestureClick::create();
|
||||
middleGesture->set_button(GDK_BUTTON_MIDDLE);
|
||||
middleGesture->signal_released().connect(
|
||||
sigc::mem_fun(*this, &TrayIconWidget::on_middle_released));
|
||||
add_controller(middleGesture);
|
||||
|
||||
secondaryGesture = Gtk::GestureClick::create();
|
||||
secondaryGesture->set_button(GDK_BUTTON_SECONDARY);
|
||||
secondaryGesture->signal_released().connect(
|
||||
@@ -47,19 +227,36 @@ TrayIconWidget::TrayIconWidget( std::string id)
|
||||
add_controller(secondaryGesture);
|
||||
}
|
||||
|
||||
TrayIconWidget::~TrayIconWidget() {
|
||||
if (aliveFlag) {
|
||||
*aliveFlag = false;
|
||||
}
|
||||
if (menuPopover) {
|
||||
menuPopover->popdown();
|
||||
menuPopover->remove_action_group("dbusmenu");
|
||||
menuPopover->set_menu_model({});
|
||||
if (menuPopover->get_parent()) {
|
||||
menuPopover->unparent();
|
||||
}
|
||||
menuPopover.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void TrayIconWidget::update(const TrayService::Item &item) {
|
||||
hasRemoteMenu = item.menuAvailable;
|
||||
menuPopupPending = false;
|
||||
menuRequestInFlight = false;
|
||||
|
||||
if (!item.menuAvailable) {
|
||||
menuModel.reset();
|
||||
menuActions.reset();
|
||||
menuPopupPending = false;
|
||||
if (menuChangedConnection.connected()) {
|
||||
menuChangedConnection.disconnect();
|
||||
}
|
||||
if (menuPopover) {
|
||||
menuPopover->insert_action_group("dbusmenu",
|
||||
Glib::RefPtr<Gio::ActionGroup>());
|
||||
menuPopover->insert_action_group(
|
||||
"dbusmenu", Glib::RefPtr<Gio::ActionGroup>());
|
||||
menuPopover->set_menu_model({});
|
||||
if (menuPopover->get_parent()) {
|
||||
menuPopover->unparent();
|
||||
}
|
||||
menuPopover.reset();
|
||||
}
|
||||
}
|
||||
@@ -89,52 +286,131 @@ void TrayIconWidget::update(const TrayService::Item &item) {
|
||||
}
|
||||
|
||||
void TrayIconWidget::on_primary_released(int /*n_press*/, double x, double y) {
|
||||
service.activate(id, -1, -1);
|
||||
int32_t sendX = static_cast<int32_t>(std::lround(x));
|
||||
int32_t sendY = static_cast<int32_t>(std::lround(y));
|
||||
|
||||
// Try the most accurate coordinates first; fall back to pointer and finally
|
||||
// to -1/-1 so apps (e.g. Spotify) see a valid activate event on both
|
||||
// Wayland and X11.
|
||||
if (!try_get_global_click_coords(GTK_WIDGET(gobj()), x, y, sendX, sendY)) {
|
||||
if (!try_get_global_pointer_coords(GTK_WIDGET(gobj()), sendX, sendY)) {
|
||||
sendX = -1;
|
||||
sendY = -1;
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "[TrayIconWidget] Activate primary id=" << id << " x="
|
||||
<< sendX << " y=" << sendY << std::endl;
|
||||
service.activate(id, sendX, sendY);
|
||||
}
|
||||
|
||||
void TrayIconWidget::on_middle_released(int /*n_press*/, double x, double y) {
|
||||
// Map middle click to the StatusNotifier SecondaryActivate event; some
|
||||
// apps (e.g. media players) use this for alternate actions like toggling
|
||||
// visibility.
|
||||
int32_t sendX = static_cast<int32_t>(std::lround(x));
|
||||
int32_t sendY = static_cast<int32_t>(std::lround(y));
|
||||
if (!try_get_global_click_coords(GTK_WIDGET(gobj()), x, y, sendX, sendY)) {
|
||||
if (!try_get_global_pointer_coords(GTK_WIDGET(gobj()), sendX, sendY)) {
|
||||
sendX = -1;
|
||||
sendY = -1;
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "[TrayIconWidget] SecondaryActivate (middle) id=" << id
|
||||
<< " x=" << sendX << " y=" << sendY << std::endl;
|
||||
service.secondaryActivate(id, sendX, sendY);
|
||||
}
|
||||
|
||||
void TrayIconWidget::on_secondary_released(int /*n_press*/, double x,
|
||||
double y) {
|
||||
// If we are not attached to a toplevel (e.g., window hidden), fall back to
|
||||
// the item's own ContextMenu instead of trying to show a popover, which
|
||||
// would crash without a mapped surface.
|
||||
GtkWidget *selfWidget = GTK_WIDGET(gobj());
|
||||
if (!gtk_widget_get_mapped(selfWidget) || !has_popup_surface(selfWidget)) {
|
||||
std::cerr << "[TrayIconWidget] Secondary fallback ContextMenu (no surface) id="
|
||||
<< id << std::endl;
|
||||
service.contextMenu(id, -1, -1);
|
||||
|
||||
if (!ensure_menu()) {
|
||||
return;
|
||||
}
|
||||
|
||||
pendingX = x;
|
||||
pendingY = y;
|
||||
|
||||
// Use dbusmenu popover when available and we have a mapped surface; else
|
||||
// fall back to the item's ContextMenu.
|
||||
if (hasRemoteMenu && has_popup_surface(selfWidget)) {
|
||||
std::cerr << "[TrayIconWidget] Requesting dbusmenu for id=" << id
|
||||
<< std::endl;
|
||||
menuPopupPending = true;
|
||||
try_popup();
|
||||
if (menuRequestInFlight) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool TrayIconWidget::ensure_menu() {
|
||||
auto layoutOpt = service.get_menu_layout(id);
|
||||
if (!layoutOpt) {
|
||||
menuRequestInFlight = true;
|
||||
auto weak = std::weak_ptr<bool>(aliveFlag);
|
||||
service.request_menu_layout(
|
||||
id, [weak, this](std::optional<TrayService::MenuNode> layout) {
|
||||
if (auto locked = weak.lock()) {
|
||||
if (*locked) {
|
||||
on_menu_layout_ready(std::move(layout));
|
||||
}
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t sendX = static_cast<int32_t>(std::lround(x));
|
||||
int32_t sendY = static_cast<int32_t>(std::lround(y));
|
||||
if (!try_get_global_click_coords(GTK_WIDGET(gobj()), x, y, sendX, sendY)) {
|
||||
(void)try_get_global_pointer_coords(GTK_WIDGET(gobj()), sendX, sendY);
|
||||
}
|
||||
if (is_wayland_display(GTK_WIDGET(gobj()))) {
|
||||
std::cerr << "[TrayIconWidget] ContextMenu wayland id=" << id
|
||||
<< " x=-1 y=-1" << std::endl;
|
||||
service.contextMenu(id, -1, -1);
|
||||
} else {
|
||||
std::cerr << "[TrayIconWidget] ContextMenu id=" << id << " x=" << sendX
|
||||
<< " y=" << sendY << std::endl;
|
||||
service.contextMenu(id, sendX, sendY);
|
||||
}
|
||||
}
|
||||
|
||||
void TrayIconWidget::on_menu_layout_ready(
|
||||
std::optional<TrayService::MenuNode> layoutOpt) {
|
||||
menuRequestInFlight = false;
|
||||
|
||||
if (!menuPopupPending) {
|
||||
return;
|
||||
}
|
||||
|
||||
GtkWidget *selfWidget = GTK_WIDGET(gobj());
|
||||
|
||||
if (!has_popup_surface(selfWidget)) {
|
||||
menuPopupPending = false;
|
||||
menuModel.reset();
|
||||
menuActions.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!layoutOpt) {
|
||||
menuPopupPending = false;
|
||||
if (menuChangedConnection.connected()) {
|
||||
menuChangedConnection.disconnect();
|
||||
}
|
||||
if (menuPopover) {
|
||||
remove_action_group("dbusmenu");
|
||||
menuPopover->set_menu_model({});
|
||||
menuPopover->unparent();
|
||||
menuPopover.reset();
|
||||
}
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &layout = *layoutOpt;
|
||||
|
||||
log_menu_tree(layout.children, 0);
|
||||
auto menu = Gio::Menu::create();
|
||||
auto actions = Gio::SimpleActionGroup::create();
|
||||
|
||||
populate_menu_items(layout.children, menu, actions);
|
||||
|
||||
const auto itemCount = menu->get_n_items();
|
||||
|
||||
if (itemCount == 0) {
|
||||
return false;
|
||||
if (menu->get_n_items() == 0) {
|
||||
menuModel.reset();
|
||||
menuActions.reset();
|
||||
menuPopupPending = false;
|
||||
return;
|
||||
}
|
||||
|
||||
menuModel = menu;
|
||||
@@ -145,7 +421,8 @@ bool TrayIconWidget::ensure_menu() {
|
||||
menuPopover =
|
||||
Glib::make_refptr_for_instance<Gtk::PopoverMenu>(rawPopover);
|
||||
if (!menuPopover) {
|
||||
return false;
|
||||
menuPopupPending = false;
|
||||
return;
|
||||
}
|
||||
|
||||
menuPopover->set_has_arrow(false);
|
||||
@@ -155,33 +432,17 @@ bool TrayIconWidget::ensure_menu() {
|
||||
|
||||
menuPopover->remove_action_group("dbusmenu");
|
||||
menuPopover->insert_action_group("dbusmenu", menuActions);
|
||||
|
||||
if (menuChangedConnection.connected()) {
|
||||
menuChangedConnection.disconnect();
|
||||
}
|
||||
menuChangedConnection = menuModel->signal_items_changed().connect(
|
||||
sigc::mem_fun(*this, &TrayIconWidget::on_menu_items_changed));
|
||||
|
||||
menuPopover->set_menu_model(menuModel);
|
||||
|
||||
return true;
|
||||
// Ensure popover is still parented to us and has a native/root before popup.
|
||||
if (!menuPopover->get_parent()) {
|
||||
menuPopover->set_parent(*this);
|
||||
}
|
||||
|
||||
void TrayIconWidget::on_menu_items_changed(guint /*position*/,
|
||||
guint /*removed*/, guint /*added*/) {
|
||||
if (!menuModel) {
|
||||
return;
|
||||
}
|
||||
|
||||
try_popup();
|
||||
}
|
||||
|
||||
void TrayIconWidget::try_popup() {
|
||||
if (!menuPopupPending || !menuPopover || !menuModel) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (menuModel->get_n_items() == 0) {
|
||||
GtkWidget *popoverWidget = GTK_WIDGET(menuPopover->gobj());
|
||||
if (!popoverWidget || !gtk_widget_get_root(popoverWidget) ||
|
||||
!gtk_widget_get_native(popoverWidget) || !has_popup_surface(selfWidget)) {
|
||||
menuPopupPending = false;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -241,12 +502,50 @@ void TrayIconWidget::populate_menu_items(
|
||||
|
||||
void TrayIconWidget::on_menu_action(const Glib::VariantBase & /*parameter*/,
|
||||
int itemId) {
|
||||
service.activate_menu_item(id, itemId);
|
||||
// Pop down immediately so the popover doesn't outlive us if the item
|
||||
// removes itself synchronously (e.g., "Exit"), which would otherwise lead
|
||||
// to use-after-free.
|
||||
if (menuPopover) {
|
||||
menuPopover->popdown();
|
||||
// Also detach to avoid double-unparent if the item disappears during
|
||||
// the ensuing D-Bus call.
|
||||
if (menuPopover->get_parent()) {
|
||||
menuPopover->unparent();
|
||||
}
|
||||
}
|
||||
|
||||
int32_t sendX = -1;
|
||||
int32_t sendY = -1;
|
||||
(void)try_get_pending_coords(sendX, sendY);
|
||||
|
||||
std::cerr << "[TrayIconWidget] Menu action id=" << this->id
|
||||
<< " item=" << itemId << " x=" << sendX << " y=" << sendY
|
||||
<< std::endl;
|
||||
|
||||
const uint32_t nowMs = static_cast<uint32_t>(g_get_monotonic_time() / 1000);
|
||||
// Use button 1 for menu activation events; some dbusmenu handlers ignore
|
||||
// secondary-button payloads for activate.
|
||||
service.activate_menu_item(id, itemId, sendX, sendY, 1 /*button*/, nowMs);
|
||||
}
|
||||
|
||||
bool TrayIconWidget::try_get_pending_coords(int32_t &outX, int32_t &outY) const {
|
||||
outX = -1;
|
||||
outY = -1;
|
||||
int32_t sendX = static_cast<int32_t>(std::lround(pendingX));
|
||||
int32_t sendY = static_cast<int32_t>(std::lround(pendingY));
|
||||
if (!try_get_global_click_coords(GTK_WIDGET(gobj()), pendingX, pendingY,
|
||||
sendX, sendY)) {
|
||||
if (!try_get_global_pointer_coords(GTK_WIDGET(gobj()), sendX, sendY)) {
|
||||
sendX = -1;
|
||||
sendY = -1;
|
||||
}
|
||||
}
|
||||
|
||||
outX = sendX;
|
||||
outY = sendY;
|
||||
return (sendX != -1 || sendY != -1);
|
||||
}
|
||||
|
||||
TrayWidget::TrayWidget()
|
||||
: Gtk::Box(Gtk::Orientation::HORIZONTAL) {
|
||||
set_valign(Gtk::Align::CENTER);
|
||||
@@ -307,7 +606,6 @@ void TrayWidget::on_item_removed(const std::string &id) {
|
||||
}
|
||||
|
||||
remove(*it->second);
|
||||
it->second->unparent();
|
||||
icons.erase(it);
|
||||
|
||||
if (icons.empty()) {
|
||||
|
||||
Reference in New Issue
Block a user