forked from pop-os/shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
3050 lines (2463 loc) · 103 KB
/
extension.ts
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const Me = imports.misc.extensionUtils.getCurrentExtension();
import * as Config from 'config';
import * as Forest from 'forest';
import * as Ecs from 'ecs';
import * as Events from 'events';
import * as Focus from 'focus';
import * as Geom from 'geom';
import * as GrabOp from 'grab_op';
import * as Keybindings from 'keybindings';
import * as Lib from 'lib';
import * as log from 'log';
import * as PanelSettings from 'panel_settings';
import * as Rect from 'rectangle';
import * as Settings from 'settings';
import * as Tiling from 'tiling';
import * as Window from 'window';
import * as launcher from 'launcher';
import * as auto_tiler from 'auto_tiler';
import * as node from 'node';
import * as utils from 'utils';
import * as Executor from 'executor';
import * as movement from 'movement';
import * as stack from 'stack';
import * as add_exception from 'dialog_add_exception';
import * as exec from 'executor';
import * as dbus_service from 'dbus_service';
import * as scheduler from 'scheduler';
import type { Entity } from 'ecs';
import type { ExtEvent } from 'events';
import { Rectangle } from 'rectangle';
import type { Indicator } from 'panel_settings';
import type { Launcher } from 'launcher';
import { Fork } from './fork';
const display = global.display;
const wim = global.window_manager;
const wom = global.workspace_manager;
const Movement = movement.Movement;
const GLib: GLib = imports.gi.GLib;
const { Gio, Meta, St, Shell } = imports.gi;
const { GlobalEvent, WindowEvent } = Events;
const { cursor_rect, is_keyboard_op, is_resize_op, is_move_op } = Lib;
const Main = imports.ui.main;
const { layoutManager, loadTheme, overview, panel, setThemeStylesheet, screenShield, sessionMode, windowAttentionHandler } = Main;
const { ScreenShield } = imports.ui.screenShield;
const { AppSwitcher, AppIcon, WindowSwitcherPopup } = imports.ui.altTab;
const { SwitcherList } = imports.ui.switcherPopup;
const { Workspace } = imports.ui.workspace;
const { WorkspaceThumbnail } = imports.ui.workspaceThumbnail;
const Tags = Me.imports.tags;
const STYLESHEET_PATHS = ['light', 'dark', 'highcontrast'].map(stylesheet_path);
const STYLESHEETS = STYLESHEET_PATHS.map((path) => Gio.File.new_for_path(path));
const GNOME_VERSION = imports.misc.config.PACKAGE_VERSION;
enum Style { Light, Dark, HighContrast }
interface Display {
area: Rectangle;
ws: Rectangle;
}
interface Monitor extends Rectangular {
index: number;
}
interface Injection {
object: any;
method: string;
func: any;
}
export class Ext extends Ecs.System<ExtEvent> {
/** Mechanism for managing keybindings */
keybindings: Keybindings.Keybindings = new Keybindings.Keybindings(this);
/** Manage interactions with GSettings */
settings: Settings.ExtensionSettings = new Settings.ExtensionSettings();
// Widgets
/** An overlay which shows a preview of where a window will be moved */
overlay: St.Widget = new St.BoxLayout({ style_class: "pop-shell-overlay", visible: false });
/** The application launcher, focus search, and calculator dialog */
window_search: Launcher = new launcher.Launcher(this);
/** DBus */
dbus: dbus_service.Service = new dbus_service.Service();
// State
/** Animate window movements */
animate_windows: boolean = true;
button: any = null;
button_gio_icon_auto_on: any = null;
button_gio_icon_auto_off: any = null;
conf: Config.Config = new Config.Config();
conf_watch: null | [any, SignalID] = null;
/** Column sizes in snap-to-grid */
column_size: number = 32;
/** The currently-loaded theme variant */
current_style: Style = Style.Dark;
/** Set when the display configuration has been triggered for execution */
displays_updating: SignalID | null = null;
/** Row size in snap-to-grid */
row_size: number = 32;
/** The known display configuration, for tracking monitor removals and changes */
displays: [number, Map<number, Display>] = [global.display.get_primary_monitor(), new Map()];
/** The current scaling factor in GNOME Shell */
dpi: number = St.ThemeContext.get_for_stage(global.stage).scale_factor;
drag_signal: null | SignalID = null
/** If set, the user is currently selecting a window to add to floating exceptions */
exception_selecting: boolean = false;
/** The number of pixels between windows */
gap_inner: number = 0;
/** Exactly half of the value of the inner gap */
gap_inner_half: number = 0;
/** Previously-set value of the inner gap */
gap_inner_prev: number = 0;
/** The number of pixels around a display's work area */
gap_outer: number = 0;
/** Previously-set value of the outer gap */
gap_outer_prev: number = 0;
/** Information about a current possible grab operation */
grab_op: GrabOp.GrabOp | null = null;
/** A display config update is triggered on a workspace addition */
ignore_display_update: boolean = false;
/** Functions replaced in GNOME */
injections: Array<Injection> = new Array();
/** The window that was focused before the last window */
private prev_focused: [null | Entity, null | Entity] = [null, null];
/** Initially set to true when the extension is initializing */
init: boolean = true;
was_locked: boolean = false;
/** Set when a window is being moved by the mouse */
moved_by_mouse: boolean = false
private workareas_update: null | SignalID = null
/** Record of misc. global objects and their attached signals */
private signals: Map<GObject.Object, Array<SignalID>> = new Map();
private size_requests: Map<GObject.Object, SignalID> = new Map();
/** Stores windows that were focused on a workspace */
private workspace_active: Map<number, null | Entity> = new Map()
// Entity-component associations
/** Store for stable sequences of each registered window */
ids: Ecs.Storage<number> = this.register_storage();
/** Store for keeping track of which monitor + workspace a window is on */
monitors: Ecs.Storage<[number, number]> = this.register_storage();
/** Stores movements that have been queued */
movements: Ecs.Storage<Rectangular> = this.register_storage();
/** Store for names associated with windows */
names: Ecs.Storage<string> = this.register_storage();
/** Signal ID which handles size-changed signals */
size_changed_signal: SignalID = 0;
/** Store for size-changed signals attached to each window */
size_signals: Ecs.Storage<SignalID[]> = this.register_storage();
/** Set to true if a window is snapped to the grid */
snapped: Ecs.Storage<boolean> = this.register_storage();
/** Primary storage for the window entities, containing the actual window */
windows: Ecs.Storage<Window.ShellWindow> = this.register_storage();
/** Signals which have been registered for each window */
window_signals: Ecs.Storage<Array<SignalID>> = this.register_storage();
// Systems
/** Manages automatic tiling behaviors in the shell */
auto_tiler: auto_tiler.AutoTiler | null = null;
/** Performs focus selections */
focus_selector: Focus.FocusSelector = new Focus.FocusSelector();
/** Calculates window placements when tiling and focus-switching */
tiler: Tiling.Tiler = new Tiling.Tiler(this);
constructor() {
super(new Executor.GLibExecutor());
this.load_settings();
this.reload_theme()
this.register_fn(() => load_theme(this.current_style));
this.conf.reload();
if (this.settings.int) {
this.settings.int.connect('changed::gtk-theme', () => {
this.register(Events.global(GlobalEvent.GtkThemeChanged));
});
}
if (this.settings.shell) {
this.settings.shell.connect('changed::name', () => {
this.register(Events.global(GlobalEvent.GtkShellChanged));
});
}
this.dbus.FocusUp = () => this.focus_up()
this.dbus.FocusDown = () => this.focus_down()
this.dbus.FocusLeft = () => this.focus_left()
this.dbus.FocusRight = () => this.focus_right()
this.dbus.Launcher = () => this.window_search.open(this)
this.dbus.WindowFocus = (window: [number, number]) => {
const target_window = this.windows.get(window)
if (target_window) {
target_window.activate()
this.on_focused(target_window)
}
this.window_search.close()
}
this.dbus.WindowList = (): Array<[[number, number], string, string, string]> => {
const wins = new Array()
for (const window of this.tab_list(Meta.TabList.NORMAL, null)) {
const string = window.window_app.get_id();
wins.push([
window.entity,
window.title(),
window.name(this),
string ? string : ""
])
}
return wins;
}
this.dbus.WindowQuit = (win: [number, number]) => {
this.windows.get(win)?.meta.delete(global.get_current_time())
this.window_search.close()
}
}
// System interface
/** Registers a generic callback to be executed in the event loop. */
register_fn(callback: () => void, name?: string) {
this.register({ tag: 1, callback, name });
}
/** Executes an event on the system */
run(event: ExtEvent) {
switch (event.tag) {
/** Callback Event */
case 1:
(event.callback)();
break
/** Window Event */
case 2:
let win = event.window;
/** Validate that the window's actor still exists. */
if (!win.actor_exists()) return;
if (event.kind.tag === 1) {
const { window } = event;
let movement = this.movements.remove(window.entity);
if (!movement) return;
let actor = window.meta.get_compositor_private();
if (!actor) {
this.auto_tiler?.detach_window(this, window.entity);
return;
}
actor.remove_all_transitions();
const { x, y, width, height } = movement;
window.meta.move_resize_frame(true, x, y, width, height);
window.meta.move_frame(true, x, y)
this.monitors.insert(window.entity, [
win.meta.get_monitor(),
win.workspace_id()
]);
if (win.activate_after_move) {
win.activate_after_move = false;
win.activate();
}
return;
}
switch (event.kind.event) {
case WindowEvent.Maximize:
this.unset_grab_op()
this.on_maximize(win);
break
case WindowEvent.Minimize:
this.unset_grab_op()
this.on_minimize(win);
break;
case WindowEvent.Size:
if (this.auto_tiler && !win.is_maximized() && !win.meta.is_fullscreen()) {
this.auto_tiler.reflow(this, win.entity);
}
break
case WindowEvent.Workspace:
this.on_workspace_changed(win)
break
case WindowEvent.Fullscreen:
if (this.auto_tiler) {
let attachment = this.auto_tiler.attached.get(win.entity);
if (attachment) {
if (!win.meta.is_fullscreen()) {
let fork = this.auto_tiler.forest.forks.get(win.entity);
if (fork) {
this.auto_tiler.reflow(this, win.entity);
}
if (win.stack !== null) {
this.auto_tiler.forest.stacks.get(win.stack)?.set_visible(true)
}
} else { // not full screened
if (win.stack !== null) {
this.auto_tiler.forest.stacks.get(win.stack)?.set_visible(false)
}
}
}
}
break
}
break
/** Window Create Event */
case 3:
let actor = event.window.get_compositor_private();
if (!actor) return;
this.on_window_create(event.window, actor);
break
/** Stateless global events */
case 4:
switch (event.event) {
case GlobalEvent.GtkShellChanged:
this.on_gtk_shell_changed();
break;
case GlobalEvent.GtkThemeChanged:
this.on_gtk_theme_change();
break;
case GlobalEvent.MonitorsChanged:
this.update_display_configuration(false);
break;
case GlobalEvent.OverviewShown:
this.on_overview_shown();
break;
}
break
}
}
// Extension methods
activate_window(window: Window.ShellWindow | null) {
if (window) {
window.activate();
}
}
active_monitor(): number {
return display.get_current_monitor();
}
active_window_list(): Array<Window.ShellWindow> {
let workspace = wom.get_active_workspace();
return this.tab_list(Meta.TabList.NORMAL_ALL, workspace);
}
active_workspace(): number {
return wom.get_active_workspace_index();
}
actor_of(entity: Entity): null | Clutter.Actor {
const window = this.windows.get(entity);
return window ? window.meta.get_compositor_private() : null;
}
/// Connects a callback signal to a GObject, and records the signal.
connect(object: GObject.Object, property: string, callback: (...args: any) => boolean | void): SignalID {
const signal = object.connect(property, callback);
const entry = this.signals.get(object);
if (entry) {
entry.push(signal);
} else {
this.signals.set(object, [signal]);
}
return signal;
}
connect_meta(win: Window.ShellWindow, signal: string, callback: (...args: any[]) => void): number {
const id = win.meta.connect(signal, () => {
if (win.actor_exists()) callback();
});
this.window_signals.get_or(win.entity, () => new Array()).push(id);
return id;
}
connect_size_signal(win: Window.ShellWindow, signal: string, func: () => void): number {
return this.connect_meta(win, signal, () => {
if (!this.contains_tag(win.entity, Tags.Blocked)) func();
});
}
connect_window(win: Window.ShellWindow) {
const size_event = () => {
const old = this.size_requests.get(win.meta)
if (old) {
try { GLib.source_remove(old) } catch (_) { }
}
const new_s = GLib.timeout_add(GLib.PRIORITY_LOW, 500, () => {
this.register(Events.window_event(win, WindowEvent.Size));
this.size_requests.delete(win.meta)
return false
})
this.size_requests.set(win.meta, new_s)
}
this.connect_meta(win, 'workspace-changed', () => {
this.register(Events.window_event(win, WindowEvent.Workspace));
})
this.size_signals.insert(win.entity, [
this.connect_size_signal(win, 'size-changed', size_event),
this.connect_size_signal(win, 'position-changed', size_event),
this.connect_size_signal(win, 'notify::minimized', () => {
this.register(Events.window_event(win, WindowEvent.Minimize));
}),
]);
}
exception_add(win: Window.ShellWindow) {
this.exception_selecting = false;
let d = new add_exception.AddExceptionDialog(
// Cancel
() => this.exception_dialog(),
// this_app
() => {
let wmclass = win.meta.get_wm_class();
if (wmclass !== null && wmclass.length === 0) {
wmclass = win.name(this)
}
if (wmclass) this.conf.add_app_exception(wmclass);
this.exception_dialog()
},
// current-window
() => {
let wmclass = win.meta.get_wm_class();
if (wmclass) this.conf.add_window_exception(
wmclass,
win.title()
);
this.exception_dialog()
},
// Reload the tiling config on dialog close
() => {
this.conf.reload()
this.tiling_config_reapply()
}
);
d.open();
}
exception_dialog() {
let path = Me.dir.get_path() + "/floating_exceptions/main.js";
const event_handler = (event: string): boolean => {
switch (event) {
case "MODIFIED":
this.register_fn(() => {
this.conf.reload()
this.tiling_config_reapply()
})
break
case "SELECT":
this.register_fn(() => this.exception_select())
return false
}
return true
}
const ipc = utils.async_process_ipc(["gjs", path])
if (ipc) {
const generator = (stdout: any, res: any) => {
try {
const [bytes,] = stdout.read_line_finish(res)
if (bytes) {
if (event_handler((imports.byteArray.toString(bytes) as string).trim())) {
ipc.stdout.read_line_async(0, ipc.cancellable, generator)
}
}
} catch (why) {
log.error(`failed to read response from floating exceptions dialog: ${why}`)
}
}
ipc.stdout.read_line_async(0, ipc.cancellable, generator)
}
}
exception_select() {
GLib.timeout_add(GLib.PRIORITY_LOW, 500, () => {
this.exception_selecting = true
overview.show()
return false
})
}
exit_modes() {
this.tiler.exit(this);
this.window_search.reset();
this.window_search.close();
this.overlay.visible = false;
}
find_monitor_to_retach(width: number, height: number): [number, Display] {
if (!this.settings.workspaces_only_on_primary()) {
for (const [index, display] of this.displays[1]) {
if (display.area.width == width && display.area.height == height) {
return [index, display];
}
}
}
const primary = display.get_primary_monitor();
return [primary, this.displays[1].get(primary) as Display];
}
find_unused_workspace(monitor: number): [number, any] {
if (!this.auto_tiler) return [0, wom.get_workspace_by_index(0)]
let id = 0
const tiled_windows = new Array<Window.ShellWindow>()
for (const [window] of this.auto_tiler.attached.iter()) {
if (!this.auto_tiler.attached.contains(window)) continue
const win = this.windows.get(window)
if (win && !win.reassignment && win.meta.get_monitor() === monitor) tiled_windows.push(win)
}
cancel:
while (true) {
for (const window of tiled_windows) {
if (window.workspace_id() === id) {
id += 1
continue cancel
}
}
break
}
let new_work
if (id + 1 === wom.get_n_workspaces()) {
id += 1
new_work = wom.append_new_workspace(true, global.get_current_time())
} else {
new_work = wom.get_workspace_by_index(id)
}
return [id, new_work];
}
focus_left() {
this.stack_select(
(id, stack) => id === 0 ? null : stack.tabs[id - 1].entity,
() => this.activate_window(this.focus_selector.left(this, null))
);
}
focus_right() {
this.stack_select(
(id, stack) => stack.tabs.length > id + 1 ? stack.tabs[id + 1].entity : null,
() => this.activate_window(this.focus_selector.right(this, null))
)
}
focus_down() {
this.activate_window(this.focus_selector.down(this, null))
}
focus_up() {
this.activate_window(this.focus_selector.up(this, null))
}
focus_window(): Window.ShellWindow | null {
return this.get_window(display.get_focus_window())
}
stack_select(
select: (id: number, stack: stack.Stack) => Entity | null,
focus_shift: () => void,
) {
const switched = this.stack_switch((stack: any) => {
if (!stack) return false;
const stack_con = this.auto_tiler?.forest.stacks.get(stack.idx);
if (stack_con) {
const id = stack_con.active_id;
if (id !== -1) {
const next = select(id, stack_con);
if (next) {
stack_con.activate(next);
const window = this.windows.get(next)
if (window) {
window.activate();
return true;
}
}
}
}
return false;
});
if (!switched) {
focus_shift();
}
}
stack_switch(apply: (stack: node.NodeStack) => boolean) {
const window = this.focus_window();
if (window) {
if (this.auto_tiler) {
const node = this.auto_tiler.find_stack(window.entity);
return node ? apply(node[1].inner as node.NodeStack) : false;
}
}
}
/// Fetches the window component from the entity associated with the metacity window metadata.
get_window(meta: Meta.Window | null): Window.ShellWindow | null {
let entity = this.window_entity(meta);
return entity ? this.windows.get(entity) : null;
}
inject(object: any, method: string, func: any) {
const prev = object[method];
this.injections.push({ object, method, func: prev })
object[method] = func;
}
injections_add() {
const screen_unlock_fn = ScreenShield.prototype['deactivate'];
this.inject(ScreenShield.prototype, 'deactivate', (args: any) => {
screen_unlock_fn.apply(screenShield, [args]);
this.update_display_configuration(true);
})
}
injections_remove() {
for (const { object, method, func } of this.injections.splice(0)) {
object[method] = func
}
}
load_settings() {
this.set_gap_inner(this.settings.gap_inner());
this.set_gap_outer(this.settings.gap_outer());
this.gap_inner_prev = this.gap_inner;
this.gap_outer_prev = this.gap_outer;
this.column_size = this.settings.column_size() * this.dpi;
this.row_size = this.settings.row_size() * this.dpi;
}
monitor_work_area(monitor: number): Rectangle {
const meta = wom
.get_active_workspace()
.get_work_area_for_monitor(monitor);
return Rect.Rectangle.from_meta(meta as Rectangular);
}
monitor_area(monitor: number): Rectangle | null {
const rect = global.display.get_monitor_geometry(monitor)
return rect ? Rect.Rectangle.from_meta(rect as Rectangular) : null
}
on_active_workspace_changed() {
this.register_fn(() => {
this.exit_modes();
this.restack()
const activate_window = (window: Window.ShellWindow) => {
this.on_focused(window)
window.activate(true)
this.prev_focused = [null, window.entity]
}
const focused = this.focus_window()
if (focused && focused.same_workspace()) {
activate_window(focused)
return
}
// Activate the last-active window on workspace.
const workspace_id = this.active_workspace()
const active = this.workspace_active.get(workspace_id)
if (active) {
const window = this.windows.get(active)
if (window && window.meta.get_workspace().index() == workspace_id && !window.meta.minimized) {
activate_window(window)
return
}
}
// If window was not found, activate the first window on workspace.
const workspace = wom.get_workspace_by_index(workspace_id)
if (workspace) {
for (const win of workspace.list_windows()) {
const window = this.get_window(win)
if (window && !window.meta.minimized) {
activate_window(window)
return
}
}
}
})
}
on_destroy(win: Entity) {
// Exit tiling adjustment mode on window destroy.
if (this.tiler.window !== null && win == this.tiler.window) this.tiler.exit(this)
const [prev_a, prev_b] = this.prev_focused
if (prev_a && Ecs.entity_eq(win, prev_a)) {
this.prev_focused[0] = null
} else if (prev_b && Ecs.entity_eq(win, prev_b)) {
this.prev_focused[1] = this.prev_focused[0]
this.prev_focused[0] = null
}
const window = this.windows.get(win);
if (!window) return;
const stack = window.stack
window.destroying = true;
// Disconnect all signals on this window
this.window_signals.take_with(win, (signals) => {
for (const signal of signals) {
window.meta.disconnect(signal);
}
});
if (this.auto_tiler) {
const entity = this.auto_tiler.attached.get(win);
if (entity) {
const fork = this.auto_tiler.forest.forks.get(entity);
if (fork?.right?.is_window(win)) {
const entity = fork.right.inner.kind === 3
? fork.right.inner.entities[0]
: fork.right.inner.entity;
this.windows.with(entity, (sibling) => sibling.activate())
}
}
}
if (this.auto_tiler) this.auto_tiler.detach_window(this, win);
// If destroyed window belonged to a stack, ensure that the next window
// to be focused is also a window in the same stack
if (this.auto_tiler && stack !== null) {
const stack_object = this.auto_tiler.forest.stacks.get(stack)
const prev = this.prev_focused[1]
if (stack_object && prev) {
const prev_window = this.windows.get(prev)
if (prev_window) {
if (prev_window.stack !== stack) {
stack_object.auto_activate()
this.prev_focused = [null, stack_object.active]
this.windows.get(stack_object.active)?.activate()
}
}
}
}
this.movements.remove(win)
this.windows.remove(win)
this.delete_entity(win);
}
on_display_move(_from_id: number, _to_id: number) {
if (!this.auto_tiler) return;
}
/** Triggered when a window has been focused */
on_focused(win: Window.ShellWindow) {
this.workspace_active.set(this.active_workspace(), win.entity)
scheduler.setForeground(win.meta)
this.size_signals_unblock(win);
if (this.exception_selecting) {
this.exception_add(win)
}
// Track history of focused windows, but do not permit duplicates.
if (this.prev_focused[1] !== win.entity) {
this.prev_focused[0] = this.prev_focused[1];
this.prev_focused[1] = win.entity;
}
// Update the active tab in the stack.
if (null !== this.auto_tiler && null !== win.stack) {
ext?.auto_tiler?.forest.stacks.get(win.stack)?.activate(win.entity)
}
this.unmaximize_workspace(win)
this.show_border_on_focused()
if (this.auto_tiler && win.is_tilable(this) && this.prev_focused[0] !== null) {
let prev = this.windows.get(this.prev_focused[0]);
let is_attached = this.auto_tiler.attached.contains(this.prev_focused[0]);
if (prev && prev !== win && is_attached && prev.actor_exists() && prev.name(this) !== win.name(this) && prev.workspace_id() === win.workspace_id()) {
if (prev.rect().contains(win.rect())) {
if (prev.is_maximized()) {
prev.meta.unmaximize(Meta.MaximizeFlags.BOTH);
}
} else if (prev.stack) {
prev.meta.unmaximize(Meta.MaximizeFlags.BOTH)
this.auto_tiler.forest.stacks.get(prev.stack)?.restack()
}
}
}
if (this.conf.log_on_focus) {
let msg = `focused Window(${win.entity}) {\n`
+ ` class: "${win.meta.get_wm_class()}",\n`
+ ` cmdline: ${win.cmdline()},\n`
+ ` monitor: ${win.meta.get_monitor()},\n`
+ ` name: ${win.name(this)},\n`
+ ` rect: ${win.rect().fmt()},\n`
+ ` workspace: ${win.workspace_id()},\n`
+ ` xid: ${win.xid()},\n`
+ ` stack: ${win.stack},\n`
if (this.auto_tiler) {
msg += ` fork: (${this.auto_tiler.attached.get(win.entity)}),\n`;
}
log.debug(msg + '}');
}
}
on_tile_attach(entity: Entity, window: Entity) {
if (this.auto_tiler) {
if (!this.auto_tiler.attached.contains(window)) {
this.windows.with(window, (w) => {
if (w.prev_rect === null) {
w.prev_rect = w.meta.get_frame_rect();
}
})
}
this.auto_tiler.attached.insert(window, entity);
}
}
on_tile_detach(win: Entity) {
this.windows.with(win, (window) => {
if (window.prev_rect && !window.ignore_detach) {
this.register(Events.window_move(this, window, window.prev_rect));
window.prev_rect = null;
}
})
}
show_border_on_focused() {
this.hide_all_borders();
const focus = this.focus_window()
if (focus) focus.show_border()
}
hide_all_borders() {
for (const win of this.windows.values()) {
win.hide_border();
}
}
maximized_on_active_display(): boolean {
const aws = this.workspace_id();
for (const window of this.windows.values()) {
if (!window.actor_exists()) continue;
const wws = this.workspace_id(window);
if (aws[0] === wws[0] && aws[1] === wws[1]) {
if (window.is_maximized()) return true
}
}
return false;
}
on_gap_inner() {
let current = this.settings.gap_inner();
this.set_gap_inner(current);
let prev_gap = this.gap_inner_prev / 4 / this.dpi;
if (current != prev_gap) {
this.update_inner_gap()
Gio.Settings.sync();
}
}
update_inner_gap() {
if (this.auto_tiler) {
for (const [entity,] of this.auto_tiler.forest.toplevel.values()) {
const fork = this.auto_tiler.forest.forks.get(entity);
if (fork) {
this.auto_tiler.tile(this, fork, fork.area);
}
}
} else {
this.update_snapped();
}
}
/** Unmaximize any maximized windows on the same workspace. */
unmaximize_workspace(win: Window.ShellWindow) {
if (this.auto_tiler) {
let mon
let work