From d78dbc9b18d0fb842958f4283edefe10a1289982 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Tue, 9 May 2023 17:00:28 +0900 Subject: [PATCH] Calculate a tiling layout --- sources/layout.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 sources/layout.js diff --git a/sources/layout.js b/sources/layout.js new file mode 100644 index 0000000..4abafe8 --- /dev/null +++ b/sources/layout.js @@ -0,0 +1,94 @@ +"use strict"; + +const GObject = imports.gi.GObject; + +class FairyWindow { + constructor(handle) { + this.handle = handle; + this.floating = false; + this.fullscreen = false; + this.monitor = 0; + this.tags = 1; + } +} + +var Layout = GObject.registerClass( + class Layout extends GObject.Object { + _init() { + super._init(); + // /** + // * Map> + // * @type {Map>} + // */ + // this._monitors = new Map(); + // + // /** + // * The + // * @type {Map} + // */ + // this._monitorsTags = new Map(); + + this._layout = { + type: "tiled", + nmaster: 1, + nfact: 60, + }; + + /** + * @type {FairyWindow[]} + */ + this._windows = []; + } + + newWindow(mon, workspace, handle) { + this._windows.push(new FairyWindow(handle)); + // this._monitors[mon] ??= new Map(); + // this._monitors[mon][workspace] ??= []; + // this._monitors[mon][workspace].push(new FairyWindow(handle)); + } + + /** + * @param {number} mon + * @param {number} tags + * + * @typedef WindowGeometry + * @type {object} + * @property {Meta.Window} handle + * @property {number} x - in percentage from the top left + * @property {number} y - in percentage from the top left + * @property {number} width - in percentage (0-100) + * @property {number} height - in percentage + * @returns WindowGeometry[] + */ + render(mon, tags) { + const { nmaster, nfact } = this._layout; + const windows = this._windows.filter( + (x) => x.monitor === mon && x.tags & tags + ); + log(`${windows.length} windows for monitor ${mon} with tags ${tags}`); + + // TODO: Implement other layouts + if (this._layout.type === "tiled") { + return windows.map((x, i) => { + const stackLength = i <= nmaster + ? Math.min(nmaster, windows.length) + : window.length - nmaster; + const stackIndex = i <= nmaster ? i : i - nmaster; + return { + handle: x.handle, + maximized: false, + minimized: false, + x: i <= nmaster ? 0 : nfact, + y: stackIndex * (100 - (100 / stackLength)), + width: windows.length <= nmaster + ? 100 + : i <= nmaster + ? nfact + : 100 - nfact, + height: 100 / stackLength, + }; + }); + } + } + } +);