changeset 56:08747f3a718b

Add new setting to hide indicator menu items The setting hide-menu-items is an array of strings containing the actions associated with menu items that should be hidden from the indicator menu.
author Guido Berhoerster <guido+pui@berhoerster.name>
date Tue, 16 Aug 2022 16:19:02 +0200
parents 24cba399757f
children 76989c84bd20
files org.guido-berhoerster.code.package-update-indicator.gschema.xml pui-application.c
diffstat 2 files changed, 50 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/org.guido-berhoerster.code.package-update-indicator.gschema.xml	Wed Aug 17 16:55:17 2022 +0200
+++ b/org.guido-berhoerster.code.package-update-indicator.gschema.xml	Tue Aug 16 16:19:02 2022 +0200
@@ -25,5 +25,10 @@
       <description>Whether the indicator is active even when packages are up to
       date.</description>
     </key>
+    <key name="hide-menu-items" type="as">
+      <default>[]</default>
+      <summary>Hide menu items</summary>
+      <description>List of indicator menu items to be hidden.</description>
+    </key>
   </schema>
 </schemalist>
--- a/pui-application.c	Wed Aug 17 16:55:17 2022 +0200
+++ b/pui-application.c	Tue Aug 16 16:19:02 2022 +0200
@@ -191,6 +191,39 @@
 }
 
 static void
+update_menu_item(GtkWidget* widget, gpointer user_data)
+{
+	gchar **	hidden_menu_items = user_data;
+	const gchar *	action_name;
+	const gchar *	name;
+
+	g_return_if_fail(GTK_IS_MENU_ITEM(widget));
+	action_name = gtk_actionable_get_action_name(GTK_ACTIONABLE(widget));
+	if (!g_str_has_prefix(action_name, "app.")) {
+		return;
+	}
+	name = action_name + strlen("app.");
+	gtk_widget_set_visible(widget,
+	    !g_strv_contains((const gchar * const *)hidden_menu_items, name));
+}
+
+static void
+update_menu_items(PuiApplication *self)
+{
+	gchar **	hidden_menu_items;
+	GtkMenu *	menu;
+
+	menu = app_indicator_get_menu(self->indicator);
+	hidden_menu_items = g_settings_get_strv(self->settings,
+	    "hide-menu-items");
+	g_return_if_fail(hidden_menu_items != NULL);
+	g_return_if_fail((menu != NULL) && GTK_IS_CONTAINER(menu));
+	gtk_container_foreach(GTK_CONTAINER(menu), update_menu_item,
+	    hidden_menu_items);
+	g_strfreev(hidden_menu_items);
+}
+
+static void
 update_indicator(PuiApplication *self, const gchar *title)
 {
 	switch (self->state) {
@@ -357,6 +390,7 @@
 	}
 
 	update_indicator(self, title);
+	update_menu_items(self);
 	update_notification(self, title, body);
 
 	g_debug("indicator icon: %s, notification title: \"%s\", "
@@ -428,6 +462,13 @@
 }
 
 static void
+on_hide_menu_items_changed(GSettings *settings, gchar *key, gpointer user_data)
+{
+	g_debug("setting hide-menu-items changed");
+	update_menu_items(user_data);
+}
+
+static void
 on_backend_restart_required(PuiBackend *backend, gpointer user_data)
 {
 	PuiApplication	*self = user_data;
@@ -531,6 +572,10 @@
 	g_settings_bind(self->settings, "always-active", self,
 	    "always-active", G_SETTINGS_BIND_GET);
 
+	/* watch hide-menu-items setting in order to update the menu */
+	g_signal_connect(self->settings, "changed::hide-menu-items",
+	    G_CALLBACK(on_hide_menu_items_changed), self);
+
 	/* ensure the ui reflects the current state */
 	update_ui(self);