From 2e35426df16363b93fec05f9898fe3a98ba1fe3c Mon Sep 17 00:00:00 2001 From: Shengyu Zhang Date: Wed, 13 Dec 2023 13:02:30 +0800 Subject: [PATCH 1/2] Sui(fix): some trayicon/appindicator fixes --- src/sui/sui_app.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/sui/sui_app.c b/src/sui/sui_app.c index 522f6c81..a8eb3e2b 100644 --- a/src/sui/sui_app.c +++ b/src/sui/sui_app.c @@ -44,6 +44,10 @@ #include "sui_window.h" #include "sui_prefs_dialog.h" +// See also $PROJECT_ROOT/data/icons/meson.build. +#define APP_ICON PACKAGE_APPID +#define APP_ATTENTION_ICON PACKAGE_APPID ".Red" + struct _SuiApplication { GtkApplication parent; @@ -356,9 +360,12 @@ void sui_application_send_notification(SuiApplication *self, } void sui_application_highlight_tray_icon(SuiApplication *self, bool highlight){ -#ifndef ENABLE_APP_INDICATOR +#ifdef ENABLE_APP_INDICATOR + app_indicator_set_status(self->tray_icon, highlight ? + APP_INDICATOR_STATUS_ATTENTION : APP_INDICATOR_STATUS_ACTIVE); +#else gtk_status_icon_set_from_icon_name(self->tray_icon, - highlight ? "srain-red": PACKAGE); + highlight ? APP_ATTENTION_ICON : APP_ICON); #endif } @@ -457,7 +464,7 @@ static void show_about_dialog(SuiApplication *self){ "comments", PACKAGE_DESC, "authors", authors, "documenters", documentors, - "logo-icon-name", PACKAGE, + "logo-icon-name", APP_ICON, "title", _("About Srain"), "translator-credits", translators, NULL); @@ -475,14 +482,17 @@ static void on_startup(SuiApplication *self){ g_object_unref(builder); #ifdef ENABLE_APP_INDICATOR - self->tray_icon = app_indicator_new(PACKAGE_APPID, PACKAGE_APPID, - APP_INDICATOR_CATEGORY_APPLICATION_STATUS); + self->tray_icon = app_indicator_new(PACKAGE_APPID, APP_ICON, + APP_INDICATOR_CATEGORY_COMMUNICATIONS); app_indicator_set_status(self->tray_icon, APP_INDICATOR_STATUS_ACTIVE); - app_indicator_set_attention_icon(self->tray_icon, "srain-red"); + app_indicator_set_attention_icon_full(self->tray_icon, APP_ATTENTION_ICON, _("Srain Icon for Attention")); app_indicator_set_menu(self->tray_icon, self->menu); + app_indicator_set_title(self->tray_icon, PACKAGE); #else self->tray_icon = gtk_status_icon_new_from_icon_name(PACKAGE_APPID); g_signal_connect(self->tray_icon, "activate", G_CALLBACK(tray_icon_on_click), self); + g_signal_connect(self->tray_icon, "popup-menu", G_CALLBACK(tray_icon_on_popup_menu), self); + gtk_status_icon_set_tooltip_text(self->tray_icon, PACKAGE); #endif // Attach to any widget to connect to action From 932f40e5fa294cd6d204ab79111b3542b98c402e Mon Sep 17 00:00:00 2001 From: Shengyu Zhang Date: Sun, 11 Feb 2024 13:45:36 +0800 Subject: [PATCH 2/2] Sui(impl): make activate menu item works --- data/ui/app.glade | 10 ++++---- src/sui/sui_app.c | 58 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/data/ui/app.glade b/data/ui/app.glade index d30d1597..8ceadcbf 100644 --- a/data/ui/app.glade +++ b/data/ui/app.glade @@ -6,10 +6,12 @@ True False - - True + + activate_menu_item + False False - _Show window + app.activate + _Activate True @@ -27,7 +29,7 @@ True False app.about - _About + About True diff --git a/src/sui/sui_app.c b/src/sui/sui_app.c index a8eb3e2b..d62a099b 100644 --- a/src/sui/sui_app.c +++ b/src/sui/sui_app.c @@ -91,13 +91,15 @@ static int on_handle_local_options(SuiApplication *self, GVariantDict *options, gpointer user_data); static int on_command_line(SuiApplication *self, GApplicationCommandLine *cmdline, gpointer user_data); -static void on_activate_about(GSimpleAction *action, GVariant *parameter, +static void on_action_about(GSimpleAction *action, GVariant *parameter, gpointer user_data); -static void on_activate_prefs(GSimpleAction *action, GVariant *parameter, +static void on_action_prefs(GSimpleAction *action, GVariant *parameter, gpointer user_data); -static void on_activate_exit(GSimpleAction *action, GVariant *parameter, +static void on_action_exit(GSimpleAction *action, GVariant *parameter, gpointer user_data); -static void on_toggle_server_visibility(GSimpleAction *action, GVariant *parameter, +static void on_action_toggle_server_visibility(GSimpleAction *action, + GVariant *parameter, gpointer user_data); +static void on_action_activate(GSimpleAction *action, GVariant *parameter, gpointer user_data); #ifndef ENABLE_APP_INDICATOR @@ -157,19 +159,23 @@ static const GOptionEntry option_entries[] = { static const GActionEntry action_entries[] = { { .name = "about", - .activate = on_activate_about, + .activate = on_action_about, }, { .name = "preferences", - .activate = on_activate_prefs, + .activate = on_action_prefs, }, { .name = "exit", - .activate = on_activate_exit, + .activate = on_action_exit, }, { .name = "toggle-server-visibility", - .activate = on_toggle_server_visibility, + .activate = on_action_toggle_server_visibility, + }, + { + .name = "activate", + .activate = on_action_activate, }, { NULL }, }; @@ -361,6 +367,7 @@ void sui_application_send_notification(SuiApplication *self, void sui_application_highlight_tray_icon(SuiApplication *self, bool highlight){ #ifdef ENABLE_APP_INDICATOR + // TODO: works on KDE, but doesn't work on waybar. app_indicator_set_status(self->tray_icon, highlight ? APP_INDICATOR_STATUS_ATTENTION : APP_INDICATOR_STATUS_ACTIVE); #else @@ -488,6 +495,23 @@ static void on_startup(SuiApplication *self){ app_indicator_set_attention_icon_full(self->tray_icon, APP_ATTENTION_ICON, _("Srain Icon for Attention")); app_indicator_set_menu(self->tray_icon, self->menu); app_indicator_set_title(self->tray_icon, PACKAGE); + + // Show "Activate" menu item because we can not activate application + // by left-clicking the app indicator. + GList *menu_items; + menu_items = gtk_container_get_children(GTK_CONTAINER(self->menu)); + for (GList *iter = menu_items; iter != NULL; iter = g_list_next(iter)) { + GtkWidget *item; + const char *name; + + item = GTK_WIDGET(iter->data); + name = gtk_widget_get_name(item); + if (g_strcmp0(name, "activate_menu_item") == 0) { + gtk_widget_set_visible(item, TRUE); + break; + } + } + g_list_free(menu_items); #else self->tray_icon = gtk_status_icon_new_from_icon_name(PACKAGE_APPID); g_signal_connect(self->tray_icon, "activate", G_CALLBACK(tray_icon_on_click), self); @@ -572,7 +596,7 @@ static int on_command_line(SuiApplication *self, return 0; } -static void on_activate_about(GSimpleAction *action, GVariant *parameter, +static void on_action_about(GSimpleAction *action, GVariant *parameter, gpointer user_data){ SuiApplication *self; @@ -580,7 +604,7 @@ static void on_activate_about(GSimpleAction *action, GVariant *parameter, show_about_dialog(self); } -static void on_activate_prefs(GSimpleAction *action, GVariant *parameter, +static void on_action_prefs(GSimpleAction *action, GVariant *parameter, gpointer user_data){ SuiApplication *self; SuiPrefsDialog *dialog; @@ -597,7 +621,7 @@ static void on_activate_prefs(GSimpleAction *action, GVariant *parameter, gtk_widget_destroy(GTK_WIDGET(dialog)); } -static void on_activate_exit(GSimpleAction *action, GVariant *parameter, +static void on_action_exit(GSimpleAction *action, GVariant *parameter, gpointer user_data){ SuiApplication *self; @@ -606,7 +630,7 @@ static void on_activate_exit(GSimpleAction *action, GVariant *parameter, } -static void on_toggle_server_visibility(GSimpleAction *action, GVariant *parameter, +static void on_action_toggle_server_visibility(GSimpleAction *action, GVariant *parameter, gpointer user_data){ SuiApplication *self; SuiWindow *win; @@ -616,6 +640,14 @@ static void on_toggle_server_visibility(GSimpleAction *action, GVariant *parame sui_window_toggle_server_visibility(win); } +static void on_action_activate(GSimpleAction *action, GVariant *parameter, + gpointer user_data){ + SuiApplication *self; + + self = user_data; + g_application_activate(G_APPLICATION(self)); +} + #ifndef ENABLE_APP_INDICATOR static void tray_icon_on_click(GtkStatusIcon *status_icon, gpointer user_data){ GList *wins; @@ -638,6 +670,6 @@ static void tray_icon_on_popup_menu(GtkStatusIcon *status_icon, guint button, self = user_data; - gtk_menu_popup(self->menu, NULL, NULL, NULL, NULL, button, activate_time); + gtk_menu_popup_at_pointer(self->menu, NULL); } #endif