Skip to content

Commit

Permalink
Icon In COntext menu
Browse files Browse the repository at this point in the history
elementary#73

This brach adopt from plank
  • Loading branch information
torikulhabib committed Aug 13, 2021
1 parent 0bdb984 commit c0527ef
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 16 deletions.
54 changes: 38 additions & 16 deletions src/Widgets/AppContextMenu.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions src/Widgets/MenuIcon.vala
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
*/

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;
});
}
}
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit c0527ef

Please sign in to comment.