Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
11 changes: 10 additions & 1 deletion config/scripts/mobile-web-app-session-terminal-init.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,17 @@ describeRender(
// `.xterm-screen` does: it is created by `term.open()`, which only ever runs from the
// document's `init`. The emulator run had the surface with zero children inside a correctly
// sized container, which is this element missing.
// Then the grid readable: xterm sizes its one-cell helper textarea only on a cursor move or
// resize, so a read the moment the screen exists can land before the replay has drained.
await page.waitForFunction(
() => document.querySelector('.xterm-screen') !== null,
() => {
const cell = document.querySelector('#terminal-surface .xterm-helper-textarea')
return (
document.querySelector('.xterm-screen') !== null &&
cell !== null &&
cell.getBoundingClientRect().width > 0
)
},
undefined,
{
timeout: 60_000,
Expand Down
32 changes: 18 additions & 14 deletions mobile/src/session/MobileSessionActiveContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function MobileSessionActiveContent({
toastAnimatedStyle,
createTabBusy
} = controller
return showLoadingState ? (
const content = showLoadingState ? (
<View style={styles.emptyState}>
<ActivityIndicator size="small" color={colors.textSecondary} />
</View>
Expand Down Expand Up @@ -192,19 +192,7 @@ export function MobileSessionActiveContent({
)}
</View>
) : (
<View
// Why: react-native-web observes onLayout only on a View that mounts with it; unkeyed, this reuses the loading View and never reports.
key="terminal-frame"
style={styles.terminalFrame}
onLayout={(e) => {
terminalFrameHeightRef.current = e.nativeEvent.layout.height
// Why: notify height imperatively so dock settling re-fits the PTY without rerendering SessionScreen.
const nextWidth = Math.round(e.nativeEvent.layout.width)
const nextHeight = Math.round(e.nativeEvent.layout.height)
setTerminalFrameWidth((prev) => (prev === nextWidth ? prev : nextWidth))
notifyTerminalFrameHeight(nextHeight)
}}
>
<View style={styles.terminalFrame}>
{terminals.map((terminal) => (
<TerminalPaneView
key={terminal.handle}
Expand Down Expand Up @@ -256,4 +244,20 @@ export function MobileSessionActiveContent({
)}
</View>
)
return (
<View
// Why: one frame under every branch; react-native-web observes onLayout only on a View that mounts with it.
style={styles.contentFrame}
onLayout={(e) => {
terminalFrameHeightRef.current = e.nativeEvent.layout.height
// Why: notify height imperatively so dock settling re-fits the PTY without rerendering SessionScreen.
const nextWidth = Math.round(e.nativeEvent.layout.width)
const nextHeight = Math.round(e.nativeEvent.layout.height)
setTerminalFrameWidth((prev) => (prev === nextWidth ? prev : nextWidth))
notifyTerminalFrameHeight(nextHeight)
}}
>
{content}
</View>
)
}
5 changes: 5 additions & 0 deletions mobile/src/session/mobile-session-frame-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ export const mobileSessionFrameStyles = StyleSheet.create({
height: 18,
backgroundColor: colors.borderSubtle
},
// The content slot every branch renders in, measured as the terminal's frame; clips nothing.
contentFrame: {
flex: 1,
minHeight: 0
},
terminalFrame: {
flex: 1,
minHeight: 0,
Expand Down
17 changes: 11 additions & 6 deletions mobile/src/session/mobile-session-route-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,18 @@ const HEAD_TIMER_CLEANUP_SHA256 = 'c73f1d1c2cc89642f3d727d6f3b6b81860a9d6f342345
// 529 -> 530, and the host-JSX hash: `key="terminal-frame"`, so the page's frame mounts with its
// onLayout rather than reusing the loading View. Native measured 47 rows before and after: its
// frame reported either way, and its window is its frame, so both measure paths agree there.
//
// 531 -> 530, and the host-JSX and style hashes: the key left, and one `contentFrame` View wraps
// every branch and carries the frame's onLayout, so the page's frame mounts with it. The page measured
// 47 rows before and after; native was measured only on main's bundle (47), and the wrapper is a
// flex:1 View around the same flex:1 frame, so its box is the frame's.
const HEAD_RUNTIME_STRING_SHA256 =
'1be5398cdbdf96b6f7738b63a0e19f536496844140a947939a7f71824b08fe50'
'7dd03af1ad61e2f394b8cba35a422f7195de60ae5d608923b52575a56a4dbe42'
// Moved by both of the dock's fields: their refs, and the live one's submit handler, are the seam's now.
const HEAD_HOST_JSX_SHA256 = 'ca4c8b46af86a05cb671de91c22111c9e4aaeefd4a04c7b8b09976ca01a31c9b'
const HEAD_HOST_JSX_SHA256 = '1478283a1597c88920aedfea9f6ed13d119ea8f93546e1628d0918cd0a5248ef'
const HEAD_LEAF_JSX_SHA256 = 'c7e1a4b90197697f1eaa640c38da63281b4f7b84fb036ae2152f00c2f7d7cb77'
const HEAD_STYLE_REFERENCE_SHA256 =
'295a3501c2c6d7bea7c8bbf38b3f3534f01344cd7e1b91bb8e07c040821d596a'
'56a005a1f65b30c11092e3422caef67810e1ec50f66fdd06471c370138b1eeb6'
const HEAD_IDENTITY_FIELD_SHA256 =
'91146853930a34dd1f3d80e5c97fbacd7cf19fb93dd26fe8fc6f29169622f9d6'
const HEAD_NAVIGATION_SHA256 = '9d96f5dad7de555d6553eac39c0fab00efad507470fd562cb9beaa32db16f512'
Expand Down Expand Up @@ -611,14 +616,14 @@ describe('mobile session route extraction parity', () => {

it('preserves runtime strings, styles, and the expanded JSX tree', () => {
const strings = readRuntimeStrings()
expect(strings).toHaveLength(531)
expect(strings).toHaveLength(530)
expect(hash(strings)).toBe(HEAD_RUNTIME_STRING_SHA256)
const jsx = readJsxFacts(readDefinitions())
expect(jsx.host).toHaveLength(124)
expect(jsx.host).toHaveLength(125)
expect(hash(jsx.host)).toBe(HEAD_HOST_JSX_SHA256)
expect(jsx.leaf).toHaveLength(61)
expect(hash(jsx.leaf)).toBe(HEAD_LEAF_JSX_SHA256)
expect(jsx.styleReferences).toHaveLength(172)
expect(jsx.styleReferences).toHaveLength(173)
expect(hash(jsx.styleReferences)).toBe(HEAD_STYLE_REFERENCE_SHA256)
})
})
35 changes: 33 additions & 2 deletions mobile/src/session/mobile-session-terminal-frame-layout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ import { MobileSessionActiveContent } from './MobileSessionActiveContent'

type Controller = Parameters<typeof MobileSessionActiveContent>[0]['controller']

type Branch = 'loading' | 'pending' | 'terminal'

function controller(
showLoadingState: boolean,
branch: Branch | boolean,
notifyTerminalFrameHeight: (height: number) => void
): Controller {
const shown = branch === true ? 'loading' : branch === false ? 'terminal' : branch
const scope = {
showLoadingState,
showLoadingState: shown === 'loading',
activePendingTerminalTab: shown === 'pending' ? { title: 'Loading terminal' } : null,
isPendingTerminalRecoveryParked: false,
showEmptyState: false,
terminals: [],
terminalFrameHeightRef: { current: 0 },
Expand Down Expand Up @@ -82,4 +87,30 @@ describe('the terminal frame on the page', () => {
})
expect(heights).toEqual([FRAME.height])
})

it('stays one mounted frame across loading, a pending terminal and the terminal', () => {
// A frame that remounts per branch reports again on every return; one that stays reports once.
const heights: number[] = []
const notify = (height: number): void => {
heights.push(height)
}
let renderer: ReturnType<typeof create> | undefined
const show = (branch: Branch) => {
const element = createElement(MobileSessionActiveContent, {
controller: controller(branch, notify)
})
act(() => {
if (renderer) {
renderer.update(element)
} else {
renderer = create(element)
}
})
}
show('loading')
show('terminal')
show('pending')
show('terminal')
expect(heights).toEqual([FRAME.height])
})
})
12 changes: 11 additions & 1 deletion mobile/src/terminal/TerminalWebView.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export const TerminalWebView = forwardRef<TerminalWebViewHandle, Props>(
// scrollback, and the controller's identity changes with every callback prop.
}, [generation])

const handleHostLayout = useCallback(() => {
documentRef.current?.notifyViewport()
}, [])

const handleReload = useCallback(() => {
clearEngineError()
resetReadiness()
Expand All @@ -112,7 +116,13 @@ export const TerminalWebView = forwardRef<TerminalWebViewHandle, Props>(

return (
<View style={[TERMINAL_WEBVIEW_FRAME_STYLES.container, props.style]}>
<View key={generation} ref={hostRef} style={TERMINAL_WEBVIEW_FRAME_STYLES.webview} />
{/* Why: mounted with onLayout, so react-native-web observes it; the document sizes to this box. */}
<View
key={generation}
ref={hostRef}
style={TERMINAL_WEBVIEW_FRAME_STYLES.webview}
onLayout={handleHostLayout}
/>
{engineError ? (
<TerminalWebViewEngineErrorOverlay message={engineError} onReload={handleReload} />
) : null}
Expand Down
16 changes: 11 additions & 5 deletions mobile/src/terminal/document/document-host-seams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ export type TerminalDocumentViewportRect = {
top: number
width: number
height: number
/** The host is `display:none` and the size is the last one it had; a fit waits for it to show. */
hidden?: boolean
}

/** What the host reports: a new box, or the same box back from being hidden. */
export type TerminalViewportChange = 'resized' | 'shown'

/** The document's runtime error reporter, taking the window error handler's own arguments. */
export type TerminalDocumentErrorReporter = (
message: string | (Event & { message?: unknown }),
Expand Down Expand Up @@ -65,8 +70,8 @@ export type TerminalDocumentHostSeams = {
hasEngine: () => boolean
/** Every fit, pan, scroll and overlay bound, and every client point mapped into the grid. */
viewportRect: () => TerminalDocumentViewportRect
/** `fit-scale`: calls back when that box changes size, handing back its removal. */
observeViewport: (onChange: () => void) => () => void
/** `fit-scale`: calls back when that box changes size or is shown again, handing back its removal. */
observeViewport: (onChange: (change: TerminalViewportChange) => void) => () => void
/**
* Where this document's elements are: the node its markup was planted in, or null for the page
* the document is running in.
Expand Down Expand Up @@ -222,10 +227,11 @@ export function windowViewportRect(): TerminalDocumentViewportRect {
}

/** The WebView's frame changes size exactly when its window does. */
export function observeWindowViewport(onChange: () => void) {
window.addEventListener('resize', onChange)
export function observeWindowViewport(onChange: (change: TerminalViewportChange) => void) {
const listener = () => onChange('resized')
window.addEventListener('resize', listener)
return function () {
window.removeEventListener('resize', onChange)
window.removeEventListener('resize', listener)
}
}

Expand Down
6 changes: 3 additions & 3 deletions mobile/src/terminal/document/document-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ export type TerminalDocumentState = {
removeWebglRecovery: (() => void) | null
/** `fit-scale`: the generation of the retry loop; a bump abandons the one in flight. */
fitRetryToken: number
/** `fit-scale`: the viewport box the last fit was committed for, or null before one. */
fittedBox: { width: number; height: number } | null
/** `fit-scale`: the reason of a fit held while the host is hidden, or null when none is owed. */
fitPending: string | null
/** `mouse-click-drag`: the mouse gesture in progress, or null. */
mouseGesture: TerminalMouseGesture | null
/** `tap-dispatch`: what the document-level dispatcher has latched onto. */
Expand Down Expand Up @@ -308,7 +308,7 @@ function createTerminalDocumentState(): TerminalDocumentState {
removeTapDispatch: null,
removeWebglRecovery: null,
fitRetryToken: 0,
fittedBox: null,
fitPending: null,
mouseGesture: null,
touchDispatch: {
mode: 'idle',
Expand Down
33 changes: 14 additions & 19 deletions mobile/src/terminal/document/fit-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
updateTransform
} from './viewport-transform'
import type { TerminalDocumentScope } from './document-scope'
import type { TerminalViewportChange } from './document-host-seams'
import { scheduleDocumentFrame } from './document-frame-registry'

/** The narrowest grid a fit or a text-scale change will fit to. */
Expand Down Expand Up @@ -65,23 +66,15 @@ export function adjustRowsForViewport() {}
// so a backgrounded WebView never spins forever.
const FIT_RETRY_MAX_FRAMES = 60

function isFittedBox(scope: TerminalDocumentScope) {
const { width, height } = scope.viewportRect()
const fitted = scope.fittedBox
return fitted !== null && fitted.width === width && fitted.height === height
}

function hasViewportWidth(scope: TerminalDocumentScope) {
const width = scope.viewportRect().width
return Number.isFinite(width) && width > 0
function isViewportShown(scope: TerminalDocumentScope) {
const { width, hidden } = scope.viewportRect()
return hidden !== true && Number.isFinite(width) && width > 0
}

export function applyFitScale(scope: TerminalDocumentScope, reason: string) {
if (!scope.term || !scope.term.element) {
return
}
// Why: a fit asked for while hidden is dropped until a box arrives, and that box may be the old one.
scope.fittedBox = null
const token = ++scope.fitRetryToken
let attempts = 0
let lastScrollWidth = -1
Expand All @@ -92,8 +85,9 @@ export function applyFitScale(scope: TerminalDocumentScope, reason: string) {
if (!scope.term || !scope.term.element) {
return
}
// Why: a display:none host measures 0 wide; the fit stays pending until observeViewport reports a box.
if (!hasViewportWidth(scope)) {
// Why: a hidden grid may not measure its cells, so the fit is held until the host is shown.
if (!isViewportShown(scope)) {
scope.fitPending = reason
return
}
attempts++
Expand Down Expand Up @@ -134,8 +128,7 @@ export function commitFitScale(
return
}
const preSnapScale = computeFitScale(scope)
const { width, height } = scope.viewportRect()
scope.fittedBox = { width, height }
scope.fitPending = null
scope.currentScale = preSnapScale
// Why: when scale is very close to 1 (e.g. 0.97 from xterm scrollbar
// sub-pixels) snap to 1 to avoid imperceptible shrinkage that prevents
Expand Down Expand Up @@ -181,10 +174,12 @@ export function commitFitScale(
* had to copy the five calls into its mount to get it at all (ruling 24).
*/
export function startFitScale(scope: TerminalDocumentScope) {
const refit = () => {
// Why: a hidden screen reports 0x0 and then its old box again; native never refits on that, so
// only a box other than the one last fitted refits, and the user's pan and zoom survive.
if (!hasViewportWidth(scope) || isFittedBox(scope)) {
const refit = (change: TerminalViewportChange) => {
// Why: showing the same box again is not a resize; only a fit held while hidden runs, so pan and zoom survive.
if (change === 'shown') {
if (scope.fitPending !== null) {
applyFitScale(scope, scope.fitPending)
}
return
}
applyFitScale(scope, 'window-resize')
Expand Down
9 changes: 6 additions & 3 deletions mobile/src/terminal/document/host-seams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { handleMsg } from './host-message-router'
import { notify } from './host-notify'
import { flog } from './viewport-transform'
import { attachWebglAddon } from './webgl-recovery'
import type { TerminalDocumentHost } from './document-host-seams'
import type { TerminalDocumentHost, TerminalViewportChange } from './document-host-seams'
import { documentSourceText } from './document-module-source.test-support'

/**
Expand Down Expand Up @@ -299,7 +299,7 @@ describe('the document host seams, once the page sets them', () => {

describe("the document's viewport", () => {
it('refits when the host says its box changed, and not on a window resize it does not own', () => {
const changes: (() => void)[] = []
const changes: ((change: TerminalViewportChange) => void)[] = []
const scope = startedScope({
createTerminal: () => terminalDouble(),
observeViewport: (onChange) => {
Expand All @@ -312,7 +312,10 @@ describe("the document's viewport", () => {
window.dispatchEvent(new Event('resize'))
expect(scope.panX).toBe(50)
expect(changes).toHaveLength(1)
changes[0]!()
// The same box shown again with no fit held is not a resize: the pan stays.
changes[0]!('shown')
expect(scope.panX).toBe(50)
changes[0]!('resized')
expect(scope.panX).toBe(0)
})

Expand Down
6 changes: 4 additions & 2 deletions mobile/src/terminal/document/text-scaling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ export function applyTextScale(scope: TerminalDocumentScope, scale: number) {
if (cellW > 0 && cellH > 0) {
const cols = Math.floor(scope.viewportRect().width / cellW)
if (cols < MIN_FIT_COLS) {
// Why: hidden (0 wide) or too narrow; the next box must refit at the new cell size.
scope.fittedBox = null
Comment thread
pullfrog[bot] marked this conversation as resolved.
Resolved
// Why: too narrow to resize, but a hidden host still owes the refit at the new cell size on show.
if (scope.viewportRect().hidden) {
applyFitScale(scope, 'text-scale')
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
return
}
const rows = Math.max(8, Math.floor(scope.viewportRect().height / cellH))
Expand Down
Loading
Loading