diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index 9be026a5..004e88c1 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -23,6 +23,7 @@ import 'chat_composer_controller.dart'; import '../ui/transcript/chat_attachment_frame.dart'; import '../../../ui/adaptive/glass_pill.dart'; import 'chat_reading_width.dart'; +import 'composer_focus_restore.dart'; /// The composer's placeholder when the chat names no working directory. const String chatComposerPlaceholder = 'Message the agent'; @@ -136,6 +137,7 @@ class _ChatComposerBarState extends State { text: widget.composer.draft, ); final FocusNode _focus = FocusNode(debugLabel: 'chat-composer'); + late final ComposerFocusRestorer _focusRestorer; final ChatAttachmentDraft _attachments = ChatAttachmentDraft(); /// The picked bytes behind each chip, positionally aligned with the draft's @@ -176,11 +178,22 @@ class _ChatComposerBarState extends State { @override void initState() { super.initState(); + _focusRestorer = ComposerFocusRestorer( + focus: _focus, + canRestore: _composerIsCurrent, + ); widget.composer.addListener(_syncDraft); _listenForPaste(); unawaited(widget.composer.loadDraft()); } + /// Whether the composer is still where the operator works: mounted, shown, + /// and not covered by a dialog, sheet or another pushed route. + bool _composerIsCurrent() => + mounted && + TickerMode.valuesOf(context).enabled && + (ModalRoute.of(context)?.isCurrent ?? true); + @override void didUpdateWidget(covariant ChatComposerBar oldWidget) { super.didUpdateWidget(oldWidget); @@ -603,6 +616,7 @@ class _ChatComposerBarState extends State { void dispose() { _stopListeningForPaste(); widget.composer.removeListener(_syncDraft); + _focusRestorer.dispose(); _text.dispose(); _focus.dispose(); super.dispose(); diff --git a/app/lib/features/chat/conversation/composer_focus_restore.dart b/app/lib/features/chat/conversation/composer_focus_restore.dart new file mode 100644 index 00000000..78c7c2cf --- /dev/null +++ b/app/lib/features/chat/conversation/composer_focus_restore.dart @@ -0,0 +1,85 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; + +/// How long before the window loses focus a composer blur still counts as +/// caused by that switch. The web engine closes the text connection (and so +/// unfocuses the field) on the input's own `blur`, which the browser fires +/// just before the window's `blur` that Flutter reports as `inactive`. +const Duration composerBlurGrace = Duration(milliseconds: 250); + +/// Whether this host restores composer focus when its window comes back. +/// Only desktop hosts (native or a desktop browser): on phones, focusing the +/// field on resume would pop the on-screen keyboard unasked. +bool get composerFocusRestoreSupported => switch (defaultTargetPlatform) { + TargetPlatform.macOS || + TargetPlatform.windows || + TargetPlatform.linux => true, + _ => false, +}; + +/// Returns focus to the composer [focus] when the app window regains focus, +/// if the composer held it when the window lost it, like chat web apps do. +/// +/// On web the engine reports window `blur` as [AppLifecycleState.inactive] +/// and window `focus` (or the tab becoming visible) as `resumed`. Nothing is +/// restored when, by then, another control holds focus or [canRestore] says +/// the composer is no longer the operator's place (a dialog, sheet or other +/// route covers it). The text controller keeps the caret and selection while +/// unfocused, so requesting focus puts them back as they were. +class ComposerFocusRestorer { + ComposerFocusRestorer({ + required this.focus, + required this.canRestore, + bool? enabled, + DateTime Function()? now, + }) : _now = now ?? DateTime.now { + if (!(enabled ?? composerFocusRestoreSupported)) return; + focus.addListener(_onFocusChange); + _listener = AppLifecycleListener(onStateChange: _onStateChange); + } + + final FocusNode focus; + final bool Function() canRestore; + final DateTime Function() _now; + AppLifecycleListener? _listener; + + bool _away = false; + bool _restore = false; + DateTime? _blurredAt; + + void _onFocusChange() { + _blurredAt = focus.hasPrimaryFocus ? null : _now(); + } + + bool get _blurredJustNow { + final at = _blurredAt; + return at != null && _now().difference(at) <= composerBlurGrace; + } + + void _onStateChange(AppLifecycleState state) { + if (state != AppLifecycleState.resumed) { + // Only the first step away from resumed decides; later inactive/hidden + // steps of the same absence must not overwrite it. + if (!_away) { + _away = true; + _restore = focus.hasPrimaryFocus || _blurredJustNow; + } + return; + } + final restore = _away && _restore; + _away = false; + _restore = false; + if (restore && _focusIsFree && canRestore()) focus.requestFocus(); + } + + /// True when no other control took focus while the window was away. + bool get _focusIsFree { + final current = FocusManager.instance.primaryFocus; + return current == null || current == focus || current is FocusScopeNode; + } + + void dispose() { + _listener?.dispose(); + if (_listener != null) focus.removeListener(_onFocusChange); + } +} diff --git a/app/test/features/chat/conversation/composer_focus_restore_test.dart b/app/test/features/chat/conversation/composer_focus_restore_test.dart new file mode 100644 index 00000000..dc638f79 --- /dev/null +++ b/app/test/features/chat/conversation/composer_focus_restore_test.dart @@ -0,0 +1,198 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/conversation/composer_focus_restore.dart'; + +import 'chat_composer_bar_fixtures.dart'; + +final _desktop = TargetPlatformVariant.only(TargetPlatform.macOS); +final _phone = TargetPlatformVariant.only(TargetPlatform.android); + +final _field = find.byKey(const Key('chat-composer-field')); + +FocusNode _composerFocus(WidgetTester tester) => tester + .widget( + find.descendant(of: _field, matching: find.byType(EditableText)), + ) + .focusNode; + +TextEditingController _controller(WidgetTester tester) => tester + .widget( + find.descendant(of: _field, matching: find.byType(EditableText)), + ) + .controller; + +/// The window loses focus (web: window `blur`) and comes back (`focus`). +Future _switchAway( + WidgetTester tester, { + Future Function()? whileAway, + bool viaHidden = false, +}) async { + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); + if (viaHidden) { + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden); + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); + } + await tester.pump(); + if (whileAway != null) await whileAway(); + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + await tester.pump(); +} + +Future _open(WidgetTester tester) async { + final gateway = ComposerBarGateway()..page = fixtureTurn(); + addTearDown(gateway.close); + await tester.pumpWidget(composerThread(gateway)); + await tester.pumpAndSettle(); + return gateway; +} + +Future _typeWithCaret(WidgetTester tester) async { + await tester.showKeyboard(_field); + await tester.enterText(_field, 'hello world'); + _controller(tester).selection = const TextSelection( + baseOffset: 2, + extentOffset: 5, + ); + await tester.pump(); +} + +void main() { + testWidgets( + 'focus and selection return to the composer when the window comes back', + (tester) async { + await _open(tester); + await _typeWithCaret(tester); + // The web engine closes the text connection on the editing element's + // blur, which the browser fires just before the window's blur; the + // framework has unfocused the field by the time `inactive` arrives, so + // FocusManager's own suspend/resume cannot bring it back. + _composerFocus(tester).unfocus(); + await tester.pump(); + expect(_composerFocus(tester).hasFocus, isFalse); + await _switchAway(tester); + expect(_composerFocus(tester).hasPrimaryFocus, isTrue); + expect( + _controller(tester).selection, + const TextSelection(baseOffset: 2, extentOffset: 5), + ); + }, + variant: _desktop, + ); + + testWidgets( + 'switching tabs (inactive, hidden, inactive, resumed) restores too', + (tester) async { + await _open(tester); + await _typeWithCaret(tester); + _composerFocus(tester).unfocus(); + await tester.pump(); + await _switchAway(tester, viaHidden: true); + expect(_composerFocus(tester).hasPrimaryFocus, isTrue); + }, + variant: _desktop, + ); + + testWidgets('nothing is restored when the composer was not focused', ( + tester, + ) async { + await _open(tester); + expect(_composerFocus(tester).hasFocus, isFalse); + await _switchAway(tester); + expect(_composerFocus(tester).hasFocus, isFalse); + }, variant: _desktop); + + testWidgets('nothing is restored when another control holds focus', ( + tester, + ) async { + await _open(tester); + await _typeWithCaret(tester); + final other = FocusNode(debugLabel: 'other'); + addTearDown(other.dispose); + await _switchAway( + tester, + whileAway: () async { + // A focusable outside the field takes focus while away. + other + .attach(tester.element(_field)) + .reparent(parent: FocusManager.instance.rootScope); + other.requestFocus(); + await tester.pump(); + }, + ); + expect(other.hasPrimaryFocus, isTrue); + expect(_composerFocus(tester).hasFocus, isFalse); + }, variant: _desktop); + + testWidgets('nothing is restored while a dialog covers the chat', ( + tester, + ) async { + await _open(tester); + await _typeWithCaret(tester); + await _switchAway( + tester, + whileAway: () async { + showDialog( + context: tester.element(_field), + builder: (_) => const AlertDialog(content: Text('dialog')), + ); + await tester.pumpAndSettle(); + FocusManager.instance.primaryFocus?.unfocus(); + await tester.pump(); + }, + ); + expect(find.text('dialog'), findsOneWidget); + expect(_composerFocus(tester).hasFocus, isFalse); + }, variant: _desktop); + + testWidgets('phones do not refocus (no unasked on-screen keyboard)', ( + tester, + ) async { + await _open(tester); + await _typeWithCaret(tester); + await _switchAway( + tester, + whileAway: () async { + _composerFocus(tester).unfocus(); + await tester.pump(); + }, + ); + expect(_composerFocus(tester).hasFocus, isFalse); + }, variant: _phone); + + testWidgets('a blur well before the window left is the operator\'s own', ( + tester, + ) async { + var now = DateTime(2026); + final focus = FocusNode(); + final restorer = ComposerFocusRestorer( + focus: focus, + canRestore: () => true, + enabled: true, + now: () => now, + ); + addTearDown(() { + restorer.dispose(); + focus.dispose(); + }); + await tester.pumpWidget( + MaterialApp( + home: Material(child: TextField(focusNode: focus)), + ), + ); + focus.requestFocus(); + await tester.pump(); + focus.unfocus(); + await tester.pump(); + now = now.add(composerBlurGrace * 2); + await _switchAway(tester); + expect(focus.hasFocus, isFalse); + + focus.requestFocus(); + await tester.pump(); + focus.unfocus(); + await tester.pump(); + now = now.add(composerBlurGrace ~/ 2); + await _switchAway(tester); + expect(focus.hasPrimaryFocus, isTrue); + }); +} diff --git a/app/tool/composer_focus_restore_probe.mjs b/app/tool/composer_focus_restore_probe.mjs new file mode 100644 index 00000000..ef1ef385 --- /dev/null +++ b/app/tool/composer_focus_restore_probe.mjs @@ -0,0 +1,190 @@ +import { createServer } from 'node:http'; +import { readFile, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; +import { spawn, execFile } from 'node:child_process'; +import { promisify } from 'node:util'; +import { once } from 'node:events'; +import { setTimeout as delay } from 'node:timers/promises'; +import { join, extname, resolve, sep } from 'node:path'; +import { tmpdir } from 'node:os'; + +// Real Chrome check that the composer regains focus (caret and selection +// kept) when the browser window comes back, and stays unfocused when it was +// not focused before. Window switching is reproduced by the same DOM events +// the browser sends: the editing element's blur (relatedTarget null), then +// window blur, later window focus. +// +// Usage (from app/), with the composer probe build of chat_composer_probe.mjs: +// OPENCODE_ARTIFACT_DIR= node tool/composer_focus_restore_probe.mjs build/composer_probe_material +// +// The fixture is served over loopback only. Flutter paints text on a canvas, so +// assertions read the semantics tree (aria-label/innerText) and the focused +// editing element; captures are PNGs for visual inspection. + +const artifactDir = process.env.OPENCODE_ARTIFACT_DIR; +if (!artifactDir) throw new Error('Set OPENCODE_ARTIFACT_DIR for the captures'); +await mkdir(artifactDir, { recursive: true }); +const root = resolve(process.argv[2] ?? join(process.cwd(), 'build/composer_probe_material')); + +const mime = { + '.html': 'text/html', + '.js': 'text/javascript', + '.json': 'application/json', + '.wasm': 'application/wasm', + '.png': 'image/png', + '.ico': 'image/x-icon', + '.ttf': 'font/ttf', + '.otf': 'font/otf', + '.woff': 'font/woff', + '.woff2': 'font/woff2', +}; +const server = createServer(async (req, res) => { + try { + const path = resolve(root, '.' + decodeURIComponent(new URL(req.url, 'http://localhost').pathname)); + if (path !== root && !path.startsWith(root + sep)) return res.writeHead(403).end(); + const filePath = path === root || path.endsWith('/') ? join(path, 'index.html') : path; + const file = await readFile(filePath); + res.writeHead(200, { 'content-type': mime[extname(filePath)] ?? 'application/octet-stream' }).end(file); + } catch { + res.writeHead(404).end('not found'); + } +}); +server.listen(0, '127.0.0.1'); +await once(server, 'listening'); +const port = server.address().port; +const profile = await mkdtemp(join(tmpdir(), 'chat-composer-chrome-')); +const chrome = spawn('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', [ + '--headless=new', '--no-first-run', '--no-default-browser-check', '--disable-gpu', + '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank', +], { stdio: ['ignore', 'ignore', 'pipe'] }); +let stderr = ''; +chrome.stderr.on('data', data => { stderr += data; }); + +let ws; +try { + const waitFor = async (fn, what) => { + for (let i = 0; i < 120; i++) { const value = await fn(); if (value) return value; await delay(100); } + throw new Error(`Timed out waiting for ${what}`); + }; + const endpoint = await waitFor( + () => stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/)?.[1], + 'Chrome DevTools', + ); + ws = new WebSocket(endpoint); + await once(ws, 'open'); + let id = 0; + const pending = new Map(); + ws.addEventListener('message', event => { + const msg = JSON.parse(event.data); + if (msg.method?.includes('detached') || msg.method?.includes('crashed')) { + console.error('CDP event', JSON.stringify(msg)); + } + if (msg.method === 'Runtime.exceptionThrown') { + console.error('PAGE EXCEPTION', JSON.stringify(msg.params?.exceptionDetails?.exception?.description ?? msg.params?.exceptionDetails?.text)); + } + if (msg.id && pending.has(msg.id)) { pending.get(msg.id)(msg); pending.delete(msg.id); } + }); + const send = (method, params = {}, sessionId) => new Promise(resolve => { + const callId = ++id; + pending.set(callId, resolve); + ws.send(JSON.stringify({ id: callId, method, params, ...(sessionId ? { sessionId } : {}) })); + }); + const created = await send('Target.createTarget', { url: 'about:blank' }); + if (!created.result) throw new Error(`Target creation failed: ${JSON.stringify(created)}`); + const session = (await send('Target.attachToTarget', { targetId: created.result.targetId, flatten: true })).result.sessionId; + await send('Page.enable', {}, session); + await send('Runtime.enable', {}, session); + const navigation = await send('Page.navigate', { url: `http://127.0.0.1:${port}/` }, session); + if (navigation.error || navigation.result?.errorText) throw new Error(JSON.stringify(navigation)); + const evaluate = async expression => { + const response = await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }, session); + if (response.error) throw new Error(`Runtime.evaluate failed: ${JSON.stringify(response)}`); + return response.result?.result?.value; + }; + // Wait for the app, but leave semantics off: that is the operator's normal + // mode, where the engine's own editing element (and its blur handling) + // backs the field. + for (let i = 0; i < 120; i++) { + if (await evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))")) break; + await delay(100); + } + await delay(1800); + const click = async (x, y) => { + for (const type of ['mousePressed', 'mouseReleased']) { + await send('Input.dispatchMouseEvent', { type, x, y, button: 'left', clickCount: 1 }, session); + } + }; + // The composer sits at the bottom; click upward until an editing element + // takes focus. + const focusField = async () => { + for (let y = 790; y > 500; y -= 10) { + await click(640, y); + await delay(150); + if ((await state()).editing) return y; + } + throw new Error('No click focused the composer'); + }; + const type = async text => { await send('Input.insertText', { text }, session); await delay(250); }; + const setSize = async (width, height, scale = 1) => { + await send('Emulation.setDeviceMetricsOverride', { + width, height, deviceScaleFactor: scale, mobile: width < 600, + }, session); + await delay(400); + }; + const state = () => evaluate(`(() => { + const a = document.activeElement; + // The composer is the fixture's only text field, so a focused editing + // element (engine textarea or semantics textarea) is the composer. + const editing = Boolean(a && (a.tagName === 'TEXTAREA' || a.tagName === 'INPUT')); + return { editing, value: editing ? a.value : null, + start: editing ? a.selectionStart : null, end: editing ? a.selectionEnd : null }; + })()`); + const switchWindow = async () => { + // Two evaluations, as two event dispatches in the browser: microtasks + // (the framework handling the engine's closed text connection) run in + // between, so Flutter has already unfocused the field before the window + // blur reaches it. One script doing both would hide that. + await evaluate('document.activeElement?.blur()'); + await evaluate("window.dispatchEvent(new FocusEvent('blur'))"); + await delay(600); + const away = await state(); + await evaluate("window.dispatchEvent(new FocusEvent('focus'))"); + await delay(800); + return { away, back: await state() }; + }; + const failures = []; + const check = (ok, what) => { console.log(`${ok ? 'PASS' : 'FAIL'} ${what}`); if (!ok) failures.push(what); }; + + await setSize(1280, 800); + console.log('composer at y', await focusField()); + await type('hello world'); + await evaluate(`(() => { const a = document.activeElement; a.setSelectionRange(2, 5); a.dispatchEvent(new Event('selectionchange')); document.dispatchEvent(new Event('selectionchange')); })()`); + await delay(400); + const before = await state(); + console.log('before', JSON.stringify(before)); + const focused = await switchWindow(); + console.log('focused case', JSON.stringify(focused)); + check(!focused.away.editing, 'composer loses DOM focus while the window is away'); + check(focused.back.editing && focused.back.value === 'hello world', 'composer regains focus with its text'); + check(focused.back.start === before.start && focused.back.end === before.end, `selection kept (${before.start}-${before.end})`); + await send('Page.captureScreenshot', { format: 'png' }, session).then(r => + writeFile(join(artifactDir, 'focus-restored.png'), Buffer.from(r.result.data, 'base64'))); + + // Control: composer not focused when the window leaves. + // The operator clicks the transcript (tap outside unfocuses the field), + // then switches windows a moment later. + await click(640, 200); + await delay(1000); + console.log('after click away', JSON.stringify(await state())); + const unfocused = await switchWindow(); + console.log('unfocused case', JSON.stringify(unfocused)); + check(!unfocused.back.editing, 'composer stays unfocused when it was not focused'); + + if (failures.length) throw new Error(`Failed: ${failures.join('; ')}`); + console.log('composer focus restore probe passed'); +} finally { + ws?.close(); + chrome.kill('SIGTERM'); + await Promise.race([once(chrome, 'exit'), delay(3000)]); + server.close(); + await rm(profile, { recursive: true, force: true }); +}