Skip to content

Commit 24653cc

Browse files
committed
fix: Debounce focus triggers so windows are more likely to be placed where intended
1 parent 4fc8df1 commit 24653cc

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

src/extension.ts

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const wom = global.workspace_manager;
3535

3636
const Movement = movement.Movement;
3737

38+
const GLib: GLib = imports.gi.GLib;
39+
3840
const { Gio, Meta, St } = imports.gi;
3941
const { GlobalEvent, WindowEvent } = Events;
4042
const { cursor_rect, is_move_op } = Lib;
@@ -136,6 +138,8 @@ export class Ext extends Ecs.System<ExtEvent> {
136138
/** Record of misc. global objects and their attached signals */
137139
private signals: Map<GObject.Object, Array<SignalID>> = new Map();
138140

141+
/** Used to debounce on_focus triggers */
142+
private focus_trigger: null | SignalID = null;
139143

140144
// Entity-component associations
141145

@@ -283,7 +287,7 @@ export class Ext extends Ecs.System<ExtEvent> {
283287
}
284288
}
285289
}
286-
290+
287291
if (win.is_maximized()) {
288292
this.size_changed_block();
289293
win.meta.unmaximize(Meta.MaximizeFlags.BOTH);
@@ -584,8 +588,16 @@ export class Ext extends Ecs.System<ExtEvent> {
584588

585589
this.size_signals_unblock(win);
586590

587-
this.prev_focused = this.last_focused;
588-
this.last_focused = win.entity;
591+
// Keep the last-focused window from being shifted too quickly. 300ms debounce
592+
if (this.focus_trigger === null) {
593+
this.focus_trigger = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 300, () => {
594+
this.focus_trigger = null;
595+
return false;
596+
});
597+
598+
this.prev_focused = this.last_focused;
599+
this.last_focused = win.entity;
600+
}
589601

590602
function activate_in_stack(ext: Ext, stack: node.NodeStack, win: Window.ShellWindow) {
591603
ext.auto_tiler?.forest.stacks.get(stack.idx)?.activate(win.entity);
@@ -1496,36 +1508,36 @@ export class Ext extends Ecs.System<ExtEvent> {
14961508
}
14971509

14981510
auto_tile_on() {
1499-
const original = this.active_workspace();
1500-
1501-
let tiler = new auto_tiler.AutoTiler(
1502-
new Forest.Forest()
1503-
.connect_on_attach((entity: Entity, window: Entity) => {
1504-
tiler.attached.insert(window, entity);
1505-
}),
1506-
this.register_storage()
1507-
);
1511+
const original = this.active_workspace();
1512+
1513+
let tiler = new auto_tiler.AutoTiler(
1514+
new Forest.Forest()
1515+
.connect_on_attach((entity: Entity, window: Entity) => {
1516+
tiler.attached.insert(window, entity);
1517+
}),
1518+
this.register_storage()
1519+
);
15081520

1509-
this.auto_tiler = tiler;
1521+
this.auto_tiler = tiler;
15101522

1511-
this.settings.set_tile_by_default(true);
1512-
this.tiling_toggle_switch.setToggleState(true);
1513-
this.button.icon.gicon = this.button_gio_icon_auto_on; // type: Gio.Icon
1523+
this.settings.set_tile_by_default(true);
1524+
this.tiling_toggle_switch.setToggleState(true);
1525+
this.button.icon.gicon = this.button_gio_icon_auto_on; // type: Gio.Icon
15141526

1515-
for (const window of this.windows.values()) {
1516-
if (window.is_tilable(this)) {
1517-
let actor = window.meta.get_compositor_private();
1518-
if (actor) {
1519-
if (!window.meta.minimized) {
1520-
tiler.auto_tile(this, window, false);
1521-
}
1527+
for (const window of this.windows.values()) {
1528+
if (window.is_tilable(this)) {
1529+
let actor = window.meta.get_compositor_private();
1530+
if (actor) {
1531+
if (!window.meta.minimized) {
1532+
tiler.auto_tile(this, window, false);
15221533
}
15231534
}
15241535
}
1525-
1526-
this.register_fn(() => this.switch_to_workspace(original));
15271536
}
15281537

1538+
this.register_fn(() => this.switch_to_workspace(original));
1539+
}
1540+
15291541
unset_grab_op() {
15301542
if (this.grab_op !== null) {
15311543
let window = this.windows.get(this.grab_op.entity);

0 commit comments

Comments
 (0)