-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathextension.js
402 lines (308 loc) · 11.1 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
'use strict';
/* exported init enable disable */
const { GLib, GObject, Gio, Meta, Shell } = imports.gi;
const ByteArray = imports.byteArray;
const Main = imports.ui.main;
const Me = imports.misc.extensionUtils.getCurrentExtension();
let settings = null;
let current_window = null;
let bus_watch_id = null;
let dbus_action_group = null;
let resizing = false;
let wayland_client = null;
let subprocess = null;
const APP_ID = 'com.github.amezin.ddterm';
const APP_DBUS_PATH = '/com/github/amezin/ddterm';
const WINDOW_PATH_PREFIX = `${APP_DBUS_PATH}/window/`;
const SUBPROCESS_ARGV = [Me.dir.get_child('com.github.amezin.ddterm').get_path(), '--undecorated'];
const IS_WAYLAND_COMPOSITOR = Meta.is_wayland_compositor();
const USE_WAYLAND_CLIENT = Meta.WaylandClient && IS_WAYLAND_COMPOSITOR;
const SIGINT = 2;
class ExtensionDBusInterface {
constructor() {
let [_, xml] = Me.dir.get_child('com.github.amezin.ddterm.Extension.xml').load_contents(null);
this.dbus = Gio.DBusExportedObject.wrapJSObject(ByteArray.toString(xml), this);
}
BeginResize() {
if (!current_window || !current_window.maximized_vertically)
return;
const workarea = workarea_for_window(current_window);
const target_rect = target_rect_for_workarea(workarea);
Main.wm.skipNextEffect(current_window.get_compositor_private());
current_window.unmaximize(Meta.MaximizeFlags.VERTICAL);
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
move_resize_window(current_window, target_rect);
return GLib.SOURCE_REMOVE;
});
}
}
const DBUS_INTERFACE = new ExtensionDBusInterface().dbus;
class WaylandClientStub {
constructor(subprocess_launcher) {
this.subprocess_launcher = subprocess_launcher;
}
spawnv(_display, argv) {
return this.subprocess_launcher.spawnv(argv);
}
hide_from_window_list(_win) {
}
show_in_window_list(_win) {
}
owns_window(_win) {
return true;
}
}
function init() {
}
function enable() {
disconnect_settings();
settings = imports.misc.extensionUtils.getSettings();
Main.wm.addKeybinding(
'ddterm-toggle-hotkey',
settings,
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.NORMAL,
toggle
);
stop_dbus_watch();
bus_watch_id = Gio.bus_watch_name(
Gio.BusType.SESSION,
APP_ID,
Gio.BusNameWatcherFlags.NONE,
dbus_appeared,
dbus_disappeared
);
disconnect_created_handler();
global.display.connect('window-created', handle_created);
disconnect_focus_tracking();
global.display.connect('notify::focus-window', focus_window_changed);
settings.connect('changed::window-above', set_window_above);
settings.connect('changed::window-stick', set_window_stick);
settings.connect('changed::window-height', update_window_height);
settings.connect('changed::window-skip-taskbar', set_skip_taskbar);
DBUS_INTERFACE.export(Gio.DBus.session, '/org/gnome/Shell/Extensions/ddterm');
}
function disable() {
DBUS_INTERFACE.unexport();
if (Main.sessionMode.allowExtensions) {
// Stop the app only if the extension isn't being disabled because of
// lock screen/switch to other mode where extensions aren't allowed.
// Because when the session switches back to normal mode we want to
// keep all open terminals.
if (dbus_action_group)
dbus_action_group.activate_action('quit', null);
else if (subprocess)
subprocess.send_signal(SIGINT);
}
stop_dbus_watch();
dbus_action_group = null;
disconnect_created_handler();
disconnect_focus_tracking();
Main.wm.removeKeybinding('ddterm-toggle-hotkey');
disconnect_settings();
}
function spawn_app() {
if (subprocess)
return;
const subprocess_launcher = Gio.SubprocessLauncher.new(Gio.SubprocessFlags.NONE);
const context = global.create_app_launch_context(0, -1);
subprocess_launcher.set_environ(context.get_environment());
if (settings.get_boolean('force-x11-gdk-backend'))
subprocess_launcher.setenv('GDK_BACKEND', 'x11', true);
if (USE_WAYLAND_CLIENT && subprocess_launcher.getenv('GDK_BACKEND') !== 'x11')
wayland_client = Meta.WaylandClient.new(subprocess_launcher);
else
wayland_client = new WaylandClientStub(subprocess_launcher);
subprocess = wayland_client.spawnv(global.display, SUBPROCESS_ARGV);
subprocess.wait_async(null, subprocess_terminated);
}
function subprocess_terminated(source) {
if (subprocess === source) {
subprocess = null;
wayland_client = null;
}
}
function toggle() {
if (dbus_action_group)
dbus_action_group.activate_action('toggle', null);
else
spawn_app();
}
function dbus_appeared(connection, name) {
dbus_action_group = Gio.DBusActionGroup.get(connection, name, APP_DBUS_PATH);
}
function dbus_disappeared() {
dbus_action_group = null;
}
function handle_created(display, win) {
const handler_ids = [
win.connect('notify::gtk-application-id', track_window),
win.connect('notify::gtk-window-object-path', track_window),
];
const disconnect = () => {
handler_ids.forEach(handler => win.disconnect(handler));
};
handler_ids.push(win.connect('unmanaging', disconnect));
handler_ids.push(win.connect('unmanaged', disconnect));
track_window(win);
}
function focus_window_changed() {
if (!current_window || current_window.is_hidden())
return;
if (!settings || !settings.get_boolean('hide-when-focus-lost'))
return;
const win = global.display.focus_window;
if (win !== null) {
if (current_window === win || current_window.is_ancestor_of_transient(win))
return;
}
if (dbus_action_group)
dbus_action_group.activate_action('hide', null);
}
function is_dropdown_terminal_window(win) {
if (!wayland_client) {
// On X11, shell can be restarted, and the app will keep running.
// Accept windows from previously launched app instances.
if (IS_WAYLAND_COMPOSITOR)
return false;
} else if (!wayland_client.owns_window(win)) {
return false;
}
return (
win.gtk_application_id === APP_ID &&
win.gtk_window_object_path &&
win.gtk_window_object_path.startsWith(WINDOW_PATH_PREFIX)
);
}
function set_window_above() {
if (current_window === null)
return;
if (settings.get_boolean('window-above'))
current_window.make_above();
else
current_window.unmake_above();
}
function set_window_stick() {
if (current_window === null)
return;
if (settings.get_boolean('window-stick'))
current_window.stick();
else
current_window.unstick();
}
function set_skip_taskbar() {
if (!current_window || !wayland_client)
return;
if (settings.get_boolean('window-skip-taskbar'))
wayland_client.hide_from_window_list(current_window);
else
wayland_client.show_in_window_list(current_window);
}
function track_window(win) {
if (!is_dropdown_terminal_window(win)) {
untrack_window(win);
return;
}
if (win === current_window)
return;
current_window = win;
win.connect('unmanaging', untrack_window);
win.connect('unmanaged', untrack_window);
win.connect('notify::maximized-vertically', unmaximize_window);
const workarea = Main.layoutManager.getWorkAreaForMonitor(Main.layoutManager.currentMonitor.index);
const target_rect = target_rect_for_workarea(workarea);
move_resize_window(win, target_rect);
// !!! Sometimes size-changed is emitted from .move_resize_frame() with .get_frame_rect() returning old/incorrect size.
// Current workaround - 'resizing' flag
win.connect('size-changed', update_height_setting);
Main.activateWindow(win);
set_window_above();
set_window_stick();
set_skip_taskbar();
}
function workarea_for_window(win) {
// Can't use window.monitor here - it's out of sync
const monitor = global.display.get_monitor_index_for_rect(win.get_frame_rect());
if (monitor < 0)
return null;
return Main.layoutManager.getWorkAreaForMonitor(monitor);
}
function target_rect_for_workarea(workarea) {
const target_rect = workarea.copy();
target_rect.height *= settings.get_double('window-height');
return target_rect;
}
function unmaximize_window(win) {
if (!win || win !== current_window)
return;
if (!win.maximized_vertically)
return;
const workarea = workarea_for_window(current_window);
const target_rect = target_rect_for_workarea(workarea);
if (target_rect.height < workarea.height)
win.unmaximize(Meta.MaximizeFlags.VERTICAL);
}
function move_resize_window(win, target_rect) {
resizing = true;
try {
win.move_resize_frame(false, target_rect.x, target_rect.y, target_rect.width, target_rect.height);
} finally {
resizing = false;
}
}
function update_height_setting(win) {
if (resizing)
return;
if (!win || win !== current_window)
return;
if (win.maximized_vertically)
return;
const workarea = workarea_for_window(win);
const current_height = win.get_frame_rect().height / workarea.height;
if (Math.abs(current_height - settings.get_double('window-height')) > 0.0001)
settings.set_double('window-height', current_height);
}
function update_window_height() {
if (!current_window)
return;
const workarea = workarea_for_window(current_window);
if (!workarea)
return;
const target_rect = target_rect_for_workarea(workarea);
if (target_rect.equal(current_window.get_frame_rect()))
return;
if (current_window.maximized_vertically && target_rect.height < workarea.height) {
Main.wm.skipNextEffect(current_window.get_compositor_private());
current_window.unmaximize(Meta.MaximizeFlags.VERTICAL);
}
move_resize_window(current_window, target_rect);
}
function untrack_window(win) {
if (win === current_window)
current_window = null;
if (win) {
GObject.signal_handlers_disconnect_by_func(win, untrack_window);
GObject.signal_handlers_disconnect_by_func(win, update_height_setting);
GObject.signal_handlers_disconnect_by_func(win, unmaximize_window);
}
}
function stop_dbus_watch() {
if (bus_watch_id) {
Gio.bus_unwatch_name(bus_watch_id);
bus_watch_id = null;
}
}
function disconnect_created_handler() {
GObject.signal_handlers_disconnect_by_func(global.display, handle_created);
}
function disconnect_focus_tracking() {
GObject.signal_handlers_disconnect_by_func(global.display, focus_window_changed);
}
function disconnect_settings() {
if (settings) {
GObject.signal_handlers_disconnect_by_func(settings, set_window_above);
GObject.signal_handlers_disconnect_by_func(settings, set_window_stick);
GObject.signal_handlers_disconnect_by_func(settings, update_window_height);
GObject.signal_handlers_disconnect_by_func(settings, set_skip_taskbar);
}
}