-
Notifications
You must be signed in to change notification settings - Fork 1
/
applet.js
207 lines (172 loc) · 7.89 KB
/
applet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Custom Applications Menu Cinnamon Applet
// Developed by Nicolas LLOBERA - [email protected]
// Modified by Sahil Ahuja - [email protected]
// version: 2.0 (12-09-2013)
// License: GPLv3
// Copyright © 2016 Sahil Ahuja
/* Import */
const Applet = imports.ui.applet;
const Cinnamon = imports.gi.Cinnamon;
const Gettext = imports.gettext;
const _ = Gettext.gettext;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const Main = imports.ui.main;
const PopupMenu = imports.ui.popupMenu;
const St = imports.gi.St;
const appletUUID = 'custom-apps-menu@sahil87';
const AppletDirectory = imports.ui.appletManager.appletMeta[appletUUID].path;
imports.searchPath.push(AppletDirectory);
const PopupMenuExtension = imports.popupMenuExtension;
const AppSys = Cinnamon.AppSystem.get_default();
let SettingsFile;
function MyApplet(orientation) {
this._init(orientation);
}
MyApplet.prototype = {
__proto__: Applet.IconApplet.prototype,
_init: function(orientation) {
Applet.IconApplet.prototype._init.call(this, orientation);
try {
this.set_applet_icon_name("emblem-favorite");
this.set_applet_tooltip("Custom Applications Menu");
//Select the correct SettingsFile
SettingsFile = AppletDirectory + "/applications.json";
let SettingsFileOverride = AppletDirectory + "/applications-override.json";
let file = Gio.file_new_for_path(SettingsFileOverride);
if(file.query_exists(null)) {
SettingsFile = SettingsFileOverride;
}
// watch settings file for changes
file = Gio.file_new_for_path(SettingsFile);
this._monitor = file.monitor(Gio.FileMonitorFlags.NONE, null);
this._monitor.connect('changed', Lang.bind(this, this._on_settingsfile_changed));
this.menuManager = new PopupMenu.PopupMenuManager(this);
this.menu = new Applet.AppletPopupMenu(this, orientation);
this.menuManager.addMenu(this.menu);
this._readSettings();
this._createMenuRecursive(this.menu, this.applications);
this._createContextMenu();
}
catch (e) {
global.logError(e);
};
},
on_applet_clicked: function(event) {
this.menu.toggle();
},
// if applications.json is modified
_on_settingsfile_changed: function() {
this._readSettings();
this.menu.removeAll();
this._createMenuRecursive(this.menu, this.applications);
},
// create the menu items from the applications array
_createMenuRecursive: function(parentMenuItem, applications) {
for (var i=0; i<applications.length; i++) {
var application = applications[i];
// if the current application has been unactivated
if ("active" in application && !application.active)
continue;
// get command, displayName and icon from the desktop file
// only fill empty values
if (typeof(application.desktopFile) === "string") // if desktopFile is defined and a string
this._getDataFromDesktopFile(application);
// default icon name
if (typeof(application.iconName) === "undefined")
application.iconName = "image-missing";
// if it's a menu
if ("menu" in application) {
// PopupSubMenuMenuItem without icon makes the menu larger
/*var menuItem;
if (typeof(application.iconName) === "undefined")
menuItem = new PopupMenu.PopupSubMenuMenuItem(application.displayName);
else
menuItem = new PopupMenuExtension.PopupLeftImageSubMenuMenuItem(application.displayName, application.iconName);*/
if (typeof(application.displayName) === "undefined")
application.displayName = "sub-menu";
var menuItem = new PopupMenuExtension.PopupLeftImageSubMenuMenuItem(application.displayName, application.iconName);
this._createMenuRecursive(menuItem.menu, application.menu);
parentMenuItem.addMenuItem(menuItem);
}
else {
if (typeof(application.command) !== "string") { // if command is not defined or not a string, go to the next application
global.logError("Undefined or unvalid command for item " + (i+1) + ".");
continue;
}
else if (application.command === "S")
parentMenuItem.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
else {
// default display name
if (typeof(application.displayName) === "undefined")
application.displayName = application.command.split(" ")[0];
this._createMenuItem(parentMenuItem, application);
}
}
}
},
// extract the command, the name and the icone name from the desktop file
_getDataFromDesktopFile: function(application) {
let appInfo = null;
let desktopFile = application.desktopFile + ".desktop";
let app = AppSys.lookup_app(desktopFile);
if (!app) {
// for cinnamon-settings for instance
app = AppSys.lookup_settings_app(desktopFile);
}
if (app)
appInfo = app.get_app_info();
else {
global.logError("Desktop file " + application.desktopFile + " not found");
return;
}
if (typeof(application.command) === "undefined")
application.command = appInfo.get_commandline();
if (typeof(application.displayName) === "undefined")
application.displayName = appInfo.get_name();
if (typeof(application.iconName) === "undefined") {
let icon = appInfo.get_icon();
if (icon) {
if (icon instanceof(Gio.FileIcon))
application.iconName = icon.get_file().get_path();
else
application.iconName = icon.get_names().toString();
}
}
},
// parse the applications.json file into the applications variable
_readSettings: function() {
let jsonFileContent = Cinnamon.get_file_contents_utf8_sync(SettingsFile);
this.applications = JSON.parse(jsonFileContent);
},
_createMenuItem: function(parentMenuItem, application) {
var menuItem = new PopupMenuExtension.PopupImageLeftMenuItem(
application.displayName, application.iconName, application.command);
menuItem.connect("activate", function(actor, event) {
// As application variable is not accessible here,
// the application variable is passed to the PopupImageLeftMenuItem ctor to be accessible throw the actor argument
// which is the menuItem itself
Main.Util.spawnCommandLine(actor.command);
});
parentMenuItem.addMenuItem(menuItem);
},
_createContextMenu: function() {
this.edit_menu_item = new Applet.MenuItem(_("Edit"), Gtk.STOCK_EDIT, function() {
Main.Util.spawnCommandLine("xdg-open " + SettingsFile);
});
this._applet_context_menu.addMenuItem(this.edit_menu_item);
this.help_menu_item = new Applet.MenuItem(_("Help"), Gtk.STOCK_HELP, function() {
Main.Util.spawnCommandLine("xdg-open " + AppletDirectory + "/README.txt");
});
this._applet_context_menu.addMenuItem(this.help_menu_item);
this.about_menu_item = new Applet.MenuItem(_("About"), Gtk.STOCK_ABOUT, function() {
Main.Util.spawnCommandLine("xdg-open " + AppletDirectory + "/ABOUT.txt");
});
this._applet_context_menu.addMenuItem(this.about_menu_item);
}
};
function main(metadata, orientation) {
return new MyApplet(orientation);
}