mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
feat(native): Android tablets get the shortcuts, two panes meet at a foldable's fold
The Android shell now reports keyCommands and windowLayout, so the large screen layout no longer assumes an iPad: - The keyboard host hands the shortcuts to the app whenever the shell has the keyCommands feature, whatever the platform. Before it also required iOS, so an Android tablet with a keyboard answered the keys in the page and never listed them in the system's shortcuts. - useWindowFold follows the layout the Android shell reports (feature windowLayout). When a vertical fold separates the window, a foldable half open like a book, MobileShell marks it with data-fold and its place, and the leading pane (the trip's plan, a tab's summary, a list beside its detail) ends at the fold while the trailing one starts there, on either reading direction. A flat fold that does not separate stays one screen. - The split size class starts at 840pt, Android's expanded width, instead of 900pt, so a foldable unfolded on its side (about 841dp) gets two panes like an iPad does. An 11" iPad upright (834pt) stays a single pane. The address screen of the native shell follows the same width. - TrekShell exports NativeWindowLayout for it. Also: the sheet's width and anchor types and the collab board type stay private to their modules, which nothing else imports (knip).
This commit is contained in:
@@ -170,7 +170,7 @@ export interface SharedFilePayload {
|
||||
* pixels, Android's size classes, and a foldable's posture with the hinge's
|
||||
* place in the window. `posture` is none on a device without a fold.
|
||||
*/
|
||||
interface NativeWindowLayout {
|
||||
export interface NativeWindowLayout {
|
||||
width: number;
|
||||
height: number;
|
||||
widthClass: 'compact' | 'medium' | 'expanded';
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
// FE-NATIVE-FOLD-001 onwards: the fold an Android foldable reports, as two-pane screens use it.
|
||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import type { NativeWindowLayout } from './trekShell';
|
||||
import { _resetWindowFold, foldOf, useWindowFold } from './windowFold';
|
||||
|
||||
const shell = vi.hoisted(() => ({
|
||||
features: [] as string[],
|
||||
layout: null as unknown,
|
||||
listener: null as null | ((layout: unknown) => void),
|
||||
}));
|
||||
|
||||
vi.mock('./capabilities', () => ({
|
||||
hasNativeFeature: (feature: string) => shell.features.includes(feature),
|
||||
useNativeCapabilities: () => ({
|
||||
caps: { shellApi: 3, platform: 'android', appVersion: '', build: '', features: shell.features },
|
||||
updateRequired: false,
|
||||
}),
|
||||
}));
|
||||
vi.mock('./trekShell', () => ({
|
||||
TrekShell: {
|
||||
getWindowLayout: vi.fn(() => Promise.resolve(shell.layout)),
|
||||
addListener: vi.fn((_event: string, fn: (layout: unknown) => void) => {
|
||||
shell.listener = fn;
|
||||
return Promise.resolve({ remove: () => Promise.resolve() });
|
||||
}),
|
||||
},
|
||||
}));
|
||||
|
||||
function layout(over: Partial<NativeWindowLayout> = {}): NativeWindowLayout {
|
||||
return {
|
||||
width: 841,
|
||||
height: 701,
|
||||
widthClass: 'expanded',
|
||||
heightClass: 'medium',
|
||||
posture: 'halfOpened',
|
||||
orientation: 'vertical',
|
||||
separating: true,
|
||||
hinge: { left: 420, top: 0, right: 421, bottom: 701 },
|
||||
...over,
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
_resetWindowFold();
|
||||
shell.features = [];
|
||||
shell.layout = null;
|
||||
shell.listener = null;
|
||||
});
|
||||
|
||||
afterEach(() => _resetWindowFold());
|
||||
|
||||
describe('foldOf', () => {
|
||||
it('FE-NATIVE-FOLD-001: a book posture splits the window at its hinge', () => {
|
||||
expect(foldOf(layout())).toEqual({ start: 420, end: 421 });
|
||||
});
|
||||
|
||||
it('FE-NATIVE-FOLD-002: a seamless flat fold, a horizontal one or none at all splits nothing', () => {
|
||||
expect(foldOf(layout({ posture: 'flat', separating: false }))).toBeNull();
|
||||
expect(foldOf(layout({ orientation: 'horizontal' }))).toBeNull();
|
||||
expect(foldOf(layout({ posture: 'none', hinge: undefined }))).toBeNull();
|
||||
expect(foldOf(null)).toBeNull();
|
||||
// A hinge outside the window is not one to lay panes against.
|
||||
expect(foldOf(layout({ hinge: { left: 0, top: 0, right: 0, bottom: 701 } }))).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('useWindowFold', () => {
|
||||
it('FE-NATIVE-FOLD-003: without the shell feature there is no fold to follow', () => {
|
||||
const { result } = renderHook(() => useWindowFold());
|
||||
expect(result.current).toBeNull();
|
||||
expect(shell.listener).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-NATIVE-FOLD-004: follows the layout the Android shell reports, as the device folds and unfolds', async () => {
|
||||
shell.features = ['windowLayout'];
|
||||
shell.layout = layout();
|
||||
const { result } = renderHook(() => useWindowFold());
|
||||
await waitFor(() => expect(result.current).toEqual({ start: 420, end: 421 }));
|
||||
|
||||
act(() => shell.listener?.(layout({ posture: 'flat', separating: false })));
|
||||
expect(result.current).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
import { useEffect, useSyncExternalStore } from 'react';
|
||||
|
||||
import { hasNativeFeature, useNativeCapabilities } from './capabilities';
|
||||
import { reportNativeFailure } from './reportFailure';
|
||||
import { TrekShell, type NativeWindowLayout } from './trekShell';
|
||||
|
||||
/** A fold down the window, in CSS pixels from its left edge: where it begins and ends. */
|
||||
export interface WindowFold {
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The fold a two-pane screen should respect: a vertical one that separates the
|
||||
* window into two sides, as a foldable half open like a book has, or a device
|
||||
* whose two screens meet there. A flat fold that does not separate (a Galaxy
|
||||
* Fold opened all the way) is one seamless screen, and a horizontal one runs
|
||||
* across both panes rather than between them.
|
||||
*/
|
||||
export function foldOf(layout: NativeWindowLayout | null): WindowFold | null {
|
||||
if (!layout?.hinge || layout.posture === 'none') return null;
|
||||
if (layout.orientation !== 'vertical' || !layout.separating) return null;
|
||||
const { left, right } = layout.hinge;
|
||||
if (!(left > 0 && right >= left && right < layout.width)) return null;
|
||||
return { start: left, end: right };
|
||||
}
|
||||
|
||||
let layout: NativeWindowLayout | null = null;
|
||||
let listening = false;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function publish(next: NativeWindowLayout): void {
|
||||
layout = next;
|
||||
for (const listener of listeners) listener();
|
||||
}
|
||||
|
||||
/** Starts following the window layout the Android shell reports (feature windowLayout), once. */
|
||||
function follow(): void {
|
||||
if (listening || !hasNativeFeature('windowLayout')) return;
|
||||
listening = true;
|
||||
TrekShell.getWindowLayout().then(publish, reportNativeFailure);
|
||||
TrekShell.addListener('windowLayout', publish).catch(reportNativeFailure);
|
||||
}
|
||||
|
||||
function subscribe(listener: () => void): () => void {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
}
|
||||
|
||||
const current = () => layout;
|
||||
|
||||
/**
|
||||
* The fold of the window, while the app's shell reports one (Android, feature
|
||||
* windowLayout); null in a browser, on iOS and on a device without one. Two-pane
|
||||
* screens end their leading pane at it, so no row runs across the hinge.
|
||||
*/
|
||||
export function useWindowFold(): WindowFold | null {
|
||||
const { caps } = useNativeCapabilities();
|
||||
const supported = caps.features.includes('windowLayout');
|
||||
useEffect(() => {
|
||||
if (supported) follow();
|
||||
}, [supported]);
|
||||
const reported = useSyncExternalStore(subscribe, current, current);
|
||||
return supported ? foldOf(reported) : null;
|
||||
}
|
||||
|
||||
/** Test helper: forget the layout and the subscription. */
|
||||
export function _resetWindowFold(): void {
|
||||
layout = null;
|
||||
listening = false;
|
||||
listeners.clear();
|
||||
}
|
||||
Reference in New Issue
Block a user