-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextension.js
253 lines (208 loc) · 6.91 KB
/
extension.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Arya: Automatic Recorder of Your Activity.
* Copyright (C) 2012 Jon Crussell
*
* 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, see <http://www.gnu.org/licenses/>.
*/
const Lang = imports.lang;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Shell = imports.gi.Shell;
const St = imports.gi.St;
const ScreenSaver = imports.misc.screenSaver;
const APPMENU_ICON_SIZE = 22;
/**
* TODO:
* * Save/Load to/from a file
* * Add interface to see pretty graphs over time
*/
function init() {
return new ActivityRecorder();
}
function ActivityRecorder() {
this._init();
}
ActivityRecorder.prototype = {
__proto__: PanelMenu.Button.prototype,
_init: function() {
// Setup the menu button
PanelMenu.Button.prototype._init.call(this, St.Align.START);
this.button = new St.Bin({
style_class: 'panel-button',
reactive: true,
can_focus: true,
x_fill: true,
y_fill: false,
track_hover: true
});
let icon = new St.Icon({
icon_name: 'system-run',
icon_type: St.IconType.SYMBOLIC,
style_class: 'system-status-icon'
});
this.button.set_child(icon);
this.actor.add_actor(this.button);
// Refresh the menu (with updated times) every time it opens
this.menu.connect('open-state-changed', Lang.bind(this, this._onMenuOpenStateChanged));
this._reset();
},
_reset: function() {
// Setup state
this._usage = {};
this._updateState();
this._swap_time = Date.now();
},
// Recalculate the menu which shows time for each app
_refresh: function() {
this._recordTime();
let menu = this.menu;
menu.removeAll();
let usage = this._usage;
let ids = Object.keys(usage).sort(function(x,y) { return (usage[y] - usage[x]) });
let app_system = Shell.AppSystem.get_default();
let count = 0;
let total = 0;
ids.forEach(function(id) {
if(usage[id] < 1) return;
let app = app_system.lookup_app(id);
if(app) {
let mins = Math.round(usage[id]);
let icon = app.create_icon_texture(APPMENU_ICON_SIZE);
let str = makeTimeStrFromMins(mins);
menu.addMenuItem(new AppUsageMenuItem(icon, app.get_name(), str));
count += 1; total += mins;
}
});
if(count == 0) {
menu.addMenuItem(new PopupMenu.PopupMenuItem("Insufficient History... get to work!"));
}
else { // Add Total and Reset
menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
menu.addMenuItem(new TotalUsageMenuItem(makeTimeStrFromMins(total)));
item = new PopupMenu.PopupMenuItem(_("Clear History"));
item.connect('activate', Lang.bind(this, this._reset));
this.menu.addMenuItem(item);
}
},
// Callback for when app focus changes
_onFocusChanged: function() {
this._refresh();
this._updateState();
},
// Callback for when screensaver state changed
_onScreenSaverChanged: function(object, senderName, [isActive]) {
if(!isActive) { // Changed from screen saver to awake
this._swap_time = Date.now();
}
else { // Changed from awake to screen saver
this._recordTime();
}
},
// Callback for when the menu is opened or closed
_onMenuOpenStateChanged: function(menu, isOpen) {
if(isOpen) { // Changed from closed to open
this._refresh();
}
},
// Update the current app and touch the swap time
_updateState: function() {
this._curr_app = this._getCurrentAppId();
},
// Get the current app or null
_getCurrentAppId: function() {
let tracker = Shell.WindowTracker.get_default();
let focusedApp = tracker.focus_app;
// Not an application window
if(!focusedApp) {
return null;
}
return focusedApp.get_id();
},
// Update the total time for the current app
_recordTime: function() {
let swap_time = this._swap_time;
this._swap_time = Date.now();
// No previous app
if(this._curr_app == null) {
return;
}
let mins = (Date.now() - swap_time) / 1000 / 60;
this._usage[this._curr_app] = (this._usage[this._curr_app] || 0) + mins;
},
enable: function() {
// Add menu to panel
Main.panel._rightBox.insert_child_at_index(this.actor, 0);
Main.panel._menus.addMenu(this.menu);
// Connect to the tracker
let tracker = Shell.WindowTracker.get_default();
this._tracker_id = tracker.connect("notify::focus-app", Lang.bind(this, this._onFocusChanged));
// Add Listener for screensaver
this._screenSaverProxy = new ScreenSaver.ScreenSaverProxy();
this._screensaver_id = this._screenSaverProxy.connectSignal('ActiveChanged', Lang.bind(this, this._onScreenSaverChanged));
},
disable: function() {
// Remove menu from panel
Main.panel._menus.removeMenu(this.menu);
Main.panel._rightBox.remove_actor(this.actor);
// Remove tracker
let tracker = Shell.WindowTracker.get_default();
tracker.disconnect(this._tracker_id);
this._screenSaverProxy.disconnect(this._screensaver_id);
}
}
function makeTimeStrFromMins(mins) {
if(mins > 60) { // Report usage in hours
return Math.round(mins*100/60)/100 + " hours";
}
if(mins == 1) {
return mins + " minute";
}
else {
return mins + " minutes"
}
}
/**
* From: http://blog.fpmurphy.com/2011/05/more-gnome-shell-customization.html
*/
function AppUsageMenuItem() {
this._init.apply(this, arguments);
}
AppUsageMenuItem.prototype = {
__proto__: PopupMenu.PopupBaseMenuItem.prototype,
_init: function(icon, text1, text2, params) {
PopupMenu.PopupBaseMenuItem.prototype._init.call(this, params);
this.label1 = new St.Label({ text: text1 });
this.label2 = new St.Label({ text: text2 });
this.icon = icon;
this.addActor(this.label1);
this.addActor(this.icon, { align: St.Align.END });
this.addActor(this.label2, { align: St.Align.END });
}
};
function TotalUsageMenuItem() {
this._init.apply(this, arguments);
}
TotalUsageMenuItem.prototype = {
__proto__: PopupMenu.PopupBaseMenuItem.prototype,
_init: function(time, params) {
PopupMenu.PopupBaseMenuItem.prototype._init.call(this, params);
this.label1 = new St.Label({ text: "Total" });
this.label2 = new St.Label({ text: "" });
this.label3 = new St.Label({ text: time });
this.addActor(this.label1);
this.addActor(this.label2, { align: St.Align.END });
this.addActor(this.label3, { align: St.Align.END });
}
};