mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-20 04:59:34 +01:00
fix(mobile): pinned field on phones: swipes, focus scroll, viewport pan, iOS lift
Found on a real Android phone (Chrome) and iPhone (Safari) with Send on Enter:
- A swipe that started on the live screen closed the keyboard: the content
touchstart blurred the focused input before the gesture was known to be a
tap or a scroll. It now skips the visible #cjkInput
(_contentTouchStartBlurs()); a real tap still closes it (inert content, a
link, a long press).
- Focusing the field scrolled the terminal off screen: KeyboardHandler's
focusin scrollIntoView({ block: 'center' }) centred the fixed field in its
ancestors. isInputElement() skips #cjkInput like xterm's inputs.
- Android pans the visual viewport to reveal the field (offsetTop 285 in a
458/744 viewport), leaving only the terminal's last line visible. On phones
.app now follows visualViewport.offsetTop while the keyboard is up, and the
fixed bars inside .app are lifted from .app's bottom, so a partial pan does
not lift them twice. Non-phone branches and resetLayout() clear the shift.
- On iOS the first keyboard open reported innerHeight as the visual height
(323 vs 640), zeroing the bars' lift; the layout height is now the larger of
innerHeight and documentElement.clientHeight.
Tests in test/keyboard-handler-pinned-field.test.ts and
test/terminal-touch-tap.test.ts; docs in architecture-invariants and
src/web/CLAUDE.md; changeset sentence.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3c5b7e2199
commit
e85bb83353
7 files changed
+272
-16
No files matched your search
@@ -0,0 +1,167 @@
|
||||
// Port: none (pure logic in a vm context — no browser, no server).
|
||||
//
|
||||
// KeyboardHandler scrolls a newly focused text field into view (scrollInputIntoView,
|
||||
// `scrollIntoView({ block: 'center' })`) once the keyboard is up. The pinned CJK /
|
||||
// Send on Enter field (#cjkInput) is position:fixed and already lifted above the
|
||||
// keyboard by updateLayoutForKeyboard(), so scrolling it into view only centred it in
|
||||
// its scrollable ancestors and pushed the terminal off screen. It must be skipped,
|
||||
// like xterm's own inputs, while ordinary inputs keep the behaviour.
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import vm from 'node:vm';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
const SOURCE = readFileSync(resolve(import.meta.dirname, '../src/web/public/mobile-handlers.js'), 'utf8');
|
||||
|
||||
function loadHandler() {
|
||||
const context = vm.createContext({
|
||||
console,
|
||||
navigator: { userAgent: 'test', maxTouchPoints: 1 },
|
||||
window: { addEventListener: () => {}, matchMedia: () => ({ matches: false }), scrollTo: () => {} },
|
||||
document: {
|
||||
body: { classList: { add: () => {}, remove: () => {} } },
|
||||
addEventListener: () => {},
|
||||
getElementById: () => null,
|
||||
querySelector: () => null,
|
||||
},
|
||||
setTimeout: () => 1,
|
||||
clearTimeout: () => {},
|
||||
});
|
||||
vm.runInContext(`${SOURCE}\nglobalThis.__KH = KeyboardHandler;`, context, { filename: 'mobile-handlers.js' });
|
||||
return (context as { __KH: any }).__KH;
|
||||
}
|
||||
|
||||
function element(tagName: string, { id = '', inside = '' }: { id?: string; inside?: string } = {}) {
|
||||
return { tagName, id, type: '', isContentEditable: false, closest: (sel: string) => (sel === inside ? {} : null) };
|
||||
}
|
||||
|
||||
describe('KeyboardHandler.isInputElement', () => {
|
||||
it('skips the pinned CJK field, which the keyboard layout positions itself', () => {
|
||||
expect(loadHandler().isInputElement(element('TEXTAREA', { id: 'cjkInput' }))).toBe(false);
|
||||
});
|
||||
|
||||
it('still skips xterm inputs and still scrolls ordinary fields into view', () => {
|
||||
const kh = loadHandler();
|
||||
expect(kh.isInputElement(element('TEXTAREA', { inside: '.xterm' }))).toBe(false);
|
||||
expect(kh.isInputElement(element('TEXTAREA', { id: 'someOtherField' }))).toBe(true);
|
||||
expect(kh.isInputElement(element('INPUT'))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
function loadLayout({
|
||||
focused = 'cjkInput',
|
||||
offsetTop,
|
||||
height = 458,
|
||||
width = 390,
|
||||
innerHeight = 744,
|
||||
clientHeight = 744,
|
||||
}: {
|
||||
focused?: string;
|
||||
offsetTop: number;
|
||||
height?: number;
|
||||
width?: number;
|
||||
innerHeight?: number;
|
||||
clientHeight?: number;
|
||||
}) {
|
||||
const el = (offsetHeight = 36) => ({
|
||||
style: {} as Record<string, string>,
|
||||
offsetHeight,
|
||||
classList: { contains: () => true },
|
||||
});
|
||||
// .app is sized to the visual viewport (--app-height), which is what its fixed
|
||||
// children are placed against once it carries a transform.
|
||||
const nodes: Record<string, any> = {
|
||||
'.app': el(height),
|
||||
'.toolbar': el(),
|
||||
'.main': el(),
|
||||
'.keyboard-accessory-bar': el(),
|
||||
};
|
||||
const cjk = { ...el(), id: 'cjkInput' };
|
||||
const context = vm.createContext({
|
||||
console,
|
||||
navigator: { userAgent: 'test', maxTouchPoints: 1 },
|
||||
window: {
|
||||
addEventListener: () => {},
|
||||
matchMedia: () => ({ matches: false }),
|
||||
scrollTo: () => {},
|
||||
innerHeight,
|
||||
innerWidth: width, // MobileDetection (defined in the module) reads this
|
||||
visualViewport: { offsetTop, height },
|
||||
},
|
||||
document: {
|
||||
body: { classList: { add: () => {}, remove: () => {} } },
|
||||
documentElement: { clientHeight },
|
||||
addEventListener: () => {},
|
||||
activeElement: focused === 'cjkInput' ? cjk : { id: focused },
|
||||
getElementById: (id: string) => (id === 'cjkInput' ? cjk : null),
|
||||
querySelector: (sel: string) => nodes[sel] || null,
|
||||
},
|
||||
setTimeout: () => 1,
|
||||
clearTimeout: () => {},
|
||||
});
|
||||
vm.runInContext(`${SOURCE}\nglobalThis.__KH = KeyboardHandler;`, context, { filename: 'mobile-handlers.js' });
|
||||
const kh = (context as { __KH: any }).__KH;
|
||||
kh.keyboardVisible = true;
|
||||
kh.initialViewportHeight = 744;
|
||||
return { kh, nodes, cjk };
|
||||
}
|
||||
|
||||
describe('KeyboardHandler follows the visual-viewport pan on phones', () => {
|
||||
it('moves the app down by the pan and lifts nothing more (measured Android case)', () => {
|
||||
const { kh, nodes, cjk } = loadLayout({ offsetTop: 285 });
|
||||
kh.updateLayoutForKeyboard();
|
||||
expect(nodes['.app'].style.transform).toBe('translateY(285px)');
|
||||
// .app bottom = 285 + 458 = 743, visual bottom = 743: the bars already sit on it.
|
||||
expect(nodes['.toolbar'].style.transform).toBe('');
|
||||
expect(nodes['.keyboard-accessory-bar'].style.transform).toBe('');
|
||||
expect(cjk.style.transform).toBe('');
|
||||
});
|
||||
|
||||
it('does not lift the bars twice on a partial pan', () => {
|
||||
// Mid-animation: offsetTop 100 in a 744 layout. Lifting by the layout gap (186px)
|
||||
// inside the shifted .app would float the bars 186px above the keyboard.
|
||||
const { kh, nodes, cjk } = loadLayout({ offsetTop: 100 });
|
||||
kh.updateLayoutForKeyboard();
|
||||
expect(nodes['.app'].style.transform).toBe('translateY(100px)');
|
||||
expect(nodes['.toolbar'].style.transform).toBe('');
|
||||
expect(nodes['.keyboard-accessory-bar'].style.transform).toBe('');
|
||||
expect(cjk.style.transform).toBe('');
|
||||
});
|
||||
|
||||
it('keeps the old lift when there is no pan', () => {
|
||||
const { kh, nodes } = loadLayout({ offsetTop: 0 });
|
||||
kh.updateLayoutForKeyboard();
|
||||
expect(nodes['.app'].style.transform).toBe('');
|
||||
expect(nodes['.toolbar'].style.transform).toBe('translateY(-286px)');
|
||||
});
|
||||
|
||||
it('follows a pan whatever has focus, so a focus change cannot drop the shift', () => {
|
||||
const { kh, nodes } = loadLayout({ focused: 'someOtherField', offsetTop: 285 });
|
||||
kh.updateLayoutForKeyboard();
|
||||
expect(nodes['.app'].style.transform).toBe('translateY(285px)');
|
||||
});
|
||||
|
||||
it('never shifts the app outside the phone layout (tablet landscape)', () => {
|
||||
const { kh, nodes } = loadLayout({ offsetTop: 285, width: 1024 });
|
||||
nodes['.app'].style.transform = 'translateY(285px)'; // left over from portrait
|
||||
kh.updateLayoutForKeyboard();
|
||||
expect(nodes['.app'].style.transform).toBe('');
|
||||
});
|
||||
|
||||
it('lifts the bars when iOS reports innerHeight as the shrunk visual height', () => {
|
||||
// Measured on an iPhone, first keyboard open: innerHeight 323 = visual height,
|
||||
// documentElement.clientHeight 640. Lifting by innerHeight - visual bottom gave 0.
|
||||
const { kh, nodes, cjk } = loadLayout({ offsetTop: 0, height: 323, innerHeight: 323, clientHeight: 640 });
|
||||
kh.updateLayoutForKeyboard();
|
||||
expect(nodes['.toolbar'].style.transform).toBe('translateY(-317px)');
|
||||
expect(nodes['.keyboard-accessory-bar'].style.transform).toBe('translateY(-317px)');
|
||||
expect(cjk.style.transform).toBe('translateY(-317px)');
|
||||
});
|
||||
|
||||
it('clears the shift when the layout resets', () => {
|
||||
const { kh, nodes } = loadLayout({ offsetTop: 285 });
|
||||
kh.updateLayoutForKeyboard();
|
||||
kh.resetLayout();
|
||||
expect(nodes['.app'].style.transform).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -139,6 +139,28 @@ describe('terminal touch tap mouse guard', () => {
|
||||
expect(app._isMobileTerminalInputFocused()).toBe(true);
|
||||
});
|
||||
|
||||
it('a content touchstart blurs xterm input but leaves the pinned CJK field focused', () => {
|
||||
const { app, setActiveElement } = loadTerminalUiHarness();
|
||||
setActiveElement({ id: '', classList: { contains: (c: string) => c === 'xterm-helper-textarea' } });
|
||||
expect(app._contentTouchStartBlurs()).toBe(true);
|
||||
// A swipe that starts on the live screen must not close the keyboard the field owns.
|
||||
setActiveElement({ id: 'cjkInput', classList: { contains: () => false } });
|
||||
expect(app._contentTouchStartBlurs()).toBe(false);
|
||||
setActiveElement(null);
|
||||
expect(app._contentTouchStartBlurs()).toBe(true);
|
||||
});
|
||||
|
||||
it('the content touchstart only blurs through _contentTouchStartBlurs()', () => {
|
||||
// The touchstart listener lives in initTerminal, which this harness does not run,
|
||||
// so pin its one line: an unconditional blur there closes the pinned field's
|
||||
// keyboard on every swipe that starts on the live screen.
|
||||
const code = readFileSync(resolve(import.meta.dirname, '../src/web/public/terminal-ui.js'), 'utf8');
|
||||
const branch = code.slice(code.indexOf("if (touchStartIntent === 'content') {"));
|
||||
const body = branch.slice(0, branch.indexOf('// Hold still and this press becomes a text selection.'));
|
||||
expect(body).toContain('if (this._contentTouchStartBlurs()) this._blurMobileTerminalInput();');
|
||||
expect(body.match(/_blurMobileTerminalInput\(\)/g)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('routes a readback row to the TUI while keeping the prompt row as keyboard input', () => {
|
||||
const { app } = loadTerminalUiHarness();
|
||||
app.activeSessionId = 'sess-1';
|
||||
@@ -967,9 +989,27 @@ describe('terminal link tap', () => {
|
||||
app._isOutsideWorktreePath = () => false;
|
||||
windowRef.open = vi.fn();
|
||||
app.registerFilePathLinkProvider();
|
||||
return { app, windowRef, sent };
|
||||
return { app, windowRef, sent, setActiveElement: harness.setActiveElement };
|
||||
}
|
||||
|
||||
it('a link tap closes the keyboard of the pinned CJK field, which touchstart left focused', () => {
|
||||
const line = 'Login at https://claude.ai/oauth/authorize?code=true&client_id=abc to finish';
|
||||
const { app, setActiveElement } = linkHarness([line, '', '❯ ']);
|
||||
const cjk = { id: 'cjkInput', classList: { contains: () => false }, blur: vi.fn() };
|
||||
setActiveElement(cjk);
|
||||
expect(app._handleMobileTerminalTap(at(line.indexOf('https')), false, 'content')).toBe('link');
|
||||
expect(cjk.blur).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('a link tap leaves focus alone otherwise (touchstart already blurred xterm input)', () => {
|
||||
const line = 'Login at https://claude.ai/oauth/authorize?code=true&client_id=abc to finish';
|
||||
const { app, setActiveElement } = linkHarness([line, '', '❯ ']);
|
||||
const other = { id: '', classList: { contains: () => false }, blur: vi.fn() };
|
||||
setActiveElement(other);
|
||||
expect(app._handleMobileTerminalTap(at(line.indexOf('https')), false, 'content')).toBe('link');
|
||||
expect(other.blur).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('opens a URL under the finger in a new tab', () => {
|
||||
const line = 'Login at https://claude.ai/oauth/authorize?code=true&client_id=abc to finish';
|
||||
const { app, windowRef } = linkHarness([line, '', '❯ ']);
|
||||
|
||||
Reference in new issue
Block a user