diff --git a/src/lib/actions/Actions.ts b/src/lib/actions/Actions.ts index ab6ac3e..bc83f0b 100644 --- a/src/lib/actions/Actions.ts +++ b/src/lib/actions/Actions.ts @@ -5,267 +5,261 @@ namespace Actions { private readonly config: Actions.Config, ) {} - public getAction(name: string) { - switch (name) { - case "focus-left": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - const prevColumn = grid.getPrevColumn(column); - if (prevColumn === null) { - return; - } - prevColumn.focus(); - }); - }; + public "focus-left"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + const prevColumn = grid.getPrevColumn(column); + if (prevColumn === null) { + return; + } + prevColumn.focus(); + }); + } - case "focus-right": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - const nextColumn = grid.getNextColumn(column); - if (nextColumn === null) { - return; - } - nextColumn.focus(); - }); - }; + public "focus-right"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + const nextColumn = grid.getNextColumn(column); + if (nextColumn === null) { + return; + } + nextColumn.focus(); + }); + } - case "focus-up": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - const prevWindow = column.getPrevWindow(window); - if (prevWindow === null) { - return; - } - prevWindow.focus(); - }); - }; + public "focus-up"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + const prevWindow = column.getPrevWindow(window); + if (prevWindow === null) { + return; + } + prevWindow.focus(); + }); + } - case "focus-down": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - const nextWindow = column.getNextWindow(window); - if (nextWindow === null) { - return; - } - nextWindow.focus(); - }); - }; + public "focus-down"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + const nextWindow = column.getNextWindow(window); + if (nextWindow === null) { + return; + } + nextWindow.focus(); + }); + } - case "focus-start": return () => { - this.world.do((clientManager, desktopManager) => { - const grid = desktopManager.getCurrentDesktop().grid; - const firstColumn = grid.getFirstColumn(); - if (firstColumn === null) { - return; - } - firstColumn.focus(); - }); - }; + public "focus-start"() { + this.world.do((clientManager, desktopManager) => { + const grid = desktopManager.getCurrentDesktop().grid; + const firstColumn = grid.getFirstColumn(); + if (firstColumn === null) { + return; + } + firstColumn.focus(); + }); + } - case "focus-end": return () => { - this.world.do((clientManager, desktopManager) => { - const grid = desktopManager.getCurrentDesktop().grid; - const lastColumn = grid.getLastColumn(); - if (lastColumn === null) { - return; - } - lastColumn.focus(); - }); - }; + public "focus-end"() { + this.world.do((clientManager, desktopManager) => { + const grid = desktopManager.getCurrentDesktop().grid; + const lastColumn = grid.getLastColumn(); + if (lastColumn === null) { + return; + } + lastColumn.focus(); + }); + } - case "window-move-left": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - if (column.getWindowCount() === 1) { - // move from own column into existing column - const prevColumn = grid.getPrevColumn(column); - if (prevColumn === null) { - return; - } - window.moveToColumn(prevColumn); - grid.desktop.autoAdjustScroll(); - } else { - // move from shared column into own column - const newColumn = new Column(grid, grid.getPrevColumn(column)); - window.moveToColumn(newColumn); - } - }); - }; + public "window-move-left"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + if (column.getWindowCount() === 1) { + // move from own column into existing column + const prevColumn = grid.getPrevColumn(column); + if (prevColumn === null) { + return; + } + window.moveToColumn(prevColumn); + grid.desktop.autoAdjustScroll(); + } else { + // move from shared column into own column + const newColumn = new Column(grid, grid.getPrevColumn(column)); + window.moveToColumn(newColumn); + } + }); + } - case "window-move-right": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - if (column.getWindowCount() === 1) { - // move from own column into existing column - const nextColumn = grid.getNextColumn(column); - if (nextColumn === null) { - return; - } - window.moveToColumn(nextColumn); - grid.desktop.autoAdjustScroll(); - } else { - // move from shared column into own column - const newColumn = new Column(grid, column); - window.moveToColumn(newColumn); - } - }); - }; + public "window-move-right"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + if (column.getWindowCount() === 1) { + // move from own column into existing column + const nextColumn = grid.getNextColumn(column); + if (nextColumn === null) { + return; + } + window.moveToColumn(nextColumn); + grid.desktop.autoAdjustScroll(); + } else { + // move from shared column into own column + const newColumn = new Column(grid, column); + window.moveToColumn(newColumn); + } + }); + } - case "window-move-up": return () => { - // TODO (optimization): only arrange moved windows - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - column.moveWindowUp(window); - }); - }; + public "window-move-up"() { + // TODO (optimization): only arrange moved windows + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + column.moveWindowUp(window); + }); + } - case "window-move-down": return () => { - // TODO (optimization): only arrange moved windows - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - column.moveWindowDown(window); - }); - }; + public "window-move-down"() { + // TODO (optimization): only arrange moved windows + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + column.moveWindowDown(window); + }); + } - case "window-move-start": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - const newColumn = new Column(grid, null); - window.moveToColumn(newColumn); - }); - }; + public "window-move-start"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + const newColumn = new Column(grid, null); + window.moveToColumn(newColumn); + }); + } - case "window-move-end": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - const newColumn = new Column(grid, grid.getLastColumn()); - window.moveToColumn(newColumn); - }); - }; + public "window-move-end"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + const newColumn = new Column(grid, grid.getLastColumn()); + window.moveToColumn(newColumn); + }); + } - case "window-toggle-floating": return () => { - const kwinClient = Workspace.activeWindow; - this.world.do((clientManager, desktopManager) => { - clientManager.toggleFloatingClient(kwinClient); - }); - }; + public "window-toggle-floating"() { + const kwinClient = Workspace.activeWindow; + this.world.do((clientManager, desktopManager) => { + clientManager.toggleFloatingClient(kwinClient); + }); + } - case "column-move-left": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - grid.moveColumnLeft(column); - }); - }; + public "column-move-left"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + grid.moveColumnLeft(column); + }); + } - case "column-move-right": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - grid.moveColumnRight(column); - }); - }; + public "column-move-right"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + grid.moveColumnRight(column); + }); + } - case "column-move-start": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - grid.moveColumn(column, null); - }); - }; + public "column-move-start"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + grid.moveColumn(column, null); + }); + } - case "column-move-end": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - grid.moveColumn(column, grid.getLastColumn()); - }); - }; + public "column-move-end"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + grid.moveColumn(column, grid.getLastColumn()); + }); + } - case "column-toggle-stacked": return () => { - this.world.doIfTiledFocused(false, (clientManager, desktopManager, window, column, grid) => { - column.toggleStacked(); - }); - }; + public "column-toggle-stacked"() { + this.world.doIfTiledFocused(false, (clientManager, desktopManager, window, column, grid) => { + column.toggleStacked(); + }); + } - case "column-width-increase": return () => { - this.world.doIfTiledFocused(false, (clientManager, desktopManager, window, column, grid) => { - this.config.columnResizer.increaseWidth(column, this.config.manualResizeStep); - }); - }; + public "column-width-increase"() { + this.world.doIfTiledFocused(false, (clientManager, desktopManager, window, column, grid) => { + this.config.columnResizer.increaseWidth(column, this.config.manualResizeStep); + }); + } - case "column-width-decrease": return () => { - this.world.doIfTiledFocused(false, (clientManager, desktopManager, window, column, grid) => { - this.config.columnResizer.decreaseWidth(column, this.config.manualResizeStep); - }); - }; + public "column-width-decrease"() { + this.world.doIfTiledFocused(false, (clientManager, desktopManager, window, column, grid) => { + this.config.columnResizer.decreaseWidth(column, this.config.manualResizeStep); + }); + } - case "columns-width-equalize": return () => { - this.world.do((clientManager, desktopManager) => { - desktopManager.getCurrentDesktop().equalizeVisibleColumnsWidths(); - }); - }; + public "columns-width-equalize"() { + this.world.do((clientManager, desktopManager) => { + desktopManager.getCurrentDesktop().equalizeVisibleColumnsWidths(); + }); + } - case "grid-scroll-left": return () => { - this.gridScroll(-this.config.manualScrollStep); - }; + public "grid-scroll-left"() { + this.gridScroll(-this.config.manualScrollStep); + } - case "grid-scroll-right": return () => { - this.gridScroll(this.config.manualScrollStep); - }; + public "grid-scroll-right"() { + this.gridScroll(this.config.manualScrollStep); + } - case "grid-scroll-start": return () => { - 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"() { + this.world.do((clientManager, desktopManager) => { + const grid = desktopManager.getCurrentDesktop().grid; + const firstColumn = grid.getFirstColumn(); + if (firstColumn === null) { + return; + } + grid.desktop.scrollToColumn(firstColumn); + }); + } - case "grid-scroll-end": return () => { - 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"() { + this.world.do((clientManager, desktopManager) => { + const grid = desktopManager.getCurrentDesktop().grid; + const lastColumn = grid.getLastColumn(); + if (lastColumn === null) { + return; + } + grid.desktop.scrollToColumn(lastColumn); + }); + } - case "grid-scroll-focused": return () => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - grid.desktop.scrollCenterRange(column); - }) - }; + public "grid-scroll-focused"() { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + grid.desktop.scrollCenterRange(column); + }) + } - case "grid-scroll-left-column": return () => { - 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"() { + this.world.do((clientManager, desktopManager) => { + const grid = desktopManager.getCurrentDesktop().grid; + const column = grid.getLeftmostVisibleColumn(grid.desktop.getCurrentVisibleRange(), true); + if (column === null) { + return; + } - const prevColumn = grid.getPrevColumn(column); - if (prevColumn === null) { - return; - } + const prevColumn = grid.getPrevColumn(column); + if (prevColumn === null) { + return; + } - grid.desktop.scrollToColumn(prevColumn); - }); - }; + grid.desktop.scrollToColumn(prevColumn); + }); + } - case "grid-scroll-right-column": return () => { - 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"() { + this.world.do((clientManager, desktopManager) => { + const grid = desktopManager.getCurrentDesktop().grid; + const column = grid.getRightmostVisibleColumn(grid.desktop.getCurrentVisibleRange(), true); + if (column === null) { + return; + } - const nextColumn = grid.getNextColumn(column); - if (nextColumn === null) { - return; - } + const nextColumn = grid.getNextColumn(column); + if (nextColumn === null) { + return; + } - grid.desktop.scrollToColumn(nextColumn); - }); - }; + grid.desktop.scrollToColumn(nextColumn); + }); + } - case "screen-switch": return () => { - this.world.do((clientManager, desktopManager) => { - desktopManager.selectScreen(Workspace.activeScreen); - }); - }; - - default: throw new Error("unknown action: " + name); - } + public "screen-switch"() { + this.world.do((clientManager, desktopManager) => { + desktopManager.selectScreen(Workspace.activeScreen); + }); } private gridScroll(amount: number) { diff --git a/src/lib/actions/Getter.ts b/src/lib/actions/Getter.ts index 7a6f30b..57c91fb 100644 --- a/src/lib/actions/Getter.ts +++ b/src/lib/actions/Getter.ts @@ -8,12 +8,12 @@ namespace Actions { this.numActions = new NumActions(world); } - public getAction(action: string) { - return this.actions.getAction(action); + public getAction(action: keyof Actions) { + return this.actions[action].bind(this.actions); } - public getNumAction(action: string) { - return this.numActions.getNumAction(action); + public getNumAction(action: keyof NumActions) { + return this.numActions[action].bind(this.numActions); } } diff --git a/src/lib/actions/NumActions.ts b/src/lib/actions/NumActions.ts index f443778..0b33a5e 100644 --- a/src/lib/actions/NumActions.ts +++ b/src/lib/actions/NumActions.ts @@ -2,74 +2,68 @@ namespace Actions { export class NumActions { constructor(private readonly world: World) {} - public getNumAction(name: string) { - switch (name) { - case "focus-": return (columnIndex: number) => { - this.world.do((clientManager, desktopManager) => { - const grid = desktopManager.getCurrentDesktop().grid; - const targetColumn = grid.getColumnAtIndex(columnIndex); - if (targetColumn === null) { - return; - } - targetColumn.focus(); - }); - }; + public "focus-"(columnIndex: number) { + this.world.do((clientManager, desktopManager) => { + const grid = desktopManager.getCurrentDesktop().grid; + const targetColumn = grid.getColumnAtIndex(columnIndex); + if (targetColumn === null) { + return; + } + targetColumn.focus(); + }); + }; - case "window-move-to-column-": return (columnIndex: number) => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - const targetColumn = grid.getColumnAtIndex(columnIndex); - if (targetColumn === null) { - return; - } - window.moveToColumn(targetColumn); - grid.desktop.autoAdjustScroll(); - }); - }; + public "window-move-to-column-"(columnIndex: number) { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + const targetColumn = grid.getColumnAtIndex(columnIndex); + if (targetColumn === null) { + return; + } + window.moveToColumn(targetColumn); + grid.desktop.autoAdjustScroll(); + }); + }; - case "column-move-to-column-": return (columnIndex: number) => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { - const targetColumn = grid.getColumnAtIndex(columnIndex); - if (targetColumn === null || targetColumn === column) { - return; - } - if (targetColumn.isToTheRightOf(column)) { - grid.moveColumn(column, targetColumn); - } else { - grid.moveColumn(column, grid.getPrevColumn(targetColumn)); - } - }); - }; + public "column-move-to-column-"(columnIndex: number) { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, grid) => { + const targetColumn = grid.getColumnAtIndex(columnIndex); + if (targetColumn === null || targetColumn === column) { + return; + } + if (targetColumn.isToTheRightOf(column)) { + grid.moveColumn(column, targetColumn); + } else { + grid.moveColumn(column, grid.getPrevColumn(targetColumn)); + } + }); + }; - case "column-move-to-desktop-": return (desktopIndex: number) => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, oldGrid) => { - const kwinDesktop = Workspace.desktops[desktopIndex]; - if (kwinDesktop === undefined) { - return; - } - const newGrid = desktopManager.getDesktopInCurrentActivity(kwinDesktop).grid; - if (newGrid === null || newGrid === oldGrid) { - return; - } - column.moveToGrid(newGrid, newGrid.getLastColumn()); - }); - }; + public "column-move-to-desktop-"(desktopIndex: number) { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, oldGrid) => { + const kwinDesktop = Workspace.desktops[desktopIndex]; + if (kwinDesktop === undefined) { + return; + } + const newGrid = desktopManager.getDesktopInCurrentActivity(kwinDesktop).grid; + if (newGrid === null || newGrid === oldGrid) { + return; + } + column.moveToGrid(newGrid, newGrid.getLastColumn()); + }); + }; - case "tail-move-to-desktop-": return (desktopIndex: number) => { - this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, oldGrid) => { - const kwinDesktop = Workspace.desktops[desktopIndex]; - if (kwinDesktop === undefined) { - return; - } - const newGrid = desktopManager.getDesktopInCurrentActivity(kwinDesktop).grid; - if (newGrid === null || newGrid === oldGrid) { - return; - } - oldGrid.evacuateTail(newGrid, column); - }); - }; - - default: throw new Error("unknown num action: " + name); - } - } + public "tail-move-to-desktop-"(desktopIndex: number) { + this.world.doIfTiledFocused(true, (clientManager, desktopManager, window, column, oldGrid) => { + const kwinDesktop = Workspace.desktops[desktopIndex]; + if (kwinDesktop === undefined) { + return; + } + const newGrid = desktopManager.getDesktopInCurrentActivity(kwinDesktop).grid; + if (newGrid === null || newGrid === oldGrid) { + return; + } + oldGrid.evacuateTail(newGrid, column); + }); + }; } } diff --git a/src/lib/keyBindings/loader.ts b/src/lib/keyBindings/loader.ts index 88859ab..436fe2b 100644 --- a/src/lib/keyBindings/loader.ts +++ b/src/lib/keyBindings/loader.ts @@ -1,12 +1,12 @@ type KeyBinding = { - name: string; + name: keyof Actions.Actions; description: string; comment?: string; defaultKeySequence: string; }; type NumKeyBinding = { - name: string; + name: keyof Actions.NumActions; description: string; comment?: string; defaultModifiers: string; @@ -51,7 +51,6 @@ function registerNumKeyBindings(actionGetter: Actions.Getter, shortcutActions: S } } -// TODO: refactor function registerKeyBindings(world: World, config: Actions.Config) { const actionGetter = new Actions.Getter(world, config); const shortcutActions: ShortcutAction[] = []; diff --git a/src/lib/utils/ShortcutAction.ts b/src/lib/utils/ShortcutAction.ts index 2787128..af66c88 100644 --- a/src/lib/utils/ShortcutAction.ts +++ b/src/lib/utils/ShortcutAction.ts @@ -1,7 +1,7 @@ class ShortcutAction { private readonly shortcutHandler: ShortcutHandler; - constructor(keyBinding: KeyBinding, f: () => void) { + constructor(keyBinding: ShortcutAction.KeyBinding, f: () => void) { this.shortcutHandler = ShortcutAction.initShortcutHandler(keyBinding); this.shortcutHandler.activated.connect(f); } @@ -10,7 +10,7 @@ class ShortcutAction { this.shortcutHandler.destroy(); } - private static initShortcutHandler(keyBinding: KeyBinding) { + private static initShortcutHandler(keyBinding: ShortcutAction.KeyBinding) { return Qt.createQmlObject( `import QtQuick 6.0 import org.kde.kwin 3.0 @@ -23,3 +23,11 @@ ShortcutHandler { ); } } + +namespace ShortcutAction { + export type KeyBinding = { + name: string; + description: string; + defaultKeySequence: string; + }; +}