-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathwindow.ts
608 lines (493 loc) · 18.6 KB
/
window.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
// @ts-ignore
const Me = imports.misc.extensionUtils.getCurrentExtension();
import * as Config from 'config';
import * as lib from 'lib';
import * as log from 'log';
import * as once_cell from 'once_cell';
import * as Rect from 'rectangle';
import * as Tags from 'tags';
import * as Tweener from 'tweener';
import * as utils from 'utils';
import * as xprop from 'xprop';
import type { Entity } from './ecs';
import type { Ext } from './extension';
import type { Rectangle } from './rectangle';
const { DefaultPointerPosition } = Config;
const { Gdk, Meta, Shell, St, GLib } = imports.gi;
const { OnceCell } = once_cell;
export var window_tracker = Shell.WindowTracker.get_default();
const WM_TITLE_BLACKLIST: Array<string> = [
'Firefox',
'Nightly', // Firefox Nightly
'Tor Browser'
];
enum RESTACK_STATE {
RAISED,
WORKSPACE_CHANGED,
NORMAL
}
enum RESTACK_SPEED {
RAISED = 430,
WORKSPACE_CHANGED = 300,
NORMAL = 200
}
interface X11Info {
normal_hints: once_cell.OnceCell<lib.SizeHint | null>;
wm_role_: once_cell.OnceCell<string | null>;
xid_: once_cell.OnceCell<string | null>;
}
export class ShellWindow {
entity: Entity;
meta: Meta.Window;
ext: Ext;
stack: number | null = null;
known_workspace: number;
grab: boolean = false;
activate_after_move: boolean = false;
ignore_detach: boolean = false;
was_attached_to?: [Entity, boolean | number];
// Awaiting reassignment after a display update
reassignment: boolean = false
// True if this window is currently smart-gapped
smart_gapped: boolean = false;
border: St.Bin = new St.Bin({ style_class: 'pop-shell-active-hint pop-shell-border-normal' });
prev_rect: null | Rectangular = null;
private was_hidden: boolean = false;
private window_app: any;
private extra: X11Info = {
normal_hints: new OnceCell(),
wm_role_: new OnceCell(),
xid_: new OnceCell()
};
private border_size = 0;
constructor(entity: Entity, window: Meta.Window, window_app: any, ext: Ext) {
this.window_app = window_app;
this.entity = entity;
this.meta = window;
this.ext = ext;
this.known_workspace = this.workspace_id()
if (this.may_decorate()) {
if (!window.is_client_decorated()) {
if (ext.settings.show_title()) {
this.decoration_show(ext);
} else {
this.decoration_hide(ext);
}
}
}
this.bind_window_events();
this.bind_hint_events();
global.window_group.add_child(this.border);
this.hide_border();
this.restack();
this.update_border_layout();
if (this.meta.get_compositor_private()?.get_stage())
this.on_style_changed();
}
activate(): void {
activate(this.ext.conf.default_pointer_position, this.meta);
}
actor_exists(): boolean {
return this.meta.get_compositor_private() !== null;
}
private bind_window_events() {
this.ext.window_signals.get_or(this.entity, () => new Array())
.push(
this.meta.connect('size-changed', () => { this.window_changed() }),
this.meta.connect('position-changed', () => { this.window_changed() }),
this.meta.connect('workspace-changed', () => { this.workspace_changed() }),
this.meta.connect('raised', () => { this.window_raised() }),
);
}
private bind_hint_events() {
let settings = this.ext.settings;
let change_id = settings.ext.connect('changed', (_, key) => {
if (this.border) {
if (key === 'hint-color-rgba') {
this.update_hint_colors();
}
}
return false;
});
this.border.connect('destroy', () => { settings.ext.disconnect(change_id) });
this.border.connect('style-changed', () => {
this.on_style_changed();
});
this.update_hint_colors();
}
/**
* Adjust the colors for:
* - border hint
* - overlay
*/
private update_hint_colors() {
let settings = this.ext.settings;
const color_value = settings.hint_color_rgba();
if (this.ext.overlay) {
const gdk = new Gdk.RGBA();
// TODO Probably move overlay color/opacity to prefs.js in future,
// For now mimic the hint color with lower opacity
const overlay_alpha = 0.3;
const orig_overlay = 'rgba(53, 132, 228, 0.3)';
gdk.parse(color_value);
if (utils.is_dark(gdk.to_string())) {
// too dark, use the blue overlay
gdk.parse(orig_overlay);
}
gdk.alpha = overlay_alpha
this.ext.overlay.set_style(`background: ${gdk.to_string()}`);
}
if (this.border)
this.border.set_style(`border-color: ${color_value}`);
}
cmdline(): string | null {
let pid = this.meta.get_pid(), out = null;
if (-1 === pid) return out;
const path = '/proc/' + pid + '/cmdline';
if (!utils.exists(path)) return out;
const result = utils.read_to_string(path);
if (result.kind == 1) {
out = result.value.trim();
} else {
log.error(`failed to fetch cmdline: ${result.value.format()}`);
}
return out;
}
private decoration(_ext: Ext, callback: (xid: string) => void): void {
if (this.may_decorate()) {
const xid = this.xid();
if (xid) callback(xid);
}
}
decoration_hide(ext: Ext): void {
if (this.ignore_decoration()) return;
this.was_hidden = true;
this.decoration(ext, (xid) => xprop.set_hint(xid, xprop.MOTIF_HINTS, xprop.HIDE_FLAGS));
}
decoration_show(ext: Ext): void {
if (!this.was_hidden) return;
this.decoration(ext, (xid) => xprop.set_hint(xid, xprop.MOTIF_HINTS, xprop.SHOW_FLAGS));
}
icon(_ext: Ext, size: number): any {
let icon = this.window_app.create_icon_texture(size);
if (!icon) {
icon = new St.Icon({
icon_name: 'applications-other',
icon_type: St.IconType.FULLCOLOR,
icon_size: size
});
}
return icon;
}
ignore_decoration(): boolean {
const name = this.meta.get_wm_class();
if (name === null) return true;
return WM_TITLE_BLACKLIST.findIndex((n) => name.startsWith(n)) !== -1;
}
is_maximized(): boolean {
return this.meta.get_maximized() !== 0;
}
/**
* Window is maximized, 0 gapped or smart gapped
*/
is_max_screen(): boolean {
// log.debug(`title: ${this.meta.get_title()}`);
// log.debug(`max: ${this.is_maximized()}, 0-gap: ${this.ext.settings.gap_inner() === 0}, smart: ${this.smart_gapped}`);
return this.is_maximized() || this.ext.settings.gap_inner() === 0 || this.smart_gapped;
}
is_tilable(ext: Ext): boolean {
let tile_checks = () => {
let wm_class = this.meta.get_wm_class();
return !this.meta.is_skip_taskbar()
// Only normal windows will be considered for tiling
&& this.meta.window_type == Meta.WindowType.NORMAL
// Transient windows are most likely dialogs
&& !this.is_transient()
// If a window lacks a class, it's probably an web browser dialog
&& wm_class !== null
// Blacklist any windows that happen to leak through our filter
&& !ext.conf.window_shall_float(wm_class, this.meta.get_title());
};
return !ext.contains_tag(this.entity, Tags.Floating)
&& tile_checks()
}
is_transient(): boolean {
return this.meta.get_transient_for() !== null;
}
may_decorate(): boolean {
const xid = this.xid();
return xid ? xprop.may_decorate(xid) : false;
}
move(ext: Ext, rect: Rectangular, on_complete?: () => void, animate: boolean = true) {
if ((!this.same_workspace() && this.is_maximized())) {
return
}
this.hide_border();
const clone = Rect.Rectangle.from_meta(rect);
const meta = this.meta;
const actor = meta.get_compositor_private();
if (actor) {
meta.unmaximize(Meta.MaximizeFlags.HORIZONTAL);
meta.unmaximize(Meta.MaximizeFlags.VERTICAL);
meta.unmaximize(Meta.MaximizeFlags.HORIZONTAL | Meta.MaximizeFlags.VERTICAL);
const entity_string = String(this.entity);
ext.movements.insert(this.entity, clone);
const onComplete = () => {
ext.register({ tag: 2, window: this, kind: { tag: 1 } });
if (on_complete) ext.register_fn(on_complete);
ext.tween_signals.delete(entity_string);
if (meta.appears_focused) {
this.update_border_layout();
this.update_border_state();
}
};
if (animate && ext.animate_windows && !ext.init) {
const current = meta.get_frame_rect();
const buffer = meta.get_buffer_rect();
const dx = current.x - buffer.x;
const dy = current.y - buffer.y;
const slot = ext.tween_signals.get(entity_string);
if (slot !== undefined) {
const [signal, callback] = slot;
Tweener.remove(actor);
utils.source_remove(signal);
callback();
}
const x = clone.x - dx;
const y = clone.y - dy;
const duration = ext.tiler.moving ? 49 : 149;
Tweener.add(this, { x, y, duration, mode: null });
ext.tween_signals.set(entity_string, [
Tweener.on_window_tweened(this, onComplete),
onComplete
]);
} else {
onComplete();
}
}
}
name(ext: Ext): string {
return ext.names.get_or(this.entity, () => "unknown");
}
private on_style_changed() {
this.border_size = this.border.get_theme_node().get_border_width(St.Side.TOP);
}
rect(): Rectangle {
return Rect.Rectangle.from_meta(this.meta.get_frame_rect());
}
size_hint(): lib.SizeHint | null {
return this.extra.normal_hints.get_or_init(() => {
const xid = this.xid();
return xid ? xprop.get_size_hints(xid) : null;
});
}
swap(ext: Ext, other: ShellWindow): void {
let ar = this.rect().clone();
let br = other.rect().clone();
other.move(ext, ar);
this.move(ext, br, () => place_pointer_on(this.ext.conf.default_pointer_position, this.meta));
}
wm_role(): string | null {
return this.extra.wm_role_.get_or_init(() => {
const xid = this.xid();
return xid ? xprop.get_window_role(xid) : null
});
}
workspace_id(): number {
const workspace = this.meta.get_workspace();
if (workspace) {
return workspace.index();
} else {
this.meta.change_workspace_by_index(0, false);
return 0;
}
}
xid(): string | null {
return this.extra.xid_.get_or_init(() => {
if (utils.is_wayland()) return null;
return xprop.get_xid(this.meta);
})
}
update_border_state() {
this.restack();
if (this.ext.settings.active_hint()) {
let border = this.border;
if (!this.meta.is_fullscreen() &&
!this.is_maximized() &&
!this.meta.minimized &&
this.same_workspace() &&
this.meta.appears_focused) {
border.show();
}
else {
border.hide();
}
}
}
same_workspace() {
const workspace = this.meta.get_workspace();
if (workspace) {
let workspace_id = workspace.index();
return workspace_id === global.workspace_manager.get_active_workspace_index()
}
return false;
}
/**
* Sort the window group/always top group with each window border
* @param updateState NORMAL, RAISED, WORKSPACE_CHANGED
*/
restack(updateState: RESTACK_STATE = RESTACK_STATE.NORMAL) {
this.update_border_layout();
if (this.meta.is_fullscreen()) {
this.hide_border()
}
let restackSpeed = RESTACK_SPEED.NORMAL;
switch (updateState) {
case RESTACK_STATE.NORMAL:
restackSpeed = RESTACK_SPEED.NORMAL
break;
case RESTACK_STATE.RAISED:
restackSpeed = RESTACK_SPEED.RAISED
break;
case RESTACK_STATE.WORKSPACE_CHANGED:
restackSpeed = RESTACK_SPEED.WORKSPACE_CHANGED
break;
}
GLib.timeout_add(GLib.PRIORITY_LOW, restackSpeed, () => {
let border = this.border;
let actor = this.meta.get_compositor_private();
let win_group = global.window_group;
if (actor && border && win_group) {
this.update_border_layout();
// move the border above the window group first
win_group.set_child_above_sibling(border, null);
if (this.always_top_windows.length > 0) {
// honor the always-top windows
for (const above_actor of this.always_top_windows) {
if (actor != above_actor) {
if (border.get_parent() === above_actor.get_parent()) {
win_group.set_child_below_sibling(border, above_actor);
}
}
}
// Move the border above the current window actor
if (border.get_parent() === actor.get_parent()) {
win_group.set_child_above_sibling(border, actor);
}
}
// Honor transient windows
for (const window of this.ext.windows.values()) {
const parent = window.meta.get_transient_for()
const window_actor = window.meta.get_compositor_private();
if (!parent || !window_actor) continue
const parent_actor = parent.get_compositor_private()
if (!parent_actor && parent_actor !== actor) continue
win_group.set_child_below_sibling(border, window_actor)
}
}
return false; // make sure it runs once
});
}
get always_top_windows(): Clutter.Actor[] {
let above_windows: Clutter.Actor[] = new Array();
for (const actor of global.get_window_actors()) {
if (actor && actor.get_meta_window() && actor.get_meta_window().is_above())
above_windows.push(actor);
}
return above_windows;
}
hide_border() {
let b = this.border;
if (b) b.hide();
}
update_border_layout() {
let rect = this.meta.get_frame_rect();
let border = this.border;
let borderSize = this.border_size;
if (border) {
if (!this.is_max_screen()) {
border.remove_style_class_name('pop-shell-border-maximize');
} else {
borderSize = 0;
border.add_style_class_name('pop-shell-border-maximize');
}
let stack_number = this.stack;
if (stack_number !== null) {
const stack = this.ext.auto_tiler?.forest.stacks.get(stack_number);
if (stack) {
let stack_tab_height = stack.tabs_height;
if (borderSize === 0 || this.grab) { // not in max screen state
stack_tab_height = 0;
}
border.set_position(rect.x - borderSize, rect.y - stack_tab_height - borderSize);
border.set_size(rect.width + 2 * borderSize, rect.height + stack_tab_height + 2 * borderSize);
}
} else {
border.set_position(rect.x - borderSize, rect.y - borderSize);
border.set_size(rect.width + (2 * borderSize), rect.height + (2 * borderSize));
}
}
}
private window_changed() {
this.update_border_layout();
this.update_border_state();
}
private window_raised() {
this.restack(RESTACK_STATE.RAISED);
this.update_border_state();
if (this.ext.conf.move_pointer_on_switch && !pointer_already_on_window(this.meta)) {
place_pointer_on(this.ext.conf.default_pointer_position, this.meta);
}
}
private workspace_changed() {
this.restack(RESTACK_STATE.WORKSPACE_CHANGED);
}
}
/// Activates a window, and moves the mouse point.
export function activate(default_pointer_position: Config.DefaultPointerPosition, win: Meta.Window) {
win.raise();
win.unminimize();
win.activate(global.get_current_time());
if (!pointer_already_on_window(win)) {
place_pointer_on(default_pointer_position, win)
}
}
export function place_pointer_on(default_pointer_position: Config.DefaultPointerPosition, win: Meta.Window) {
const rect = win.get_frame_rect();
let x = rect.x;
let y = rect.y;
switch (default_pointer_position) {
case DefaultPointerPosition.TopLeft:
x += 8;
y += 8;
break;
case DefaultPointerPosition.BottomLeft:
x += 8;
y += (rect.height - 16);
break;
case DefaultPointerPosition.TopRight:
x += (rect.width - 16);
y += 8;
break;
case DefaultPointerPosition.BottomRight:
x += (rect.width - 16);
y += (rect.height - 16);
break;
case DefaultPointerPosition.Center:
x += (rect.width / 2) + 8;
y += (rect.height / 2) + 8;
break;
default:
x += 8;
y += 8;
}
const display = Gdk.DisplayManager.get().get_default_display();
display
.get_default_seat()
.get_pointer()
.warp(display.get_default_screen(), x, y);
}
function pointer_already_on_window(meta: Meta.Window): boolean {
const cursor = lib.cursor_rect()
return cursor.intersects(meta.get_frame_rect())
}