-
Notifications
You must be signed in to change notification settings - Fork 460
/
appSpread.js
210 lines (174 loc) · 7.06 KB
/
appSpread.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
import {Atk, Clutter} from './dependencies/gi.js';
import {
Main,
SearchController,
Workspace,
WorkspaceThumbnail,
} from './dependencies/shell/ui.js';
import {Utils} from './imports.js';
export class AppSpread {
constructor() {
this.app = null;
this.supported = true;
this.windows = [];
// fail early and do nothing, if mandatory gnome shell functions are missing
if (Main.overview.isDummy ||
!Workspace?.Workspace?.prototype._isOverviewWindow ||
!WorkspaceThumbnail?.WorkspaceThumbnail?.prototype._isOverviewWindow) {
log('Dash to dock: Unable to temporarily replace shell functions ' +
'for app spread - using previews instead');
this.supported = false;
return;
}
this._signalHandlers = new Utils.GlobalSignalsHandler();
this._methodInjections = new Utils.InjectionsHandler();
this._vfuncInjections = new Utils.VFuncInjectionsHandler();
}
get isInAppSpread() {
return !!this.app;
}
destroy() {
if (!this.supported)
return;
this._hideAppSpread();
this._signalHandlers.destroy();
this._methodInjections.destroy();
this._vfuncInjections.destroy();
}
toggle(app) {
const newApp = this.app !== app;
if (this.app)
Main.overview.hide(); // also triggers hook 'hidden'
if (app && newApp)
this._showAppSpread(app);
}
_updateWindows() {
this.windows = this.app.get_windows();
}
_restoreDefaultWindows() {
const {workspaceManager} = global;
for (let i = 0; i < workspaceManager.nWorkspaces; i++) {
const metaWorkspace = workspaceManager.get_workspace_by_index(i);
metaWorkspace.list_windows().forEach(w => metaWorkspace.emit('window-added', w));
}
}
_filterWindows() {
const {workspaceManager} = global;
for (let i = 0; i < workspaceManager.nWorkspaces; i++) {
const metaWorkspace = workspaceManager.get_workspace_by_index(i);
metaWorkspace.list_windows().filter(w => !this.windows.includes(w)).forEach(
w => metaWorkspace.emit('window-removed', w));
}
}
_restoreDefaultOverview() {
this._hideAppSpread();
this._restoreDefaultWindows();
}
_showAppSpread(app) {
if (this.isInAppSpread)
return;
// Checked in overview "hide" event handler _hideAppSpread
this.app = app;
this._updateWindows();
// we need to hook into overview 'hidden' like this, in case app spread
// overview is hidden by choosing another app it should then do its
// cleanup too
this._signalHandlers.add(Main.overview, 'hidden', () => this._hideAppSpread());
const appSpread = this;
this._methodInjections.add([
// Filter workspaces to only show current app windows
Workspace.Workspace.prototype, '_isOverviewWindow',
function (originalMethod, window) {
/* eslint-disable no-invalid-this */
const isOverviewWindow = originalMethod.call(this, window);
return isOverviewWindow && appSpread.windows.includes(window);
/* eslint-enable no-invalid-this */
},
],
[
// Filter thumbnails to only show current app windows
WorkspaceThumbnail.WorkspaceThumbnail.prototype, '_isOverviewWindow',
function (originalMethod, windowActor) {
/* eslint-disable no-invalid-this */
const isOverviewWindow = originalMethod.call(this, windowActor);
return isOverviewWindow && appSpread.windows.includes(windowActor.metaWindow);
/* eslint-enable no-invalid-this */
},
]);
const activitiesButton = Main.panel.statusArea?.activities;
if (activitiesButton) {
this._signalHandlers.add(Main.overview, 'showing', () => {
activitiesButton.remove_style_pseudo_class('overview');
activitiesButton.remove_accessible_state(Atk.StateType.CHECKED);
});
this._vfuncInjections.add([
activitiesButton.constructor.prototype,
'event',
function (event) {
if (event.type() === Clutter.EventType.TOUCH_END ||
event.type() === Clutter.EventType.BUTTON_RELEASE) {
if (Main.overview.shouldToggleByCornerOrButton())
appSpread._restoreDefaultOverview();
}
return Clutter.EVENT_PROPAGATE;
},
],
[
activitiesButton.constructor.prototype,
'key_release_event',
function (keyEvent) {
const {keyval} = keyEvent;
if (keyval === Clutter.KEY_Return || keyval === Clutter.KEY_space) {
if (Main.overview.shouldToggleByCornerOrButton())
appSpread._restoreDefaultOverview();
}
return Clutter.EVENT_PROPAGATE;
},
]);
}
this._signalHandlers.add(Main.overview.dash.showAppsButton, 'notify::checked', () => {
if (Main.overview.dash.showAppsButton.checked)
this._restoreDefaultOverview();
});
// If closing windows in AppSpread, and only one window left:
// exit app spread and focus remaining window (handled in _hideAppSpread)
this._signalHandlers.add(this.app, 'windows-changed', () => {
this._updateWindows();
if (this.windows.length <= 1)
Main.overview.hide();
});
this._disableSearch();
Main.overview.show();
}
_hideAppSpread() {
if (!this.isInAppSpread)
return;
if (Main.overview.visible) {
Main.panel.statusArea?.activities.add_style_pseudo_class('overview');
Main.panel.statusArea?.activities.add_accessible_state(Atk.StateType.CHECKED);
}
// Restore original behaviour
this.app = null;
this._enableSearch();
this._methodInjections.clear();
this._signalHandlers.clear();
this._vfuncInjections.clear();
// Check reason for leaving AppSpread was closing app windows and only one window left...
if (this.windows.length === 1)
Main.activateWindow(this.windows[0]);
this.windows = [];
}
_disableSearch() {
if (!SearchController.SearchController.prototype._shouldTriggerSearch)
return;
if (Main.overview.searchEntry)
Main.overview.searchEntry.opacity = 0;
this._methodInjections.add(
SearchController.SearchController.prototype,
'_shouldTriggerSearch', () => false);
}
_enableSearch() {
if (Main.overview.searchEntry)
Main.overview.searchEntry.opacity = 255;
}
}