Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(mobile): a field inside an app dialog gets the focus it asks for — and the keyboard

dee3ef3fa570423
diff --git a/mobile/lib/shared/widgets/app_dialog.dart b/mobile/lib/shared/widgets/app_dialog.dart
index eabe47c80..25db0cbe5 100644
--- a/mobile/lib/shared/widgets/app_dialog.dart
+++ b/mobile/lib/shared/widgets/app_dialog.dart
@@ -216,14 +216,24 @@ class _DismissOnEscape extends StatelessWidget {
// not every dialog focuses something of its own — so the scope takes
// focus itself when nothing else claims it.
//
- // ⚠️ `Focus`, not `FocusScope`. Both autofocus, and a descendant that
- // also autofocuses (the model picker's search field) wins either way —
- // but a `FocusScope` additionally becomes the dialog's focus ROOT,
- // which changes where traversal wraps and what `unfocus` falls back
- // to. Nothing here wants to move those; this only needs to be a node
- // in the chain that holds focus when no descendant asks for it.
+ // ⚠️ **A `FocusScope`, not a `Focus`, and the difference is the
+ // dialog's own field.** An autofocus is only granted to a scope that
+ // has no focused child yet, and the first one asked wins. A plain
+ // `Focus` here asks the ROUTE's scope, and it asks first — an
+ // ancestor registers during build, while a `TextField` registers its
+ // autofocus a frame later — so it took the route's focus and every
+ // autofocusing field inside was refused: the rename dialog opened
+ // with no caret and no keyboard, its name selected for a first key
+ // that could not reach it. As a scope of its own, this node holds
+ // focus only until something inside asks: a field's autofocus goes to
+ // THIS scope, which has no focused child, and is granted.
+ //
+ // Being a scope also makes it where traversal wraps and where an
+ // `unfocus` lands; it wraps the whole dialog, so both visit exactly
+ // what the route's own scope would, and a field that lets go leaves
+ // focus inside the `Shortcuts`, where Escape still works.
// `skipTraversal` keeps it out of the tab order it is not a stop in.
- child: Focus(autofocus: true, skipTraversal: true, child: child),
+ child: FocusScope(autofocus: true, skipTraversal: true, child: child),
),
);
}
diff --git a/mobile/test/shared/app_dialog_test.dart b/mobile/test/shared/app_dialog_test.dart
new file mode 100644
index 000000000..0c15078b5
--- /dev/null
+++ b/mobile/test/shared/app_dialog_test.dart
@@ -0,0 +1,166 @@
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/shared/widgets/app_dialog.dart';
+
+/// The veil every dialog on the phone stands on — the rename dialog, the phone
+/// name, the folder picker, every confirmation. It draws its own barrier, so
+/// the three things Material would otherwise have done for it — a tap outside,
+/// Escape, and the focus a field inside asks for — are all its own to get
+/// right.
+void main() {
+ late ValueNotifier<Object?> result;
+
+ Future<void> open(
+ WidgetTester tester,
+ WidgetBuilder builder, {
+ bool barrierDismissible = true,
+ }) async {
+ result = ValueNotifier<Object?>('<closed>');
+ await tester.pumpWidget(
+ MaterialApp(
+ home: Builder(
+ builder: (context) => Scaffold(
+ body: Center(
+ child: TextButton(
+ onPressed: () async {
+ result.value = '<open>';
+ result.value = await showAppDialog<Object?>(
+ context: context,
+ barrierDismissible: barrierDismissible,
+ builder: builder,
+ );
+ },
+ child: const Text('Open'),
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ await tester.tap(find.text('Open'));
+ await tester.pumpAndSettle();
+ }
+
+ Widget card(Widget child) => Dialog(
+ child: Padding(padding: const EdgeInsets.all(20), child: child),
+ );
+
+ testWidgets(
+ 'a field that asks for focus gets it — and the keyboard with it',
+ (tester) async {
+ final focus = FocusNode();
+ addTearDown(focus.dispose);
+ await open(
+ tester,
+ (_) => card(TextField(focusNode: focus, autofocus: true)),
+ );
+ // The veil's own Escape handler must not take the focus a field inside
+ // asked for: that is how the rename dialog opened with no caret.
+ expect(focus.hasPrimaryFocus, isTrue);
+ expect(tester.testTextInput.hasAnyClients, isTrue);
+
+ // Escape still reaches the veil from inside the field.
+ await tester.sendKeyEvent(LogicalKeyboardKey.escape);
+ await tester.pumpAndSettle();
+ expect(result.value, isNull);
+ },
+ );
+
+ testWidgets('with nothing focusable inside, Escape still closes it', (
+ tester,
+ ) async {
+ await open(tester, (_) => card(const Text('Delete this agent?')));
+ await tester.sendKeyEvent(LogicalKeyboardKey.escape);
+ await tester.pumpAndSettle();
+ expect(find.text('Delete this agent?'), findsNothing);
+ expect(result.value, isNull);
+ });
+
+ testWidgets('Escape still closes it after a field has let go of focus', (
+ tester,
+ ) async {
+ final focus = FocusNode();
+ addTearDown(focus.dispose);
+ await open(
+ tester,
+ (_) => card(TextField(focusNode: focus, autofocus: true)),
+ );
+ // What the folder picker does to put the keyboard away.
+ focus.unfocus();
+ await tester.pump();
+ await tester.sendKeyEvent(LogicalKeyboardKey.escape);
+ await tester.pumpAndSettle();
+ expect(result.value, isNull);
+ });
+
+ testWidgets('a tap on the veil closes it; a tap on the panel does not', (
+ tester,
+ ) async {
+ await open(
+ tester,
+ (_) => card(const SizedBox(width: 200, height: 120, child: Text('Hi'))),
+ );
+ await tester.tap(find.text('Hi'));
+ await tester.pumpAndSettle();
+ expect(find.text('Hi'), findsOneWidget);
+
+ await tester.tapAt(const Offset(4, 4));
+ await tester.pumpAndSettle();
+ expect(find.text('Hi'), findsNothing);
+ expect(result.value, isNull);
+ });
+
+ testWidgets('a dialog that opted out of dismissal ignores the veil and '
+ 'Escape alike', (tester) async {
+ await open(
+ tester,
+ barrierDismissible: false,
+ (context) => card(
+ TextButton(
+ onPressed: () => Navigator.of(context).pop('done'),
+ child: const Text('Done'),
+ ),
+ ),
+ );
+ await tester.tapAt(const Offset(4, 4));
+ await tester.sendKeyEvent(LogicalKeyboardKey.escape);
+ await tester.pumpAndSettle();
+ expect(find.text('Done'), findsOneWidget);
+
+ await tester.tap(find.text('Done'));
+ await tester.pumpAndSettle();
+ expect(result.value, 'done');
+ });
+
+ testWidgets('the veil is a tint with no blur on the phone', (tester) async {
+ await open(tester, (_) => card(const Text('Hi')));
+ // A blur re-filters the streaming terminal behind it every frame.
+ expect(kDialogVeilBlur, 0);
+ expect(find.byType(BackdropFilter), findsNothing);
+ expect(
+ find.byWidgetPredicate(
+ (widget) => widget is ColoredBox && widget.color == kDialogVeilTint,
+ ),
+ findsOneWidget,
+ );
+ });
+
+ testWidgets('a dialog button is thumb-sized, on the dialog corner', (
+ tester,
+ ) async {
+ final style = appDialogButtonStyle(
+ background: Colors.blue,
+ foreground: Colors.white,
+ disabledBackground: Colors.grey,
+ disabledForeground: Colors.black,
+ );
+ expect(style.minimumSize!.resolve({}), const Size.fromHeight(46));
+ expect(
+ (style.shape!.resolve({}) as RoundedRectangleBorder).borderRadius,
+ BorderRadius.circular(kDialogControlRadius),
+ );
+ expect(style.backgroundColor!.resolve({WidgetState.disabled}), Colors.grey);
+ expect(style.textStyle!.resolve({})!.fontSize, 16);
+ });
+}