From c0527ef6bd4ec00879a1016453846144f2be51ce Mon Sep 17 00:00:00 2001 From: ItsMicin Date: Fri, 13 Aug 2021 13:10:35 +0700 Subject: [PATCH 1/3] Icon In COntext menu https://github.com/elementary/applications-menu/issues/73 This brach adopt from plank --- src/Widgets/AppContextMenu.vala | 54 +++++++++++++++++++++++---------- src/Widgets/MenuIcon.vala | 47 ++++++++++++++++++++++++++++ src/meson.build | 1 + 3 files changed, 86 insertions(+), 16 deletions(-) create mode 100644 src/Widgets/MenuIcon.vala diff --git a/src/Widgets/AppContextMenu.vala b/src/Widgets/AppContextMenu.vala index f96bc0676..ab581c3f7 100644 --- a/src/Widgets/AppContextMenu.vala +++ b/src/Widgets/AppContextMenu.vala @@ -18,6 +18,9 @@ public class Slingshot.AppContextMenu : Gtk.Menu { public signal void app_launched (); + private const string DESKTOP_ACTION_KEY = "Actions"; + private const string DESKTOP_ACTION_GROUP_NAME = "Desktop Action %s"; + public string desktop_id { get; construct; } public string desktop_path { get; construct; } private DesktopAppInfo app_info; @@ -54,15 +57,30 @@ public class Slingshot.AppContextMenu : Gtk.Menu { construct { app_info = new DesktopAppInfo (desktop_id); - foreach (unowned string _action in app_info.list_actions ()) { - string action = _action.dup (); - var menuitem = new Gtk.MenuItem.with_mnemonic (app_info.get_action_name (action)); - add (menuitem); - - menuitem.activate.connect (() => { - app_info.launch_action (action, new AppLaunchContext ()); - app_launched (); - }); + KeyFile file; + try { + file = new KeyFile (); + file.load_from_file (app_info.get_filename (), 0); + if (file.has_key (KeyFileDesktop.GROUP, DESKTOP_ACTION_KEY)) { + foreach (unowned string g_action in file.get_string_list (KeyFileDesktop.GROUP, DESKTOP_ACTION_KEY)) { + var action = g_action.dup (); + var group = DESKTOP_ACTION_GROUP_NAME.printf (action); + + var menuitem = new MenuIcon () { + menu_label = file.get_locale_string (group, KeyFileDesktop.KEY_NAME) + }; + if (file.has_key (group, KeyFileDesktop.KEY_ICON)) { + menuitem.menu_image = file.get_locale_string (group, KeyFileDesktop.KEY_ICON); + } + add (menuitem); + menuitem.activate.connect (() => { + app_info.launch_action (action, new AppLaunchContext ()); + app_launched (); + }); + } + } + } catch (Error e) { + critical ("%s", e.message); } #if HAS_PLANK @@ -73,14 +91,14 @@ public class Slingshot.AppContextMenu : Gtk.Menu { has_system_item = true; - var plank_menuitem = new Gtk.MenuItem (); - plank_menuitem.use_underline = true; - + var plank_menuitem = new MenuIcon () { + menu_image = "plank" + }; docked = (desktop_uri in plank_client.get_persistent_applications ()); if (docked) { - plank_menuitem.label = _("Remove from _Dock"); + plank_menuitem.menu_label = _("Remove from _Dock"); } else { - plank_menuitem.label = _("Add to _Dock"); + plank_menuitem.menu_label = _("Add to _Dock"); } plank_menuitem.activate.connect (plank_menuitem_activate); @@ -94,12 +112,16 @@ public class Slingshot.AppContextMenu : Gtk.Menu { add (new Gtk.SeparatorMenuItem ()); } - uninstall_menuitem = new Gtk.MenuItem.with_label (_("Uninstall")) { + uninstall_menuitem = new MenuIcon () { + menu_label = _("Uninstall"), + menu_image = "edit-delete", sensitive = false }; uninstall_menuitem.activate.connect (uninstall_menuitem_activate); - appcenter_menuitem = new Gtk.MenuItem.with_label (_("View in AppCenter")) { + appcenter_menuitem = new MenuIcon () { + menu_label = _("View in AppCenter"), + menu_image = "io.elementary.appcenter", sensitive = false }; appcenter_menuitem.activate.connect (open_in_appcenter); diff --git a/src/Widgets/MenuIcon.vala b/src/Widgets/MenuIcon.vala new file mode 100644 index 000000000..813e1e5fe --- /dev/null +++ b/src/Widgets/MenuIcon.vala @@ -0,0 +1,47 @@ +/* + * Copyright 2021 elementary, Inc. (https://elementary.io) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * + * Authored by: Torikulhabib + * + */ + +private class MenuIcon : Gtk.MenuItem { + public string menu_image { get; set; } + public string menu_label { get; set; } + + construct { + var label = new Gtk.Label (null) { + xalign = 0 + }; + + var image_menu = new Gtk.Image (); + + var grid = new Gtk.Grid (); + grid.add (image_menu); + grid.add (label); + add (grid); + + notify["menu-image"].connect (()=>{ + image_menu.set_from_gicon (new ThemedIcon (menu_image), Gtk.IconSize.BUTTON); + }); + + notify["menu-label"].connect (()=>{ + label.label = menu_label; + }); + } +} diff --git a/src/meson.build b/src/meson.build index 09cfd10f1..4163d7fed 100644 --- a/src/meson.build +++ b/src/meson.build @@ -27,6 +27,7 @@ sources = [ 'Widgets/Switcher.vala', 'Widgets/SearchItem.vala', 'Widgets/PageChecker.vala', + 'Widgets/MenuIcon.vala', 'synapse-core/Actions/TerminalRunnerAction.vala', 'synapse-core/Actions/RunnerAction.vala', From 3311f4b23f2e4edb1eb61c88626d4efe603f69dd Mon Sep 17 00:00:00 2001 From: ItsMicin Date: Fri, 13 Aug 2021 13:35:16 +0700 Subject: [PATCH 2/3] Update AppContextMenu.vala --- src/Widgets/AppContextMenu.vala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Widgets/AppContextMenu.vala b/src/Widgets/AppContextMenu.vala index ab581c3f7..f7e7095c8 100644 --- a/src/Widgets/AppContextMenu.vala +++ b/src/Widgets/AppContextMenu.vala @@ -96,9 +96,9 @@ public class Slingshot.AppContextMenu : Gtk.Menu { }; docked = (desktop_uri in plank_client.get_persistent_applications ()); if (docked) { - plank_menuitem.menu_label = _("Remove from _Dock"); + plank_menuitem.menu_label = _("Remove from Dock"); } else { - plank_menuitem.menu_label = _("Add to _Dock"); + plank_menuitem.menu_label = _("Add to Dock"); } plank_menuitem.activate.connect (plank_menuitem_activate); From 28e8cfa1c425ff740fdf8f06fa7b45993d26963a Mon Sep 17 00:00:00 2001 From: ItsMicin Date: Mon, 16 Aug 2021 16:57:25 +0700 Subject: [PATCH 3/3] Update AppContextMenu.vala --- src/Widgets/AppContextMenu.vala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Widgets/AppContextMenu.vala b/src/Widgets/AppContextMenu.vala index f7e7095c8..93efe4e78 100644 --- a/src/Widgets/AppContextMenu.vala +++ b/src/Widgets/AppContextMenu.vala @@ -57,9 +57,8 @@ public class Slingshot.AppContextMenu : Gtk.Menu { construct { app_info = new DesktopAppInfo (desktop_id); - KeyFile file; try { - file = new KeyFile (); + var file = new KeyFile (); file.load_from_file (app_info.get_filename (), 0); if (file.has_key (KeyFileDesktop.GROUP, DESKTOP_ACTION_KEY)) { foreach (unowned string g_action in file.get_string_list (KeyFileDesktop.GROUP, DESKTOP_ACTION_KEY)) {