Skip to content

Commit

Permalink
Minor style change in layout-tiling.
Browse files Browse the repository at this point in the history
  • Loading branch information
wnayes committed Jan 7, 2024
1 parent 63ef126 commit faabf34
Showing 1 changed file with 64 additions and 72 deletions.
136 changes: 64 additions & 72 deletions packages/layout-tiling/index.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,84 @@
/// <reference types="vite/client" />
import {
IGeometry,
LayoutFunction,
LayoutPluginConfig,
WindowPosition,
addLayoutResult,
windowIsDialog,
} from "@bond-wm/shared";
import { IGeometry, LayoutPluginConfig, WindowPosition, addLayoutResult, windowIsDialog } from "@bond-wm/shared";
import TilingIcon from "./tiling.png";

const TilingLayout: LayoutFunction = ({ windows, screen }) => {
const results = new Map<number, IGeometry>();
/** A tiling layout for bond-wm. */
const Plugin: LayoutPluginConfig = {
name: "Tiling",
icon: TilingIcon,
supportsMaximize: false,
fn: ({ windows, screen }) => {
const results = new Map<number, IGeometry>();

const { workArea } = screen;
const { workArea } = screen;

const windowsWithinGrid = [];
const windowsWithinGrid = [];

for (const win of windows) {
if (win.fullscreen) {
addLayoutResult(results, win, screen, {
x: 0,
y: 0,
width: screen.width,
height: screen.height,
});
} else if (windowIsDialog(win)) {
// Center the window.
addLayoutResult(results, win, screen, {
x: workArea.x + Math.max(0, Math.floor((workArea.width - win.outer.width) / 2)),
y: workArea.y + Math.max(0, Math.floor((workArea.height - win.outer.height) / 2)),
width: win.outer.width,
height: win.outer.height,
});
} else if (win.position === WindowPosition.UserPositioned) {
addLayoutResult(results, win, screen, {
x: win.outer.x,
y: win.outer.y,
width: win.outer.width,
height: win.outer.height,
});
} else {
windowsWithinGrid.push(win);
for (const win of windows) {
if (win.fullscreen) {
addLayoutResult(results, win, screen, {
x: 0,
y: 0,
width: screen.width,
height: screen.height,
});
} else if (windowIsDialog(win)) {
// Center the window.
addLayoutResult(results, win, screen, {
x: workArea.x + Math.max(0, Math.floor((workArea.width - win.outer.width) / 2)),
y: workArea.y + Math.max(0, Math.floor((workArea.height - win.outer.height) / 2)),
width: win.outer.width,
height: win.outer.height,
});
} else if (win.position === WindowPosition.UserPositioned) {
addLayoutResult(results, win, screen, {
x: win.outer.x,
y: win.outer.y,
width: win.outer.width,
height: win.outer.height,
});
} else {
windowsWithinGrid.push(win);
}
}
}

const GridWidth = 2;
const GridHeight = Math.ceil(windowsWithinGrid.length / GridWidth);
const GridCellWidth = Math.floor(workArea.width / GridWidth);
const GridCellHeight = Math.floor(workArea.height / GridHeight);
let x = 0;
let y = 0;
for (let i = 0; i < windowsWithinGrid.length; i++) {
const win = windowsWithinGrid[i];
const GridWidth = 2;
const GridHeight = Math.ceil(windowsWithinGrid.length / GridWidth);
const GridCellWidth = Math.floor(workArea.width / GridWidth);
const GridCellHeight = Math.floor(workArea.height / GridHeight);
let x = 0;
let y = 0;
for (let i = 0; i < windowsWithinGrid.length; i++) {
const win = windowsWithinGrid[i];

// When the last window is odd out, give it the full remaining Grid width.
// This assumes GridWidth == 2 but could be generalized if needed.
if (i === windowsWithinGrid.length - 1 && windowsWithinGrid.length % 2 !== 0) {
addLayoutResult(results, win, screen, {
x: workArea.x + x * GridCellWidth,
y: workArea.y + y * GridCellHeight,
width: workArea.width,
height: GridCellHeight,
});
continue;
}

// When the last window is odd out, give it the full remaining Grid width.
// This assumes GridWidth == 2 but could be generalized if needed.
if (i === windowsWithinGrid.length - 1 && windowsWithinGrid.length % 2 !== 0) {
addLayoutResult(results, win, screen, {
x: workArea.x + x * GridCellWidth,
y: workArea.y + y * GridCellHeight,
width: workArea.width,
width: GridCellWidth,
height: GridCellHeight,
});
continue;
}

addLayoutResult(results, win, screen, {
x: workArea.x + x * GridCellWidth,
y: workArea.y + y * GridCellHeight,
width: GridCellWidth,
height: GridCellHeight,
});

x++;
if (x >= GridWidth) {
x = 0;
y++;
x++;
if (x >= GridWidth) {
x = 0;
y++;
}
}
}

return results;
return results;
},
};

/** A tiling layout for bond-wm. */
const Plugin: LayoutPluginConfig = {
name: "Tiling",
icon: TilingIcon,
supportsMaximize: false,
fn: TilingLayout,
};
export default Plugin;

0 comments on commit faabf34

Please sign in to comment.