Something went wrong. Try again.
A reusable desktop shell: your sidecar, your index, safe APIs, installers for every platform
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748// Where a window goes when it opens again: the bounds it had last time, if// they still land on a screen that is there. Pure, so the tests can feed it// displays of their own.
// How much of the window must show for the old place to count.const MIN_VISIBLE = { width: 120, height: 80 };
const overlap = (a, b) => ({ width: Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x), height: Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y),});
const isBounds = (b) => b && ['x', 'y', 'width', 'height'].every((k) => Number.isFinite(b[k])) && b.width > 0 && b.height > 0;
// The saved bounds, fitted to the display they mostly sit on; null when no// display shows enough of them (unplugged monitor, say), or they make no sense.export function fitBounds (saved, displays) { if (!isBounds(saved) || !displays?.length) return null; let best = null; for (const d of displays) { const area = d.workArea ?? d.bounds; if (!area) continue; const o = overlap(saved, area); if (o.width < MIN_VISIBLE.width || o.height < MIN_VISIBLE.height) continue; if (!best || o.width * o.height > best.size) best = { area, size: o.width * o.height }; } if (!best) return null; const { area } = best; const width = Math.min(saved.width, area.width); const height = Math.min(saved.height, area.height); // Keep the window inside the work area it is on, so a title bar never hides under a menu bar. const x = Math.max(area.x, Math.min(saved.x, area.x + area.width - width)); const y = Math.max(area.y, Math.min(saved.y, area.y + area.height - height)); return { x: Math.round(x), y: Math.round(y), width: Math.round(width), height: Math.round(height) };}
// What to remember of a window: its normal bounds (not the maximised or// full-screen ones), its state, and the display it is on.export function windowState (win, screen) { const bounds = win.getNormalBounds(); return { bounds, maximized: win.isMaximized(), fullScreen: win.isFullScreen(), display: screen?.getDisplayMatching?.(bounds)?.id ?? null, };}