Skip to content

Commit

Permalink
move setLayout to LayoutStore
Browse files Browse the repository at this point in the history
  • Loading branch information
esjeon committed Feb 15, 2019
1 parent cddec7d commit b979fc3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
16 changes: 1 addition & 15 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
*/
Expand Down
36 changes: 28 additions & 8 deletions src/layoutstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit b979fc3

Please sign in to comment.