Skip to content

Commit fd90ac9

Browse files
committed
Added support for custom icon, hiding icon and menu title
1 parent 5dd258c commit fd90ac9

File tree

3 files changed

+91
-14
lines changed

3 files changed

+91
-14
lines changed

examples/.commands-full.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"title": "Commands",
3+
"showIcon": true,
4+
"icon": "utilities-terminal",
5+
"menu": [
6+
{
7+
"title": "Termimal",
8+
"command": "gnome-terminal",
9+
"icon": "utilities-terminal"
10+
},
11+
{
12+
"title": "File Manager 3",
13+
"command": "nautilus",
14+
"icon": "folder"
15+
},
16+
{
17+
"type": "separator"
18+
},
19+
{
20+
"title": "Web Browser",
21+
"command": "firefox",
22+
"icon": "web-browser"
23+
},
24+
{
25+
"type": "separator"
26+
},
27+
{
28+
"title": "SSH Connections",
29+
"type": "submenu",
30+
"submenu": [
31+
{
32+
"title": "Connect to Server (SSH)",
33+
"command": "gnome-terminal -- bash -c 'ssh root@10.144.1.2 -p 8022'",
34+
"icon": "utilities-terminal"
35+
}
36+
]
37+
}
38+
]
39+
}

examples/.commands-quick.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[
2+
{
3+
"title": "Termimal",
4+
"command": "gnome-terminal",
5+
"icon": "utilities-terminal"
6+
},
7+
{
8+
"title": "File Manager 3",
9+
"command": "nautilus",
10+
"icon": "folder"
11+
},
12+
{
13+
"type": "separator"
14+
},
15+
{
16+
"title": "Web Browser",
17+
"command": "firefox",
18+
"icon": "web-browser"
19+
},
20+
{
21+
"type": "separator"
22+
},
23+
{
24+
"title": "SSH Connections",
25+
"type": "submenu",
26+
"submenu": [
27+
{
28+
"title": "Connect to Server (SSH)",
29+
"command": "gnome-terminal -- bash -c 'ssh root@10.144.1.2 -p 8022'",
30+
"icon": "utilities-terminal"
31+
}
32+
]
33+
}
34+
]

extension.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ function reloadExtension() {
1515
enable();
1616
}
1717

18-
function populateMenuItems(menu, cmds) {
18+
function populateMenuItems(menu, cmds, level) {
1919
cmds.forEach((cmd) => {
2020
if (cmd.type === 'separator') {
2121
menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
2222
return;
2323
}
2424
if (!cmd.title) { return; }
25-
if (cmd.type === 'submenu') {
25+
if (cmd.type === 'submenu' && level === 0) { // Stop submenu from being added after the first level
2626
let submenu;
2727
if (!cmd.submenu) { return; }
2828
submenu = new PopupMenu.PopupSubMenuMenuItem(cmd.title);
29-
populateMenuItems(submenu.menu, cmd.submenu);
29+
populateMenuItems(submenu.menu, cmd.submenu, level + 1);
3030
menu.addMenuItem(submenu);
3131
return;
3232
}
@@ -51,25 +51,29 @@ const CommandMenuPopup = GObject.registerClass(
5151
class CommandMenuPopup extends PanelMenu.Button {
5252
_init () {
5353
super._init(0);
54+
let menuTitle = commands.title && commands.title.length > 0 ? commands.title : "";
5455
let box = new St.BoxLayout();
56+
if (commands.showIcon !== false || (menuTitle === "")) {
57+
var menuIcon = commands.icon && commands.icon.length > 0 ? {
58+
icon_name: commands.icon,
59+
style_class: 'system-status-icon'
60+
} : {
61+
gicon : Gio.icon_new_for_string( Me.dir.get_path() + '/icon.svg' ),
62+
style_class : 'system-status-icon',
63+
};
64+
let icon = new St.Icon(menuIcon);
65+
box.add(icon);
66+
}
5567

56-
var menuIcon = commands.icon && commands.icon.length > 0 ? {
57-
icon_name: commands.icon,
58-
style_class: 'system-status-icon'
59-
} : {
60-
gicon : Gio.icon_new_for_string( Me.dir.get_path() + '/icon.svg' ),
61-
style_class : 'system-status-icon',
62-
};
63-
let icon = new St.Icon(menuIcon);
64-
box.add(icon);
6568
let toplabel = new St.Label({
66-
text: commands.text && commands.text.length > 0 ? commands.text : "",
69+
text: menuTitle,
6770
y_expand: true,
6871
y_align: Clutter.ActorAlign.CENTER
6972
});
7073
box.add(toplabel);
7174
this.add_child(box);
72-
populateMenuItems(this.menu, commands.menu);
75+
let level = 0;
76+
populateMenuItems(this.menu, commands.menu, level);
7377

7478
let editBtn = new PopupMenu.PopupMenuItem('Edit Commands');
7579
editBtn.connect('activate', () => {

0 commit comments

Comments
 (0)