From 11e1458180f7bff00869a9380cea04918620da40 Mon Sep 17 00:00:00 2001 From: Peter Fajdiga Date: Thu, 5 Sep 2024 00:54:56 +0200 Subject: [PATCH] call World methods outside of Actions --- src/lib/actions/Actions.ts | 361 +++++++++++++----------------- src/lib/keyBindings/definition.ts | 60 ++--- src/lib/keyBindings/loader.ts | 4 +- 3 files changed, 184 insertions(+), 241 deletions(-) diff --git a/src/lib/actions/Actions.ts b/src/lib/actions/Actions.ts index 44b6ded..76bef95 100644 --- a/src/lib/actions/Actions.ts +++ b/src/lib/actions/Actions.ts @@ -1,272 +1,215 @@ namespace Actions { export class Actions { constructor( - private readonly world: World, private readonly config: Actions.Config, ) {} - public "focus-left"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { + public "focus-left"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + const leftColumn = grid.getLeftColumn(column); + if (leftColumn === null) { + return; + } + leftColumn.focus(); + } + + public "focus-right"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + const rightColumn = grid.getRightColumn(column); + if (rightColumn === null) { + return; + } + rightColumn.focus(); + } + + public "focus-up"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + const aboveWindow = column.getAboveWindow(window); + if (aboveWindow === null) { + return; + } + aboveWindow.focus(); + } + + public "focus-down"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + const belowWindow = column.getBelowWindow(window); + if (belowWindow === null) { + return; + } + belowWindow.focus(); + } + + public "focus-start"(clientManager: ClientManager, desktopManager: DesktopManager) { + const grid = desktopManager.getCurrentDesktop().grid; + const firstColumn = grid.getFirstColumn(); + if (firstColumn === null) { + return; + } + firstColumn.focus(); + } + + public "focus-end"(clientManager: ClientManager, desktopManager: DesktopManager) { + const grid = desktopManager.getCurrentDesktop().grid; + const lastColumn = grid.getLastColumn(); + if (lastColumn === null) { + return; + } + lastColumn.focus(); + } + + public "window-move-left"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + if (column.getWindowCount() === 1) { + // move from own column into existing column const leftColumn = grid.getLeftColumn(column); if (leftColumn === null) { return; } - leftColumn.focus(); - }); + window.moveToColumn(leftColumn); + grid.desktop.autoAdjustScroll(); + } else { + // move from shared column into own column + const newColumn = new Column(grid, grid.getLeftColumn(column)); + window.moveToColumn(newColumn); + } } - public "focus-right"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { + public "window-move-right"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + if (column.getWindowCount() === 1) { + // move from own column into existing column const rightColumn = grid.getRightColumn(column); if (rightColumn === null) { return; } - rightColumn.focus(); - }); - } - - public "focus-up"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - const aboveWindow = column.getAboveWindow(window); - if (aboveWindow === null) { - return; - } - aboveWindow.focus(); - }); - } - - public "focus-down"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - const belowWindow = column.getBelowWindow(window); - if (belowWindow === null) { - return; - } - belowWindow.focus(); - }); - } - - public "focus-start"() { - this.world.do((clientManager, desktopManager) => { - const grid = desktopManager.getCurrentDesktop().grid; - const firstColumn = grid.getFirstColumn(); - if (firstColumn === null) { - return; - } - firstColumn.focus(); - }); - } - - public "focus-end"() { - this.world.do((clientManager, desktopManager) => { - const grid = desktopManager.getCurrentDesktop().grid; - const lastColumn = grid.getLastColumn(); - if (lastColumn === null) { - return; - } - lastColumn.focus(); - }); - } - - public "window-move-left"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - if (column.getWindowCount() === 1) { - // move from own column into existing column - const leftColumn = grid.getLeftColumn(column); - if (leftColumn === null) { - return; - } - window.moveToColumn(leftColumn); - grid.desktop.autoAdjustScroll(); - } else { - // move from shared column into own column - const newColumn = new Column(grid, grid.getLeftColumn(column)); - window.moveToColumn(newColumn); - } - }); - } - - public "window-move-right"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - if (column.getWindowCount() === 1) { - // move from own column into existing column - const rightColumn = grid.getRightColumn(column); - if (rightColumn === null) { - return; - } - window.moveToColumn(rightColumn); - grid.desktop.autoAdjustScroll(); - } else { - // move from shared column into own column - const newColumn = new Column(grid, column); - window.moveToColumn(newColumn); - } - }); - } - - public "window-move-up"() { - // TODO (optimization): only arrange moved windows - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - column.moveWindowUp(window); - }); - } - - public "window-move-down"() { - // TODO (optimization): only arrange moved windows - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - column.moveWindowDown(window); - }); - } - - public "window-move-start"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - const newColumn = new Column(grid, null); + window.moveToColumn(rightColumn); + grid.desktop.autoAdjustScroll(); + } else { + // move from shared column into own column + const newColumn = new Column(grid, column); window.moveToColumn(newColumn); - }); + } } - public "window-move-end"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - const newColumn = new Column(grid, grid.getLastColumn()); - window.moveToColumn(newColumn); - }); + // TODO (optimization): only arrange moved windows + public "window-move-up"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + column.moveWindowUp(window); } - public "window-toggle-floating"() { + // TODO (optimization): only arrange moved windows + public "window-move-down"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + column.moveWindowDown(window); + } + + public "window-move-start"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + const newColumn = new Column(grid, null); + window.moveToColumn(newColumn); + } + + public "window-move-end"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + const newColumn = new Column(grid, grid.getLastColumn()); + window.moveToColumn(newColumn); + } + + public "window-toggle-floating"(clientManager: ClientManager, desktopManager: DesktopManager) { const kwinClient = Workspace.activeWindow; - this.world.do((clientManager, desktopManager) => { - clientManager.toggleFloatingClient(kwinClient); - }); + clientManager.toggleFloatingClient(kwinClient); } - public "column-move-left"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - grid.moveColumnLeft(column); - }); + public "column-move-left"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + grid.moveColumnLeft(column); } - public "column-move-right"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - grid.moveColumnRight(column); - }); + public "column-move-right"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + grid.moveColumnRight(column); } - public "column-move-start"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - grid.moveColumn(column, null); - }); + public "column-move-start"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + grid.moveColumn(column, null); } - public "column-move-end"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - grid.moveColumn(column, grid.getLastColumn()); - }); + public "column-move-end"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + grid.moveColumn(column, grid.getLastColumn()); } - public "column-toggle-stacked"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - column.toggleStacked(); - }); + public "column-toggle-stacked"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + column.toggleStacked(); } - public "column-width-increase"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - this.config.columnResizer.increaseWidth(column, this.config.manualResizeStep); - }); + public "column-width-increase"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + this.config.columnResizer.increaseWidth(column, this.config.manualResizeStep); } - public "column-width-decrease"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - this.config.columnResizer.decreaseWidth(column, this.config.manualResizeStep); - }); + public "column-width-decrease"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + this.config.columnResizer.decreaseWidth(column, this.config.manualResizeStep); } - public "columns-width-equalize"() { - this.world.do((clientManager, desktopManager) => { - desktopManager.getCurrentDesktop().equalizeVisibleColumnsWidths(); - }); + public "columns-width-equalize"(clientManager: ClientManager, desktopManager: DesktopManager) { + desktopManager.getCurrentDesktop().equalizeVisibleColumnsWidths(); } - public "grid-scroll-left"() { - this.gridScroll(-this.config.manualScrollStep); + public "grid-scroll-left"(clientManager: ClientManager, desktopManager: DesktopManager) { + this.gridScroll(desktopManager, -this.config.manualScrollStep); } - public "grid-scroll-right"() { - this.gridScroll(this.config.manualScrollStep); + public "grid-scroll-right"(clientManager: ClientManager, desktopManager: DesktopManager) { + this.gridScroll(desktopManager, this.config.manualScrollStep); } - public "grid-scroll-start"() { - this.world.do((clientManager, desktopManager) => { - const grid = desktopManager.getCurrentDesktop().grid; - const firstColumn = grid.getFirstColumn(); - if (firstColumn === null) { - return; - } - grid.desktop.scrollToColumn(firstColumn); - }); + public "grid-scroll-start"(clientManager: ClientManager, desktopManager: DesktopManager) { + const grid = desktopManager.getCurrentDesktop().grid; + const firstColumn = grid.getFirstColumn(); + if (firstColumn === null) { + return; + } + grid.desktop.scrollToColumn(firstColumn); } - public "grid-scroll-end"() { - this.world.do((clientManager, desktopManager) => { - const grid = desktopManager.getCurrentDesktop().grid; - const lastColumn = grid.getLastColumn(); - if (lastColumn === null) { - return; - } - grid.desktop.scrollToColumn(lastColumn); - }); + public "grid-scroll-end"(clientManager: ClientManager, desktopManager: DesktopManager) { + const grid = desktopManager.getCurrentDesktop().grid; + const lastColumn = grid.getLastColumn(); + if (lastColumn === null) { + return; + } + grid.desktop.scrollToColumn(lastColumn); } - public "grid-scroll-focused"() { - this.world.doIfTiledFocused((clientManager, desktopManager, window, column, grid) => { - grid.desktop.scrollCenterRange(column); - }) + public "grid-scroll-focused"(clientManager: ClientManager, desktopManager: DesktopManager, window: Window, column: Column, grid: Grid) { + grid.desktop.scrollCenterRange(column); } - public "grid-scroll-left-column"() { - this.world.do((clientManager, desktopManager) => { - const grid = desktopManager.getCurrentDesktop().grid; - const column = grid.getLeftmostVisibleColumn(grid.desktop.getCurrentVisibleRange(), true); - if (column === null) { - return; - } + public "grid-scroll-left-column"(clientManager: ClientManager, desktopManager: DesktopManager) { + const grid = desktopManager.getCurrentDesktop().grid; + const column = grid.getLeftmostVisibleColumn(grid.desktop.getCurrentVisibleRange(), true); + if (column === null) { + return; + } - const leftColumn = grid.getLeftColumn(column); - if (leftColumn === null) { - return; - } + const leftColumn = grid.getLeftColumn(column); + if (leftColumn === null) { + return; + } - grid.desktop.scrollToColumn(leftColumn); - }); + grid.desktop.scrollToColumn(leftColumn); } - public "grid-scroll-right-column"() { - this.world.do((clientManager, desktopManager) => { - const grid = desktopManager.getCurrentDesktop().grid; - const column = grid.getRightmostVisibleColumn(grid.desktop.getCurrentVisibleRange(), true); - if (column === null) { - return; - } + public "grid-scroll-right-column"(clientManager: ClientManager, desktopManager: DesktopManager) { + const grid = desktopManager.getCurrentDesktop().grid; + const column = grid.getRightmostVisibleColumn(grid.desktop.getCurrentVisibleRange(), true); + if (column === null) { + return; + } - const rightColumn = grid.getRightColumn(column); - if (rightColumn === null) { - return; - } + const rightColumn = grid.getRightColumn(column); + if (rightColumn === null) { + return; + } - grid.desktop.scrollToColumn(rightColumn); - }); + grid.desktop.scrollToColumn(rightColumn); } - public "screen-switch"() { - this.world.do((clientManager, desktopManager) => { - desktopManager.selectScreen(Workspace.activeScreen); - }); + public "screen-switch"(clientManager: ClientManager, desktopManager: DesktopManager) { + desktopManager.selectScreen(Workspace.activeScreen); } - private gridScroll(amount: number) { - this.world.do((clientManager, desktopManager) => { - const grid = desktopManager.getCurrentDesktop().grid; - grid.desktop.adjustScroll(amount, false); - }); + private gridScroll(desktopManager: DesktopManager, amount: number) { + const grid = desktopManager.getCurrentDesktop().grid; + grid.desktop.adjustScroll(amount, false); } } } diff --git a/src/lib/keyBindings/definition.ts b/src/lib/keyBindings/definition.ts index 52fe415..0c481db 100644 --- a/src/lib/keyBindings/definition.ts +++ b/src/lib/keyBindings/definition.ts @@ -1,185 +1,185 @@ -function getKeyBindings(actions: Actions.Actions): KeyBinding[] { +function getKeyBindings(world: World, actions: Actions.Actions): KeyBinding[] { return [ { name: "window-toggle-floating", description: "Toggle floating", defaultKeySequence: "Meta+Space", - action: actions["window-toggle-floating"], + action: () => world.do(actions["window-toggle-floating"]), }, { name: "focus-left", description: "Move focus left", defaultKeySequence: "Meta+A", - action: actions["focus-left"], + action: () => world.doIfTiledFocused(actions["focus-left"]), }, { name: "focus-right", description: "Move focus right", comment: "Clashes with default KDE shortcuts, may require manual remapping", defaultKeySequence: "Meta+D", - action: actions["focus-right"], + action: () => world.doIfTiledFocused(actions["focus-right"]), }, { name: "focus-up", description: "Move focus up", comment: "Clashes with default KDE shortcuts, may require manual remapping", defaultKeySequence: "Meta+W", - action: actions["focus-up"], + action: () => world.doIfTiledFocused(actions["focus-up"]), }, { name: "focus-down", description: "Move focus down", comment: "Clashes with default KDE shortcuts, may require manual remapping", defaultKeySequence: "Meta+S", - action: actions["focus-down"], + action: () => world.doIfTiledFocused(actions["focus-down"]), }, { name: "focus-start", description: "Move focus to start", defaultKeySequence: "Meta+Home", - action: actions["focus-start"], + action: () => world.do(actions["focus-start"]), }, { name: "focus-end", description: "Move focus to end", defaultKeySequence: "Meta+End", - action: actions["focus-end"], + action: () => world.do(actions["focus-end"]), }, { name: "window-move-left", description: "Move window left", comment: "Moves window out of and into columns", defaultKeySequence: "Meta+Shift+A", - action: actions["window-move-left"], + action: () => world.doIfTiledFocused(actions["window-move-left"]), }, { name: "window-move-right", description: "Move window right", comment: "Moves window out of and into columns", defaultKeySequence: "Meta+Shift+D", - action: actions["window-move-right"], + action: () => world.doIfTiledFocused(actions["window-move-right"]), }, { name: "window-move-up", description: "Move window up", defaultKeySequence: "Meta+Shift+W", - action: actions["window-move-up"], + action: () => world.doIfTiledFocused(actions["window-move-up"]), }, { name: "window-move-down", description: "Move window down", defaultKeySequence: "Meta+Shift+S", - action: actions["window-move-down"], + action: () => world.doIfTiledFocused(actions["window-move-down"]), }, { name: "window-move-start", description: "Move window to start", defaultKeySequence: "Meta+Shift+Home", - action: actions["window-move-start"], + action: () => world.doIfTiledFocused(actions["window-move-start"]), }, { name: "window-move-end", description: "Move window to end", defaultKeySequence: "Meta+Shift+End", - action: actions["window-move-end"], + action: () => world.doIfTiledFocused(actions["window-move-end"]), }, { name: "column-toggle-stacked", description: "Toggle stacked layout for focused column", comment: "One window in the column visible, others shaded; not supported on Wayland", defaultKeySequence: "Meta+X", - action: actions["column-toggle-stacked"], + action: () => world.doIfTiledFocused(actions["column-toggle-stacked"]), }, { name: "column-move-left", description: "Move column left", defaultKeySequence: "Meta+Ctrl+Shift+A", - action: actions["column-move-left"], + action: () => world.doIfTiledFocused(actions["column-move-left"]), }, { name: "column-move-right", description: "Move column right", defaultKeySequence: "Meta+Ctrl+Shift+D", - action: actions["column-move-right"], + action: () => world.doIfTiledFocused(actions["column-move-right"]), }, { name: "column-move-start", description: "Move column to start", defaultKeySequence: "Meta+Ctrl+Shift+Home", - action: actions["column-move-start"], + action: () => world.doIfTiledFocused(actions["column-move-start"]), }, { name: "column-move-end", description: "Move column to end", defaultKeySequence: "Meta+Ctrl+Shift+End", - action: actions["column-move-end"], + action: () => world.doIfTiledFocused(actions["column-move-end"]), }, { name: "column-width-increase", description: "Increase column width", defaultKeySequence: "Meta+Ctrl++", - action: actions["column-width-increase"], + action: () => world.doIfTiledFocused(actions["column-width-increase"]), }, { name: "column-width-decrease", description: "Decrease column width", defaultKeySequence: "Meta+Ctrl+-", - action: actions["column-width-decrease"], + action: () => world.doIfTiledFocused(actions["column-width-decrease"]), }, { name: "columns-width-equalize", description: "Equalize widths of visible columns", defaultKeySequence: "Meta+Ctrl+X", - action: actions["columns-width-equalize"], + action: () => world.do(actions["columns-width-equalize"]), }, { name: "grid-scroll-focused", description: "Center focused window", comment: "Scrolls so that the focused window is centered in the screen", defaultKeySequence: "Meta+Alt+Return", - action: actions["grid-scroll-focused"], + action: () => world.doIfTiledFocused(actions["grid-scroll-focused"]), }, { name: "grid-scroll-left-column", description: "Scroll one column to the left", defaultKeySequence: "Meta+Alt+A", - action: actions["grid-scroll-left-column"], + action: () => world.do(actions["grid-scroll-left-column"]), }, { name: "grid-scroll-right-column", description: "Scroll one column to the right", defaultKeySequence: "Meta+Alt+D", - action: actions["grid-scroll-right-column"], + action: () => world.do(actions["grid-scroll-right-column"]), }, { name: "grid-scroll-left", description: "Scroll left", defaultKeySequence: "Meta+Alt+PgUp", - action: actions["grid-scroll-left"], + action: () => world.do(actions["grid-scroll-left"]), }, { name: "grid-scroll-right", description: "Scroll right", defaultKeySequence: "Meta+Alt+PgDown", - action: actions["grid-scroll-right"], + action: () => world.do(actions["grid-scroll-right"]), }, { name: "grid-scroll-start", description: "Scroll to start", defaultKeySequence: "Meta+Alt+Home", - action: actions["grid-scroll-start"], + action: () => world.do(actions["grid-scroll-start"]), }, { name: "grid-scroll-end", description: "Scroll to end", defaultKeySequence: "Meta+Alt+End", - action: actions["grid-scroll-end"], + action: () => world.do(actions["grid-scroll-end"]), }, { name: "screen-switch", description: "Move Karousel grid to the current screen", defaultKeySequence: "Meta+Ctrl+Return", - action: actions["screen-switch"], + action: () => world.do(actions["screen-switch"]), }, ]; } diff --git a/src/lib/keyBindings/loader.ts b/src/lib/keyBindings/loader.ts index afa7d9a..94cc44c 100644 --- a/src/lib/keyBindings/loader.ts +++ b/src/lib/keyBindings/loader.ts @@ -53,11 +53,11 @@ function registerNumKeyBindings(shortcutActions: ShortcutAction[], numKeyBinding } function registerKeyBindings(world: World, config: Actions.Config) { - const actions = new Actions.Actions(world, config); + const actions = new Actions.Actions(config); const numActions = new Actions.NumActions(world); const shortcutActions: ShortcutAction[] = []; - for (const keyBinding of getKeyBindings(actions)) { + for (const keyBinding of getKeyBindings(world, actions)) { registerKeyBinding(shortcutActions, keyBinding); }