Repository navigation
Expand file tree
/
Copy pathappMenu.js
More file actions
135 lines (110 loc) · 4.46 KB
/
Copy pathappMenu.js
File metadata and controls
135 lines (110 loc) · 4.46 KB
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
// SPDX-License-Identifier: GPL-3.0-or-later
import * as AppFavorites from 'resource:///org/gnome/shell/ui/appFavorites.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import {gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
const MAX_TITLE_LENGTH = 44;
export class AppContextMenu extends PopupMenu.PopupMenu {
// `side` is the screen edge the bar sits on; the menu opens away from
// it. onHide(app) runs for Hide from App Bar.
constructor(appButton, side, onHide) {
super(appButton, 0.5, side);
this._appButton = appButton;
this._onHide = onHide;
}
rebuild() {
this.removeAll();
const {app} = this._appButton;
const windows = this._appButton.getWindows();
this._addWindowItems(windows);
this._addActionItems(app);
this._addPinItems(app);
this._addCloseItems(windows);
}
_addWindowItems(windows) {
if (windows.length === 0)
return;
for (const window of windows) {
const item = new PopupMenu.PopupMenuItem(
truncate(window.get_title() || this._appButton.app.get_name()));
if (window === global.display.focus_window &&
PopupMenu.Ornament?.DOT !== undefined)
item.setOrnament(PopupMenu.Ornament.DOT);
item.connect('activate', () => Main.activateWindow(window));
this.addMenuItem(item);
}
this.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
}
// As in the shell's app menu, a desktop file's own new-window action
// replaces the generic New Window item.
_addActionItems(app) {
const appInfo = app.get_app_info();
const actions = appInfo?.list_actions() ?? [];
const items = [];
if (app.can_open_new_window() && !actions.includes('new-window')) {
const item = new PopupMenu.PopupMenuItem(_('New Window'));
item.connect('activate', () => {
Main.overview.hide();
this._appButton.animateLaunch();
app.open_new_window(-1);
});
items.push(item);
}
for (const action of actions) {
const item = new PopupMenu.PopupMenuItem(
appInfo.get_action_name(action));
item.connect('activate', () => {
Main.overview.hide();
app.launch_action(action, global.get_current_time(), -1);
});
items.push(item);
}
if (items.length === 0)
return;
items.forEach(item => this.addMenuItem(item));
this.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
}
_addPinItems(app) {
const favorites = AppFavorites.getAppFavorites();
const appId = app.get_id();
const isPinned = favorites.isFavorite(appId);
const item = new PopupMenu.PopupMenuItem(
isPinned ? _('Unpin from Taskbar') : _('Pin to Taskbar'));
item.connect('activate', () => {
if (isPinned)
favorites.removeFavorite(appId);
else
favorites.addFavorite(appId);
});
this.addMenuItem(item);
// A window-backed app's id changes every launch, so it can't be hidden.
if (app.get_app_info()) {
const hide = new PopupMenu.PopupMenuItem(_('Hide from App Bar'));
hide.connect('activate', () => this._onHide(app));
this.addMenuItem(hide);
}
}
_addCloseItems(windows) {
if (windows.length === 0)
return;
this.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
if (windows.some(w => !w.minimized)) {
const item = new PopupMenu.PopupMenuItem(
windows.length > 1 ? _('Minimize All') : _('Minimize'));
item.connect('activate', () => windows.forEach(w => w.minimize()));
this.addMenuItem(item);
}
const close = new PopupMenu.PopupMenuItem(
windows.length > 1 ? _('Close All') : _('Close'));
close.connect('activate', () => {
const time = global.get_current_time();
windows.forEach(w => w.delete(time));
});
this.addMenuItem(close);
}
}
function truncate(text) {
return text.length > MAX_TITLE_LENGTH
? `${text.substring(0, MAX_TITLE_LENGTH - 1).trimEnd()}…`
: text;
}