From 8ebffbe639efcf1cfec5addad29e2777d1bbd5d2 Mon Sep 17 00:00:00 2001 From: Mikhail Zolotukhin Date: Mon, 1 Nov 2021 13:41:35 +0300 Subject: [PATCH] feat: make layouts togglable --- src/controller/action.test.ts | 6 +++--- src/controller/action.ts | 18 +++++++++--------- src/controller/index.ts | 14 +++++++------- src/engine/index.ts | 6 +++--- src/engine/layout_store.ts | 25 ++++++++++--------------- 5 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/controller/action.test.ts b/src/controller/action.test.ts index 70b012d8..0874f65a 100644 --- a/src/controller/action.test.ts +++ b/src/controller/action.test.ts @@ -355,7 +355,7 @@ describe("action", () => { fakeEngine = createMock({ cycleLayout: jest.fn(), - setLayout: jest.fn(), + toggleLayout: jest.fn(), currentWindow: jest.fn().mockReturnValue(fakeCurrentWindow), }); }); @@ -382,11 +382,11 @@ describe("action", () => { describe("set layout", () => { it("executes correctly when asking to set Monocle Layout", () => { - const action = new Action.SetMonocleLayout(fakeEngine, fakeLog); + const action = new Action.ToggleMonocleLayout(fakeEngine, fakeLog); action.execute(); - expect(fakeEngine.setLayout).toBeCalledWith("MonocleLayout"); + expect(fakeEngine.toggleLayout).toBeCalledWith("MonocleLayout"); }); }); }); diff --git a/src/controller/action.ts b/src/controller/action.ts index 4945e1f0..109f3463 100644 --- a/src/controller/action.ts +++ b/src/controller/action.ts @@ -452,7 +452,7 @@ export class SwitchToPreviousLayout extends ActionImpl implements Action { } } -abstract class SetCurrentLayout extends ActionImpl implements Action { +abstract class ToggleCurrentLayout extends ActionImpl implements Action { constructor( protected engine: Engine, protected layoutId: string, @@ -465,11 +465,11 @@ abstract class SetCurrentLayout extends ActionImpl implements Action { } public executeWithoutLayoutOverride(): void { - this.engine.setLayout(this.layoutId); + this.engine.toggleLayout(this.layoutId); } } -export class SetTileLayout extends SetCurrentLayout { +export class ToggleTileLayout extends ToggleCurrentLayout { constructor(protected engine: Engine, protected log: Log) { super( engine, @@ -482,7 +482,7 @@ export class SetTileLayout extends SetCurrentLayout { } } -export class SetMonocleLayout extends SetCurrentLayout { +export class ToggleMonocleLayout extends ToggleCurrentLayout { constructor(protected engine: Engine, protected log: Log) { super( engine, @@ -495,7 +495,7 @@ export class SetMonocleLayout extends SetCurrentLayout { } } -export class SetThreeColumnLayout extends SetCurrentLayout { +export class ToggleThreeColumnLayout extends ToggleCurrentLayout { constructor(protected engine: Engine, protected log: Log) { super( engine, @@ -508,7 +508,7 @@ export class SetThreeColumnLayout extends SetCurrentLayout { } } -export class SetSpreadLayout extends SetCurrentLayout { +export class ToggleSpreadLayout extends ToggleCurrentLayout { constructor(protected engine: Engine, protected log: Log) { super( engine, @@ -521,7 +521,7 @@ export class SetSpreadLayout extends SetCurrentLayout { } } -export class SetStairLayout extends SetCurrentLayout { +export class ToggleStairLayout extends ToggleCurrentLayout { constructor(protected engine: Engine, protected log: Log) { super( engine, @@ -534,7 +534,7 @@ export class SetStairLayout extends SetCurrentLayout { } } -export class SetFloatingLayout extends SetCurrentLayout { +export class ToggleFloatingLayout extends ToggleCurrentLayout { constructor(protected engine: Engine, protected log: Log) { // NOTE: space is intentional (Temporary) super( @@ -548,7 +548,7 @@ export class SetFloatingLayout extends SetCurrentLayout { } } -export class SetQuarterLayout extends SetCurrentLayout { +export class ToggleQuarterLayout extends ToggleCurrentLayout { constructor(protected engine: Engine, protected log: Log) { super( engine, diff --git a/src/controller/index.ts b/src/controller/index.ts index 9ec189a2..37537852 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -403,13 +403,13 @@ export class ControllerImpl implements Controller { new Action.SwitchToNextLayout(this.engine, this.log), new Action.SwitchToPreviousLayout(this.engine, this.log), - new Action.SetTileLayout(this.engine, this.log), - new Action.SetMonocleLayout(this.engine, this.log), - new Action.SetThreeColumnLayout(this.engine, this.log), - new Action.SetStairLayout(this.engine, this.log), - new Action.SetSpreadLayout(this.engine, this.log), - new Action.SetFloatingLayout(this.engine, this.log), - new Action.SetQuarterLayout(this.engine, this.log), + new Action.ToggleTileLayout(this.engine, this.log), + new Action.ToggleMonocleLayout(this.engine, this.log), + new Action.ToggleThreeColumnLayout(this.engine, this.log), + new Action.ToggleStairLayout(this.engine, this.log), + new Action.ToggleSpreadLayout(this.engine, this.log), + new Action.ToggleFloatingLayout(this.engine, this.log), + new Action.ToggleQuarterLayout(this.engine, this.log), new Action.Rotate(this.engine, this.log), new Action.RotatePart(this.engine, this.log), diff --git a/src/engine/index.ts b/src/engine/index.ts index 9518431a..1be078ba 100644 --- a/src/engine/index.ts +++ b/src/engine/index.ts @@ -146,7 +146,7 @@ export interface Engine { /** * Set the layout of the current surface to the specified layout. */ - setLayout(layoutClassID: string): void; + toggleLayout(layoutClassID: string): void; /** * Minimize all windows on the surface except the given window. @@ -584,8 +584,8 @@ export class EngineImpl implements Engine { } } - public setLayout(layoutClassID: string): void { - const layout = this.layouts.setLayout( + public toggleLayout(layoutClassID: string): void { + const layout = this.layouts.toggleLayout( this.controller.currentSurface, layoutClassID ); diff --git a/src/engine/layout_store.ts b/src/engine/layout_store.ts index 9ca1804f..6e551d74 100644 --- a/src/engine/layout_store.ts +++ b/src/engine/layout_store.ts @@ -45,16 +45,14 @@ export class LayoutStoreEntry { return this.loadLayout(this.currentID); } - public setLayout(targetID: string): WindowsLayout { + public toggleLayout(targetID: string): WindowsLayout { const targetLayout = this.loadLayout(targetID); - if ( - targetLayout instanceof MonocleLayout && - this.currentLayout instanceof MonocleLayout - ) { - /* toggle Monocle "OFF" */ + + // Toggle if requested, set otherwise + if (this.currentID === targetID) { this.currentID = this.previousID; this.previousID = targetID; - } else if (this.currentID !== targetID) { + } else { this.previousID = this.currentID; this.currentID = targetID; } @@ -80,10 +78,7 @@ export class LayoutStoreEntry { export default class LayoutStore { private store: { [key: string]: LayoutStoreEntry }; - private config: Config; - - constructor(config: Config) { - this.config = config; + constructor(private config: Config) { this.store = {}; } @@ -100,14 +95,14 @@ export default class LayoutStore { return this.getEntry(srf.id).cycleLayout(step); } - public setLayout( - srf: DriverSurface, + public toggleLayout( + surf: DriverSurface, layoutClassID: string ): WindowsLayout | null { - if (srf.ignore) { + if (surf.ignore) { return null; } - return this.getEntry(srf.id).setLayout(layoutClassID); + return this.getEntry(surf.id).toggleLayout(layoutClassID); } private getEntry(key: string): LayoutStoreEntry {