-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathapp_menu.js
316 lines (255 loc) · 9.4 KB
/
app_menu.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const Lang = imports.lang;
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const Shell = imports.gi.Shell;
const St = imports.gi.St;
const Tweener = imports.ui.tweener;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const display = Utils.display;
let SHOW_DELAY = 350;
let SHOW_DURATION = 0.15;
let HIDE_DURATION = 0.1;
var AppMenu = new Lang.Class({
Name: 'NoTitleBar.AppMenu',
_init: function(settings) {
this._wmCallbackIDs = [];
this._focusCallbackID = 0;
this._tooltipCallbackID = 0;
this._globalCallBackID = 0;
this._isEnabled = false;
this._appMenu = Main.panel.statusArea.appMenu;
this._activeWindow = null;
this._awCallbackID = 0;
// Tooltip
this._tooltip = null;
this._showTooltip = false;
this._tooltipIsShown = false;
this._tooltipDelayCallbackID = 0;
this._menuCallbackID = 0;
// Load settings
this._settings = settings;
this._settingsId = this._settings.connect('changed::change-appmenu',
Lang.bind(this, function() {
if (this._settings.get_boolean('change-appmenu'))
this._enable();
else
this._disable();
}));
if (this._settings.get_boolean('change-appmenu'))
this._enable();
else
this._disable();
},
/**
* AppMenu synchronization
*/
_updateAppMenu: function() {
let win = global.display.focus_window;
if (!win) {
return false;
}
let title = win.title;
// Not the topmost maximized window.
if (win !== Utils.getWindow()) {
let app = Shell.WindowTracker.get_default().get_window_app(win);
title = app.get_name();
}
// Not on the primary monitor
if (this._settings.get_boolean('only-main-monitor') && !win.is_on_primary_monitor()) {
let app = Shell.WindowTracker.get_default().get_window_app(win);
title = app.get_name();
}
this._appMenu._label.set_text(title);
this._tooltip.text = title;
return false;
},
_updateAppMenuWidth: function() {
this._restoreAppMenuWidth();
let width = this._settings.get_int('app-menu-width');
if (width > -1)
this._appMenu._label.set_style('max-width: ' + width + 'px');
this._updateAppMenu();
},
_restoreAppMenuWidth: function() {
this._appMenu._label.set_style('max-width');
},
/**
* Track the focused window's title
*/
_changeActiveWindow: function (win) {
if (win === this._activeWindow) {
return;
}
if (this._activeWindow) {
this._activeWindow.disconnect(this._awCallbackID);
}
this._activeWindow = win;
if (win) {
this._awCallbackID = win.connect('notify::title', Lang.bind(this, this._updateAppMenu));
this._updateAppMenu();
}
},
/**
* Focus change
*/
_onFocusChange: function() {
let input_mode_check = (global.stage_input_mode === undefined)
? true
: global.stage_input_mode == Shell.StageInputMode.FOCUSED;
if (!Shell.WindowTracker.get_default().focus_app && input_mode_check) {
// If the app has just lost focus to the panel, pretend
// nothing happened; otherwise you can't keynav to the
// app menu.
return false;
}
this._changeActiveWindow(global.display.focus_window);
return false;
},
/**
* tooltip
*/
_resetMenuCallback: function() {
if (this._menuCallbackID) {
this._appMenu.menu.disconnect(this._menuCallbackID);
this._menuCallbackID = 0;
}
},
_onAppMenuHover: function(actor) {
let hover = actor.get_hover();
if (this._showTooltip === hover) {
return false;
}
// We are not in the right state, let's fix that.
this._showTooltip = hover;
if (this._showTooltip) {
this._tooltipDelayCallbackID = Mainloop.timeout_add(SHOW_DELAY, Lang.bind(this, function() {
// Something wants us to stop.
if (this._tooltipDelayCallbackID === 0) {
return false;
}
let label = this._appMenu._label;
if (!label.get_clutter_text().get_layout().is_ellipsized()) {
// Do not need to hide.
this._tooltipDelayCallbackID = 0;
return false;
}
if (!this._tooltipIsShown) {
Main.uiGroup.add_actor(this._tooltip);
this._tooltipIsShown = true;
}
this._resetMenuCallback();
this._menuCallbackID = this._appMenu.menu.connect('open-state-changed', function(menu, open) {
if (!this._tooltip) {
return;
}
if (open && this._tooltipIsShown) {
Main.uiGroup.remove_actor(this._tooltip);
this._tooltipIsShown = false;
} else if (!this._tooltipIsShown) {
Main.uiGroup.add_actor(this._tooltip);
this._tooltipIsShown = true;
}
});
let [px, py] = Main.panel.actor.get_transformed_position();
let [bx, by] = label.get_transformed_position();
let [w, h] = label.get_transformed_size();
let y = py + Main.panel.actor.get_height() + 3;
let x = bx - Math.round((this._tooltip.get_width() - w)/2);
this._tooltip.opacity = 0;
this._tooltip.set_position(x, y);
Tweener.removeTweens(this._tooltip);
Tweener.addTween(this._tooltip, {
opacity: 255,
time: SHOW_DURATION,
transition: 'easeOutQuad',
});
return false;
}));
} else if (this._tooltipDelayCallbackID > 0) {
// If the event ran, then we hide.
this._resetMenuCallback();
Tweener.removeTweens(this._tooltip);
Tweener.addTween(this._tooltip, {
opacity: 0,
time: HIDE_DURATION,
transition: 'easeOutQuad',
onComplete: Lang.bind(this, function() {
if (this._tooltipIsShown) {
Main.uiGroup.remove_actor(this._tooltip);
this._tooltipIsShown = false;
}
})
});
this._tooltipDelayCallbackID = 0;
}
return false;
},
_enable: function() {
this._tooltip = new St.Label({
style_class: 'tooltip dash-label',
text: '',
opacity: 0
});
this._wmCallbackIDs = this._wmCallbackIDs.concat(Utils.onSizeChange(Lang.bind(this, this._updateAppMenu)));
this._focusCallbackID = global.display.connect('notify::focus-window', Lang.bind(this, this._onFocusChange));
this._onFocusChange();
this._tooltipCallbackID = this._appMenu.actor.connect('notify::hover',
Lang.bind(this, this._onAppMenuHover));
this._globalCallBackID = display.connect('restacked',
Lang.bind(this, this._updateAppMenu));
this._labelId = this._settings.connect('changed::app-menu-width',
Lang.bind(this, function() {
if (this._settings.get_boolean('change-appmenu'))
this._updateAppMenuWidth();
}));
this._updateAppMenuWidth();
this._isEnabled = true;
},
_disable: function() {
this._wmCallbackIDs.forEach(function(id) {
global.window_manager.disconnect(id);
});
this._wmCallbackIDs = [];
if (this._focusCallbackID) {
global.display.disconnect(this._focusCallbackID);
this._focusCallbackID = 0;
}
if (this._globalCallBackID) {
display.disconnect(this._globalCallBackID);
this._globalCallBackID = 0;
}
if (this._tooltipCallbackID) {
this._appMenu.actor.disconnect(this._tooltipCallbackID);
this._tooltipCallbackID = 0;
}
if (this._activeWindow) {
this._activeWindow.disconnect(this._awCallbackID);
this._awCallbackID = 0;
this._activeWindow = null;
}
if (this._tooltipDelayCallbackID) {
Mainloop.source_remove(this._tooltipDelayCallbackID);
this._tooltipDelayCallbackID = 0;
}
this._resetMenuCallback();
if (this._tooltip) {
this._tooltip.destroy();
this._tooltip = null;
}
if (this._labelId) {
this._settings.disconnect(this._labelId);
}
this._restoreAppMenuWidth();
this._isEnabled = false;
},
destroy: function() {
if (this._isEnabled)
this._disable();
if (this._settingsId) {
this._settings.disconnect(this._settingsId);
this._settingsId = null;
}
},
});