Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/web-ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,11 @@
}, 1200);
}

var isWindows = (
(typeof navigator.userAgent === 'string' && navigator.userAgent.toUpperCase().includes('WINDOWS'))
|| (typeof navigator.platform === 'string' && navigator.platform.toUpperCase().includes('WIN'))
);
if (controls && window.__OPENBITFUN_SHOW_STARTUP_WINDOW_CONTROLS__) {
var isWindows = (
(typeof navigator.userAgent === 'string' && navigator.userAgent.toUpperCase().includes('WINDOWS'))
|| (typeof navigator.platform === 'string' && navigator.platform.toUpperCase().includes('WIN'))
);
controls.classList.toggle('window-controls--windows', isWindows);
controls.querySelectorAll('[data-startup-window-control-platform]').forEach(function (glyph) {
if (glyph.getAttribute('data-startup-window-control-platform') !== (isWindows ? 'windows' : 'generic')) {
Expand Down Expand Up @@ -570,12 +570,17 @@
});
}
}
function isStartupOverlayInteractive() {
return startupOverlay && startupOverlay.isConnected && !startupOverlay.hasAttribute('inert');
}
function invokeWindowControl(action) {
if (!tauri || typeof tauri.invoke !== 'function') return;
var requestVersion = ++stateRequestVersion;
tauri.invoke('startup_window_control', { request: { action: action } })
.then(function (response) {
if (requestVersion === stateRequestVersion) applyWindowState(response);
if (requestVersion === stateRequestVersion && isStartupOverlayInteractive()) {
applyWindowState(response);
}
})
.catch(function () {});
}
Expand All @@ -588,17 +593,16 @@
var windowLabel = currentWindow.label;
function canUseStartupWindowGesture(event) {
return !event.defaultPrevented && event.button === 0
&& event.target === startupOverlay && startupOverlay.isConnected
&& !startupOverlay.hasAttribute('inert');
&& event.target === startupOverlay && isStartupOverlayInteractive();
}
startupOverlay.addEventListener('mousedown', function (event) {
if (!canUseStartupWindowGesture(event) || event.detail > 1) return;
Promise.all([
tauri.invoke('plugin:window|is_maximized', { label: windowLabel }),
tauri.invoke('plugin:window|is_fullscreen', { label: windowLabel })
]).then(function (states) {
if (states[0] !== false || states[1] !== false
|| !startupOverlay.isConnected || startupOverlay.hasAttribute('inert')) return;
if (typeof states[0] !== 'boolean' || (states[0] && !isWindows) || states[1] !== false
|| !isStartupOverlayInteractive()) return;
return tauri.invoke('plugin:window|start_dragging', { label: windowLabel });
}).catch(function () {});
});
Expand All @@ -617,10 +621,26 @@
invokeWindowControl(action);
});
});
invokeWindowControl('get_state');
window.setTimeout(function () {
if (controls.isConnected) invokeWindowControl('get_state');
}, 120);
function refreshWindowState() {
if (isStartupOverlayInteractive()) invokeWindowControl('get_state');
}
// The native caption drag can restore or snap the window after IPC returns.
var resizeFrame = null;
function scheduleWindowStateRefresh() {
if (resizeFrame !== null || !isStartupOverlayInteractive()) return;
resizeFrame = window.requestAnimationFrame(function () {
resizeFrame = null;
refreshWindowState();
});
}
refreshWindowState();
var initialStateTimer = window.setTimeout(refreshWindowState, 120);
window.addEventListener('resize', scheduleWindowStateRefresh);
window.addEventListener('openbitfun:startup-overlay-hidden', function () {
window.removeEventListener('resize', scheduleWindowStateRefresh);
window.clearTimeout(initialStateTimer);
if (resizeFrame !== null) window.cancelAnimationFrame(resizeFrame);
}, { once: true });
}
})();
</script>
Expand Down
67 changes: 63 additions & 4 deletions src/web-ui/src/app/components/SceneTopBar/SceneTopBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('SceneTopBar', () => {
vi.clearAllMocks();
isMaximized.mockResolvedValue(false);
isFullscreen.mockResolvedValue(false);
vi.stubGlobal('navigator', { platform: 'Linux x86_64', userAgent: 'Linux' });
vi.stubGlobal('__TAURI_INTERNALS__', {
invoke: vi.fn(),
metadata: { currentWindow: { label: 'main' } },
Expand Down Expand Up @@ -189,7 +190,9 @@ describe('SceneTopBar', () => {
expect(maximize).toHaveBeenCalledTimes(3);
});

it('excludes the whole tab from window gestures as other tabs open and close', async () => {
it.each([false, true])('excludes the whole tab on Windows with maximized=%s as other tabs open and close', async maximized => {
vi.stubGlobal('navigator', { platform: 'Win32', userAgent: 'Windows' });
isMaximized.mockResolvedValue(maximized);
for (const tabCount of [1, 2, 1]) {
const toolbar = renderBar(tabCount);
const tab = toolbar.querySelector('[role="tab"]')!;
Expand All @@ -204,7 +207,9 @@ describe('SceneTopBar', () => {
}
});

it('leaves multi-tab labels and item padding to tab interaction', async () => {
it.each([false, true])('leaves Windows multi-tab labels and item padding to tab interaction with maximized=%s', async maximized => {
vi.stubGlobal('navigator', { platform: 'Win32', userAgent: 'Windows' });
isMaximized.mockResolvedValue(maximized);
const toolbar = renderBar(2);
const tab = toolbar.querySelector<HTMLButtonElement>('[role="tab"][data-openbitfun-value="1"]')!;
for (const target of [tab, tab.querySelector('span')!, tab.closest('[data-openbitfun-part="item"]')!]) {
Expand All @@ -217,7 +222,9 @@ describe('SceneTopBar', () => {
expect(maximize).not.toHaveBeenCalled();
});

it.each([1, 2])('excludes close buttons, nested SVGs, scene actions and editable fields with %i tabs', async tabCount => {
it.each([1, 2])('excludes close buttons, nested SVGs, scene actions and editable fields with %i tabs on maximized Windows', async tabCount => {
vi.stubGlobal('navigator', { platform: 'Win32', userAgent: 'Windows' });
isMaximized.mockResolvedValue(true);
const toolbar = renderBar(tabCount);
for (const target of toolbar.querySelectorAll('button:not([role="tab"]), svg, path, input, [contenteditable] span')) {
await mouseDown(target);
Expand All @@ -229,6 +236,58 @@ describe('SceneTopBar', () => {
expect(maximize).not.toHaveBeenCalled();
});

it('keeps maximized Windows panel tabs, padding and close icons outside window gestures', async () => {
vi.stubGlobal('navigator', { platform: 'Win32', userAgent: 'Windows' });
isMaximized.mockResolvedValue(true);
const { TabGroup } = await import('@openbitfun/ui');
const select = vi.fn();
const close = vi.fn();
act(() => root.render(<SceneTopBar panelOpen onMaximize={maximize} panelTabs={
<TabGroup value="0" onValueChange={select} items={[
{ value: '0', label: <span>First panel</span> },
{ value: '1', label: <span>Second panel</span>, endAction:
<button aria-label="Close panel" onClick={close}><svg><path d="M0 0L1 1" /></svg></button> },
]} />
} />));
const panel = host.querySelector('.openbitfun-scene-top-bar__panel-region')!;
for (const target of panel.querySelectorAll('[role="tab"], [data-openbitfun-part="item"], span, button, svg, path')) {
await mouseDown(target);
doubleClick(target);
}
act(() => panel.querySelector<HTMLButtonElement>('[role="tab"][data-openbitfun-value="1"]')!.click());
act(() => panel.querySelector<HTMLButtonElement>('[aria-label="Close panel"]')!.click());
expect(select).toHaveBeenCalledWith('1');
expect(close).toHaveBeenCalledOnce();
expect(startDragging).not.toHaveBeenCalled();
expect(maximize).not.toHaveBeenCalled();
});

it('delegates maximized Windows blank chrome to native dragging without rerendering', async () => {
vi.stubGlobal('navigator', { platform: 'Win32', userAgent: 'Windows' });
const toolbar = renderBar(2);
await mouseDown(toolbar);
expect(startDragging).toHaveBeenCalledOnce();

isMaximized.mockResolvedValue(true);
await mouseDown(toolbar);
expect(startDragging).toHaveBeenCalledTimes(2);
expect(isMaximized).toHaveBeenCalledTimes(2);
expect(isFullscreen).toHaveBeenCalledTimes(2);
});

it.each([false, true])('blocks Windows fullscreen with maximized=%s and allows dragging after leaving it without rerendering', async maximized => {
vi.stubGlobal('navigator', { platform: 'Win32', userAgent: 'Windows' });
isMaximized.mockResolvedValue(maximized);
isFullscreen.mockResolvedValue(true);
const toolbar = renderBar(2);
await mouseDown(toolbar);
expect(startDragging).not.toHaveBeenCalled();

isFullscreen.mockResolvedValue(false);
await mouseDown(toolbar);
expect(startDragging).toHaveBeenCalledOnce();
});

it('uses click detail to preserve double-click maximize without blocking separate drags', async () => {
const toolbar = renderBar(2);
await mouseDown(toolbar);
Expand All @@ -245,7 +304,7 @@ describe('SceneTopBar', () => {
{ maximized: true, fullscreen: false },
{ maximized: false, fullscreen: true },
{ maximized: true, fullscreen: true },
])('blocks dragging while maximized=$maximized/fullscreen=$fullscreen and restores it without rerendering', async ({ maximized, fullscreen }) => {
])('keeps non-Windows dragging blocked while maximized=$maximized/fullscreen=$fullscreen and restores it without rerendering', async ({ maximized, fullscreen }) => {
const toolbar = renderBar(2);
await mouseDown(toolbar);
expect(startDragging).toHaveBeenCalledOnce();
Expand Down
159 changes: 150 additions & 9 deletions src/web-ui/src/app/startup/startupPreload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,84 @@ describe('startup preload shell', () => {
dom.window.close();
});

it('refreshes the maximize button when a Windows caption drag restores the startup window', async () => {
let isMaximized = true;
const invoke = vi.fn(async (command: string, payload: { request?: { action: string } }) => {
if (command === 'startup_window_control' && payload.request?.action === 'get_state') return { isMaximized };
if (command === 'plugin:window|is_maximized') return isMaximized;
if (command === 'plugin:window|is_fullscreen') return false;
if (command === 'plugin:window|start_dragging') return;
throw new Error(`Unexpected window command: ${command}`);
});
const dom = new JSDOM(readIndexHtml(), {
url: 'http://localhost:1422/',
runScripts: 'dangerously',
pretendToBeVisual: true,
beforeParse(window) {
Object.defineProperty(window.navigator, 'platform', { value: 'Win32' });
Object.assign(window, {
__OPENBITFUN_BOOTSTRAP_LOCALE__: 'en-US',
__OPENBITFUN_SHOW_STARTUP_WINDOW_CONTROLS__: true,
__TAURI_INTERNALS__: { invoke, metadata: { currentWindow: { label: 'main' } } },
});
},
});

try {
const { window } = dom;
const overlay = window.document.getElementById('openbitfun-startup-overlay')!;
const controls = overlay.querySelector('[data-startup-window-controls]')!;
const button = controls.querySelector('[data-startup-window-action="toggle_maximize"]')!;
const getStateCalls = () => invoke.mock.calls.filter(([command, payload]) =>
command === 'startup_window_control' && payload.request?.action === 'get_state',
);
const resize = () => window.dispatchEvent(new window.Event('resize'));
const nextFrame = () => new Promise<void>(resolve => window.requestAnimationFrame(() => resolve()));

await vi.waitFor(() => expect(getStateCalls()).toHaveLength(2));
expect(button.getAttribute('aria-label')).toBe('Restore');
expect(button.querySelector('svg:not([style*="display: none"])')?.classList).toContain('lucide-copy');

overlay.dispatchEvent(new window.MouseEvent('mousedown', { bubbles: true, button: 0, detail: 1 }));
await vi.waitFor(() => expect(invoke).toHaveBeenCalledWith('plugin:window|start_dragging', { label: 'main' }));

// The native window changes size after the drag request has been accepted.
isMaximized = false;
resize();
resize();
await nextFrame();
expect(controls.hasAttribute('data-openbitfun-state')).toBe(false);
expect(button.getAttribute('aria-label')).toBe('Maximize');
expect(button.getAttribute('title')).toBe('Maximize');
expect(button.querySelector('svg:not([style*="display: none"])')?.classList).toContain('lucide-square');
expect(getStateCalls()).toHaveLength(3);

resize();
overlay.setAttribute('inert', '');
await nextFrame();
expect(getStateCalls()).toHaveLength(3);

resize();
window.dispatchEvent(new window.Event('openbitfun:startup-overlay-hidden'));
overlay.remove();
resize();
await nextFrame();
expect(getStateCalls()).toHaveLength(3);
} finally {
dom.window.close();
}
});

it.each([
{ maximized: true, fullscreen: false, showControls: true },
{ maximized: false, fullscreen: true, showControls: true },
{ maximized: true, fullscreen: true, showControls: true },
{ maximized: true, fullscreen: false, showControls: false },
])('guards pre-React dragging with maximized=$maximized/fullscreen=$fullscreen/controls=$showControls', async ({ maximized, fullscreen, showControls }) => {
...['Win32', 'Linux x86_64', 'MacIntel'].flatMap(platform => [
{ platform, maximized: false, fullscreen: false, showControls: true },
{ platform, maximized: true, fullscreen: false, showControls: true },
{ platform, maximized: false, fullscreen: true, showControls: true },
{ platform, maximized: true, fullscreen: true, showControls: true },
]),
{ platform: 'Win32', maximized: true, fullscreen: false, showControls: false },
{ platform: 'Win32', maximized: true, fullscreen: true, showControls: false },
])('guards pre-React $platform dragging with maximized=$maximized/fullscreen=$fullscreen/controls=$showControls', async ({ platform, maximized, fullscreen, showControls }) => {
const state = { maximized, fullscreen };
const invoke = vi.fn(async (command: string, payload: { request?: { action: string } }) => {
if (command === 'plugin:window|is_maximized') return state.maximized;
Expand All @@ -174,6 +246,8 @@ describe('startup preload shell', () => {
url: 'http://localhost:1422/',
runScripts: 'dangerously',
beforeParse(window) {
Object.defineProperty(window.navigator, 'platform', { value: platform });
Object.defineProperty(window.navigator, 'userAgent', { value: '' });
Object.assign(window, {
__OPENBITFUN_SHOW_STARTUP_WINDOW_CONTROLS__: showControls,
__TAURI_INTERNALS__: { invoke, metadata: { currentWindow: { label: 'main' } } },
Expand All @@ -192,7 +266,9 @@ describe('startup preload shell', () => {
expect(dom.window.document.querySelector('[data-tauri-drag-region]')).toBeNull();
mouseDown(overlay);
await flush();
expect(dragCalls()).toHaveLength(0);
const initiallyAllowed = !fullscreen && (!maximized || platform === 'Win32');
const initialDragCount = initiallyAllowed ? 1 : 0;
expect(dragCalls()).toHaveLength(initialDragCount);

// Restore through the static shell, without React or a cached resize update.
state.fullscreen = false;
Expand All @@ -202,20 +278,85 @@ describe('startup preload shell', () => {
expect(state.maximized).toBe(false);
mouseDown(overlay);
await flush();
expect(dragCalls()).toEqual([['plugin:window|start_dragging', { label: 'main' }]]);
expect(dragCalls()).toEqual(Array.from({ length: initialDragCount + 1 }, () =>
['plugin:window|start_dragging', { label: 'main' }],
));

mouseDown(overlay, { button: 1 });
mouseDown(overlay, { button: 2 });
mouseDown(overlay, { detail: 2 });
mouseDown(overlay.querySelector('[data-startup-window-action="toggle_maximize"] svg')!);
await flush();
expect(dragCalls()).toHaveLength(1);
expect(dragCalls()).toHaveLength(initialDragCount + 1);

// A pending state read must not start dragging after the startup handoff.
mouseDown(overlay);
overlay.setAttribute('inert', '');
await flush();
expect(dragCalls()).toHaveLength(1);
expect(dragCalls()).toHaveLength(initialDragCount + 1);

overlay.removeAttribute('inert');
mouseDown(overlay);
overlay.remove();
await flush();
expect(dragCalls()).toHaveLength(initialDragCount + 1);
} finally {
dom.window.close();
}
});

it.each(['browser', 'missing-metadata'])('does not expose startup gestures in a %s runtime', async runtime => {
const invoke = vi.fn(async () => false);
const dom = new JSDOM(readIndexHtml(), {
url: 'http://localhost:1422/',
runScripts: 'dangerously',
beforeParse(window) {
Object.defineProperty(window.navigator, 'platform', { value: 'Win32' });
if (runtime === 'missing-metadata') {
Object.assign(window, { __TAURI_INTERNALS__: { invoke } });
}
},
});
try {
const overlay = dom.window.document.getElementById('openbitfun-startup-overlay')!;
overlay.dispatchEvent(new dom.window.MouseEvent('mousedown', { bubbles: true, button: 0, detail: 1 }));
overlay.dispatchEvent(new dom.window.MouseEvent('dblclick', { bubbles: true, button: 0, detail: 2 }));
await new Promise(resolve => setTimeout(resolve, 0));
expect(invoke).not.toHaveBeenCalled();
} finally {
dom.window.close();
}
});

it.each([
{ maximized: undefined, fullscreen: false },
{ maximized: 'false', fullscreen: false },
{ maximized: false, fullscreen: undefined },
{ maximized: false, fullscreen: 'false' },
])('requires boolean startup window state with maximized=$maximized/fullscreen=$fullscreen', async ({ maximized, fullscreen }) => {
const invoke = vi.fn(async (command: string) => {
if (command === 'plugin:window|is_maximized') return maximized;
if (command === 'plugin:window|is_fullscreen') return fullscreen;
if (command === 'plugin:window|start_dragging') return;
throw new Error(`Unexpected window command: ${command}`);
});
const dom = new JSDOM(readIndexHtml(), {
url: 'http://localhost:1422/',
runScripts: 'dangerously',
beforeParse(window) {
Object.defineProperty(window.navigator, 'platform', { value: 'Win32' });
Object.assign(window, {
__TAURI_INTERNALS__: { invoke, metadata: { currentWindow: { label: 'main' } } },
});
},
});
try {
const overlay = dom.window.document.getElementById('openbitfun-startup-overlay')!;
overlay.dispatchEvent(new dom.window.MouseEvent('mousedown', { bubbles: true, button: 0, detail: 1 }));
await new Promise(resolve => setTimeout(resolve, 0));
expect(invoke.mock.calls.map(([command]) => command)).toEqual([
'plugin:window|is_maximized', 'plugin:window|is_fullscreen',
]);
} finally {
dom.window.close();
}
Expand Down
Loading