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.

feat: refactor settings and menu components for improved responsiveness and structure

Dev jacob6b8f33dc3f79
diff --git a/desktop/lib/screens/swarm_menu_bus.dart b/desktop/lib/screens/swarm_menu_bus.dart
index 71265dfc9..c138d03a3 100644
--- a/desktop/lib/screens/swarm_menu_bus.dart
+++ b/desktop/lib/screens/swarm_menu_bus.dart
@@ -1,8 +1,7 @@
-import 'dart:io' show Platform;
-
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
+import '../core/runtime_platform.dart';
import '../core/test_run.dart';
/// The seam both native menu surfaces talk through.
@@ -20,7 +19,9 @@ class SwarmMenuBus {
/// run on macOS machines too, and there the channel has nobody on the other
/// end, so the bar would draw no state and its rows would reach no handler.
/// A test that wants the channel asks for [SwarmMenuBus.forChannel].
- SwarmMenuBus() : _channelBacked = Platform.isMacOS && !kUnderTest;
+ /// [RuntimePlatform], not `dart:io`: the browser build constructs this too,
+ /// and `Platform` throws there before the first frame.
+ SwarmMenuBus() : _channelBacked = RuntimePlatform.isMacOS && !kUnderTest;
/// The channel, even on a Linux test host — a screen forced onto the
/// native-tabs path must keep talking to something a test can mock.
diff --git a/desktop/lib/screens/swarm_screen.dart b/desktop/lib/screens/swarm_screen.dart
index 7076d8d6e..bbf4a786c 100644
--- a/desktop/lib/screens/swarm_screen.dart
+++ b/desktop/lib/screens/swarm_screen.dart
@@ -2434,6 +2434,7 @@ class _SwarmScreenState extends State<SwarmScreen> {
initialSection: section,
experimentalFeatures: _experimentalFeatures,
source: 'swarm',
+ compactBelow: widget.chrome?.compactBelow ?? 0,
),
);
@@ -5531,6 +5532,7 @@ class _SwarmScreenState extends State<SwarmScreen> {
source: 'shortcut',
initialSection: SettingsSection.debug,
experimentalFeatures: _experimentalFeatures,
+ compactBelow: widget.chrome?.compactBelow ?? 0,
),
),
};
diff --git a/desktop/lib/settings/settings_body.dart b/desktop/lib/settings/settings_body.dart
new file mode 100644
index 000000000..5b375ed4a
--- /dev/null
+++ b/desktop/lib/settings/settings_body.dart
@@ -0,0 +1,53 @@
+import 'package:flutter/material.dart';
+
+import '../state/app_state.dart';
+import 'experimental_features.dart';
+import 'sections/about_section.dart';
+import 'sections/account_section.dart';
+import 'sections/debug_section.dart';
+import 'sections/devices_section.dart';
+import 'sections/experimental_section.dart';
+import 'sections/notifications_section.dart';
+import 'sections/shortcuts_section.dart';
+import 'sections/tracking_section.dart';
+import 'sections/usage_section.dart';
+import 'settings_section.dart';
+
+/// The screen behind a [SettingsSection].
+///
+/// Switch immediately, disposing the previous section instead of retaining it
+/// for a cross-fade while the new section starts its work.
+class SettingsBody extends StatelessWidget {
+ const SettingsBody({
+ super.key,
+ required this.section,
+ required this.notifier,
+ this.experimentalFeatures,
+ });
+
+ final SettingsSection section;
+ final AppNotifier notifier;
+ final ExperimentalFeaturesStore? experimentalFeatures;
+
+ @override
+ Widget build(BuildContext context) {
+ final screen = switch (section) {
+ SettingsSection.account => AccountSection(notifier: notifier),
+ SettingsSection.usage => const UsageSection(),
+ SettingsSection.customize => throw StateError(
+ 'Customization opens over the workspace.',
+ ),
+ SettingsSection.notifications => const NotificationsSection(),
+ SettingsSection.experimental => ExperimentalSection(
+ store: experimentalFeatures ?? notifier.experimentalFeatures,
+ controller: notifier.swarmSettings,
+ ),
+ SettingsSection.devices => const DevicesSection(),
+ SettingsSection.shortcuts => const ShortcutsSection(),
+ SettingsSection.debug => const DebugSection(),
+ SettingsSection.tracking => const TrackingSection(),
+ SettingsSection.about => AboutSection(notifier: notifier),
+ };
+ return KeyedSubtree(key: ValueKey(section), child: screen);
+ }
+}
diff --git a/desktop/lib/settings/settings_compact.dart b/desktop/lib/settings/settings_compact.dart
new file mode 100644
index 000000000..7cde27d13
--- /dev/null
+++ b/desktop/lib/settings/settings_compact.dart
@@ -0,0 +1,93 @@
+import 'package:flutter/material.dart';
+import 'package:lucide_icons_flutter/lucide_icons.dart';
+
+import '../shared/layouts/widgets/sidebar_item.dart';
+import '../shared/theme/app_theme.dart' as grid;
+import '../shared/widgets/section_scaffold.dart';
+import 'settings_nav.dart';
+import 'settings_section.dart';
+
+/// One settings section on a narrow screen (the web on a phone): the section
+/// takes the width, and the way back to the list of sections sits above it,
+/// shaped like the list's own "Back to app" row.
+class SettingsCompactSection extends StatelessWidget {
+ const SettingsCompactSection({
+ super.key,
+ required this.onBack,
+ required this.child,
+ });
+
+ final VoidCallback onBack;
+ final Widget child;
+
+ @override
+ Widget build(BuildContext context) => Column(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ Padding(
+ padding: const EdgeInsets.fromLTRB(
+ SectionScaffold.contentPadding - SidebarItem.iconGutter,
+ SectionScaffold.contentPadding / 2,
+ SectionScaffold.contentPadding - SidebarItem.iconGutter,
+ 0,
+ ),
+ child: SizedBox(
+ height: SectionScaffold.headingHeight(context),
+ child: Center(
+ child: SidebarItem(
+ key: const Key('settings-compact-back'),
+ icon: LucideIcons.arrowLeft300,
+ label: 'Settings',
+ onTap: onBack,
+ ),
+ ),
+ ),
+ ),
+ Expanded(child: child),
+ ],
+ );
+}
+
+/// Settings on a phone: the list of sections, or one open section. The
+/// browser's (or Android's) Back leaves the section before it leaves Settings,
+/// as the on-screen way back does.
+class SettingsCompactScreen extends StatelessWidget {
+ const SettingsCompactScreen({
+ super.key,
+ required this.section,
+ required this.sectionOpen,
+ required this.onSelect,
+ required this.onBack,
+ required this.onKeyEvent,
+ required this.body,
+ });
+
+ final SettingsSection section;
+ final bool sectionOpen;
+ final ValueChanged<SettingsSection> onSelect;
+ final VoidCallback onBack;
+ final FocusOnKeyEventCallback onKeyEvent;
+ final Widget body;
+
+ @override
+ Widget build(BuildContext context) => PopScope(
+ canPop: !sectionOpen,
+ onPopInvokedWithResult: (didPop, _) {
+ if (!didPop) onBack();
+ },
+ child: Focus(
+ onKeyEvent: onKeyEvent,
+ child: Scaffold(
+ backgroundColor: grid.AppPalette.windowBg,
+ body: sectionOpen
+ ? SettingsCompactSection(onBack: onBack, child: body)
+ : SettingsNav(
+ section: section,
+ onSelect: onSelect,
+ railWidth: double.infinity,
+ autofocusSearch: false,
+ ),
+ ),
+ ),
+ );
+}
diff --git a/desktop/lib/settings/settings_nav.dart b/desktop/lib/settings/settings_nav.dart
index 788800402..de3f92945 100644
--- a/desktop/lib/settings/settings_nav.dart
+++ b/desktop/lib/settings/settings_nav.dart
@@ -11,11 +11,25 @@ import 'settings_section.dart';
/// The settings list: the way back, a field that narrows the list to what you
/// type, then one row per screen with the open one highlighted.
class SettingsNav extends StatefulWidget {
- const SettingsNav({super.key, required this.section, required this.onSelect});
+ const SettingsNav({
+ super.key,
+ required this.section,
+ required this.onSelect,
+ this.railWidth = SettingsNav.width,
+ this.autofocusSearch = true,
+ });
final SettingsSection section;
final ValueChanged<SettingsSection> onSelect;
+ /// [width] beside a section; the whole width where it is the whole screen
+ /// (a phone's list of sections).
+ final double railWidth;
+
+ /// Whether the filter takes the keys on open. Not on a phone, where focus
+ /// raises the on-screen keyboard over half the list.
+ final bool autofocusSearch;
+
/// The machine rail's own default width, so Settings opens without the left
/// column jumping.
static const double width = 260;
@@ -104,7 +118,7 @@ class _SettingsNavState extends State<SettingsNav> {
// The rail and the section begin below the native title bar.
grid.AppTheme.watch(context);
return Container(
- width: SettingsNav.width,
+ width: widget.railWidth,
color: grid.AppSurface.recess,
child: Padding(
// Rows add their own icon gutter: their glyphs and group captions sit
@@ -137,6 +151,7 @@ class _SettingsNavState extends State<SettingsNav> {
child: _SearchField(
controller: _search,
focusNode: _searchFocus,
+ autofocus: widget.autofocusSearch,
onChanged: (value) => setState(() => _query = value),
onSubmitted: (_) => _chooseMatch(),
),
@@ -176,12 +191,14 @@ class _SearchField extends StatelessWidget {
const _SearchField({
required this.controller,
required this.focusNode,
+ required this.autofocus,
required this.onChanged,
required this.onSubmitted,
});
final TextEditingController controller;
final FocusNode focusNode;
+ final bool autofocus;
final ValueChanged<String> onChanged;
final ValueChanged<String> onSubmitted;
@@ -196,7 +213,7 @@ class _SearchField extends StatelessWidget {
key: const Key('settings-search-field'),
controller: controller,
focusNode: focusNode,
- autofocus: true,
+ autofocus: autofocus,
onChanged: onChanged,
onSubmitted: onSubmitted,
onEditingComplete: () {},
diff --git a/desktop/lib/settings/settings_screen.dart b/desktop/lib/settings/settings_screen.dart
index 390a2af15..e17ab8630 100644
--- a/desktop/lib/settings/settings_screen.dart
+++ b/desktop/lib/settings/settings_screen.dart
@@ -6,17 +6,10 @@ import '../shared/theme/app_theme.dart' as grid;
import '../state/app_state.dart';
import '../widgets/harness_customize_pane.dart';
import 'experimental_features.dart';
-import 'sections/about_section.dart';
-import 'sections/account_section.dart';
-import 'sections/debug_section.dart';
-import 'sections/devices_section.dart';
-import 'sections/experimental_section.dart';
-import 'sections/shortcuts_section.dart';
-import 'sections/tracking_section.dart';
-import 'sections/usage_section.dart';
+import 'settings_body.dart';
+import 'settings_compact.dart';
import 'settings_nav.dart';
import 'settings_section.dart';
-import 'sections/notifications_section.dart';
/// Opens Settings over the app.
///
@@ -38,6 +31,9 @@ Future<void> showSettingsScreen(
// because a pane reachable several ways is close to meaningless as a bare
// count.
required String source,
+ // Narrower than this, the list and a section take turns on the screen (a
+ // phone). Desktop passes nothing: its window never gets that narrow.
+ double compactBelow = 0,
}) async {
if (initialSection == SettingsSection.customize) {
await showHarnessCustomizePane(context);
@@ -52,6 +48,7 @@ Future<void> showSettingsScreen(
initialSection: initialSection,
experimentalFeatures: experimentalFeatures,
source: source,
+ compactBelow: compactBelow,
),
transitionDuration: Duration.zero,
reverseTransitionDuration: Duration.zero,
@@ -72,6 +69,7 @@ class SettingsScreen extends StatefulWidget {
this.initialSection,
this.experimentalFeatures,
this.source = 'unknown',
+ this.compactBelow = 0,
}) : assert(
initialSection != SettingsSection.customize,
'Open workspace actions through showSettingsScreen.',
@@ -90,6 +88,10 @@ class SettingsScreen extends StatefulWidget {
/// does not show as selected.
final SettingsSection? initialSection;
+ /// Below this width the list of sections and one section take turns on the
+ /// screen instead of sitting side by side.
+ final double compactBelow;
+
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
@@ -98,6 +100,12 @@ class _SettingsScreenState extends State<SettingsScreen> {
late SettingsSection _section =
widget.initialSection ?? kDefaultSettingsSection;
+ /// Narrow screens only: a section is open over the list. Opening Settings on
+ /// a named section starts there; otherwise the list comes first.
+ late bool _sectionOpen = widget.initialSection != null;
+
+ bool get _compact => MediaQuery.sizeOf(context).width < widget.compactBelow;
+
KeyEventResult _key(FocusNode node, KeyEvent event) {
if (event is! KeyDownEvent ||
event.logicalKey != LogicalKeyboardKey.escape ||
@@ -138,6 +146,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
Navigator.of(context).pop(target);
return;
}
+ if (_compact) setState(() => _sectionOpen = true);
if (target == _section) return;
analytics.screenView(_screenName(target), source: 'rail');
setState(() => _section = target);
@@ -155,6 +164,21 @@ class _SettingsScreenState extends State<SettingsScreen> {
grid.AppTheme.watch(context);
// The native title bar already owns window dragging. Settings starts below
// it, with the same content inset on both sides of the divider.
+ final body = SettingsBody(
+ section: _section,
+ notifier: widget.notifier,
+ experimentalFeatures: widget.experimentalFeatures,
+ );
+ if (_compact) {
+ return SettingsCompactScreen(
+ section: _section,
+ sectionOpen: _sectionOpen,
+ onSelect: _show,
+ onBack: () => setState(() => _sectionOpen = false),
+ onKeyEvent: _key,
+ body: body,
+ );
+ }
return Focus(
onKeyEvent: _key,
child: Scaffold(
@@ -164,54 +188,10 @@ class _SettingsScreenState extends State<SettingsScreen> {
children: [
SettingsNav(section: _section, onSelect: _show),
VerticalDivider(width: 1, color: grid.AppPalette.divider),
- Expanded(
- child: _SettingsBody(
- section: _section,
- notifier: widget.notifier,
- experimentalFeatures: widget.experimentalFeatures,
- ),
- ),
+ Expanded(child: body),
],
),
),
);
}
}
-
-/// The screen behind a [SettingsSection].
-///
-/// Switch immediately, disposing the previous section instead of retaining it
-/// for a cross-fade while the new section starts its work.
-class _SettingsBody extends StatelessWidget {
- const _SettingsBody({
- required this.section,
- required this.notifier,
- this.experimentalFeatures,
- });
-
- final SettingsSection section;
- final AppNotifier notifier;
- final ExperimentalFeaturesStore? experimentalFeatures;
-
- @override
- Widget build(BuildContext context) {
- final screen = switch (section) {
- SettingsSection.account => AccountSection(notifier: notifier),
- SettingsSection.usage => const UsageSection(),
- SettingsSection.customize => throw StateError(
- 'Customization opens over the workspace.',
- ),
- SettingsSection.notifications => const NotificationsSection(),
- SettingsSection.experimental => ExperimentalSection(
- store: experimentalFeatures ?? notifier.experimentalFeatures,
- controller: notifier.swarmSettings,
- ),
- SettingsSection.devices => const DevicesSection(),
- SettingsSection.shortcuts => const ShortcutsSection(),
- SettingsSection.debug => const DebugSection(),
- SettingsSection.tracking => const TrackingSection(),
- SettingsSection.about => AboutSection(notifier: notifier),
- };
- return KeyedSubtree(key: ValueKey(section), child: screen);
- }
-}
diff --git a/desktop/lib/widgets/linux_menu_bar.dart b/desktop/lib/widgets/linux_menu_bar.dart
index 93d84b6cf..146b3b909 100644
--- a/desktop/lib/widgets/linux_menu_bar.dart
+++ b/desktop/lib/widgets/linux_menu_bar.dart
@@ -1,10 +1,9 @@
-import 'dart:io' show Platform;
-
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:lucide_icons_flutter/lucide_icons.dart';
import 'package:window_manager/window_manager.dart';
+import '../core/runtime_platform.dart';
import '../core/test_run.dart';
import '../screens/swarm_menu_bus.dart';
import '../shared/theme/app_theme.dart' as grid;
@@ -95,7 +94,8 @@ class _LinuxMenuBarState extends State<LinuxMenuBar> {
@override
Widget build(BuildContext context) {
- final visible = widget.visible ?? (Platform.isLinux && !kUnderTest);
+ // RuntimePlatform: false in a browser, where `dart:io` Platform throws.
+ final visible = widget.visible ?? (RuntimePlatform.isLinux && !kUnderTest);
if (!visible) return const SizedBox.shrink();
grid.AppTheme.watch(context);
return Container(
diff --git a/desktop/test/settings_compact_test.dart b/desktop/test/settings_compact_test.dart
new file mode 100644
index 000000000..646073001
--- /dev/null
+++ b/desktop/test/settings_compact_test.dart
@@ -0,0 +1,130 @@
+import 'dart:io';
+import 'dart:ui' as ui;
+
+import 'package:flutter/material.dart';
+import 'package:flutter/rendering.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/auth/auth_session.dart';
+import 'package:harness/core/config.dart';
+import 'package:harness/settings/settings_screen.dart';
+import 'package:harness/settings/settings_section.dart';
+import 'package:harness/shared/theme/app_theme.dart' as grid;
+import 'package:harness/state/app_state.dart';
+
+import 'support/real_fonts.dart';
+
+/// Settings on a phone (the web build's compact width): the list of sections
+/// and one section take turns instead of squeezing side by side.
+void main() {
+ setUpAll(loadRealFonts);
+ final boundary = GlobalKey();
+ const settingsButton = Key('open-settings');
+
+ Future<void> mount(
+ WidgetTester tester,
+ Size size, {
+ SettingsSection? initialSection,
+ }) async {
+ final notifier = AppNotifier(
+ config: AppConfig.dev,
+ authSession: AuthSession(),
+ configStore: null,
+ );
+ addTearDown(notifier.dispose);
+ tester.view.devicePixelRatio = 1;
+ tester.view.physicalSize = size;
+ addTearDown(tester.view.reset);
+ await tester.pumpWidget(
+ RepaintBoundary(
+ key: boundary,
+ child: MaterialApp(
+ theme: grid.buildAppTheme(brightness: Brightness.dark),
+ home: Builder(
+ builder: (context) => Scaffold(
+ body: TextButton(
+ key: settingsButton,
+ onPressed: () => showSettingsScreen(
+ context,
+ notifier,
+ initialSection: initialSection,
+ source: 'test',
+ compactBelow: 720,
+ ),
+ child: const Text('Settings'),
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ await tester.tap(find.byKey(settingsButton));
+ await tester.pumpAndSettle();
+ }
+
+ Future<void> capture(WidgetTester tester, String name) async {
+ final output = Platform.environment['HARNESS_REFINEMENT_CAPTURE_DIR'];
+ if (output == null) return;
+ await tester.runAsync(() async {
+ final image =
+ await (boundary.currentContext!.findRenderObject()
+ as RenderRepaintBoundary)
+ .toImage();
+ final data = await image.toByteData(format: ui.ImageByteFormat.png);
+ Directory(output).createSync(recursive: true);
+ File('$output/$name.png').writeAsBytesSync(data!.buffer.asUint8List());
+ image.dispose();
+ });
+ }
+
+ final list = find.byKey(const Key('settings-back-button'));
+ final back = find.byKey(const Key('settings-compact-back'));
+
+ testWidgets('a phone opens the list, then one section at full width', (
+ tester,
+ ) async {
+ await mount(tester, const Size(390, 844));
+ expect(list, findsOneWidget);
+ expect(back, findsNothing);
+ // No keyboard thrown over the list the moment it opens.
+ final search = find.byKey(const Key('settings-search-field'));
+ expect(tester.widget<TextField>(search).focusNode!.hasFocus, isFalse);
+ await capture(tester, 'settings-phone-list');
+
+ await tester.tap(find.text(SettingsSection.notifications.label));
+ await tester.pumpAndSettle();
+ expect(list, findsNothing);
+ expect(back, findsOneWidget);
+ await capture(tester, 'settings-phone-section');
+
+ await tester.tap(back);
+ await tester.pumpAndSettle();
+ expect(list, findsOneWidget);
+ expect(tester.takeException(), isNull);
+ });
+
+ testWidgets('system Back leaves the section before leaving Settings', (
+ tester,
+ ) async {
+ await mount(
+ tester,
+ const Size(390, 844),
+ initialSection: SettingsSection.notifications,
+ );
+ expect(back, findsOneWidget);
+ await tester.binding.handlePopRoute();
+ await tester.pumpAndSettle();
+ expect(list, findsOneWidget);
+ await tester.binding.handlePopRoute();
+ await tester.pumpAndSettle();
+ expect(find.byType(SettingsScreen), findsNothing);
+ });
+
+ testWidgets('a wide window keeps the list beside the section', (
+ tester,
+ ) async {
+ await mount(tester, const Size(1280, 800));
+ expect(list, findsOneWidget);
+ expect(back, findsNothing);
+ expect(find.text(SettingsSection.notifications.label), findsOneWidget);
+ });
+}
diff --git a/desktop/test/web/native_menus_web_test.dart b/desktop/test/web/native_menus_web_test.dart
new file mode 100644
index 000000000..93a37682c
--- /dev/null
+++ b/desktop/test/web/native_menus_web_test.dart
@@ -0,0 +1,24 @@
+@TestOn('browser')
+library;
+
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/screens/swarm_menu_bus.dart';
+import 'package:harness/widgets/linux_menu_bar.dart';
+
+/// The native menu surfaces sit above every screen, sign-in included, so the
+/// browser build builds them too: they must read the platform without
+/// `dart:io`, whose `Platform` throws on the web before the first frame.
+void main() {
+ test('the menu bus is built without dart:io', () {
+ expect(SwarmMenuBus.new, returnsNormally);
+ });
+
+ testWidgets('the Linux menu bar draws nothing in a browser', (tester) async {
+ await tester.pumpWidget(
+ MaterialApp(home: LinuxMenuBar(onAction: (_) async {})),
+ );
+ expect(tester.takeException(), isNull);
+ expect(find.byType(MenuBar), findsNothing);
+ });
+}