import { clampToWorkArea, mapRect, physicalSize, pickDisplayAtPoint, placeOnDisplay, unionRect, } from "../shared/display"; function escape(text) { return text.replace(/[&<>"]/g, (char) => { if (char === "&") return "&"; if (char === "<") return "<"; if (char === ">") return ">"; return """; }); } function fmt(rect) { return `${rect.x},${rect.y} · ${rect.width}×${rect.height}`; } function time(at) { return new Date(at).toLocaleTimeString("zh-CN", { hour12: false }); } function within(rect, area) { return (rect.x >= area.x - 1 && rect.y >= area.y - 1 && rect.x + rect.width <= area.x + area.width + 1 && rect.y + rect.height <= area.y + area.height + 1); } function mapSvg(displays, windowRect, preview, width = 340, height = 180) { const rects = displays.map((display) => display.bounds); if (windowRect) rects.push(windowRect); if (preview) rects.push(preview); const union = unionRect(rects); const parts = displays.map((display) => { const rect = mapRect(display.bounds, union, width, height, 12); const cls = display.primary ? "map-primary" : "map-secondary"; return ` ${escape(display.label)} · ${display.bounds.width}×${display.bounds.height} @${display.scaleFactor}x #${display.id} ${display.scaleFactor}x `; }); if (windowRect) { const rect = mapRect(windowRect, union, width, height, 12); parts.push(``); } if (preview) { const rect = mapRect(preview, union, width, height, 12); parts.push(``); } return `${parts.join("")}`; } export function mountApp(root) { root.innerHTML = `
多显示器与无边框工作台 拖动区域 · -webkit-app-region: drag
—

显示器

0 块
    主屏 其它屏 当前窗口

    模拟多显示器

    负坐标 / 竖屏 / 不同 DPI

      坐标实验

      纯函数,不依赖真实多屏

        窗口形态

        normal
        最大化— 全屏— 简单全屏— 沉浸式— bounds— contentBounds— 缩放—

        无边框与拖拽区

        frame: false
        按住这里拖动窗口 drag
        标题栏 — · 控件 —

        按钮、输入框与可点击元素必须 no-drag,否则点击会被拖拽吞掉; 双击标题栏切换最大化。

        事件

        显示器变化 / 移动 / 全屏

          就绪

          `; const displayList = root.querySelector('[data-testid="display-list"]'); const simList = root.querySelector('[data-testid="sim-list"]'); const realMap = root.querySelector('[data-testid="real-map"]'); const simMap = root.querySelector('[data-testid="sim-map"]'); const experiment = root.querySelector('[data-testid="experiment"]'); const eventsEl = root.querySelector('[data-testid="events"]'); const platformPill = root.querySelector('[data-testid="platform-pill"]'); const displayCount = root.querySelector('[data-testid="display-count"]'); const modeEl = root.querySelector('[data-testid="mode"]'); const stateMax = root.querySelector('[data-testid="state-maximized"]'); const stateFull = root.querySelector('[data-testid="state-fullscreen"]'); const stateSimple = root.querySelector('[data-testid="state-simple"]'); const stateKiosk = root.querySelector('[data-testid="state-kiosk"]'); const stateBounds = root.querySelector('[data-testid="state-bounds"]'); const stateContent = root.querySelector('[data-testid="state-content"]'); const stateScale = root.querySelector('[data-testid="state-scale"]'); const dragCss = root.querySelector('[data-testid="drag-css"]'); const noDragCss = root.querySelector('[data-testid="no-drag-css"]'); const status = root.querySelector('[data-testid="status"]'); const titlebar = root.querySelector('[data-testid="titlebar"]'); const windowControls = root.querySelector('[data-testid="window-controls"]'); let state = null; let selectedSimId = 101; function renderDisplays() { if (!state || !displayList) return; if (displayCount) displayCount.textContent = `${state.displays.length} 块`; displayList.innerHTML = state.displays .map((display) => `
        • #${display.id} ${escape(display.label)} ${display.primary ? `主屏` : ""} ${display.internal ? `内置` : `外接`} ${display.scaleFactor}x ${display.rotation ? `旋转 ${display.rotation}°` : ""} bounds ${fmt(display.bounds)} · workArea ${fmt(display.workArea)}
        • `) .join(""); if (realMap) { realMap.innerHTML = mapSvg(state.displays, state.window.bounds, null); } } function renderSim() { if (!state || !simList) return; const selected = state.simulated.find((item) => item.id === selectedSimId) ?? state.simulated[0]; const preview = selected ? placeOnDisplay(selected, { width: Math.min(1000, state.window.bounds.width), height: Math.min(700, state.window.bounds.height), }) : null; if (simMap) { simMap.innerHTML = mapSvg(state.simulated, null, preview); } simList.innerHTML = state.simulated .map((display) => `
        • ${display.rotation ? `${display.rotation}°` : "横向"}
        • `) .join(""); if (experiment) { const primary = state.simulated.find((item) => item.primary) ?? state.simulated[0]; const portrait = state.simulated.find((item) => item.rotation === 90) ?? state.simulated[2]; const left = state.simulated[0]; if (!primary || !portrait || !left) return; const negative = clampToWorkArea({ x: -500, y: -300, width: 1200, height: 900 }, primary.workArea); const overflow = clampToWorkArea({ x: 1800, y: 1000, width: 4000, height: 3000 }, primary.workArea); const leftHit = pickDisplayAtPoint(state.simulated, { x: -1000, y: 500 }); const portraitHit = pickDisplayAtPoint(state.simulated, { x: 2400, y: 800, }); const centered = placeOnDisplay(portrait, { width: 1000, height: 700 }); const physical = physicalSize(primary.bounds, primary.scaleFactor); const lines = [ ["clamp(-500,-300,1200×900)", fmt(negative)], ["clamp(1800,1000,4000×3000)", fmt(overflow)], ["pick(-1000,500)", `#${leftHit?.id ?? "?"} ${leftHit?.label ?? ""}`], [ "pick(2400,800)", `#${portraitHit?.id ?? "?"} ${portraitHit?.label ?? ""}`, ], ["center 竖屏 1000×700", fmt(centered)], ["physical 1920×1080 @2x", `${physical.width}×${physical.height}`], ]; experiment.innerHTML = lines .map(([label, value]) => `
        • ${escape(label)}${escape(value)}
        • `) .join(""); } } function renderWindow() { if (!state) return; const win = state.window; if (platformPill) platformPill.textContent = `${state.platform} · Electron ${state.electronVersion}`; if (modeEl) modeEl.textContent = win.mode; if (stateMax) stateMax.textContent = String(win.isMaximized); if (stateFull) stateFull.textContent = String(win.isFullScreen); if (stateSimple) stateSimple.textContent = String(win.isSimpleFullScreen); if (stateKiosk) stateKiosk.textContent = String(win.isKiosk); if (stateBounds) stateBounds.textContent = fmt(win.bounds); if (stateContent) stateContent.textContent = fmt(win.contentBounds); if (stateScale) stateScale.textContent = `${win.scaleFactor}x`; if (dragCss && titlebar) { dragCss.textContent = getComputedStyle(titlebar).getPropertyValue("-webkit-app-region") || "—"; } if (noDragCss && windowControls) { noDragCss.textContent = getComputedStyle(windowControls).getPropertyValue("-webkit-app-region") || "—"; } root .querySelectorAll("[data-mode]") .forEach((button) => { const mode = button.dataset.mode; button.classList.toggle("active", mode === win.mode); if (mode === "simple") button.disabled = state?.platform !== "darwin"; }); root .querySelector('[data-testid="win-max"]') ?.classList.toggle("active", win.isMaximized); } function renderEvents() { if (!state || !eventsEl) return; eventsEl.innerHTML = state.events.length === 0 ? `
        • 暂无事件
        • ` : state.events .slice(0, 7) .map((event) => `
        • ${time(event.at)} ${escape(event.kind)} ${escape(event.detail)}
        • `) .join(""); } function render(next) { state = next; renderDisplays(); renderSim(); renderWindow(); renderEvents(); } void window.api.display.snapshot().then(render); const unsubscribe = window.api.display.onEvent(() => { void window.api.display.snapshot().then(render); }); const setMode = async (mode) => { try { render(await window.api.display.setMode(mode)); if (status) status.textContent = `窗口形态:${mode}`; } catch { if (status) status.textContent = "该平台不支持此形态,见事件日志"; render(await window.api.display.refresh()); } }; root.querySelectorAll("[data-mode]").forEach((button) => { button.addEventListener("click", () => { const mode = button.dataset.mode; if (mode) void setMode(mode); }); }); displayList?.addEventListener("click", async (event) => { const button = event.target.closest("[data-move]"); const id = Number(button?.dataset.move); if (Number.isInteger(id)) { render(await window.api.display.moveTo(id)); if (status) status.textContent = `已移到显示器 #${id}`; } }); simList?.addEventListener("click", (event) => { const button = event.target.closest("[data-sim]"); const id = Number(button?.dataset.sim); if (Number.isInteger(id) && state) { selectedSimId = id; render(state); } }); titlebar?.addEventListener("dblclick", (event) => { if (event.target === titlebar || event.target.closest(".title-block")) { void window.api.display.toggleMaximize().then(render); } }); root .querySelector('[data-testid="win-min"]') ?.addEventListener("click", () => { void window.api.display.minimize(); }); root .querySelector('[data-testid="win-max"]') ?.addEventListener("click", async () => { render(await window.api.display.toggleMaximize()); }); root .querySelector('[data-testid="win-close"]') ?.addEventListener("click", () => { void window.api.display.close(); }); const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); window.__prepareShots = async (stage) => { document.body.dataset.shot = stage; if (stage !== "frameless") { await window.api.display.setMode("normal"); await sleep(250); } const base = await window.api.display.snapshot(); const primary = base.displays.find((item) => item.primary); if (primary) await window.api.display.moveTo(primary.id); selectedSimId = stage === "bounds" ? 103 : 101; await sleep(250); render(await window.api.display.refresh()); }; window.__smoke = async () => { const api = window.api.display; const first = await api.snapshot(); const primary = first.displays.find((item) => item.primary); if (!primary) throw new Error("NO_PRIMARY_DISPLAY"); await api.moveTo(primary.id); await sleep(150); const afterMove = await api.snapshot(); const moveInside = within(afterMove.window.bounds, primary.workArea); await api.center(); await sleep(150); const afterCenter = await api.snapshot(); const centerInside = within(afterCenter.window.bounds, primary.workArea); await api.toggleMaximize(); await sleep(450); const maxOn = (await api.snapshot()).window.isMaximized; await api.toggleMaximize(); await sleep(450); const maxOff = !(await api.snapshot()).window.isMaximized; await api.setMode("simple"); await sleep(250); const simpleOn = (await api.snapshot()).window.isSimpleFullScreen; await api.setMode("normal"); await sleep(350); const simpleOff = !(await api.snapshot()).window.isSimpleFullScreen; await api.setMode("kiosk"); await sleep(250); const kioskOn = (await api.snapshot()).window.isKiosk; await api.setMode("normal"); await sleep(350); const kioskOff = !(await api.snapshot()).window.isKiosk; const final = await api.snapshot(); const sim = final.simulated; if (sim.length < 3) throw new Error("BAD_SIMULATED_DISPLAYS"); const primarySim = sim.find((item) => item.primary) ?? sim[0]; const portrait = sim.find((item) => item.rotation === 90) ?? sim[2]; if (!primarySim || !portrait) throw new Error("BAD_SIMULATED_DISPLAYS"); const negative = clampToWorkArea({ x: -500, y: -300, width: 1200, height: 900 }, primarySim.workArea); const clampNegOk = negative.x >= primarySim.workArea.x && negative.y >= primarySim.workArea.y && negative.x + negative.width <= primarySim.workArea.x + primarySim.workArea.width && negative.y + negative.height <= primarySim.workArea.y + primarySim.workArea.height; const overflow = clampToWorkArea({ x: 1800, y: 1000, width: 4000, height: 3000 }, primarySim.workArea); const clampBigOk = overflow.width === primarySim.workArea.width && overflow.height === primarySim.workArea.height; const pickLeft = pickDisplayAtPoint(sim, { x: -1000, y: 500 })?.id === sim[0]?.id; const pickPortrait = pickDisplayAtPoint(sim, { x: 2400, y: 800 })?.id === portrait.id; const centered = placeOnDisplay(portrait, { width: 1000, height: 700 }); const centerPortraitOk = within(centered, portrait.workArea); const physical = physicalSize(primarySim.bounds, primarySim.scaleFactor); const physicalOk = physical.width === primarySim.bounds.width * primarySim.scaleFactor && physical.height === primarySim.bounds.height * primarySim.scaleFactor; const titlebarStyle = titlebar ? getComputedStyle(titlebar).getPropertyValue("-webkit-app-region") : ""; const controlsStyle = windowControls ? getComputedStyle(windowControls).getPropertyValue("-webkit-app-region") : ""; unsubscribe(); return { platform: first.platform, displays: first.displays.length, primaryId: first.primaryId, primaryScale: primary.scaleFactor, moveInside, centerInside, dragCss: titlebarStyle, noDragCss: controlsStyle, maximizeOn: maxOn, maximizeOff: maxOff, simpleOn, simpleOff, kioskOn, kioskOff, clampNegOk, clampBigOk, pickLeft, pickPortrait, centerPortraitOk, physicalOk, simulated: sim.length, events: final.events.length, }; }; } //# sourceMappingURL=App.js.map