Skip to content

Commit

Permalink
Merge pull request #401 from SrainApp/feature/libappindicator2
Browse files Browse the repository at this point in the history
Some trayicon/appindicator fixes
  • Loading branch information
SilverRainZ authored Feb 11, 2024
2 parents 6cfdccf + 932f40e commit 7c81bed
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 23 deletions.
10 changes: 6 additions & 4 deletions data/ui/app.glade
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkMenuItem" id="show_window_menu_item">
<property name="visible">True</property>
<object class="GtkMenuItem" id="activate_menu_item">
<property name="name">activate_menu_item</property>
<property name="visible">False</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">_Show window</property>
<property name="action-name">app.activate</property>
<property name="label" translatable="yes">_Activate</property>
<property name="use-underline">True</property>
</object>
</child>
Expand All @@ -27,7 +29,7 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="action-name">app.about</property>
<property name="label" translatable="yes">_About</property>
<property name="label" translatable="yes">About</property>
<property name="use-underline">True</property>
</object>
</child>
Expand Down
80 changes: 61 additions & 19 deletions src/sui/sui_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -87,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
Expand Down Expand Up @@ -153,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 },
};
Expand Down Expand Up @@ -356,9 +366,13 @@ 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
// 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
gtk_status_icon_set_from_icon_name(self->tray_icon,
highlight ? "srain-red": PACKAGE);
highlight ? APP_ATTENTION_ICON : APP_ICON);
#endif
}

Expand Down Expand Up @@ -457,7 +471,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);
Expand All @@ -475,14 +489,34 @@ 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);

// 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);
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
Expand Down Expand Up @@ -562,15 +596,15 @@ 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;

self = user_data;
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;
Expand All @@ -587,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;

Expand All @@ -596,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;
Expand All @@ -606,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;
Expand All @@ -628,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

0 comments on commit 7c81bed

Please sign in to comment.