feat: first draft of everything working single monitor with just mouse commands

This commit is contained in:
Lucas Oskorep
2025-04-18 01:57:29 -04:00
parent 185a8e233c
commit e1e240924a
12 changed files with 545 additions and 376 deletions

0
src/monitor.ts Normal file
View File

View File

@@ -1,20 +0,0 @@
// Utility functions and type definitions
/**
* Interface for the extension settings
*/
export interface ExtensionSettings {
keybinding1: string[];
keybinding2: string[];
keybinding3: string[];
keybinding4: string[];
dropdownOption: string;
colorSelection: string;
}
/**
* Log a message with the extension name prefix
*/
export function log(message: string): void {
console.log(`[MyExtension] ${message}`);
}

30
src/utils/logger.ts Normal file
View File

@@ -0,0 +1,30 @@
export class Logger {
static fatal(...args: any[]) {
console.log(`[Aerospike] [FATAL]`, ...args);
}
static error(...args: any[]) {
console.log(`[Aerospike] [ERROR]`, ...args);
}
static warn(...args: any[]) {
console.log(`[Aerospike] [WARN]`, ...args);
}
static info(...args: any[]) {
console.log(`[Aerospike] [INFO]`, ...args);
}
static debug(...args: any[]) {
console.log(`[Aerospike] [DEBUG]`, ...args);
}
static trace(...args: any[]) {
console.log(`[Aerospike] [TRACE]`, ...args);
}
static log(...args: any[]) {
console.log(`[Aerospike] [LOG]`, ...args);
}
}

219
src/window.ts Normal file
View File

@@ -0,0 +1,219 @@
import Meta from 'gi://Meta';
import GLib from "gi://GLib";
import Clutter from "gi://Clutter";
import {IWindowManager} from "./windowManager.js";
import {Logger} from "./utils/logger.js";
export type Signal = {
name: string;
id: number;
}
export class WindowWrapper {
_window: Meta.Window;
_signals: Signal[];
constructor(window: Meta.Window) {
this._window = window;
this._signals = [];
}
connectWindowSignals(
windowManager: IWindowManager,
): void {
const windowId = this._window.get_id();
// Handle window destruction
const destroyId = this._window.connect('unmanaging', () => {
Logger.log("REMOVING WINDOW", windowId);
windowManager.handleWindowClosed(windowId)
});
this._signals.push({name: 'unmanaging', id: destroyId});
// Handle focus changes
const focusId = this._window.connect('notify::has-focus', () => {
if (this._window.has_focus()) {
windowManager._activeWindowId = windowId;
}
});
this._signals.push({name: 'notify::has-focus', id: focusId});
// Track window movement using position-changed signal
let lastPositionChangeTime = 0;
let dragInProgress = false;
// const positionChangedId = this._window.connect('position-changed', window => {
// Logger.log("position-changed", window.get_id());
// Logger.log(window.get_monitor())
// // const currentTime = Date.now();
// // const [x, y, _] = global.get_pointer();
// //
// // // If this is the first move or it's been a while since the last move, consider it the start of a drag
// // if (!dragInProgress) {
// // dragInProgress = true;
// // Logger.log(`Window drag started for window ${windowId}. Mouse position: ${x}, ${y}`);
// // }
// //
// // // Update the time of the last position change
// // lastPositionChangeTime = currentTime;
// //
// // // Set a timeout to detect when dragging stops (when position changes stop coming in)
// // GLib.timeout_add(GLib.PRIORITY_DEFAULT, 300, () => {
// // const timeSinceLastMove = Date.now() - lastPositionChangeTime;
// // // If it's been more than 200ms since the last move and we were dragging, consider the drag ended
// // if (timeSinceLastMove >= 200 && dragInProgress) {
// // dragInProgress = false;
// // const [endX, endY, _] = global.get_pointer();
// // Logger.log(`Window drag ended for window ${windowId}. Mouse position: ${endX}, ${endY}`);
// // }
// // return GLib.SOURCE_REMOVE; // Remove the timeout
// // });
// });
// this._signals.push({name: 'position-changed', id: positionChangedId});
// Handle minimization
const minimizeId = this._window.connect('notify::minimized', () => {
if (this._window.minimized) {
Logger.log(`Window minimized: ${windowId}`);
// Remove window from managed windows temporarily
// windowManager.removeFromTree(this._window);
// If this was the active window, find a new one
windowManager.syncActiveWindow()
// Retile remaining windows
windowManager._tileWindows();
} else if (!this._window.minimized) {
Logger.log(`Window unminimized: ${windowId}`);
windowManager.addWindow(this._window);
}
});
this._signals.push({name: 'notify::minimized', id: minimizeId});
// Handle maximization
const maximizeId = this._window.connect('notify::maximized-horizontally', () => {
if (this._window.get_maximized()) {
Logger.log(`Window maximized: ${windowId}`);
} else {
Logger.log(`Window unmaximized: ${windowId}`);
}
});
this._signals.push({name: 'notify::maximized-horizontally', id: maximizeId});
}
disconnectWindowSignals(): void {
// Disconnect signals
if (this._signals) {
this._signals.forEach(signal => {
try {
if (this._window != null) {
this._window.disconnect(signal.id);
}
} catch (e) {
// Window might already be gone
}
});
}
}
resizeWindow(x: number, y: number, width: number, height: number) {
// First, ensure window is not maximized or fullscreen
if (this._window.get_maximized()) {
Logger.log("WINDOW MAXIMIZED")
this._window.unmaximize(Meta.MaximizeFlags.BOTH);
}
if (this._window.is_fullscreen()) {
Logger.log("WINDOW IS FULLSCREEN")
this._window.unmake_fullscreen();
}
Logger.log("WINDOW", this._window.get_window_type(), this._window.allows_move());
Logger.log("MONITOR INFO", getUsableMonitorSpace(this._window));
Logger.log("NEW_SIZE", x, y, width, height);
// win.move_resize_frame(false, 50, 50, 300, 300);
this._window.move_resize_frame(false, x, y, width, height);
Logger.log("RESIZED WINDOW", this._window.get_frame_rect().height, this._window.get_frame_rect().width, this._window.get_frame_rect().x, this._window.get_frame_rect().y);
}
safelyResizeWindow(x: number, y: number, width: number, height: number): void {
Logger.log("SAFELY RESIZE", x, y, width, height);
const actor = this._window.get_compositor_private();
if (!actor) {
Logger.log("No actor available, can't resize safely yet");
return;
}
// Set a flag to track if the resize has been done
let resizeDone = false;
// Connect to the first-frame signal
const id = actor.connect('first-frame', () => {
// Disconnect the signal handler
actor.disconnect(id);
if (!resizeDone) {
resizeDone = true;
// Add a small delay
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 50, () => {
try {
this.resizeWindow(x, y, width, height);
} catch (e) {
console.error("Error resizing window:", e);
}
return GLib.SOURCE_feaREMOVE;
});
}
});
// Fallback timeout in case the first-frame signal doesn't fire
// (for windows that are already mapped)
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 50, () => {
if (!resizeDone) {
resizeDone = true;
try {
this.resizeWindow(x, y, width, height);
} catch (e) {
console.error("Error resizing window (fallback):", e);
}
}
return GLib.SOURCE_REMOVE;
});
}
// if (!this._window) return;
// this._window.unmaximize(Meta.MaximizeFlags.HORIZONTAL);
// this._window.unmaximize(Meta.MaximizeFlags.VERTICAL);
// this._window.unmaximize(Meta.MaximizeFlags.BOTH);
//
// let windowActor = this._window.get_compositor_private() as Clutter.Actor;
// if (!windowActor) return;
// windowActor.remove_all_transitions();
//
// this._window.move_frame(true, x, y);
// this._window.move_resize_frame(true, x, y, width, height);
}
function getUsableMonitorSpace(window: Meta.Window) {
// Get the current workspace
const workspace = window.get_workspace();
// Get the monitor index that this window is on
const monitorIndex = window.get_monitor();
// Get the work area
const workArea = workspace.get_work_area_for_monitor(monitorIndex);
return {
x: workArea.x,
y: workArea.y,
width: workArea.width,
height: workArea.height
};
}

274
src/windowManager.ts Normal file
View File

@@ -0,0 +1,274 @@
import Meta from "gi://Meta";
import {WindowWrapper} from './window.js';
import Mtk from "@girs/mtk-16";
import {Logger} from "./utils/logger.js";
export interface IWindowManager {
_activeWindowId: number | null;
_tileWindows(): void;
addWindow(window: Meta.Window): void;
handleWindowClosed(windowId: number): void;
// removeFromTree(window: Meta.Window): void;
syncActiveWindow(): number | null;
}
export default class WindowManager implements IWindowManager {
_focusSignal: number | null;
_displaySignals: number[];
_windowCreateId: number | null;
_windows: Map<number, WindowWrapper>;
_activeWindowId: number | null;
constructor() {
this._focusSignal = null;
this._windowCreateId = null;
this._displaySignals = [];
this._windows = new Map<number, WindowWrapper>();
this._activeWindowId = null;
}
public enable(): void {
Logger.log("Starting Aerospike Window Manager");
this.captureExistingWindows();
// Connect window signals
this._windowCreateId = global.display.connect(
'window-created',
(display, window) => {
this.handleWindowCreated(display, window);
}
);
this.instantiateDisplaySignals()
}
instantiateDisplaySignals(): void {
this._displaySignals.push(
global.display.connect("grab-op-begin", (display, window, op) => {
this.handleGrabOpBegin(display, window, op)
}),
global.display.connect("grab-op-end", (display, window, op) => {
this.handleGrabOpEnd(display, window, op)
})
)
}
handleGrabOpBegin(display: Meta.Display, window: Meta.Window, op: Meta.GrabOp): void {
Logger.log("Grab Op Start");
Logger.log(display, window, op)
Logger.log(window.get_monitor())
}
handleGrabOpEnd(display: Meta.Display, window: Meta.Window, op: Meta.GrabOp): void {
Logger.log("Grab Op End ");
Logger.log(display, window, op)
this._tileWindows();
}
public disable(): void {
Logger.log("DISABLED AEROSPIKE WINDOW MANAGER!")
// Disconnect the focus signal and remove any existing borders
if (this._focusSignal) {
global.display.disconnect(this._focusSignal);
this._focusSignal = null;
}
}
public captureExistingWindows() {
Logger.log("CAPTURING WINDOWS")
const workspace = global.workspace_manager.get_active_workspace();
const windows = global.display.get_tab_list(Meta.TabList.NORMAL, workspace);
Logger.log("WINDOWS", windows);
windows.forEach(window => {
if (this._isWindowTileable(window)) {
this.addWindow(window);
}
});
this._tileWindows();
}
handleWindowCreated(display: Meta.Display, window: Meta.Window) {
Logger.log("WINDOW CREATED ON DISPLAY", window, display);
if (!this._isWindowTileable(window)) {
return;
}
Logger.log("WINDOW IS TILABLE");
const actor = window.get_compositor_private();
if (!actor) {
return;
}
this.addWindow(window);
}
/**
* Handle window closed event
*/
handleWindowClosed(windowId: number): void {
Logger.log("closing window", windowId);
const window = this._windows.get(windowId);
if (!window) {
return;
}
window.disconnectWindowSignals()
// Remove from managed windows
this._windows.delete(windowId);
this.syncActiveWindow();
// Retile remaining windows
this._tileWindows();
}
public addWindow(window: Meta.Window) {
const windowId = window.get_id();
Logger.log("ADDING WINDOW", window);
var wrapper = new WindowWrapper(window)
wrapper.connectWindowSignals(this)
// Add window to managed windows
this._windows.set(windowId, wrapper);
// If this is the first window, make it the active one
if (this._windows.size === 1 || window.has_focus()) {
this._activeWindowId = windowId;
}
this._tileWindows();
}
// public UnmanageWindow(window: Meta.Window) {
// this._windows.delete(window.get_id());
// this._unmanagedWindows.add(window.get_id())
// }
//
// public ManageWindow(window: Meta.Window) {
// this._windows.set(window.get_id(), {
// window,
// })
// }
/**
* 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 {
// // Get the active workspace
// const workspace = global.workspace_manager.get_active_workspace();
//
// // Check if there is an active window
// const activeWindow = global.display.get_focus_window();
//
// if (!activeWindow) {
// Logger.log("No active window found in GNOME");
// this._activeWindowId = null;
// return null;
// }
//
// // Get the window ID
// const windowId = activeWindow.get_id();
//
// // Check if this window is being managed by our extension
// if (this._windows.has(windowId)) {
// Logger.log(`Setting active window to ${windowId}`);
// this._activeWindowId = windowId;
// return windowId;
// } else {
// Logger.log(`Window ${windowId} is not managed by this extension`);
//
// // Try to find a managed window on the current workspace to make active
// const managedWindows = Array.from(this._windows.entries())
// .filter(([_, wrapper]) =>
// wrapper.window && wrapper.window.get_workspace() === workspace);
//
// if (managedWindows.length > 0) {
// // Take the first managed window on this workspace
// const firstWindowId = managedWindows[0][0];
// Logger.log(`Using managed window ${firstWindowId} as active instead`);
// this._activeWindowId = firstWindowId;
// return firstWindowId;
// }
//
// // No managed windows on this workspace
// Logger.log("No managed windows found on the active workspace");
// this._activeWindowId = null;
// return null;
// }
return null;
}
_isWindowTileable(window: Meta.Window) {
if (!window || !window.get_compositor_private()) {
return false;
}
const windowType = window.get_window_type();
Logger.log("WINDOW TYPE", windowType);
// Skip certain types of windows
return !window.is_skip_taskbar() &&
windowType !== Meta.WindowType.DESKTOP &&
windowType !== Meta.WindowType.DOCK &&
windowType !== Meta.WindowType.DIALOG &&
windowType !== Meta.WindowType.MODAL_DIALOG &&
windowType !== Meta.WindowType.UTILITY &&
windowType !== Meta.WindowType.MENU;
}
_tileWindows() {
Logger.log("TILING WINDOWS")
const workspace = global.workspace_manager.get_active_workspace();
const workArea = workspace.get_work_area_for_monitor(
global.display.get_primary_monitor()
);
Logger.log("Workspace", workspace);
Logger.log("WorkArea", workArea);
// Get all windows for current workspace
const windows = Array.from(this._windows.values())
.filter(({_window}) => {
if (_window != null) {
return _window.get_workspace() === workspace;
}
})
.map(x => x);
if (windows.length === 0) {
return;
}
this._tileHorizontally(windows, workArea)
}
_tileHorizontally(windows: (WindowWrapper | null)[], workArea: Mtk.Rectangle) {
const windowWidth = Math.floor(workArea.width / windows.length);
windows.forEach((window, index) => {
const x = workArea.x + (index * windowWidth);
const rect = {
x: x,
y: workArea.y,
width: windowWidth,
height: workArea.height
};
if (window != null) {
window.safelyResizeWindow(rect.x, rect.y, rect.width, rect.height);
}
});
}
}

0
src/workspace.ts Normal file
View File