Glanceboard has a 2x3 layout option

This commit is contained in:
James Babcock committed 2026-09-17 22:59:00 -07:00
1 parent bed21c9a5f
commit 17274746a7
8 files changed
+234 -36

No files matched your search

+1
View File
@@ -7,6 +7,7 @@
* Refinements to builtin games
* Optimize bluetooth message transport
* Short-the-long gesture from screen off activates the glanceboard
* Glanceboard has a 2x3 layout option
## 0.7.0 - 2026-09-12
+15 -5
View File
@@ -1,7 +1,7 @@
import { GrayImage } from "../../graphics/image";
import { onAnySettingChanged } from "../../ui/dashboard-settings";
import { glanceShowLinesSetting, glanceSlotSettings } from "./glanceboard-settings";
import { QUADRANT_LAYOUT, resolveGlanceRegions, slotDividers, type GlanceLayout, type GlanceRegion } from "./layout";
import { glanceLayout, glanceShowLinesSetting, glanceSlotSettings } from "./glanceboard-settings";
import { resolveGlanceRegions, slotDividers, type GlanceLayout, type GlanceRegion } from "./layout";
import { type GlanceWidget } from "./widget";
import { findGlanceWidget, glanceWidgetSpans } from "./widgets";
@@ -27,11 +27,18 @@ export class GlanceBoard {
private regions: LiveRegion[] = [];
private started = false;
private unsubscribeSettings: (() => void) | null = null;
private currentLayout: GlanceLayout;
constructor(
private readonly requestRender: () => void,
readonly layout: GlanceLayout = QUADRANT_LAYOUT,
) {}
private readonly fixedLayout?: GlanceLayout,
) {
this.currentLayout = fixedLayout ?? glanceLayout();
}
get layout(): GlanceLayout {
return this.currentLayout;
}
get width(): number {
return this.layout.width;
@@ -65,9 +72,12 @@ export class GlanceBoard {
/** Rebuild the live regions from the settings, keeping widgets whose region is unchanged. */
private applySlotSettings(): void {
if (!this.started) return;
const layout = this.fixedLayout ?? glanceLayout();
const layoutChanged = layout !== this.currentLayout;
this.currentLayout = layout;
const previous = new Map(this.regions.map((live) => [live.key, live]));
const next: LiveRegion[] = [];
let changed = false;
let changed = layoutChanged;
for (const region of this.currentRegions()) {
const key = regionKey(region);
const kept = previous.get(key);
+51 -21
View File
@@ -1,7 +1,7 @@
import { GrayImage } from "../../graphics/image";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { enumSettingMenuItem, toggleSettingMenuItem } from "../../ui/dashboard-settings";
import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from "../../ui/dashboard-settings";
import { type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
import { drawSelectionHighlight, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu";
@@ -12,6 +12,8 @@ import { GlanceBoard } from "./board";
import {
clearConflictingSlots,
glanceboardEnabledSetting,
glanceLayout,
glanceLayoutSetting,
glanceShowLinesSetting,
glanceShowOnHeadTiltSetting,
glanceShowOnLongPressSetting,
@@ -20,7 +22,7 @@ import {
glanceTapDurationSetting,
type GlanceSlotChoice,
} from "./glanceboard-settings";
import { QUADRANT_LAYOUT, resolveGlanceRegions, slotDividers } from "./layout";
import { resolveGlanceRegions, slotDividers } from "./layout";
import { glanceWidgetSpans } from "./widgets";
export const GLANCEBOARD_WINDOW_ID = "glanceboard";
@@ -49,24 +51,33 @@ function pageMenuLayout(width: number) {
* and the menu (enable toggle, live preview, slot contents, settings). Right
* half: a scaled preview of the board's slots with their dividers and the
* name of the widget in each. "Select Contents" moves focus into that
* preview, where scroll picks a quadrant and a tap opens the picker for it;
* preview, where scroll picks a slot and a tap opens the picker for it;
* a double-tap returns focus to the menu.
*/
class GlanceboardHomeLayer implements Layer {
private menu: MenuLayer | null = null;
private menuTop = 0;
private menuHeight = 0;
private focus: "menu" | "preview" = "menu";
private selectedSlot = 0;
constructor(private readonly setPreviewVisible: (visible: boolean) => void) {}
private currentMenu(ctx: LayerContext, menuTop: number): MenuLayer {
if (!this.menu || this.menuTop !== menuTop) {
const { width, height } = ctx.stack.getBaseSize();
const { width, height } = ctx.stack.getBaseSize();
if (!this.menu || this.menuTop !== menuTop || this.menuHeight !== height) {
const half = (width / 2) | 0;
const items: MenuItem[] = [
toggleSettingMenuItem(glanceboardEnabledSetting),
{
label: "Preview",
onSelect: (menuCtx) => menuCtx.stack.push(new GlancePreviewLayer(() => menuCtx.actions.requestRender())),
onSelect: (menuCtx) => {
menuCtx.stack.push(new GlancePreviewLayer(
() => menuCtx.actions.requestRender(),
() => this.setPreviewVisible(false),
));
this.setPreviewVisible(true);
},
},
{
label: "Select Contents",
@@ -80,6 +91,7 @@ class GlanceboardHomeLayer implements Layer {
},
];
this.menuTop = menuTop;
this.menuHeight = height;
this.menu = new MenuLayer(null, items, {
x: 8, y: menuTop, width: half - 16,
showBorder: false, minHeight: 0, maxHeight: height - menuTop,
@@ -92,9 +104,10 @@ class GlanceboardHomeLayer implements Layer {
private previewRect(width: number, height: number): { x: number; y: number; width: number; height: number } {
const half = (width / 2) | 0;
const availableWidth = width - half - 2 * PREVIEW_MARGIN;
const scale = Math.min(availableWidth / QUADRANT_LAYOUT.width, (height - 2 * PREVIEW_MARGIN) / QUADRANT_LAYOUT.height);
const previewWidth = Math.round(QUADRANT_LAYOUT.width * scale);
const previewHeight = Math.round(QUADRANT_LAYOUT.height * scale);
const layout = glanceLayout();
const scale = Math.min(availableWidth / layout.width, (height - 2 * PREVIEW_MARGIN) / layout.height);
const previewWidth = Math.round(layout.width * scale);
const previewHeight = Math.round(layout.height * scale);
return {
x: half + PREVIEW_MARGIN + (((availableWidth - previewWidth) / 2) | 0),
y: ((height - previewHeight) / 2) | 0,
@@ -123,6 +136,7 @@ class GlanceboardHomeLayer implements Layer {
const menuCtx: LayerContext = { ...ctx, stack: Object.assign(Object.create(ctx.stack), { isFocused: () => menuFocused }) };
menu.paint(menuCtx, () => image);
this.selectedSlot = Math.min(this.selectedSlot, glanceLayout().slots.length - 1);
const preview = this.previewRect(width, height);
drawSlotPreview(image, preview, {
selectedSlot: this.selectedSlot,
@@ -133,7 +147,8 @@ class GlanceboardHomeLayer implements Layer {
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (this.focus === "preview") {
const slotCount = QUADRANT_LAYOUT.slots.length;
const slotCount = glanceLayout().slots.length;
this.selectedSlot = Math.min(this.selectedSlot, slotCount - 1);
switch (event.type) {
case "scroll-up":
this.selectedSlot = (this.selectedSlot + slotCount - 1) % slotCount;
@@ -166,7 +181,7 @@ class GlanceboardHomeLayer implements Layer {
* The board's slots scaled into `rect`: dividers (full brightness, or the
* dimmest dotted line when "Show lines" is off), the widget name centred in
* each region (a merged double-height region has one name and no line
* across it), and the selected quadrant highlighted.
* across it), and the selected slot highlighted.
*/
function drawSlotPreview(
image: GrayImage,
@@ -174,7 +189,7 @@ function drawSlotPreview(
options: { selectedSlot: number; selectionFocused: boolean },
): void {
const font = getDefaultSmallFont();
const layout = QUADRANT_LAYOUT;
const layout = glanceLayout();
const settings = glanceSlotSettings(layout);
const choices = settings.map((setting) => setting.get());
const regions = resolveGlanceRegions(layout, choices, glanceWidgetSpans);
@@ -232,14 +247,15 @@ function drawDottedLine(image: GrayImage, x0: number, y0: number, x1: number, y1
/** The picker for one slot: every widget, the current one marked; choosing applies the one-per-board rule. */
function openSlotPicker(ctx: LayerContext, slotIndex: number): void {
const setting = glanceSlotSettings(QUADRANT_LAYOUT)[slotIndex];
const layout = glanceLayout();
const setting = glanceSlotSettings(layout)[slotIndex];
if (!setting) return;
const current = setting.get();
const items: MenuItem[] = setting.values.map((value) => ({
label: setting.displayValue(value),
onSelect: (menuCtx) => {
setting.set(value);
clearConflictingSlots(QUADRANT_LAYOUT, slotIndex, value);
clearConflictingSlots(layout, slotIndex, value);
menuCtx.stack.pop();
},
render: ({ image, x, y }) => {
@@ -249,7 +265,7 @@ function openSlotPicker(ctx: LayerContext, slotIndex: number): void {
openModalMenu(ctx, setting.label, items, Math.max(0, setting.values.indexOf(current)));
}
/** Settings: which sleep gestures show the board, for how long, and the slot lines. */
/** Settings: layout, sleep gestures, duration, and slot lines. */
class GlanceSettingsLayer implements Layer {
private menu: MenuLayer | null = null;
@@ -257,6 +273,7 @@ class GlanceSettingsLayer implements Layer {
if (!this.menu) {
const { width } = ctx.stack.getBaseSize();
const items: MenuItem[] = [
enumSettingMenuItem(glanceLayoutSetting),
enumSettingMenuItem(glanceTapDurationSetting),
toggleSettingMenuItem(glanceShowOnLongPressSetting),
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
@@ -280,7 +297,7 @@ class GlanceSettingsLayer implements Layer {
class GlancePreviewLayer implements Layer {
private readonly board: GlanceBoard;
constructor(requestRender: () => void) {
constructor(requestRender: () => void, private readonly onClose: () => void) {
this.board = new GlanceBoard(requestRender);
this.board.start();
}
@@ -302,27 +319,40 @@ class GlancePreviewLayer implements Layer {
onRemoved(): void {
this.board.stop();
this.onClose();
}
}
export function createGlanceboardAppWindow(options: InProcessAppOptions): InProcessWindow {
return createInProcessWindow({
let previewVisible = false;
const heightMode = () => previewVisible && glanceLayoutSetting.get() === "2x3" ? "max" as const : "medium" as const;
let unsubscribeSettings: (() => void) | undefined;
const app = createInProcessWindow({
appId: "glanceboard",
windowId: GLANCEBOARD_WINDOW_ID,
title: "Glanceboard",
iconLetter: "Gb",
icon: "eye",
closeable: true,
// 576x288: the board's own size, so the preview is pixel-exact.
heightMode: "medium",
// Settings and the contents picker stay in the default 576x288 viewport;
// only the live six-slot preview needs the taller window.
heightMode: heightMode(),
actions: options.actions,
// Not wrapped in YieldAtRootLayer: the home page routes double-click
// itself (preview focus -> menu focus, menu focus -> sidebar).
baseLayer: new GlanceboardHomeLayer(),
baseLayer: new GlanceboardHomeLayer((visible) => {
previewVisible = visible;
app.setHeightMode(heightMode());
}),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
reconfigureSurface: options.reconfigureSurface,
onClosed: options.onClosed,
onClosed: () => {
unsubscribeSettings?.();
options.onClosed();
},
});
unsubscribeSettings = onAnySettingChanged(() => app.setHeightMode(heightMode()));
return app;
}
+26 -7
View File
@@ -1,5 +1,5 @@
import { ConfigSettingBoolean, ConfigSettingEnum } from "../../ui/dashboard-settings";
import { QUADRANT_LAYOUT, slotsVerticallyAdjacent, type GlanceLayout } from "./layout";
import { QUADRANT_LAYOUT, SIX_SLOT_LAYOUT, slotsVerticallyAdjacent, type GlanceLayout } from "./layout";
import { type GlanceWidgetId } from "./widget";
import { glanceWidgetSpans } from "./widgets";
@@ -27,6 +27,19 @@ export function glanceSlotChoiceLabel(choice: GlanceSlotChoice): string {
*/
const DEFAULT_QUADRANT_CHOICES: readonly GlanceSlotChoice[] = ["system-card", "calendar", "music", "calendar"];
export const glanceLayoutSetting = new ConfigSettingEnum<"2x2" | "2x3">({
id: "glanceboard-layout",
label: "Layout",
storageKey: "glanceboard.layout",
defaultValue: "2x2",
values: ["2x2", "2x3"],
description: "Choose two columns with two or three rows of widgets.",
});
export function glanceLayout(): GlanceLayout {
return glanceLayoutSetting.get() === "2x3" ? SIX_SLOT_LAYOUT : QUADRANT_LAYOUT;
}
/**
* Whether sleep-time gestures show the board at all. Off restores the plain
* behaviour: a tap, hold or head-tilt while asleep does nothing (a head-tilt
@@ -100,17 +113,19 @@ export const glanceShowLinesSetting = new ConfigSettingBoolean({
});
/**
* One picker per layout slot, keyed by layout id and slot index so a future
* layout gets its own stored choices rather than inheriting the quadrants'.
* Both grid sizes share the original storage keys so changing row count
* preserves the first four choices and remembers the hidden bottom row.
*/
export function glanceSlotSettings(layout: GlanceLayout = QUADRANT_LAYOUT): ConfigSettingEnum<GlanceSlotChoice>[] {
export function glanceSlotSettings(layout: GlanceLayout = glanceLayout()): ConfigSettingEnum<GlanceSlotChoice>[] {
const grid = layout === QUADRANT_LAYOUT || layout === SIX_SLOT_LAYOUT;
const storageId = grid ? QUADRANT_LAYOUT.id : layout.id;
return layout.slots.map(
(slot, index) =>
new ConfigSettingEnum<GlanceSlotChoice>({
id: `glanceboard-${layout.id}-slot-${index}`,
label: slot.label,
storageKey: `glanceboard.${layout.id}.slot.${index}`,
defaultValue: (layout === QUADRANT_LAYOUT ? DEFAULT_QUADRANT_CHOICES[index] : undefined) ?? "none",
storageKey: `glanceboard.${storageId}.slot.${index}`,
defaultValue: (grid ? DEFAULT_QUADRANT_CHOICES[index] : undefined) ?? "none",
values: SLOT_CHOICES,
formatValue: glanceSlotChoiceLabel,
description: `Which widget fills the ${slot.label.toLowerCase()} slot of the Glanceboard.`,
@@ -129,9 +144,13 @@ export function clearConflictingSlots(layout: GlanceLayout, changedIndex: number
const settings = glanceSlotSettings(layout);
const spans = glanceWidgetSpans(choice);
const cleared: number[] = [];
let keptPartner = false;
settings.forEach((setting, index) => {
if (index === changedIndex || setting.get() !== choice) return;
if (spans && slotsVerticallyAdjacent(layout, changedIndex, index)) return;
if (spans && !keptPartner && slotsVerticallyAdjacent(layout, changedIndex, index)) {
keptPartner = true;
return;
}
setting.set("none");
cleared.push(index);
});
+6 -3
View File
@@ -3,12 +3,12 @@ import { GlanceBoard } from "./board";
import { createGlanceboardAppWindow, GLANCEBOARD_SURFACE_ID, GLANCEBOARD_WINDOW_ID } from "./glanceboard-app";
import {
glanceboardEnabledSetting,
glanceLayout,
glanceShowOnHeadTiltSetting,
glanceShowOnLongPressSetting,
glanceShowOnTap,
glanceTapTimeoutMs,
} from "./glanceboard-settings";
import { QUADRANT_LAYOUT } from "./layout";
const glanceboardApp: AppDefinition = {
appId: "glanceboard",
@@ -16,13 +16,16 @@ const glanceboardApp: AppDefinition = {
icon: "eye",
launch: (ctx) => ctx.launchInProcessApp(GLANCEBOARD_WINDOW_ID, GLANCEBOARD_SURFACE_ID, createGlanceboardAppWindow),
glanceboard: {
size: { width: QUADRANT_LAYOUT.width, height: QUADRANT_LAYOUT.height },
get size() {
const layout = glanceLayout();
return { width: layout.width, height: layout.height };
},
isEnabled: () => glanceboardEnabledSetting.get(),
showOnTap: glanceShowOnTap,
tapTimeoutMs: glanceTapTimeoutMs,
showOnLongPress: () => glanceShowOnLongPressSetting.get(),
showOnHeadTilt: () => glanceShowOnHeadTiltSetting.get(),
createBoard: (requestRender) => new GlanceBoard(requestRender, QUADRANT_LAYOUT),
createBoard: (requestRender) => new GlanceBoard(requestRender),
},
};
+14
View File
@@ -29,6 +29,20 @@ export const QUADRANT_LAYOUT: GlanceLayout = {
],
};
/** Six 288x144 slots: the same first two rows, plus a third at the bottom. */
export const SIX_SLOT_LAYOUT: GlanceLayout = {
id: "six-slots",
width: GLANCEBOARD_WIDTH,
height: 432,
slots: [
...QUADRANT_LAYOUT.slots.slice(0, 2),
{ label: "Middle left", rect: { x: 0, y: 144, width: 288, height: 144 } },
{ label: "Middle right", rect: { x: 288, y: 144, width: 288, height: 144 } },
{ label: "Bottom left", rect: { x: 0, y: 288, width: 288, height: 144 } },
{ label: "Bottom right", rect: { x: 288, y: 288, width: 288, height: 144 } },
],
};
/** Whether two slots stack vertically with matching width (one region if merged). */
export function slotsVerticallyAdjacent(layout: GlanceLayout, a: number, b: number): boolean {
const first = layout.slots[a]?.rect;
+22
View File
@@ -57,3 +57,25 @@ test("dividers: left edges of the right column, top edges of the bottom row, min
{ x0: 288, y0: 144, x1: 575, y1: 144 },
]);
});
test("2x3 adds a bottom row without moving or resizing the first four slots", () => {
const { SIX_SLOT_LAYOUT } = require("../.test-build/app/apps/glanceboard/layout.js");
assert.equal(SIX_SLOT_LAYOUT.width, 576);
assert.equal(SIX_SLOT_LAYOUT.height, 432);
assert.deepEqual(SIX_SLOT_LAYOUT.slots.slice(0, 4).map((slot) => slot.rect), QUADRANT_LAYOUT.slots.map((slot) => slot.rect));
assert.deepEqual(SIX_SLOT_LAYOUT.slots.slice(4).map((slot) => slot.rect), [
{ x: 0, y: 288, width: 288, height: 144 },
{ x: 288, y: 288, width: 288, height: 144 },
]);
const regions = resolveGlanceRegions(SIX_SLOT_LAYOUT, ["none", "none", "terminal", "none", "terminal", "music"], spans);
assert.deepEqual(regions, [
{ choice: "terminal", rect: { x: 0, y: 144, width: 288, height: 288 }, slots: [2, 4] },
{ choice: "music", rect: { x: 288, y: 288, width: 288, height: 144 }, slots: [5] },
]);
const { slotDividers } = require("../.test-build/app/apps/glanceboard/layout.js");
const bottomDividers = slotDividers(SIX_SLOT_LAYOUT, regions).filter((line) => line.y0 === 288);
assert.deepEqual(bottomDividers, [
{ x0: 288, y0: 288, x1: 288, y1: 431 },
{ x0: 288, y0: 288, x1: 575, y1: 288 },
]);
});
+99
View File
@@ -0,0 +1,99 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const layout = require('../.test-build/app/apps/glanceboard/layout.js');
function load(file, dependencies) {
const context = { exports: {}, console, require: (name) => {
assert.ok(name in dependencies, name);
return dependencies[name];
} };
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
return context.exports;
}
function harness() {
const values = new Map();
const listeners = new Set();
class Setting {
constructor(options) { Object.assign(this, options); }
get() { return values.get(this.storageKey) ?? this.defaultValue; }
set(value) { values.set(this.storageKey, value); }
}
const config = {
ConfigSettingBoolean: Setting, ConfigSettingEnum: Setting,
onAnySettingChanged: (listener) => { listeners.add(listener); return () => listeners.delete(listener); },
};
const widgets = {
glanceWidgetSpans: (choice) => choice === 'calendar' || choice === 'terminal',
findGlanceWidget: () => ({ create: () => ({ start() {}, stop() {}, paint() {} }) }),
};
const settings = load('app/apps/glanceboard/glanceboard-settings.ts', {
'../../ui/dashboard-settings': config, './layout': layout, './widgets': widgets,
});
class GrayImage {
constructor(width, height) { this.width = width; this.height = height; }
drawLine() {}
composeInto() {}
}
const { GlanceBoard } = load('app/apps/glanceboard/board.ts', {
'../../graphics/image': { GrayImage }, '../../ui/dashboard-settings': config,
'./glanceboard-settings': settings, './layout': layout, './widgets': widgets,
});
return { settings, GlanceBoard, values, listeners, notify: () => [...listeners].forEach((listener) => listener()) };
}
test('layout defaults to 2x2; expanding preserves stored slots and remembers the extra row', () => {
const { settings: s, values } = harness();
values.set('glanceboard.quadrants.slot.0', 'compass');
assert.equal(s.glanceLayoutSetting.get(), '2x2');
assert.equal(s.glanceLayout(), layout.QUADRANT_LAYOUT);
s.glanceLayoutSetting.set('2x3');
assert.equal(s.glanceLayout(), layout.SIX_SLOT_LAYOUT);
assert.deepEqual(Array.from(s.glanceSlotSettings(), (slot) => slot.get()),
['compass', 'calendar', 'music', 'calendar', 'none', 'none']);
s.glanceSlotSettings()[5].set('nightscout');
s.glanceLayoutSetting.set('2x2');
assert.equal(s.glanceSlotSettings().length, 4);
s.glanceLayoutSetting.set('2x3');
assert.equal(s.glanceSlotSettings()[5].get(), 'nightscout');
});
test('a spanning widget in the middle row keeps only one adjacent partner', () => {
const { settings: s } = harness();
s.glanceLayoutSetting.set('2x3');
const slots = s.glanceSlotSettings();
for (const index of [0, 2, 4]) slots[index].set('terminal');
s.clearConflictingSlots(s.glanceLayout(), 2, 'terminal');
assert.deepEqual(Array.from(slots, (slot) => slot.get()),
['terminal', 'calendar', 'terminal', 'calendar', 'none', 'none']);
slots[4].set('terminal');
s.clearConflictingSlots(s.glanceLayout(), 4, 'terminal');
assert.equal(slots[0].get(), 'none');
assert.equal(slots[2].get(), 'terminal');
});
test('an active empty board resizes and requests a frame when the layout changes', () => {
const h = harness();
for (const slot of h.settings.glanceSlotSettings(layout.SIX_SLOT_LAYOUT)) slot.set('none');
let renders = 0;
const board = new h.GlanceBoard(() => renders++);
board.start();
assert.equal(board.paint().height, 288);
h.settings.glanceLayoutSetting.set('2x3');
h.notify();
assert.equal(board.paint().width, 576);
assert.equal(board.paint().height, 432);
assert.equal(renders, 1);
h.settings.glanceLayoutSetting.set('2x2');
h.notify();
assert.equal(board.paint().height, 288);
assert.equal(renders, 2);
board.stop();
assert.equal(h.listeners.size, 0);
});