better working tray
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
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 *reply =
|
||||
g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error);
|
||||
|
||||
if (reply) {
|
||||
g_variant_unref(reply);
|
||||
}
|
||||
|
||||
GVariant *properties = create_property_list_variant();
|
||||
if (!properties) {
|
||||
return nullptr;
|
||||
if (!error) {
|
||||
return;
|
||||
}
|
||||
|
||||
GVariant *params = g_variant_new("(ii@as)", 0, -1, properties);
|
||||
g_variant_ref_sink(properties);
|
||||
|
||||
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);
|
||||
|
||||
g_variant_unref(properties);
|
||||
|
||||
if (error) {
|
||||
std::cerr << "[TrayService] GetLayout failed for " << busName
|
||||
<< menuPath << ": " << error->message << std::endl;
|
||||
g_error_free(error);
|
||||
return nullptr;
|
||||
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;
|
||||
}
|
||||
|
||||
return result;
|
||||
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,39 +460,105 @@ 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;
|
||||
}
|
||||
|
||||
MenuNode rootNode;
|
||||
parse_menu_node(rootTuple, rootNode);
|
||||
g_variant_unref(rootTuple);
|
||||
|
||||
return rootNode;
|
||||
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);
|
||||
}
|
||||
|
||||
bool TrayService::activate_menu_item(const std::string &id, int itemId) {
|
||||
@@ -531,28 +572,29 @@ 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),
|
||||
// Some tray items update state lazily and require AboutToShow(itemId)
|
||||
// before handling a click.
|
||||
call_about_to_show(connection, item.publicData.busName,
|
||||
item.publicData.menuPath, itemId);
|
||||
|
||||
// dbusmenu Event signature: (i s v u)
|
||||
// For "clicked", the payload is typically an a{sv} dictionary.
|
||||
// IMPORTANT: the 'v' argument must be a variant container, so we wrap.
|
||||
GVariantBuilder dict;
|
||||
g_variant_builder_init(&dict, G_VARIANT_TYPE("a{sv}"));
|
||||
GVariant *payloadDict = g_variant_builder_end(&dict);
|
||||
GVariant *payload = g_variant_new_variant(payloadDict);
|
||||
GVariant *params = g_variant_new(
|
||||
"(isvu)", itemId, "clicked", payload,
|
||||
static_cast<guint32>(g_get_monotonic_time() / 1000));
|
||||
|
||||
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);
|
||||
|
||||
if (result) {
|
||||
g_variant_unref(result);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
std::cerr << "[TrayService] Event failed for " << id << " (" << itemId
|
||||
<< "): " << error->message << std::endl;
|
||||
g_error_free(error);
|
||||
return false;
|
||||
}
|
||||
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 +738,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 +747,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 +767,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 +776,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 +801,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 +885,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 : "";
|
||||
}
|
||||
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 +894,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 +1112,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user