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(desktop): middle-click a tab to close it

0xdiego2585357a4aaf83
diff --git a/desktop/lib/screens/swarm_screen.dart b/desktop/lib/screens/swarm_screen.dart
index 83fd1c487..17ba52140 100644
--- a/desktop/lib/screens/swarm_screen.dart
+++ b/desktop/lib/screens/swarm_screen.dart
@@ -4,6 +4,7 @@ import 'dart:io' show Platform;
import 'package:file_selector/file_selector.dart';
import 'package:flutter/foundation.dart' show listEquals;
+import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@@ -127,6 +128,10 @@ TextStyle get _boxCaption =>
class _SwarmScreenState extends State<SwarmScreen>
with SingleTickerProviderStateMixin {
static const _channel = MethodChannel('harness/swarm_tabs');
+
+ /// The tab the middle button went down on, so an up that slid onto another
+ /// tab closes nothing. Null between presses.
+ String? _middleDownTab;
late final bool _native =
widget.nativeTabs ?? (Platform.isMacOS && !kUnderTest);
late final SwarmProjectStore _projects =
@@ -3351,102 +3356,121 @@ class _SwarmScreenState extends State<SwarmScreen>
return ReorderableDragStartListener(
key: ValueKey(swarm.id),
index: index,
- child: GestureDetector(
- onDoubleTap: () => _rename(swarm.id),
- child: _TabActionsReveal(
- builder: (showClose) => Container(
- width: _tabExtent - 2,
- margin: const EdgeInsets.only(top: 4, right: 2),
- decoration: ShapeDecoration(
- color: app.activeSwarmId == swarm.id
- ? grid.AppPalette.swarmField
- : Colors.transparent,
- shape: const TerminalTabBorder(),
- ),
- child: Row(
- children: [
- Expanded(
- child: TextButton(
- onPressed: () =>
- app.selectSwarm(swarm.id),
- style: TextButton.styleFrom(
- shape: const RoundedRectangleBorder(
- borderRadius: BorderRadius.all(
- Radius.circular(
- kTerminalCornerRadius,
+ child: Listener(
+ // Middle-click closes the tab, as it does in every
+ // browser. On the UP, and only inside the tab it went
+ // down on: a press that slid off changed its mind.
+ onPointerDown: (event) {
+ _middleDownTab = event.buttons == kTertiaryButton
+ ? swarm.id
+ : null;
+ },
+ onPointerUp: (event) {
+ final armed = _middleDownTab;
+ _middleDownTab = null;
+ if (armed == swarm.id) {
+ unawaited(app.closeSwarm(swarm.id));
+ }
+ },
+ child: GestureDetector(
+ onDoubleTap: () => _rename(swarm.id),
+ child: _TabActionsReveal(
+ builder: (showClose) => Container(
+ width: _tabExtent - 2,
+ margin: const EdgeInsets.only(top: 4, right: 2),
+ decoration: ShapeDecoration(
+ color: app.activeSwarmId == swarm.id
+ ? grid.AppPalette.swarmField
+ : Colors.transparent,
+ shape: const TerminalTabBorder(),
+ ),
+ child: Row(
+ children: [
+ Expanded(
+ child: TextButton(
+ onPressed: () =>
+ app.selectSwarm(swarm.id),
+ style: TextButton.styleFrom(
+ shape: const RoundedRectangleBorder(
+ borderRadius: BorderRadius.all(
+ Radius.circular(
+ kTerminalCornerRadius,
+ ),
),
),
+ animationDuration: Duration.zero,
+ foregroundColor:
+ app.activeSwarmId == swarm.id
+ ? Colors.white
+ : Colors.white70,
),
- animationDuration: Duration.zero,
- foregroundColor:
- app.activeSwarmId == swarm.id
- ? Colors.white
- : Colors.white70,
- ),
- child: Row(
- children: [
- if (swarm.isStore)
- StoreMark(
- key: ValueKey(
- 'tab-store:${swarm.id}',
- ),
- )
- else if (_tabAgents(swarm).length ==
- 1)
- EngineMark(
- key: ValueKey(
- 'tab-engine:${swarm.id}',
- ),
- engine: _tabEngine(swarm),
- size: 16,
- )
- else if (_tabAgents(swarm).length > 1)
- SwarmIcon(
- key: ValueKey(
- 'tab-group:${swarm.id}',
- ),
- size: 16,
- color: Colors.white70,
- )
- else
- const Icon(Icons.add, size: 16),
- const SizedBox(width: 8),
- Expanded(
- child: Text(
- swarm.name,
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- // Like the native tabs.
- style: grid.AppType.monoLabel(
- color: Colors.white,
- fontWeight: FontWeight.w400,
+ child: Row(
+ children: [
+ if (swarm.isStore)
+ StoreMark(
+ key: ValueKey(
+ 'tab-store:${swarm.id}',
+ ),
+ )
+ else if (_tabAgents(swarm).length ==
+ 1)
+ EngineMark(
+ key: ValueKey(
+ 'tab-engine:${swarm.id}',
+ ),
+ engine: _tabEngine(swarm),
+ size: 16,
+ )
+ else if (_tabAgents(swarm).length >
+ 1)
+ SwarmIcon(
+ key: ValueKey(
+ 'tab-group:${swarm.id}',
+ ),
+ size: 16,
+ color: Colors.white70,
+ )
+ else
+ const Icon(Icons.add, size: 16),
+ const SizedBox(width: 8),
+ Expanded(
+ child: Text(
+ swarm.name,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ // Like the native tabs.
+ style: grid.AppType.monoLabel(
+ color: Colors.white,
+ fontWeight: FontWeight.w400,
+ ),
),
),
- ),
- ],
+ ],
+ ),
),
),
- ),
- Opacity(
- key: ValueKey('tab-close:${swarm.id}'),
- opacity: showClose ? 1 : 0,
- alwaysIncludeSemantics: true,
- child: IconButton(
- onPressed: () => app.closeSwarm(swarm.id),
- icon: Icon(
- Icons.close,
- size: 13,
- semanticLabel: 'Close ${swarm.name}',
+ Opacity(
+ key: ValueKey('tab-close:${swarm.id}'),
+ opacity: showClose ? 1 : 0,
+ alwaysIncludeSemantics: true,
+ child: IconButton(
+ onPressed: () =>
+ app.closeSwarm(swarm.id),
+ icon: Icon(
+ Icons.close,
+ size: 13,
+ semanticLabel: 'Close ${swarm.name}',
+ ),
+ constraints:
+ const BoxConstraints.tightFor(
+ width: 30,
+ height: 30,
+ ),
+ padding: EdgeInsets.zero,
),
- constraints:
- const BoxConstraints.tightFor(
- width: 30,
- height: 30,
- ),
- padding: EdgeInsets.zero,
),
- ),
- ],
+ ],
+ ),
),
),
),
diff --git a/desktop/macos/Runner/SwarmTitlebar.swift b/desktop/macos/Runner/SwarmTitlebar.swift
index 1db6f3a83..2d7c8e279 100644
--- a/desktop/macos/Runner/SwarmTitlebar.swift
+++ b/desktop/macos/Runner/SwarmTitlebar.swift
@@ -1786,6 +1786,8 @@ private final class SwarmTabButton: NSView, NSDraggingSource, NSMenuItemValidati
var isHovered: Bool { hovered && actionsEnabled }
private let closeButton = SwarmCloseButton()
private let selectButton = SwarmSelectButton()
+ /// Whether the middle button went down on THIS tab — see otherMouseUp.
+ private var middleDown = false
private let iconView = NSImageView()
var icon: NSImage? {
get { iconView.image }
@@ -1985,6 +1987,20 @@ private final class SwarmTabButton: NSView, NSDraggingSource, NSMenuItemValidati
// The tab owns the full click sequence, including clicks on its padding.
// Forwarding mouseUp lets AppKit also treat a rename as a titlebar zoom.
override func mouseUp(with event: NSEvent) {}
+ // Middle-click closes the tab, as it does in every browser and in Ghostty —
+ // the one gesture people arrive with and find missing here. On the UP, and
+ // only when it went down on this same tab: a press that slid off is a press
+ // that changed its mind, and a tab that vanished under the button would be a
+ // click nobody could take back.
+ override func otherMouseDown(with event: NSEvent) {
+ middleDown = actionsEnabled && event.buttonNumber == 2
+ }
+ override func otherMouseUp(with event: NSEvent) {
+ defer { middleDown = false }
+ guard middleDown, event.buttonNumber == 2, actionsEnabled else { return }
+ guard bounds.contains(convert(event.locationInWindow, from: nil)) else { return }
+ emit?("close", ["id": swarmId])
+ }
override func mouseDragged(with event: NSEvent) {
guard actionsEnabled else { return }
if hypot(event.locationInWindow.x - downPoint.x, event.locationInWindow.y - downPoint.y) < 5 { return }