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.

Super Tab: a closed tab is easy to get back (#1455)

0xken15bd2fb1cf1c65
diff --git a/desktop/lib/screens/swarm_screen.dart b/desktop/lib/screens/swarm_screen.dart
index fd45ebfea..c9575fe8c 100644
--- a/desktop/lib/screens/swarm_screen.dart
+++ b/desktop/lib/screens/swarm_screen.dart
@@ -274,6 +274,12 @@ class _SwarmScreenState extends State<SwarmScreen> {
Timer? _deviceRevealCooldown;
bool _deviceRevealInFlight = false;
StreamSubscription<void>? _modelsRequests;
+ StreamSubscription<String>? _superTabClosures;
+ // The one Super Tab notice: whether it exists (queued or shown), whether it
+ // is on screen, and the closed tab its Reopen restores.
+ int _superTabNoticeSerial = 0;
+ bool _superTabNoticeLive = false, _superTabNoticeShowing = false;
+ String? _superTabReopenId;
final _shellFocus = FocusNode(debugLabel: 'Tab shell');
/// Where the keyboard waits after the active tab closes
@@ -505,6 +511,10 @@ class _SwarmScreenState extends State<SwarmScreen> {
// as itself, a conversation Harness did not start as a harness resuming it.
// Super Tab creation uses the same form as Cmd-N, without recent sessions.
app: app.activeSwarm.isSuperTab ? null : app,
+ // The palette lists the same getter. A Super Tab draft has no recents but
+ // still offers these.
+ closedSuperTabs: app.closedSuperTabs,
+ onOpenSuperTab: _openClosedSuperTab,
projects: _projects.projects,
onOpen: (row) {
if (_newHarness?.requestDismiss() == false) return;
@@ -742,6 +752,7 @@ class _SwarmScreenState extends State<SwarmScreen> {
_toggleModels(initialTab: ModelsTab.local);
}
});
+ _superTabClosures = app.superTabClosures.listen(_offerSuperTabReopen);
if (!kUnderTest) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
@@ -891,6 +902,7 @@ class _SwarmScreenState extends State<SwarmScreen> {
_modelsMenu?.removeListener(_subscriptionUsageChanged);
app.modelManager.setPanelVisible(false);
_modelsRequests?.cancel();
+ _superTabClosures?.cancel();
_keymap.removeListener(_keymapChanged);
_defaultKeymap.dispose();
grid.AppTheme.palette.removeListener(_paletteChanged);
@@ -3275,6 +3287,7 @@ class _SwarmScreenState extends State<SwarmScreen> {
/// Settings ▸ Usage. A palette that finds a setting by name is how an editor
/// makes a settings screen nobody has to navigate.
static const _settingsCommand = 'settings:';
+ static const _closedSuperTabCommand = 'super_tab.open:';
Future<void> _manageMachines() async {
Future<void> open() => app.manageMachines(
@@ -4130,6 +4143,61 @@ class _SwarmScreenState extends State<SwarmScreen> {
);
}
+ /// Like the other New Tab page rows: ask a typed New Harness draft first,
+ /// then open into this tab when it is empty.
+ void _openClosedSuperTab(String tabId) {
+ if (_newHarness?.requestDismiss() == false) return;
+ _closeNewHarness(restoreFocus: false);
+ app.openClosedSuperTab(tabId, intoSwarmId: app.activeSwarmId);
+ }
+
+ /// Closing a Super Tab asks nothing, so the way back is said right after.
+ /// A second close replaces this notice rather than queueing behind it.
+ void _offerSuperTabReopen(String historyId) {
+ if (!mounted) return;
+ final messenger = ScaffoldMessenger.maybeOf(context);
+ if (messenger == null) return;
+ // One notice serves every close: Reopen reads the latest id when pressed.
+ _superTabReopenId = historyId;
+ // Still queued: its text is the same, so it will do.
+ if (_superTabNoticeLive && !_superTabNoticeShowing) return;
+ // On screen and therefore ours: replace it so the 8 s start again.
+ if (_superTabNoticeShowing) messenger.removeCurrentSnackBar();
+ final serial = ++_superTabNoticeSerial;
+ _superTabNoticeLive = true;
+ _superTabNoticeShowing = false;
+ final notice = messenger.showSnackBar(
+ SnackBar(
+ key: const ValueKey('super-tab-closed'),
+ // A snack bar with an action stays until dismissed unless told not to.
+ persist: false,
+ duration: const Duration(seconds: 8),
+ // A queued snack bar is built only once it is current, so this is the
+ // reliable "on screen now" signal.
+ content: _SuperTabClosedText(
+ onShown: () {
+ if (serial == _superTabNoticeSerial) _superTabNoticeShowing = true;
+ },
+ ),
+ action: SnackBarAction(
+ label: 'Reopen',
+ onPressed: () {
+ if (_superTabReopenId case final id?) {
+ app.reopenClosedSwarm(historyId: id);
+ }
+ },
+ ),
+ ),
+ );
+ unawaited(
+ notice.closed.then((_) {
+ if (serial != _superTabNoticeSerial) return;
+ _superTabNoticeLive = false;
+ _superTabNoticeShowing = false;
+ }),
+ );
+ }
+
void _showPaneActionHint(String message) {
if (!mounted) return;
ScaffoldMessenger.maybeOf(context)
@@ -4301,6 +4369,12 @@ class _SwarmScreenState extends State<SwarmScreen> {
// The result list is gone before a dialog or focus-changing command runs.
// Recheck availability: a machine or pane may have changed while typing.
FocusManager.instance.applyFocusChangesIfNeeded();
+ if (command.startsWith(_closedSuperTabCommand)) {
+ if (_canExecuteCommand('super_tab.new')) {
+ _openClosedSuperTab(command.substring(_closedSuperTabCommand.length));
+ }
+ return;
+ }
if (command.startsWith(_settingsCommand)) {
final section = SettingsSection.values
.where((s) => s.name == command.substring(_settingsCommand.length))
@@ -7577,6 +7651,18 @@ class _SwarmScreenState extends State<SwarmScreen> {
shortcut: _keymap.hint(command.id),
searchFields: [command.id, ...command.keywords],
),
+ // Where New Super Tab lives: the tabs closed here, back by name.
+ if (_canExecuteCommand('super_tab.new'))
+ for (final tab in app.closedSuperTabs)
+ SwarmDestination(
+ id: 'command:$_closedSuperTabCommand${tab.id}',
+ title: tab.name,
+ detail: 'Closed Super Tabs',
+ swarmId: null,
+ current: false,
+ commandId: '$_closedSuperTabCommand${tab.id}',
+ searchFields: ['closed', 'reopen', 'super tab'],
+ ),
if (_canExecuteCommand('app.settings'))
for (final group in settingsGroups)
for (final section in group.sections)
@@ -8932,3 +9018,25 @@ class _SwarmScreenState extends State<SwarmScreen> {
),
);
}
+
+class _SuperTabClosedText extends StatefulWidget {
+ const _SuperTabClosedText({required this.onShown});
+ final VoidCallback onShown;
+
+ @override
+ State<_SuperTabClosedText> createState() => _SuperTabClosedTextState();
+}
+
+class _SuperTabClosedTextState extends State<_SuperTabClosedText> {
+ @override
+ void initState() {
+ super.initState();
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (mounted) widget.onShown();
+ });
+ }
+
+ @override
+ Widget build(BuildContext context) =>
+ const Text('Super Tab closed. Its agents keep running.');
+}
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 299b051d3..232d0be05 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -1380,6 +1380,8 @@ class AppNotifier extends ChangeNotifier {
_closeOwnerMemories();
deviceHosts.clear();
_deviceReads.clear();
+ // Names and membership belong to the account that discovered them.
+ _superTabNames = {};
}
_currentUser = profile;
final id = profile?.id;
@@ -1664,6 +1666,11 @@ class AppNotifier extends ChangeNotifier {
return;
}
var changed = false;
+ // Membership and names for the closed list: a tab hidden before a
+ // restart has no open view to take its name from.
+ final names = {for (final tab in tabs) tab.id: tab.name};
+ final namesChanged = !mapEquals(names, _superTabNames);
+ _superTabNames = names;
for (final tab in tabs) {
final existing = swarms.where((s) => s.superTabId == tab.id).firstOrNull;
if (existing != null) {
@@ -1684,10 +1691,9 @@ class AppNotifier extends ChangeNotifier {
changed = true;
}
}
- if (changed) {
- _persistLayout();
- notifyListeners();
- }
+ if (changed) _persistLayout();
+ // The closed list is read from these names, so it can change on its own.
+ if (changed || namesChanged) notifyListeners();
},
onSnapshot: (controller) {
if (_disposed || controller.accountId != currentUser?.id) return;
@@ -1720,6 +1726,30 @@ class AppNotifier extends ChangeNotifier {
},
);
+ Map<String, String> _superTabNames = {};
+ final StreamController<String> _superTabClosures =
+ StreamController<String>.broadcast();
+
+ /// Closing a Super Tab only hides it here, which looks like losing it. Each
+ /// closure announces its history id so the screen can offer to reopen it.
+ Stream<String> get superTabClosures => _superTabClosures.stream;
+
+ /// Super Tabs hidden on this computer that the account still belongs to,
+ /// by name. Saved with the hidden set, so a restart keeps them.
+ List<({String id, String name})> get closedSuperTabs {
+ final open = {for (final s in swarms) s.superTabId};
+ return [
+ for (final id in superTabs.hiddenIds)
+ if (!open.contains(id) && _superTabNames.containsKey(id))
+ (
+ id: id,
+ name: _superTabNames[id]!.trim().isEmpty
+ ? 'Super Tab'
+ : _superTabNames[id]!,
+ ),
+ ]..sort((a, b) => a.name.toLowerCase().compareTo(b.name.toLowerCase()));
+ }
+
Future<Map<String, dynamic>> superTabMachineRequest(
String machineId,
Map<String, dynamic> payload,
@@ -1769,22 +1799,53 @@ class AppNotifier extends ChangeNotifier {
swarm.name = name;
swarm.nameIsCustom = true;
swarm.isNewTabPage = false;
+ // A tab bound here is one the person belongs to.
+ _superTabNames[tabId] = name;
_rememberSuperTabVisibility(tabId, true);
_persistLayout();
notifyListeners();
}
- void openSuperTab(String tabId, String name) {
+ /// The one way to show a Super Tab: focus its view if it is open, else give
+ /// it one. An untouched New Tab page named by [intoSwarmId] becomes the view
+ /// instead of being left behind. Any history entry for the tab is dropped, so
+ /// Reopen later cannot add a second view.
+ void openSuperTab(String tabId, String name, {String? intoSwarmId}) {
_rememberSuperTabVisibility(tabId, true);
+ _closedHistory.removeWhere(
+ (entry) => entry is ClosedSwarm && entry.superTabId == tabId,
+ );
final existing = swarms.where((s) => s.superTabId == tabId).firstOrNull;
if (existing != null) {
selectSwarm(existing.id);
return;
}
+ final page = swarms
+ .where(
+ (s) =>
+ s.id == intoSwarmId &&
+ s.panes.isEmpty &&
+ (s.isNewTabPage || (s.isSuperTab && s.superTabId == null)),
+ )
+ .firstOrNull;
+ if (page != null) {
+ bindSuperTab(page.id, tabId, name);
+ selectSwarm(page.id);
+ return;
+ }
newSwarm(newTabPage: true);
bindSuperTab(activeSwarmId, tabId, name);
}
+ /// Opens a tab from [closedSuperTabs]. False when it is no longer listed
+ /// (open already, or the account has left it).
+ bool openClosedSuperTab(String tabId, {String? intoSwarmId}) {
+ final closed = closedSuperTabs.where((tab) => tab.id == tabId).firstOrNull;
+ if (closed == null) return false;
+ openSuperTab(closed.id, closed.name, intoSwarmId: intoSwarmId);
+ return true;
+ }
+
void _rememberSuperTabVisibility(String tabId, bool visible) {
if (localStore == null) return;
unawaited(superTabs.setTabVisible(tabId, visible).catchError((Object error) {
@@ -3064,10 +3125,14 @@ class AppNotifier extends ChangeNotifier {
replacement = Swarm(id: 'swarm-${_nextSwarmId++}');
swarms.add(replacement);
}
+ final historyId = 'closed-${_nextClosedHistoryId++}';
+ if (removed.superTabId != null && !_disposed) {
+ _superTabClosures.add(historyId);
+ }
_rememberClosed(
ClosedSwarm(
removed,
- historyId: 'closed-${_nextClosedHistoryId++}',
+ historyId: historyId,
index: index,
replacement: replacement,
engine: removed.panes.length == 1
@@ -3391,6 +3456,15 @@ class AppNotifier extends ChangeNotifier {
);
if (index < 0) return;
final saved = _closedHistory[index] as ClosedSwarm;
+ // The tab may have been opened another way since: focus that view.
+ if (saved.superTabId case final tabId?) {
+ final open = swarms.where((s) => s.superTabId == tabId).firstOrNull;
+ if (open != null) {
+ _closedHistory.removeAt(index);
+ selectSwarm(open.id);
+ return;
+ }
+ }
if (!_canReopenSwarm(saved)) return;
_closedHistory.removeAt(index);
if (saved.superTabId case final tabId?) {
@@ -20170,6 +20244,7 @@ class AppNotifier extends ChangeNotifier {
unawaited(_zooPushes.close());
unawaited(_daemonFrames.close());
unawaited(_modelsRequests.close());
+ unawaited(_superTabClosures.close());
super.dispose();
}
}
diff --git a/desktop/lib/super_tabs/client.dart b/desktop/lib/super_tabs/client.dart
index 852e3ad1b..300912eaf 100644
--- a/desktop/lib/super_tabs/client.dart
+++ b/desktop/lib/super_tabs/client.dart
@@ -41,6 +41,10 @@ class SuperTabClient {
final _loaded = <String, SuperTabController>{};
SuperTabController? peek(String id) => _loaded[id];
+ /// The tabs closed on this computer. Only discovery says which of them the
+ /// account still belongs to.
+ Set<String> get hiddenIds => Set.unmodifiable(_hidden);
+
void bind(String? account) {
if (_account == account) return;
_account = account;
diff --git a/desktop/lib/widgets/workspace_welcome.dart b/desktop/lib/widgets/workspace_welcome.dart
index 0d999290d..faef9afe6 100644
--- a/desktop/lib/widgets/workspace_welcome.dart
+++ b/desktop/lib/widgets/workspace_welcome.dart
@@ -40,6 +40,8 @@ class WorkspaceWelcome extends StatefulWidget {
this.projects = const [],
this.onOpen,
this.composerBuilder,
+ this.closedSuperTabs = const [],
+ this.onOpenSuperTab,
this.now = DateTime.now,
});
@@ -49,6 +51,11 @@ class WorkspaceWelcome extends StatefulWidget {
final ValueChanged<SwarmDestination>? onOpen;
final DateTime Function() now;
+ /// Super Tabs closed on this computer, so a tab closed by accident is found
+ /// on the page a person lands on, not only in the command palette.
+ final List<({String id, String name})> closedSuperTabs;
+ final ValueChanged<String>? onOpenSuperTab;
+
/// Embedded creation and the popup supply the exact same form. Welcome
/// only contributes its existing recent-session data beneath that form.
final Widget Function(Widget? recentSessions)? composerBuilder;
@@ -194,7 +201,7 @@ class _WorkspaceWelcomeState extends State<WorkspaceWelcome> {
return Material(
key: const ValueKey('workspace-welcome'),
color: grid.AppPalette.windowBg,
- child: composer(_desktopSessions()),
+ child: composer(_withClosedSuperTabs(_desktopSessions())),
);
}
final palette = grid.AppTheme.surfacePalette;
@@ -259,6 +266,11 @@ class _WorkspaceWelcomeState extends State<WorkspaceWelcome> {
accent: accent,
cell: cell,
);
+ final closed = _closedSuperTabsSection(
+ DesktopChrome.control(color: ink),
+ DesktopChrome.metadata().copyWith(color: ink.withValues(alpha: .55)),
+ ink,
+ );
final footerInset = line + 52;
return Focus(
focusNode: _focus,
@@ -350,6 +362,10 @@ class _WorkspaceWelcomeState extends State<WorkspaceWelcome> {
accent,
),
],
+ if (closed != null) ...[
+ SizedBox(height: line),
+ SizedBox(width: listWidth, child: closed),
+ ],
],
),
),
@@ -374,6 +390,62 @@ class _WorkspaceWelcomeState extends State<WorkspaceWelcome> {
);
}
+ Widget? _withClosedSuperTabs(Widget? recent) {
+ final closed = _closedSuperTabsSection(
+ DesktopChrome.control(color: DesktopChrome.muted),
+ DesktopChrome.metadata(),
+ DesktopChrome.muted,
+ );
+ if (closed == null) return recent;
+ if (recent == null) return closed;
+ return Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [recent, const SizedBox(height: 16), closed],
+ );
+ }
+
+ /// Null when nothing is closed or the host cannot open one. The caller
+ /// passes the styles of its own layout.
+ Widget? _closedSuperTabsSection(
+ TextStyle rowStyle,
+ TextStyle headerStyle,
+ Color color,
+ ) {
+ final open = widget.onOpenSuperTab;
+ if (widget.closedSuperTabs.isEmpty || open == null) return null;
+ return Column(
+ key: const ValueKey('welcome-closed-super-tabs'),
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Padding(
+ padding: const EdgeInsets.only(bottom: 8, left: 10),
+ child: Semantics(
+ header: true,
+ child: Text('Closed Super Tabs', style: headerStyle),
+ ),
+ ),
+ for (final tab in widget.closedSuperTabs)
+ TextButton(
+ key: ValueKey('welcome-closed-super-tab-${tab.id}'),
+ onPressed: () => open(tab.id),
+ style: TextButton.styleFrom(
+ foregroundColor: color,
+ alignment: Alignment.centerLeft,
+ padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
+ ),
+ child: Text(
+ tab.name,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ style: rowStyle,
+ ),
+ ),
+ ],
+ );
+ }
+
Widget? _desktopSessions() {
final sessions = _sessions;
if (sessions == null || sessions.rows.isEmpty) return null;
diff --git a/desktop/test/super_tab_discovery_test.dart b/desktop/test/super_tab_discovery_test.dart
index b2b13a68a..87d4848c6 100644
--- a/desktop/test/super_tab_discovery_test.dart
+++ b/desktop/test/super_tab_discovery_test.dart
@@ -1,16 +1,19 @@
import 'dart:async';
import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/core/models.dart';
import 'package:harness/state/app_state.dart';
import 'package:harness/state/pane_layout_store.dart';
+import 'package:harness/screens/swarm_screen.dart';
import 'package:harness/state/swarm.dart';
import 'package:harness/super_tabs/client.dart';
import 'package:harness/ws/ws_conn.dart';
import 'swarm_state_test.dart' show MemoryStore, createApp;
import 'swarm_screen_test.dart' show mount;
+import 'support/open_harness.dart';
import 'support/super_tab_fixture.dart';
class _Catalog {
@@ -92,6 +95,15 @@ AppNotifier _computer(_Catalog catalog, MemoryStore store) {
return app;
}
+/// Closes as a person does (the path behind x, Cmd+W and middle-click) and
+/// lets the saved hidden set settle without calling the client directly.
+Future<void> _close(AppNotifier app, Swarm tab) async {
+ await app.requestCloseSwarm(tab.id);
+ for (var i = 0; i < 20; i++) {
+ await Future<void>.value();
+ }
+}
+
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
@@ -184,9 +196,8 @@ void main() {
final other = _computer(catalog, MemoryStore());
await first.superTabs.refreshTabs();
final tab = first.swarms.singleWhere((s) => s.isSuperTab);
- await first.closeSwarm(tab.id);
- // Flush the same visibility intent before simulating a process restart.
- await first.superTabs.setTabVisible(tab.superTabId!, false);
+ await _close(first, tab);
+ expect(first.superTabs.hiddenIds, {tab.superTabId});
await first.superTabs.refreshTabs();
await other.superTabs.refreshTabs();
expect(first.swarms.where((s) => s.isSuperTab), isEmpty);
@@ -201,6 +212,335 @@ void main() {
expect(restarted.activeSwarm.superTabId, tab.superTabId);
});
+ test('a hidden Super Tab is listed after a restart and opening it shows it', () async {
+ final catalog = _Catalog();
+ final store = MemoryStore();
+ final first = _computer(catalog, store);
+ await first.superTabs.refreshTabs();
+ expect(first.closedSuperTabs, isEmpty);
+ final tab = first.swarms.singleWhere((s) => s.isSuperTab);
+ await _close(first, tab);
+ final restarted = _computer(catalog, store);
+ // Before discovery the app cannot tell which hidden tabs are still ours.
+ expect(restarted.closedSuperTabs, isEmpty);
+ await restarted.superTabs.refreshTabs();
+ expect(restarted.closedSuperTabs.map((t) => (t.id, t.name)), [
+ (tab.superTabId, 'Pilot'),
+ ]);
+ restarted.openSuperTab(tab.superTabId!, 'Pilot');
+ expect(restarted.closedSuperTabs, isEmpty);
+ expect(restarted.activeSwarm.superTabId, tab.superTabId);
+ });
+
+ test('a hidden Super Tab the person left is not offered', () async {
+ final catalog = _Catalog();
+ final app = _computer(catalog, MemoryStore());
+ await app.superTabs.setTabVisible('0123456789abcdef01234567', false);
+ catalog.tabs.clear();
+ await app.superTabs.refreshTabs();
+ expect(app.closedSuperTabs, isEmpty);
+ });
+
+ testWidgets('closing a Super Tab offers Reopen and Reopen shows it again', (
+ tester,
+ ) async {
+ final catalog = _Catalog();
+ final app = _computer(catalog, MemoryStore());
+ await mount(tester, app);
+ await app.superTabs.refreshTabs();
+ await tester.pump();
+ final tab = app.swarms.singleWhere((s) => s.isSuperTab);
+ await app.requestCloseSwarm(tab.id);
+ await tester.pump();
+ expect(find.text('Super Tab closed. Its agents keep running.'), findsOneWidget);
+ expect(app.swarms.where((s) => s.isSuperTab), isEmpty);
+ await tester.pump(const Duration(milliseconds: 400));
+ await tester.tap(find.text('Reopen'));
+ await tester.pump();
+ expect(app.swarms.where((s) => s.superTabId == tab.superTabId), hasLength(1));
+ expect(app.closedSuperTabs, isEmpty);
+ expect(app.superTabs.hiddenIds, isEmpty);
+ await tester.pumpAndSettle();
+ await tester.pumpWidget(const SizedBox.shrink());
+ app.superTabs.dispose();
+ await tester.pump();
+ });
+
+ testWidgets('the command palette brings a closed Super Tab back by name', (
+ tester,
+ ) async {
+ final catalog = _Catalog();
+ final store = MemoryStore();
+ final first = _computer(catalog, store);
+ await first.superTabs.refreshTabs();
+ final tab = first.swarms.singleWhere((s) => s.isSuperTab);
+ await _close(first, tab);
+ final app = _computer(catalog, store);
+ await mount(tester, app);
+ await app.superTabs.refreshTabs();
+ await tester.pump();
+ await openHarnessPicker(tester);
+ await tester.enterText(
+ find.byKey(const ValueKey('swarm-search-input')),
+ '>Pilot',
+ );
+ await tester.pumpAndSettle();
+ expect(find.text('Closed Super Tabs'), findsWidgets);
+ await tester.sendKeyEvent(LogicalKeyboardKey.enter);
+ await tester.pumpAndSettle();
+ expect(app.activeSwarm.superTabId, tab.superTabId);
+ expect(app.closedSuperTabs, isEmpty);
+ await tester.pumpWidget(const SizedBox.shrink());
+ app.superTabs.dispose();
+ await tester.pump();
+ });
+
+ testWidgets('the New Tab page shows a closed Super Tab after a restart', (
+ tester,
+ ) async {
+ final catalog = _Catalog();
+ final store = MemoryStore();
+ final first = _computer(catalog, store);
+ await first.superTabs.refreshTabs();
+ final tab = first.swarms.singleWhere((s) => s.isSuperTab);
+ await _close(first, tab);
+ final app = _computer(catalog, store)..newSwarm(newTabPage: true);
+ await mount(tester, app);
+ expect(find.text('Closed Super Tabs'), findsNothing);
+ await app.superTabs.refreshTabs();
+ await tester.pump();
+ expect(find.text('Closed Super Tabs'), findsOneWidget);
+ await tester.tap(
+ find.byKey(ValueKey('welcome-closed-super-tab-${tab.superTabId}')),
+ );
+ await tester.pumpAndSettle();
+ expect(app.activeSwarm.superTabId, tab.superTabId);
+ expect(app.closedSuperTabs, isEmpty);
+ await tester.pumpWidget(const SizedBox.shrink());
+ app.superTabs.dispose();
+ await tester.pump();
+ });
+
+ testWidgets('the notice goes after about eight seconds', (tester) async {
+ final app = _computer(_Catalog(), MemoryStore());
+ await mount(tester, app);
+ await app.superTabs.refreshTabs();
+ await tester.pump();
+ await app.closeSwarm(app.swarms.singleWhere((s) => s.isSuperTab).id);
+ await tester.pump();
+ // The countdown starts once the notice has slid in.
+ await tester.pump(const Duration(milliseconds: 400));
+ await tester.pump(const Duration(seconds: 7));
+ expect(find.text('Reopen'), findsOneWidget);
+ await tester.pump(const Duration(seconds: 2));
+ await tester.pumpAndSettle();
+ expect(find.text('Reopen'), findsNothing);
+ await tester.pumpWidget(const SizedBox.shrink());
+ });
+
+ testWidgets('closing an ordinary tab shows no Super Tab notice', (
+ tester,
+ ) async {
+ final app = _computer(_Catalog(), MemoryStore());
+ await mount(tester, app);
+ await app.superTabs.refreshTabs();
+ app.newSwarm();
+ app.newSwarm();
+ final ordinary = app.swarms.where((s) => !s.isSuperTab).last;
+ app.renameSwarm(ordinary.id, 'Scratch');
+ final before = app.swarms.length;
+ await app.requestCloseSwarm(ordinary.id);
+ await tester.pump();
+ expect(app.swarms.length, before - 1);
+ expect(app.closedHistory, hasLength(1));
+ expect(find.byType(SnackBar), findsNothing);
+ await tester.pumpWidget(const SizedBox.shrink());
+ app.superTabs.dispose();
+ await tester.pump();
+ });
+
+ test('opening a closed Super Tab twice, from any entry, keeps one view', () async {
+ final catalog = _Catalog();
+ final app = _computer(catalog, MemoryStore());
+ await app.superTabs.refreshTabs();
+ final tab = app.swarms.singleWhere((s) => s.isSuperTab);
+ final id = tab.superTabId!;
+ await _close(app, tab);
+ final historyId = app.closedHistory.single.historyId;
+ // List first, then Reopen on the notice.
+ expect(app.openClosedSuperTab(id), isTrue);
+ app.reopenClosedSwarm(historyId: historyId);
+ expect(app.swarms.where((s) => s.superTabId == id), hasLength(1));
+ expect(app.closedHistory, isEmpty);
+ // Reopen first, then the list or Settings.
+ await _close(app, app.swarms.singleWhere((s) => s.superTabId == id));
+ app.reopenClosedSwarm();
+ app.openSuperTab(id, 'Pilot');
+ expect(app.openClosedSuperTab(id), isFalse);
+ expect(app.swarms.where((s) => s.superTabId == id), hasLength(1));
+ expect(app.activeSwarm.superTabId, id);
+ });
+
+ test('a closed Super Tab opens in the empty New Tab it was chosen from', () async {
+ final catalog = _Catalog();
+ final app = _computer(catalog, MemoryStore());
+ await app.superTabs.refreshTabs();
+ final tab = app.swarms.singleWhere((s) => s.isSuperTab);
+ await _close(app, tab);
+ app.newSwarm(newTabPage: true);
+ final page = app.activeSwarm;
+ final count = app.swarms.length;
+ expect(app.openClosedSuperTab(tab.superTabId!, intoSwarmId: page.id), isTrue);
+ expect(app.swarms, hasLength(count));
+ expect(page.superTabId, tab.superTabId);
+ expect(page.isNewTabPage, isFalse);
+ expect(app.activeSwarm, same(page));
+ });
+
+ test('a tab the person has left is not offered after an account switch', () async {
+ final catalog = _Catalog();
+ final store = MemoryStore();
+ final app = _computer(catalog, store);
+ await app.superTabs.refreshTabs();
+ final tab = app.swarms.singleWhere((s) => s.isSuperTab);
+ await _close(app, tab);
+ expect(app.closedSuperTabs, hasLength(1));
+ store.values['super_tab.hidden.other'] = '["${tab.superTabId}"]';
+ catalog.fail = '/api/super-tabs';
+ app.currentUser = const CurrentUserProfile(
+ id: 'other',
+ email: '[email protected]',
+ );
+ await app.superTabs.refreshTabs();
+ expect(app.superTabs.hiddenIds, {tab.superTabId});
+ expect(app.closedSuperTabs, isEmpty);
+ });
+
+ Future<(AppNotifier, List<Swarm>)> threeTabs(WidgetTester tester) async {
+ final catalog = _Catalog()
+ ..tabs.addAll([
+ {'id': '1123456789abcdef01234567', 'name': 'Build'},
+ {'id': '2123456789abcdef01234567', 'name': 'Ship'},
+ ]);
+ final app = _computer(catalog, MemoryStore());
+ await mount(tester, app);
+ await app.superTabs.refreshTabs();
+ await tester.pump();
+ return (app, app.swarms.where((s) => s.isSuperTab).toList());
+ }
+
+ Future<void> finishTest(WidgetTester tester, AppNotifier app) async {
+ await tester.pumpAndSettle(const Duration(seconds: 10));
+ await tester.pumpWidget(const SizedBox.shrink());
+ app.superTabs.dispose();
+ await tester.pump();
+ }
+
+ final closedNotice = find.text('Super Tab closed. Its agents keep running.');
+
+ testWidgets('two closes in the same frame leave one notice for the last', (
+ tester,
+ ) async {
+ final (app, tabs) = await threeTabs(tester);
+ await app.requestCloseSwarm(tabs[0].id);
+ await app.requestCloseSwarm(tabs[1].id);
+ for (var i = 0; i < 6; i++) {
+ await tester.pump(const Duration(milliseconds: 300));
+ expect(closedNotice.evaluate(), hasLength(lessThanOrEqualTo(1)));
+ }
+ expect(tester.takeException(), isNull);
+ expect(closedNotice, findsOneWidget);
+ await tester.tap(find.text('Reopen'));
+ await tester.pump();
+ expect(
+ app.swarms.where((s) => s.isSuperTab).map((s) => s.name),
+ contains(tabs[1].name),
+ );
+ expect(
+ app.swarms.where((s) => s.isSuperTab).map((s) => s.name),
+ isNot(contains(tabs[0].name)),
+ );
+ await finishTest(tester, app);
+ });
+
+ testWidgets('closing three Super Tabs 20 ms apart leaves one notice for the last', (
+ tester,
+ ) async {
+ final (app, tabs) = await threeTabs(tester);
+ for (final tab in tabs) {
+ await app.requestCloseSwarm(tab.id);
+ await tester.pump(const Duration(milliseconds: 20));
+ }
+ for (var i = 0; i < 12; i++) {
+ await tester.pump(const Duration(milliseconds: 300));
+ expect(closedNotice.evaluate(), hasLength(lessThanOrEqualTo(1)));
+ }
+ expect(tester.takeException(), isNull);
+ expect(find.text('Reopen'), findsOneWidget);
+ await tester.tap(find.text('Reopen'));
+ await tester.pump();
+ expect(app.swarms.where((s) => s.isSuperTab).map((s) => s.name), [
+ tabs.last.name,
+ ]);
+ await finishTest(tester, app);
+ });
+
+ testWidgets('a close behind an unrelated notice leaves it and then shows ours', (
+ tester,
+ ) async {
+ final (app, tabs) = await threeTabs(tester);
+ ScaffoldMessenger.of(tester.element(find.byType(SwarmScreen))).showSnackBar(
+ const SnackBar(content: Text('Something else'), persist: false),
+ );
+ await tester.pump(const Duration(milliseconds: 400));
+ await app.requestCloseSwarm(tabs[0].id);
+ await app.requestCloseSwarm(tabs[1].id);
+ await tester.pump(const Duration(milliseconds: 400));
+ expect(find.text('Something else'), findsOneWidget);
+ expect(closedNotice, findsNothing);
+ // The unrelated one runs its 4 s; ours follows, once.
+ await tester.pump(const Duration(seconds: 4));
+ await tester.pump(const Duration(seconds: 1));
+ await tester.pump(const Duration(milliseconds: 400));
+ expect(find.text('Something else'), findsNothing);
+ expect(closedNotice, findsOneWidget);
+ expect(tester.takeException(), isNull);
+ await finishTest(tester, app);
+ });
+
+ testWidgets('a close after the notice timed out shows a fresh one', (
+ tester,
+ ) async {
+ final (app, tabs) = await threeTabs(tester);
+ await app.requestCloseSwarm(tabs[0].id);
+ await tester.pump();
+ // The countdown starts once the notice has slid in.
+ await tester.pump(const Duration(milliseconds: 400));
+ await tester.pump(const Duration(seconds: 8));
+ await tester.pump(const Duration(seconds: 1));
+ await tester.pumpAndSettle();
+ expect(closedNotice, findsNothing);
+ await app.requestCloseSwarm(tabs[1].id);
+ await tester.pump();
+ await tester.pump(const Duration(milliseconds: 400));
+ expect(closedNotice, findsOneWidget);
+ await tester.pump(const Duration(seconds: 7));
+ expect(closedNotice, findsOneWidget);
+ await finishTest(tester, app);
+ });
+
+ test('setting the same account again keeps the saved names', () async {
+ final app = _computer(_Catalog(), MemoryStore());
+ await app.superTabs.refreshTabs();
+ await _close(app, app.swarms.singleWhere((s) => s.isSuperTab));
+ expect(app.closedSuperTabs, hasLength(1));
+ app.currentUser = const CurrentUserProfile(
+ id: 'owner',
+ email: '[email protected]',
+ );
+ expect(app.closedSuperTabs, hasLength(1));
+ });
+
test(
'a discovery reply racing lead creation cannot duplicate the finished tab',
() async {
diff --git a/desktop/test/welcome_closed_super_tabs_test.dart b/desktop/test/welcome_closed_super_tabs_test.dart
new file mode 100644
index 000000000..e51de77f8
--- /dev/null
+++ b/desktop/test/welcome_closed_super_tabs_test.dart
@@ -0,0 +1,52 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/widgets/workspace_welcome.dart';
+
+const _closed = [(id: 'tab-1', name: 'Pilot'), (id: 'tab-2', name: 'Build')];
+
+Future<List<String>> _mount(
+ WidgetTester tester, {
+ List<({String id, String name})> closed = _closed,
+ bool composer = false,
+}) async {
+ tester.view.devicePixelRatio = 1;
+ tester.view.physicalSize = const Size(1280, 800);
+ addTearDown(tester.view.reset);
+ final opened = <String>[];
+ await tester.pumpWidget(
+ MaterialApp(
+ home: WorkspaceWelcome(
+ onCommand: (_) {},
+ closedSuperTabs: closed,
+ onOpenSuperTab: opened.add,
+ composerBuilder: composer
+ ? (recents) => recents ?? const SizedBox()
+ : null,
+ ),
+ ),
+ );
+ await tester.pumpAndSettle();
+ return opened;
+}
+
+void main() {
+ for (final composer in [false, true]) {
+ final layout = composer ? 'composer' : 'classic';
+ testWidgets('$layout welcome lists closed Super Tabs and opens one', (
+ tester,
+ ) async {
+ final opened = await _mount(tester, composer: composer);
+ expect(find.text('Closed Super Tabs'), findsOneWidget);
+ expect(find.text('Pilot'), findsOneWidget);
+ await tester.tap(find.byKey(const ValueKey('welcome-closed-super-tab-tab-2')));
+ expect(opened, ['tab-2']);
+ });
+
+ testWidgets('$layout welcome has no section when nothing is closed', (
+ tester,
+ ) async {
+ await _mount(tester, closed: const [], composer: composer);
+ expect(find.text('Closed Super Tabs'), findsNothing);
+ });
+ }
+}