also looking at this
feat: portable typed window controls on Linux and Windows
#326
9 files
+652
-32
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -137,12 +137,12 @@ The next multi-window milestone needs product decisions in addition to code: | ||
| 137 | 137 | application-level asynchronous source above. |
| 138 | 138 | 3. Decide whether Touch Bar should stay primary-window scoped or follow the |
| 139 | 139 | key window with separately owned item definitions and callbacks. |
| 140 | 4. Finish advanced Linux/Windows window actions and clarify which are portable | |
| 141 | versus platform-specific, without conflating close semantics: macOS retains | |
| 142 | a closed page, while Linux and Windows release it. | |
| 143 | 5. Expand platform-native integration beyond the current named-window lifecycle | |
| 144 | smokes. CI runs Linux WebKitGTK under Xvfb and Windows WebView2 on a native | |
| 145 | runner, but advanced actions and packaged-app behavior still need coverage. | |
| 140 | 4. Decide the portability contract for advanced actions beyond bounds, | |
| 141 | centering, resizability, size limits and fullscreen. Closing still differs: | |
| 142 | macOS retains a closed page, while Linux and Windows release it. | |
| 143 | 5. Expand platform-native integration beyond named-window lifecycle and core | |
| 144 | geometry/control smokes. CI runs Linux WebKitGTK under Xvfb and Windows | |
| 145 | WebView2 on a native runner; packaged-app behavior still needs coverage. | |
| 146 | 146 | |
| 147 | 147 | ## Review checklist |
| 148 | 148 | |
| @@ -32,8 +32,10 @@ The `craft-native` SDK returns a typed `Window` handle. Runtime-created | ||
| 32 | 32 | windows are available on macOS, Linux, and Windows and must provide either |
| 33 | 33 | `html` or `url` content. The common create, close, title, geometry, and event |
| 34 | 34 | routes are cross-platform; some advanced window controls remain macOS-only. |
| 35 | On Linux, the window manager may ignore position requests, especially before | |
| 36 | the window is shown. | |
| 35 | Bounds, centering, resizing constraints, resizability and fullscreen controls | |
| 36 | also work on the typed handle on all three desktop platforms. On Linux, | |
| 37 | position and fullscreen requests are advisory to the window manager (and | |
| 38 | global positioning can be unavailable under Wayland). | |
| 37 | 39 | |
| 38 | 40 | ```typescript |
| 39 | 41 | import { createWindow } from 'craft-native' |
| @@ -62,7 +64,19 @@ const settings = await createWindow({ | ||
| 62 | 64 | }) |
| 63 | 65 | |
| 64 | 66 | await settings.setTitle('Settings — Account') |
| 65 | ``` | |
| 67 | await settings.setBounds({ x: 120, y: 80, width: 800 }) | |
| 68 | await settings.setMinimumSize(480, 360) | |
| 69 | await settings.setMaximumSize(1600, 1200) | |
| 70 | await settings.setResizable(false) | |
| 71 | const canResize = await settings.isResizable() | |
| 72 | await settings.center() | |
| 73 | await settings.setFullscreen(true) | |
| 74 | await settings.toggleFullscreen() | |
| 75 | ``` | |
| 76 | ||
| 77 | The bounds update preserves omitted coordinates and dimensions. Native window | |
| 78 | managers can adjust requested geometry for decorations or screen constraints; | |
| 79 | read `getBounds()` after a resize event if you need the actual result. | |
| 66 | 80 | |
| 67 | 81 | The older `createWindow(html, options)` overload remains available when the |
| 68 | 82 | content is already in a string. |
| @@ -11,6 +11,7 @@ const platformName = isWindows ? 'Windows' : 'Linux' | ||
| 11 | 11 | |
| 12 | 12 | const mainPage = `<!doctype html><script> |
| 13 | 13 | (async () => { |
| 14 | const isWindows = ${isWindows} | |
| 14 | 15 | const report = (step) => fetch('/report?step=' + encodeURIComponent(step), { method: 'POST' }) |
| 15 | 16 | const waitFor = async (step) => { |
| 16 | 17 | for (let attempt = 0; attempt < 200; attempt++) { |
| @@ -24,6 +25,23 @@ const mainPage = `<!doctype html><script> | ||
| 24 | 25 | name: 'settings', title: 'Child ' + cycle, |
| 25 | 26 | url: location.origin + '/child?cycle=' + cycle, |
| 26 | 27 | }) |
| 28 | const call = (action, data = {}, id = 'settings') => window.craft.window._call(action, data, id) | |
| 29 | const waitSize = async (predicate, label) => { | |
| 30 | for (let attempt = 0; attempt < 40; attempt++) { | |
| 31 | const size = await call('getSize') | |
| 32 | if (predicate(size)) return size | |
| 33 | await new Promise(resolve => setTimeout(resolve, 100)) | |
| 34 | } | |
| 35 | throw new Error(label + ': child size never reached expected bounds') | |
| 36 | } | |
| 37 | const waitWindowEvent = (name) => new Promise((resolve, reject) => { | |
| 38 | const timeout = setTimeout(() => reject(new Error(name + ' event missing')), 10000) | |
| 39 | window.addEventListener('craft:window:' + name, (event) => { | |
| 40 | if (event.detail.windowId !== 'settings') return | |
| 41 | clearTimeout(timeout) | |
| 42 | resolve() | |
| 43 | }, { once: true }) | |
| 44 | }) | |
| 27 | 45 | try { |
| 28 | 46 | if (!window.craft || !window.craft.window || !window.craft.window.open) |
| 29 | 47 | throw new Error('document-start Craft bridge missing') |
| @@ -47,6 +65,49 @@ const mainPage = `<!doctype html><script> | ||
| 47 | 65 | const title = await window.craft.window._call('getTitle', {}, 'settings') |
| 48 | 66 | if (title !== 'Child 1') throw new Error('child title not routed to creator: ' + title) |
| 49 | 67 | |
| 68 | const mainBefore = await call('getBounds', {}, 'main') | |
| 69 | await call('setBounds', { x: bounds.x + 20, y: bounds.y + 15, width: 700, height: 500 }) | |
| 70 | await waitSize(size => Math.abs(size.width - 700) <= 40 && Math.abs(size.height - 500) <= 40, 'setBounds') | |
| 71 | const moved = await call('getBounds') | |
| 72 | if (isWindows && (Math.abs(moved.x - bounds.x - 20) > 16 || Math.abs(moved.y - bounds.y - 15) > 16)) | |
| 73 | throw new Error('setBounds did not move the addressed Windows child') | |
| 74 | await call('center') | |
| 75 | const centered = await call('getBounds') | |
| 76 | if (!Number.isFinite(centered.x) || !Number.isFinite(centered.y)) | |
| 77 | throw new Error('center returned invalid child coordinates') | |
| 78 | await call('setResizable', { resizable: false }) | |
| 79 | if (await call('isResizable') !== false) throw new Error('child stayed resizable') | |
| 80 | await call('setResizable', { resizable: true }) | |
| 81 | if (await call('isResizable') !== true) throw new Error('child stayed fixed-size') | |
| 82 | await call('setMinimumSize', { width: 600, height: 420 }) | |
| 83 | await call('setMaximumSize', { width: 820, height: 620 }) | |
| 84 | await call('setSize', { width: 300, height: 200 }) | |
| 85 | await waitSize(size => size.width >= 580 && size.height >= 400, 'minimum size') | |
| 86 | await call('setSize', { width: 1000, height: 800 }) | |
| 87 | await waitSize(size => size.width <= 860 && size.height <= 660, 'maximum size') | |
| 88 | await call('setMinimumSize', { width: 1, height: 1 }) | |
| 89 | await call('setMaximumSize', { width: 4096, height: 4096 }) | |
| 90 | await call('setSize', { width: 700, height: 500 }) | |
| 91 | await waitSize(size => Math.abs(size.width - 700) <= 40 && Math.abs(size.height - 500) <= 40, 'restored size') | |
| 92 | if (isWindows) { | |
| 93 | const entered = waitWindowEvent('enter-fullscreen') | |
| 94 | await call('setFullscreen', { fullscreen: true }) | |
| 95 | await entered | |
| 96 | const left = waitWindowEvent('leave-fullscreen') | |
| 97 | await call('toggleFullscreen') | |
| 98 | await left | |
| 99 | } | |
| 100 | else { | |
| 101 | // A bare Xvfb server has no window manager to honor fullscreen requests. | |
| 102 | await call('setFullscreen', { fullscreen: true }) | |
| 103 | await call('toggleFullscreen') | |
| 104 | await call('setFullscreen', { fullscreen: false }) | |
| 105 | } | |
| 106 | const mainAfter = await call('getBounds', {}, 'main') | |
| 107 | if (Math.abs(mainAfter.width - mainBefore.width) > 40 || Math.abs(mainAfter.height - mainBefore.height) > 40) | |
| 108 | throw new Error('child controls changed the main window size') | |
| 109 | await report('controls') | |
| 110 | ||
| 50 | 111 | const [queuedA, queuedB] = await Promise.all([ |
| 51 | 112 | window.craft.window.open({ name: 'queue-a', title: 'Child queue-a', url: location.origin + '/child?cycle=queue-a' }), |
| 52 | 113 | window.craft.window.open({ name: 'queue-b', title: 'Child queue-b', url: location.origin + '/child?cycle=queue-b' }), |
| @@ -151,7 +212,7 @@ const server = createServer((request, response) => { | ||
| 151 | 212 | if (step) steps.add(step) |
| 152 | 213 | response.end('ok') |
| 153 | 214 | if (step === 'done') { |
| 154 | const required = ['main', 'child-1', 'child-queue-a', 'child-queue-b', 'queued-closed', 'closed', 'child-2', 'done'] | |
| 215 | const required = ['main', 'child-1', 'controls', 'child-queue-a', 'child-queue-b', 'queued-closed', 'closed', 'child-2', 'done'] | |
| 155 | 216 | if (required.every(name => steps.has(name))) resolveDone() |
| 156 | 217 | else rejectDone(new Error(`incomplete smoke steps: ${[...steps].join(', ')}`)) |
| 157 | 218 | } |