Repository navigation
Expand file tree
/
Copy pathextension.js
More file actions
179 lines (162 loc) · 7.18 KB
/
Copy pathextension.js
File metadata and controls
179 lines (162 loc) · 7.18 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
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
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as MessageTray from 'resource:///org/gnome/shell/ui/messageTray.js';
import * as Config from 'resource:///org/gnome/shell/misc/config.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import { Logger } from './lib/utils/logger.js';
import { hasDirectionalShortcuts } from './lib/utils/shortcuts.js';
import { Engine } from './lib/app/engine/engine.js';
import { GSettingsConfigSource } from './lib/shell/config-source.js';
import { MutterDisplayPort } from './lib/shell/display-port.js';
import { MutterWindowPort } from './lib/shell/window-port.js';
import { ShellEvents } from './lib/shell/shell-events.js';
import { KeybindingAdapter } from './lib/shell/keybindings.js';
import { StIndicator } from './lib/shell/st-indicator.js';
import { MaximizeButtonHider } from './lib/shell/button-layout.js';
/**
* Composition root: creates every Shell object and the Engine, and destroys them in reverse order.
*/
export default class WorkflowTilingExtension extends Extension {
/**
* GNOME marks an extension whose enable() throws as failed and never calls disable(), so whatever was set up so
* far is undone here. disable() skips every part that was never built (each is assigned only once complete).
*/
enable() {
try {
this._enable();
} catch (e) {
try {
this.disable();
} catch (cleanupError) {
Logger.error('Cleanup after a failed enable failed', cleanupError);
}
throw e;
}
}
_enable() {
Logger.info(`Enabling ${this.metadata.name} ${this.metadata['version-name'] ?? ''} on GNOME Shell ${Config.PACKAGE_VERSION}`);
this._settings = this.getSettings();
this._configSource = new GSettingsConfigSource(this._settings);
this._configSource.onConfigChanged((config) => this._onConfigChanged(config));
this._configSource.onConfigInvalid((error) => this._suspend(error));
this._buttonHider = new MaximizeButtonHider(this._settings);
this._buttonHider.enable();
this._displayPort = new MutterDisplayPort();
this._windowPort = new MutterWindowPort(this._displayPort, Number(Config.PACKAGE_VERSION.split('.')[0]));
this._indicator = new StIndicator();
this._sessionModeId = Main.sessionMode.connect('updated', () => this._applySessionMode());
if (this._configSource.isValid()) this._start();
else this._suspend(this._configSource.error);
}
// The extension also runs in the `unlock-dialog` session mode: tiling state (slot order, overrides, evacuated
// windows) must survive the lock screen and suspend, and monitor changes while locked (undocking with the lid
// closed) must be handled. While locked only the keybindings are switched off. disable() cleans up
// unconditionally.
disable() {
Logger.info(`Disabling ${this.metadata.name}`);
if (this._sessionModeId) Main.sessionMode.disconnect(this._sessionModeId);
this._sessionModeId = 0;
this._firstRunHint?.destroy();
this._keybindings?.destroy();
this._events?.unbind();
this._engine?.destroy();
this._indicator?.destroy();
this._windowPort?.destroy();
this._displayPort?.destroy();
this._configSource?.destroy();
this._buttonHider?.disable();
this._keybindings = null;
this._events = null;
this._engine = null;
this._indicator = null;
this._windowPort = null;
this._displayPort = null;
this._configSource = null;
this._buttonHider = null;
this._firstRunHint = null;
this._settings = null;
this._suspended = false;
}
_start() {
this._engine = new Engine({
windowPort: this._windowPort,
displayPort: this._displayPort,
indicator: this._indicator,
config: this._configSource.current(),
});
this._events = new ShellEvents({
windowPort: this._windowPort,
displayPort: this._displayPort,
engine: this._engine,
});
this._events.bind();
this._engine.resync(this._windowPort.all());
this._keybindings = new KeybindingAdapter({
settings: this._settings,
engine: this._engine,
windowPort: this._windowPort,
});
this._suspended = false;
this._applySessionMode();
}
_onConfigChanged(config) {
if (!this._engine) {
this._start();
Logger.info('Valid layout JSON provided; tiling started');
Main.notify('Workflow Tiling', 'Valid layout provided. Tiling resumed.');
} else if (this._suspended) {
this._resume(config);
} else {
this._engine.applyConfig(config);
}
}
_suspend(error) {
if (this._suspended) return;
this._suspended = true;
Logger.error(`Invalid custom layouts JSON: ${error}`);
Main.notifyError('Workflow Tiling', `Invalid layouts JSON. Tiling suspended.\n${error}`);
this._events?.unbind();
this._keybindings?.unbind();
this._engine?.suspend();
}
_resume(config) {
this._suspended = false;
this._engine.applyConfig(config);
this._events.bind();
this._engine.resync(this._windowPort.all());
this._applySessionMode();
Logger.info('Valid layout JSON provided; tiling resumed');
Main.notify('Workflow Tiling', 'Valid layout provided. Tiling resumed.');
}
/** Keybindings are switched off while the screen is locked and while suspended. */
_applySessionMode() {
if (!this._keybindings) return;
Logger.debug(`Extension: session mode ${Main.sessionMode.currentMode}, locked ${Main.sessionMode.isLocked}`);
if (Main.sessionMode.isLocked || this._suspended) {
this._keybindings.unbind();
return;
}
this._keybindings.bind();
this._showFirstRunHint();
}
/**
* Once per install, while tiling runs unlocked: tells that moving and focusing windows has no shortcuts until a
* mode other than None is chosen. Not shown when the user already uses one.
*/
_showFirstRunHint() {
if (this._settings.get_boolean('first-run-hint-shown')) return;
// Set on the first check either way: choosing None later is a decision, not a first run
this._settings.set_boolean('first-run-hint-shown', true);
if (hasDirectionalShortcuts(this._settings)) return;
const source = MessageTray.getSystemSource();
const notification = new MessageTray.Notification({
source,
title: 'Workflow Tiling',
body: 'Moving and focusing windows has no keyboard shortcuts yet. Choose a mode in the settings.',
});
notification.addAction('Open Settings', () => this.openPreferences());
// Kept so disable() can remove it: its action holds this extension
notification.connect('destroy', () => (this._firstRunHint = null));
this._firstRunHint = notification;
source.addNotification(notification);
}
}