refactor: code review cleanup pass
All checks were successful
Build and Test / build (pull_request) Successful in 23s
Build and Test / release (pull_request) Has been skipped

- Collapse getHorizontalBounds/getVerticalBounds into _computeBounds(axis)
- Delete dead adjustBoundaryBothAxes method
- Collapse double-loop in getContainerForWindow into one pass
- Remove _findActiveContainer/_findContainerHoldingWindow duplication;
  call _findContainerForWindowAcrossMonitors directly
- Pass Gio.Settings via WindowManager constructor instead of public field
- Introduce keybindingActions() in extension.ts to unify setupKeybindings
  and refreshKeybinding; collapse 7-block bindSettings pattern into a loop
- Promote WindowWrapper.RESIZE_TOLERANCE to private static readonly
- Simplify redundant else-if in minimized handler
- Remove unused imports in monitor.ts (queueEvent, Mtk, Window alias)
- Deduplicate get_active_workspace() call in monitor.tileWindows()
- Strip redundant JSDoc and inline comments throughout
This commit is contained in:
Lucas Oskorep
2026-02-25 11:24:13 -05:00
parent 241f90299c
commit 22c9851886
6 changed files with 116 additions and 529 deletions

View File

@@ -1,10 +1,8 @@
import Meta from "gi://Meta";
import Gio from "gi://Gio";
// import GLib from "gi://GLib";
import {WindowWrapper} from './window.js';
import * as Main from "resource:///org/gnome/shell/ui/main.js";
// import Mtk from "@girs/mtk-16";
import {Logger} from "../utils/logger.js";
import Monitor from "./monitor.js";
import WindowContainer from "./container.js";
@@ -14,8 +12,6 @@ import {Rect} from "../utils/rect.js";
export interface IWindowManager {
_activeWindowId: number | null;
// addWindow(window: Meta.Window): void;
handleWindowClosed(winWrap: WindowWrapper): void;
handleWindowMinimized(winWrap: WindowWrapper): void;
@@ -30,8 +26,8 @@ export interface IWindowManager {
}
const _UNUSED_MONITOR_ID = -1
const _UNUSED_WINDOW_ID = -1
const _UNUSED_MONITOR_ID = -1;
const _UNUSED_WINDOW_ID = -1;
export default class WindowManager implements IWindowManager {
_displaySignals: number[] = [];
@@ -41,37 +37,33 @@ export default class WindowManager implements IWindowManager {
_activeWindowId: number | null = null;
_monitors: Map<number, Monitor> = new Map<number, Monitor>();
_minimizedItems: Map<number, WindowWrapper> = new Map<number, WindowWrapper>();
_grabbedWindowMonitor: number = _UNUSED_MONITOR_ID;
_grabbedWindowId: number = _UNUSED_WINDOW_ID;
_changingGrabbedMonitor: boolean = false;
_showingOverview: boolean = false;
// ── Resize-drag tracking ──────────────────────────────────────────────────
_isResizeDrag: boolean = false;
_resizeDragWindowId: number = _UNUSED_WINDOW_ID;
_resizeDragOp: Meta.GrabOp = Meta.GrabOp.NONE;
/** Mouse position at the start of each incremental resize step. */
_resizeDragLastMouseX: number = 0;
_resizeDragLastMouseY: number = 0;
/** Re-entrancy guard: true while tileWindows is propagating position-changed events. */
_isTiling: boolean = false;
_settings: Gio.Settings | null = null;
private readonly _settings: Gio.Settings;
constructor() {}
constructor(settings: Gio.Settings) {
this._settings = settings;
}
/** Returns the live min-ratio value from settings, falling back to 0.10. */
private _getMinRatio(): number {
return this._settings?.get_double('min-window-size-percent') ?? 0.10;
return this._settings.get_double('min-window-size-percent');
}
public enable(): void {
Logger.log("Starting Aerospike Window Manager");
// Connect window signals
this.instantiateDisplaySignals();
const mon_count = global.display.get_n_monitors();
@@ -80,8 +72,6 @@ export default class WindowManager implements IWindowManager {
}
this.captureExistingWindows();
// Sync the initially focused window
this.syncActiveWindow();
}
@@ -108,7 +98,6 @@ export default class WindowManager implements IWindowManager {
global.display.connect('notify::focus-window', () => {
this.syncActiveWindow();
}),
global.display.connect("showing-desktop-changed", () => {
Logger.log("SHOWING DESKTOP CHANGED");
}),
@@ -119,13 +108,7 @@ export default class WindowManager implements IWindowManager {
global.display.connect("in-fullscreen-changed", () => {
Logger.log("IN FULL SCREEN CHANGED");
}),
)
// this._windowManagerSignals = [
// global.window_manager.connect("show-tile-preview", (_, _metaWindow, _rect, _num) => {
// Logger.log("SHOW TITLE PREVIEW!")
// }),
// ];
);
this._workspaceManagerSignals = [
global.workspace_manager.connect("showing-desktop-changed", () => {
@@ -150,45 +133,31 @@ export default class WindowManager implements IWindowManager {
this._overviewSignals = [
Main.overview.connect("hiding", () => {
// this.fromOverview = true;
Logger.log("HIDING OVERVIEW")
this._showingOverview = false;
this._tileMonitors();
// const eventObj = {
// name: "focus-after-overview",
// callback: () => {
// Logger.log("FOCUSING AFTER OVERVIEW");
// },
// };
// this.queueEvent(eventObj);
}),
Main.overview.connect("showing", () => {
this._showingOverview = true;
Logger.log("SHOWING OVERVIEW");
}),
];
}
public disable(): void {
Logger.log("DISABLED AEROSPIKE WINDOW MANAGER!")
// Disconnect the focus signal and remove any existing borders
this.disconnectSignals();
this.removeAllWindows();
}
removeAllWindows(): void {
// Disconnect signals from minimized windows before clearing
this.disconnectMinimizedSignals();
this._minimizedItems.clear();
this._monitors.forEach((monitor: Monitor) => {
monitor.removeAllWindows();
})
}
disconnectSignals(): void {
this.disconnectDisplaySignals();
this.disconnectMonitorSignals();
@@ -222,10 +191,6 @@ export default class WindowManager implements IWindowManager {
})
}
/**
* Returns true if the grab op is a resize operation (any edge or corner).
*/
_isResizeOp(op: Meta.GrabOp): boolean {
return op === Meta.GrabOp.RESIZING_E ||
op === Meta.GrabOp.RESIZING_W ||
@@ -241,7 +206,6 @@ export default class WindowManager implements IWindowManager {
Logger.log("Grab Op Start", op);
if (this._isResizeOp(op)) {
// ── Resize drag ──────────────────────────────────────────────────
Logger.log("Resize drag begin, op=", op);
this._isResizeDrag = true;
this._resizeDragWindowId = window.get_id();
@@ -249,11 +213,8 @@ export default class WindowManager implements IWindowManager {
const [startMouseX, startMouseY] = global.get_pointer();
this._resizeDragLastMouseX = startMouseX;
this._resizeDragLastMouseY = startMouseY;
// Mark the window as dragging so safelyResizeWindow skips it while
// we tile the other windows in response to ratio changes.
this._getWrappedWindow(window)?.startDragging();
} else {
// ── Move drag (existing behaviour) ───────────────────────────────
this._getWrappedWindow(window)?.startDragging();
this._grabbedWindowMonitor = window.get_monitor();
this._grabbedWindowId = window.get_id();
@@ -264,19 +225,15 @@ export default class WindowManager implements IWindowManager {
Logger.log("Grab Op End ", op);
if (this._isResizeDrag) {
// ── Resize drag end ──────────────────────────────────────────────
Logger.log("Resize drag end, op=", op);
this._isResizeDrag = false;
this._resizeDragWindowId = _UNUSED_WINDOW_ID;
this._resizeDragLastMouseX = 0;
this._resizeDragLastMouseY = 0;
this._resizeDragOp = Meta.GrabOp.NONE;
// Stop suppressing the window, then snap everything to computed ratios
this._getWrappedWindow(window)?.stopDragging();
this._tileMonitors();
} else {
// ── Move drag end (existing behaviour) ───────────────────────────
Logger.log("primary display", display.get_primary_monitor())
this._grabbedWindowId = _UNUSED_WINDOW_ID;
this._getWrappedWindow(window)?.stopDragging();
this._tileMonitors();
@@ -288,9 +245,7 @@ export default class WindowManager implements IWindowManager {
let wrapped: WindowWrapper | undefined = undefined;
for (const monitor of this._monitors.values()) {
wrapped = monitor.getWindow(window.get_id());
if (wrapped !== undefined) {
break;
}
if (wrapped !== undefined) break;
}
return wrapped;
}
@@ -320,21 +275,13 @@ export default class WindowManager implements IWindowManager {
}
public handleWindowPositionChanged(winWrap: WindowWrapper): void {
// Ignore position changes that we triggered ourselves via tileWindows
if (this._isTiling) {
return;
}
if (this._changingGrabbedMonitor) {
return;
}
if (this._isTiling || this._changingGrabbedMonitor) return;
// ── Live resize-drag handling ─────────────────────────────────────────
if (this._isResizeDrag && winWrap.getWindowId() === this._resizeDragWindowId) {
this._handleResizeDragUpdate(winWrap);
return;
}
// ── Move-drag handling (existing behaviour) ───────────────────────────
if (winWrap.getWindowId() === this._grabbedWindowId) {
const [mouseX, mouseY, _] = global.get_pointer();
@@ -347,17 +294,14 @@ export default class WindowManager implements IWindowManager {
break;
}
}
if (monitorIndex === -1) {
return;
}
if (monitorIndex === -1) return;
if (monitorIndex !== this._grabbedWindowMonitor) {
this._changingGrabbedMonitor = true;
this._moveWindowToMonitor(winWrap.getWindow(), monitorIndex);
this._changingGrabbedMonitor = false;
}
// Guard _isTiling so that tileWindows() calls triggered by itemDragged
// (which repositions the displaced window) don't re-enter this handler.
this._isTiling = true;
try {
this._monitors.get(monitorIndex)?.itemDragged(winWrap, mouseX, mouseY);
@@ -367,29 +311,19 @@ export default class WindowManager implements IWindowManager {
}
}
/**
* Called on every position-changed event while a resize drag is in progress.
* Computes the pixel delta from the drag-start rect, maps it to the correct
* container boundary, and calls adjustBoundary() for live feedback.
*/
private _handleResizeDragUpdate(winWrap: WindowWrapper): void {
const op = this._resizeDragOp;
const winId = winWrap.getWindowId();
// Read the current mouse position — this is unclamped by the compositor
// and always reflects the true user intent, unlike the window's frame rect
// which gets clamped when adjacent windows block expansion.
const [mouseX, mouseY] = global.get_pointer();
const dx = mouseX - this._resizeDragLastMouseX;
const dy = mouseY - this._resizeDragLastMouseY;
if (dx === 0 && dy === 0) return;
// Update last position first so even if we return early the baseline advances
this._resizeDragLastMouseX = mouseX;
this._resizeDragLastMouseY = mouseY;
// Find the container that directly holds this window
const container = this._findContainerForWindowAcrossMonitors(winId);
if (!container) {
Logger.warn("_handleResizeDragUpdate: no container found for window", winId);
@@ -399,17 +333,10 @@ export default class WindowManager implements IWindowManager {
const itemIndex = container._getIndexOfWindow(winId);
if (itemIndex === -1) return;
const isHorizontal = container._orientation === 0; // Orientation.HORIZONTAL
// Map the mouse delta to the correct boundary.
//
// East/South edge → boundary AFTER the item (boundaryIndex = itemIndex)
// positive dx/dy grows this item, shrinks the next one.
// West/North edge → boundary BEFORE the item (boundaryIndex = itemIndex - 1)
// positive dx/dy moves the left edge right, growing the left neighbour
// and shrinking this item — so we negate the delta.
const isHorizontal = container._orientation === 0;
const minRatio = this._getMinRatio();
// E/S edge → boundary after the item; W/N edge → boundary before it.
let adjusted = false;
if (isHorizontal) {
if (op === Meta.GrabOp.RESIZING_E || op === Meta.GrabOp.RESIZING_NE || op === Meta.GrabOp.RESIZING_SE) {
@@ -425,8 +352,6 @@ export default class WindowManager implements IWindowManager {
}
}
// Tile all windows with the updated ratios, guarded so the resulting
// position-changed events don't re-enter this handler.
if (adjusted) {
this._isTiling = true;
try {
@@ -437,21 +362,16 @@ export default class WindowManager implements IWindowManager {
}
}
/**
* Searches all monitors for the WindowContainer that directly holds win_id.
*/
private _findContainerForWindowAcrossMonitors(winId: number): WindowContainer | null {
const activeWorkspaceIndex = global.workspace_manager.get_active_workspace().index();
for (const monitor of this._monitors.values()) {
if (activeWorkspaceIndex >= monitor._workspaces.length) continue;
const workspace = monitor._workspaces[activeWorkspaceIndex];
const container = workspace.getContainerForWindow(winId);
const container = monitor._workspaces[activeWorkspaceIndex].getContainerForWindow(winId);
if (container !== null) return container;
}
return null;
}
public handleWindowMinimized(winWrap: WindowWrapper): void {
const monitor_id = winWrap.getWindow().get_monitor()
this._minimizedItems.set(winWrap.getWindowId(), winWrap);
@@ -465,7 +385,6 @@ export default class WindowManager implements IWindowManager {
this._tileMonitors()
}
public handleWindowChangedWorkspace(winWrap: WindowWrapper): void {
const monitor = winWrap.getWindow().get_monitor();
this._monitors.get(monitor)?.removeWindow(winWrap);
@@ -484,41 +403,26 @@ export default class WindowManager implements IWindowManager {
this._tileMonitors();
}
handleWindowCreated(display: Meta.Display, window: Meta.Window) {
Logger.log("WINDOW CREATED ON DISPLAY", window, display);
if (!this._isWindowTileable(window)) {
return;
}
if (!this._isWindowTileable(window)) return;
Logger.log("WINDOW IS TILABLE");
this.addWindowToMonitor(window);
}
/**
* Handle window closed event
*/
handleWindowClosed(window: WindowWrapper): void {
const mon_id = window._window.get_monitor();
this._monitors.get(mon_id)?.removeWindow(window);
window.disconnectWindowSignals()
// Remove from managed windows
this.syncActiveWindow();
// Retile remaining windows
this._tileMonitors();
}
public addWindowToMonitor(window: Meta.Window) {
Logger.log("ADDING WINDOW TO MONITOR", window, window);
var wrapper = new WindowWrapper(window, (winWrap) => this.handleWindowMinimized(winWrap))
wrapper.connectWindowSignals(this);
this._addWindowWrapperToMonitor(wrapper);
}
_addWindowWrapperToMonitor(winWrap: WindowWrapper) {
@@ -544,7 +448,7 @@ export default class WindowManager implements IWindowManager {
"org.gnome.Shell.Extensions",
]
_isWindowTilingBlocked(window: Meta.Window) : boolean {
_isWindowTilingBlocked(window: Meta.Window): boolean {
Logger.info("title", window.get_title());
Logger.info("description", window.get_description());
Logger.info("class", window.get_wm_class());
@@ -559,17 +463,12 @@ export default class WindowManager implements IWindowManager {
}
_isWindowTileable(window: Meta.Window) {
if (!window || !window.get_compositor_private()) return false;
if (this._isWindowTilingBlocked(window)) return false;
if (!window || !window.get_compositor_private()) {
return false;
}
if (this._isWindowTilingBlocked(window)) {
return false;
}
const windowType = window.get_window_type();
Logger.log("WINDOW TILING CHECK",);
// Skip certain types of windows
return !window.is_skip_taskbar() &&
windowType !== Meta.WindowType.DESKTOP &&
windowType !== Meta.WindowType.DOCK &&
@@ -579,14 +478,6 @@ export default class WindowManager implements IWindowManager {
windowType !== Meta.WindowType.MENU;
}
/**
* Synchronizes the active window with GNOME's currently active window
*
* This function queries GNOME Shell for the current focused window and
* updates the extension's active window tracking to match.
*
* @returns The window ID of the active window, or null if no window is active
*/
public syncActiveWindow(): number | null {
const focusWindow = global.display.focus_window;
if (focusWindow) {
@@ -599,102 +490,33 @@ export default class WindowManager implements IWindowManager {
return this._activeWindowId;
}
/**
* Toggles the orientation of the active container (the container holding the active window)
*/
public toggleActiveContainerOrientation(): void {
if (this._activeWindowId === null) {
Logger.warn("No active window, cannot toggle container orientation");
return;
}
// Find the active window's container
const activeContainer = this._findActiveContainer();
if (activeContainer) {
activeContainer.toggleOrientation();
const container = this._findContainerForWindowAcrossMonitors(this._activeWindowId);
if (container) {
container.toggleOrientation();
} else {
Logger.warn("Could not find container for active window");
}
}
/**
* Resets all split ratios in the active window's container to equal fractions.
* Bound to Ctrl+Z by default.
*/
public resetActiveContainerRatios(): void {
if (this._activeWindowId === null) {
Logger.warn("No active window, cannot reset container ratios");
return;
}
const activeContainer = this._findActiveContainer();
if (activeContainer) {
const container = this._findContainerForWindowAcrossMonitors(this._activeWindowId);
if (container) {
Logger.info("Resetting container ratios to equal splits");
activeContainer.resetRatios();
container.resetRatios();
} else {
Logger.warn("Could not find container for active window");
}
}
/**
* Finds the container that directly contains the active window
* @returns The container holding the active window, or null if not found
*/
private _findActiveContainer(): WindowContainer | null {
if (this._activeWindowId === null) {
return null;
}
for (const monitor of this._monitors.values()) {
const activeWorkspaceIndex = global.workspace_manager.get_active_workspace().index();
// Bounds check to prevent accessing invalid workspace
if (activeWorkspaceIndex >= monitor._workspaces.length || activeWorkspaceIndex < 0) {
Logger.warn(`Active workspace index ${activeWorkspaceIndex} out of bounds for monitor with ${monitor._workspaces.length} workspaces`);
continue;
}
const workspace = monitor._workspaces[activeWorkspaceIndex];
// Check if the window is directly in the workspace container
const windowWrapper = workspace.getWindow(this._activeWindowId);
if (windowWrapper) {
// Try to find the parent container
const container = this._findContainerHoldingWindow(workspace, this._activeWindowId);
return container;
}
}
return null;
}
/**
* Recursively finds the container that directly contains a specific window
* @param container The container to search
* @param windowId The window ID to find
* @returns The container that directly contains the window, or null if not found
*/
private _findContainerHoldingWindow(container: WindowContainer, windowId: number): WindowContainer | null {
// Check if this container directly contains the window
for (const item of container._tiledItems) {
if (item instanceof WindowContainer) {
// Recursively search nested containers
const result = this._findContainerHoldingWindow(item, windowId);
if (result) {
return result;
}
} else if (item.getWindowId() === windowId) {
// Found it! Return this container as it directly holds the window
return container;
}
}
return null;
}
/**
* Prints the tree structure of all monitors, workspaces, containers, and windows to the logs
*/
public printTreeStructure(): void {
Logger.info("=".repeat(80));
Logger.info("WINDOW TREE STRUCTURE");
@@ -707,19 +529,15 @@ export default class WindowManager implements IWindowManager {
this._monitors.forEach((monitor: Monitor, monitorId: number) => {
const isActiveMonitor = this._activeWindowId !== null &&
monitor.getWindow(this._activeWindowId) !== undefined;
const monitorMarker = isActiveMonitor ? ' *' : '';
Logger.info(`Monitor ${monitorId}${monitorMarker}:`);
Logger.info(`Monitor ${monitorId}${isActiveMonitor ? ' *' : ''}:`);
Logger.info(` Work Area: x=${monitor._workArea.x}, y=${monitor._workArea.y}, w=${monitor._workArea.width}, h=${monitor._workArea.height}`);
monitor._workspaces.forEach((workspace, workspaceIndex) => {
const isActiveWorkspace = workspaceIndex === activeWorkspaceIndex;
const workspaceMarker = isActiveWorkspace && isActiveMonitor ? ' *' : '';
Logger.info(` Workspace ${workspaceIndex}${workspaceMarker}:`);
Logger.info(` Workspace ${workspaceIndex}${isActiveWorkspace && isActiveMonitor ? ' *' : ''}:`);
Logger.info(` Orientation: ${workspace._orientation === 0 ? 'HORIZONTAL' : 'VERTICAL'}`);
Logger.info(` Items: ${workspace._tiledItems.length}`);
this._printContainerTree(workspace, 4);
});
});
@@ -727,31 +545,20 @@ export default class WindowManager implements IWindowManager {
Logger.info("=".repeat(80));
}
/**
* Recursively prints the container tree structure
* @param container The container to print
* @param indentLevel The indentation level (number of spaces)
*/
private _printContainerTree(container: any, indentLevel: number): void {
private _printContainerTree(container: WindowContainer, indentLevel: number): void {
const indent = " ".repeat(indentLevel);
container._tiledItems.forEach((item: any, index: number) => {
container._tiledItems.forEach((item, index) => {
if (item instanceof WindowContainer) {
// Check if this container contains the active window
const containsActiveWindow = this._activeWindowId !== null &&
item.getWindow(this._activeWindowId) !== undefined;
const containerMarker = containsActiveWindow ? ' *' : '';
Logger.info(`${indent}[${index}] Container (${item._orientation === 0 ? 'HORIZONTAL' : 'VERTICAL'})${containerMarker}:`);
const containsActive = this._activeWindowId !== null &&
item.getWindow(this._activeWindowId) !== undefined;
Logger.info(`${indent}[${index}] Container (${item._orientation === 0 ? 'HORIZONTAL' : 'VERTICAL'})${containsActive ? ' *' : ''}:`);
Logger.info(`${indent} Items: ${item._tiledItems.length}`);
Logger.info(`${indent} Work Area: x=${item._workArea.x}, y=${item._workArea.y}, w=${item._workArea.width}, h=${item._workArea.height}`);
this._printContainerTree(item, indentLevel + 4);
} else {
const window = item.getWindow();
const isActiveWindow = this._activeWindowId === item.getWindowId();
const windowMarker = isActiveWindow ? ' *' : '';
Logger.info(`${indent}[${index}] Window ID: ${item.getWindowId()}${windowMarker}`);
Logger.info(`${indent}[${index}] Window ID: ${item.getWindowId()}${this._activeWindowId === item.getWindowId() ? ' *' : ''}`);
Logger.info(`${indent} Title: "${window.get_title()}"`);
Logger.info(`${indent} Class: ${window.get_wm_class()}`);
const rect = item.getRect();
@@ -759,6 +566,4 @@ export default class WindowManager implements IWindowManager {
}
});
}
}