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.

Desktop: bring Harness forward on macOS device gestures (#1002)

Deef0cc71336f46
diff --git a/desktop/integration_test/device_window_activation_test.dart b/desktop/integration_test/device_window_activation_test.dart
new file mode 100644
index 000000000..c6a578a96
--- /dev/null
+++ b/desktop/integration_test/device_window_activation_test.dart
@@ -0,0 +1,70 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/core/runtime_platform.dart';
+import 'package:harness/core/test_run.dart';
+import 'package:integration_test/integration_test.dart';
+import 'package:window_manager/window_manager.dart';
+
+import '../test/device_window_activation_test.dart'
+ show deviceWindowApp, deviceEvent;
+import '../test/swarm_screen_test.dart' show mount;
+
+void main() {
+ if (!kUnderTest || !RuntimePlatform.isMacOS) {
+ throw StateError('Run this macOS fixture with FLUTTER_TEST=1');
+ }
+ IntegrationTestWidgetsFlutterBinding.ensureInitialized();
+
+ testWidgets('device gestures reveal the hidden and minimized Mac window', (
+ tester,
+ ) async {
+ await windowManager.ensureInitialized();
+ await windowManager.show();
+ await windowManager.focus();
+ final app = deviceWindowApp();
+ await mount(tester, app);
+ try {
+ for (final minimize in [false, true]) {
+ if (minimize) {
+ await windowManager.minimize();
+ } else {
+ await windowManager.hide();
+ }
+ await tester.pump(const Duration(milliseconds: 700));
+ expect(await windowManager.isFocused(), isFalse);
+ expect(app.inForeground, isFalse);
+
+ // An automatically displayed question must leave the window alone.
+ await deviceEvent(app, 'dial_open', {
+ 'agentId': 'a0',
+ 'reason': 'question',
+ });
+ await tester.pump(const Duration(milliseconds: 100));
+ expect(await windowManager.isFocused(), isFalse);
+
+ await deviceEvent(
+ app,
+ minimize ? 'dial_scroll' : 'dial_focus',
+ minimize ? {'phase': 'down'} : {'agentId': 'a0'},
+ );
+ for (var attempt = 0; attempt < 40; attempt++) {
+ await tester.pump(const Duration(milliseconds: 50));
+ if (await windowManager.isFocused() &&
+ !await windowManager.isMinimized()) {
+ break;
+ }
+ }
+ expect(await windowManager.isVisible(), isTrue);
+ expect(await windowManager.isMinimized(), isFalse);
+ expect(await windowManager.isFocused(), isTrue);
+ expect(app.allPanes, hasLength(1));
+ expect(app.paneFocusByUser, isFalse);
+ }
+ } finally {
+ await windowManager.restore();
+ await windowManager.show();
+ await tester.pumpWidget(const SizedBox());
+ app.dispose();
+ }
+ });
+}
diff --git a/desktop/lib/screens/swarm_screen.dart b/desktop/lib/screens/swarm_screen.dart
index e26d7f1fd..111f75ffc 100644
--- a/desktop/lib/screens/swarm_screen.dart
+++ b/desktop/lib/screens/swarm_screen.dart
@@ -259,6 +259,9 @@ class _SwarmScreenState extends State<SwarmScreen> {
widget.projectStore ??
SwarmProjectStore(storage: kUnderTest ? null : HarnessFileStore.shared);
StreamSubscription<SpokenTaskRequest>? _spokenTasks;
+ StreamSubscription<void>? _deviceWindowRequests;
+ Timer? _deviceRevealCooldown;
+ bool _deviceRevealInFlight = false;
StreamSubscription<void>? _modelsRequests;
final _shellFocus = FocusNode(debugLabel: 'Tab shell');
@@ -709,6 +712,9 @@ class _SwarmScreenState extends State<SwarmScreen> {
unawaited(_projects.load());
unawaited(_navigation.load());
_spokenTasks = app.spokenTasks.listen(_openSpokenTask);
+ _deviceWindowRequests = app.deviceWindowRequests.listen(
+ (_) => unawaited(_revealForDevice()),
+ );
// The app's shared controller, so this menu and every pane's model picker show one reading.
_modelsMenu = widget.modelsMenu ?? app.modelsMenu;
_modelsMenu!.addListener(_subscriptionUsageChanged);
@@ -894,6 +900,8 @@ class _SwarmScreenState extends State<SwarmScreen> {
_commandFocus.dispose();
if (_hasCommandBar) _commandBar.dispose();
unawaited(_spokenTasks?.cancel());
+ unawaited(_deviceWindowRequests?.cancel());
+ _deviceRevealCooldown?.cancel();
app.systemNotifications.onTap = null;
if (_menuHost) {
unawaited(_menuBus.send('machinesState', {'machines': []}));
@@ -6222,6 +6230,28 @@ class _SwarmScreenState extends State<SwarmScreen> {
);
});
+ Future<void> _revealForDevice() async {
+ if (!RuntimePlatform.isMacOS ||
+ !mounted ||
+ app.inForeground ||
+ nativePickerOpen ||
+ _deviceRevealInFlight ||
+ _deviceRevealCooldown != null) {
+ return;
+ }
+ // The first gesture raises immediately. Rapid dial steps share that
+ // request, including when macOS declines it or has not reported focus yet.
+ _deviceRevealCooldown = Timer(const Duration(milliseconds: 500), () {
+ _deviceRevealCooldown = null;
+ });
+ _deviceRevealInFlight = true;
+ try {
+ await revealWindow();
+ } finally {
+ _deviceRevealInFlight = false;
+ }
+ }
+
Future<void> _openSpokenTask(SpokenTaskRequest request) async {
final spoken = SpokenTask(
voiceId: request.voiceId,
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index fc297b151..d048f6c38 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -665,6 +665,23 @@ class AppNotifier extends ChangeNotifier {
/// Words from the dial, for whoever can put a palette on screen.
Stream<SpokenTaskRequest> get spokenTasks => _spokenTasks.stream;
+
+ final StreamController<void> _deviceWindowRequests =
+ StreamController<void>.broadcast();
+
+ /// Explicit gestures at this computer's device that need its window visible.
+ /// Automatic question displays and status updates never request activation.
+ Stream<void> get deviceWindowRequests => _deviceWindowRequests.stream;
+
+ void _requestDeviceWindow(MachineState source) {
+ if (!_disposed &&
+ viewer == null &&
+ source.usesLocalTransport &&
+ !source.machine.isShared) {
+ _deviceWindowRequests.add(null);
+ }
+ }
+
final StreamController<void> _modelsRequests =
StreamController<void>.broadcast();
@@ -15671,6 +15688,9 @@ class AppNotifier extends ChangeNotifier {
'up' => 2,
_ => 1,
};
+ // A finger going down is the gesture; moves and the inertial tail
+ // must not keep asking macOS for focus after the person switches away.
+ if (phase == 0) _requestDeviceWindow(machine);
activeTerminal?.scroll(
phase,
(payload['dy'] as num?)?.round() ?? 0,
@@ -15718,6 +15738,7 @@ class AppNotifier extends ChangeNotifier {
if (agentId is String && agentId.isNotEmpty) {
final targetMachineId = _dialFocusMachine(payload, agentId);
if (targetMachineId != null) {
+ _requestDeviceWindow(machine);
unawaited(selectAgentFromDial(targetMachineId, agentId));
}
}
@@ -15754,6 +15775,9 @@ class AppNotifier extends ChangeNotifier {
// line follow from that — nothing is answered to the dial directly.
final swarmId = payload['swarmId'];
if (swarmId is String && swarmId.isNotEmpty) {
+ if (swarms.any((swarm) => swarm.id == swarmId)) {
+ _requestDeviceWindow(machine);
+ }
_fromDevice(() => selectSwarm(swarmId));
}
break;
@@ -15766,6 +15790,7 @@ class AppNotifier extends ChangeNotifier {
if (forkId is String && forkId.isNotEmpty) {
final targetMachineId = _dialFocusMachine(payload, forkId);
if (targetMachineId != null) {
+ _requestDeviceWindow(machine);
unawaited(
_fromDevice(
() => placeFork(
@@ -15911,6 +15936,7 @@ class AppNotifier extends ChangeNotifier {
if (openId is String && openId.isNotEmpty) {
final targetMachineId = _dialFocusMachine(payload, openId);
if (targetMachineId != null) {
+ if (payload['reason'] != 'question') _requestDeviceWindow(machine);
unawaited(
openAgentFromDial(
targetMachineId,
@@ -16385,6 +16411,7 @@ class AppNotifier extends ChangeNotifier {
swarm.panes.clear();
}
unawaited(_spokenTasks.close());
+ unawaited(_deviceWindowRequests.close());
unawaited(_zooPushes.close());
unawaited(_daemonFrames.close());
unawaited(_modelsRequests.close());
diff --git a/desktop/test/device_window_activation_test.dart b/desktop/test/device_window_activation_test.dart
new file mode 100644
index 000000000..84b46f88d
--- /dev/null
+++ b/desktop/test/device_window_activation_test.dart
@@ -0,0 +1,154 @@
+import 'dart:async';
+
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/core/desktop_window.dart';
+import 'package:harness/core/runtime_platform.dart';
+import 'package:harness/state/app_state.dart';
+import 'package:harness/ws/local_cli_discovery.dart';
+
+import 'swarm_screen_test.dart' show mount, terminal;
+import 'swarm_state_test.dart' show createApp;
+
+AppNotifier deviceWindowApp() {
+ final app = createApp();
+ app.machineStates['m']!.localEndpoint = LocalCliEndpoint(
+ computerId: 'fixture',
+ wsUri: Uri.parse('ws://127.0.0.1:1'),
+ protocolVersion: 1,
+ terminalProtocolVersion: 3,
+ );
+ app.adoptSessionForTest(terminal('a0', []));
+ return app;
+}
+
+Future<void> deviceEvent(
+ AppNotifier app,
+ String type, [
+ Map<String, dynamic> payload = const {},
+]) => app.handleEventForTest('m', {'type': type, 'payload': payload});
+
+void main() {
+ test(
+ 'local navigation, notification taps and scroll starts request the window',
+ () async {
+ final app = deviceWindowApp();
+ addTearDown(app.dispose);
+ var requests = 0;
+ final subscription = app.deviceWindowRequests.listen((_) => requests++);
+ addTearDown(subscription.cancel);
+
+ await deviceEvent(app, 'dial_focus', {'machineId': 'm', 'agentId': 'a0'});
+ await deviceEvent(app, 'dial_swarm', {'swarmId': app.activeSwarmId});
+ await deviceEvent(app, 'dial_scroll', {'phase': 'down', 'dy': 0});
+ await deviceEvent(app, 'dial_open', {'machineId': 'm', 'agentId': 'a0'});
+ expect(requests, 4);
+ expect(
+ app.paneFocusByUser,
+ isFalse,
+ reason: 'activation must not retake terminals',
+ );
+ expect(app.allPanes, hasLength(1));
+ },
+ );
+
+ test(
+ 'automatic updates, invalid targets and scroll tails stay quiet',
+ () async {
+ final app = deviceWindowApp();
+ addTearDown(app.dispose);
+ var requests = 0;
+ final subscription = app.deviceWindowRequests.listen((_) => requests++);
+ addTearDown(subscription.cancel);
+
+ await deviceEvent(app, 'dial_status');
+ await deviceEvent(app, 'dial_open', {
+ 'agentId': 'a0',
+ 'reason': 'question',
+ });
+ await deviceEvent(app, 'dial_focus', {'agentId': 'missing'});
+ await deviceEvent(app, 'dial_open', {'agentId': 'missing'});
+ await deviceEvent(app, 'dial_swarm', {'swarmId': 'missing'});
+ await deviceEvent(app, 'dial_scroll', {'phase': 'move', 'dy': 4});
+ await deviceEvent(app, 'dial_scroll', {'phase': 'up', 'velocity': 120});
+ expect(requests, 0);
+ },
+ );
+
+ test('a remote connection cannot request this computer’s window', () async {
+ final app = deviceWindowApp();
+ addTearDown(app.dispose);
+ app.machineStates['m']!.localEndpoint = null;
+ var requests = 0;
+ final subscription = app.deviceWindowRequests.listen((_) => requests++);
+ addTearDown(subscription.cancel);
+ await deviceEvent(app, 'dial_focus', {'agentId': 'a0'});
+ await deviceEvent(app, 'dial_open', {'agentId': 'a0'});
+ await deviceEvent(app, 'dial_swarm', {'swarmId': app.activeSwarmId});
+ await deviceEvent(app, 'dial_scroll', {'phase': 'down'});
+ expect(requests, 0);
+ });
+
+ testWidgets(
+ 'Mac device input restores and focuses once per burst, respecting native pickers',
+ (tester) async {
+ const window = MethodChannel('window_manager');
+ final calls = <String>[];
+ var minimized = true;
+ tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(window, (
+ call,
+ ) async {
+ if (call.method == 'isMinimized') return minimized;
+ calls.add(call.method);
+ if (call.method == 'restore') minimized = false;
+ return null;
+ });
+ addTearDown(
+ () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
+ window,
+ null,
+ ),
+ );
+ final app = deviceWindowApp();
+ await mount(tester, app);
+ app.foreground.value = false;
+
+ Future<void> focus() async {
+ await deviceEvent(app, 'dial_focus', {'agentId': 'a0'});
+ await tester.pump();
+ }
+
+ try {
+ await focus();
+ expect(calls, ['restore', 'show', 'focus']);
+ expect(minimized, isFalse);
+ calls.clear();
+ await focus();
+ await deviceEvent(app, 'dial_scroll', {'phase': 'down'});
+ await tester.pump();
+ expect(calls, isEmpty);
+
+ await tester.pump(const Duration(milliseconds: 500));
+ final picker = Completer<void>();
+ final picking = whileNativePicker(() => picker.future);
+ await focus();
+ expect(calls, isEmpty);
+ picker.complete();
+ await picking;
+ await focus();
+ expect(calls, ['show', 'focus']);
+
+ calls.clear();
+ await tester.pump(const Duration(milliseconds: 500));
+ app.foreground.value = true;
+ await focus();
+ expect(calls, isEmpty, reason: 'the app already has focus');
+ } finally {
+ await tester.pumpWidget(const SizedBox());
+ app.dispose();
+ }
+ },
+ skip: !RuntimePlatform.isMacOS,
+ );
+}