diff --git a/src/engine.ts b/src/engine.ts index 1a6b280..b2dd691 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -167,7 +167,7 @@ class TilingEngine { this.nextLayout(); break; case UserInput.SetLayout: - this.setLayout(data); + this.layouts.setLayout(this.driver.getCurrentContext(), data); break; } this.arrange(); @@ -227,20 +227,6 @@ class TilingEngine { this.layouts.cycleLayout(this.driver.getCurrentContext()); } - public setLayout(cls: any) { - const ctx = this.driver.getCurrentContext(); - const lastLayout = this.layouts.getCurrentLayout(ctx); - for (;;) { - this.layouts.cycleLayout(ctx); - - const layout = this.layouts.getCurrentLayout(ctx); - if (layout instanceof cls) - break; - if (layout === lastLayout) - break; - } - } - /* * Privates */ diff --git a/src/layoutstore.ts b/src/layoutstore.ts index 96a3056..6117e22 100644 --- a/src/layoutstore.ts +++ b/src/layoutstore.ts @@ -29,20 +29,17 @@ class LayoutStore { if (ctx.skipTiling) return null; - if (!this.store[ctx.path]) - this.initEntry(ctx.path); + const entry = this.getEntry(ctx.path); // TODO: if no layout, return floating layout, which is a static object. - return this.store[ctx.path][0]; + return entry[0]; } public cycleLayout(ctx: Context) { - const entry = this.store[ctx.path]; - if (!entry) { - this.initEntry(ctx.path); - return; - } + if (ctx.skipTiling) + return null; + const entry = this.getEntry(ctx.path); for (;;) { const layout = entry.shift(); if (!layout) @@ -55,6 +52,29 @@ class LayoutStore { } } + public setLayout(ctx: Context, cls: any) { + if (ctx.skipTiling) + return; + + const entry = this.getEntry(ctx.path); + const result = entry.filter((l) => l instanceof cls); + if (result.length === 0) + return; + + const layout = result[0]; + if (!layout.enabled) + return; + + const idx = entry.indexOf(layout); + entry.push(...entry.splice(0, idx)); + } + + private getEntry(key: string): ILayout[] { + if (!this.store[key]) + this.initEntry(key); + return this.store[key]; + } + private initEntry(key: string) { const entry = []; // TODO: user config