feat(app): browser-like tabs with per-tab history and persistence

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Opus 5.5 committed 2026-10-26 03:37:39 +01:00
1 parent 39e7e43071
commit 1e32cf0df3
8 files changed
+577 -33

No files matched your search

+14 -4
View File
@@ -57,14 +57,20 @@ class _QuickSwitcherState extends State<QuickSwitcher> {
});
}
void _open(int i) {
/// Ctrl/Cmd held (or `newTab`, for middle-click) opens the result in a new tab.
void _open(int i, {bool newTab = false}) {
final app = AppScope.read(context);
final query = _query;
Navigator.of(context).pop();
final AppRoute route;
if (i < _hits.length) {
app.openId(_hits[i].pageId);
} else if (_query.isNotEmpty) {
app.openTitle(_query);
route = PageTarget(id: _hits[i].pageId);
} else if (query.isNotEmpty) {
route = PageTarget(title: query);
} else {
return;
}
newTab ? app.openInNewTab(route) : app.navigate(route);
}
KeyEventResult _key(FocusNode _, KeyEvent e) {
@@ -137,6 +143,7 @@ class _QuickSwitcherState extends State<QuickSwitcher> {
_Item(
selected: i == _selected,
onTap: () => _open(i),
onMiddle: () => _open(i, newTab: true),
icon: i < _hits.length
? Icons.description_outlined
: Icons.add_rounded,
@@ -164,10 +171,12 @@ class _Item extends StatelessWidget {
required this.onTap,
required this.icon,
required this.title,
required this.onMiddle,
this.detail,
});
final bool selected;
final VoidCallback onTap;
final VoidCallback onMiddle;
final IconData icon;
final String title;
final String? detail;
@@ -179,6 +188,7 @@ class _Item extends StatelessWidget {
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: onTap,
onTertiaryTapUp: (_) => onMiddle(),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 9),
decoration: BoxDecoration(
+27 -3
View File
@@ -12,6 +12,7 @@ import 'hubs_tree.dart';
import 'quick_switcher.dart';
import 'graph_view.dart';
import 'settings_view.dart';
import 'tab_strip.dart';
/// App frame: sidebar (navigation, hubs, recents), top bar (history, search, theme,
/// settings) and the current view.
@@ -53,6 +54,10 @@ class _ShellState extends State<Shell> {
AppAction.settings: () => app.go(const SettingsRoute()),
AppAction.back: app.back,
AppAction.forward: app.forward,
AppAction.newTab: app.newTab,
AppAction.closeTab: app.closeTab,
AppAction.nextTab: app.nextTab,
AppAction.previousTab: app.previousTab,
};
return CallbackShortcuts(
bindings: {
@@ -94,13 +99,15 @@ class _ShellState extends State<Shell> {
child: Column(
children: [
_TopBar(
narrow: narrow,
onMenu: () => narrow
? _scaffold.currentState?.openDrawer()
: setState(() => _sidebar = !_sidebar),
),
if (!narrow) const TabStrip(),
Expanded(
child: KeyedSubtree(
key: ValueKey((route, app.generation)),
key: ValueKey((app.tab.uid, route, app.generation)),
child: switch (route) {
JournalsRoute() => const JournalsView(),
TimelineRoute() => const GraphView(
@@ -130,8 +137,9 @@ class _ShellState extends State<Shell> {
}
class _TopBar extends StatelessWidget {
const _TopBar({required this.onMenu});
const _TopBar({required this.onMenu, required this.narrow});
final VoidCallback onMenu;
final bool narrow;
@override
Widget build(BuildContext context) {
@@ -167,6 +175,7 @@ class _TopBar extends StatelessWidget {
app.canGoForward ? app.forward : null,
),
const Spacer(),
if (narrow) const TabCountButton(),
// Phones: an icon instead of the pill keeps the bar from overflowing.
if (MediaQuery.sizeOf(context).width < 480)
icon(
@@ -240,8 +249,14 @@ class _Sidebar extends StatelessWidget {
final app = AppScope.of(context);
final colors = context.colors;
final route = app.route;
// Ctrl/Cmd-click opens in a new tab, middle-click always does.
void go(AppRoute r) {
app.go(r);
app.navigate(r);
onNavigate();
}
void newTab(AppRoute r) {
app.openInNewTab(r);
onNavigate();
}
@@ -277,18 +292,21 @@ class _Sidebar extends StatelessWidget {
label: 'Journals',
selected: route is JournalsRoute,
onTap: () => go(const JournalsRoute()),
onMiddle: () => newTab(const JournalsRoute()),
),
_NavItem(
icon: Icons.hub_outlined,
label: 'Graph',
selected: route is GraphRoute,
onTap: () => go(const GraphRoute(GraphMode.network)),
onMiddle: () => newTab(const GraphRoute(GraphMode.network)),
),
_NavItem(
icon: Icons.description_outlined,
label: 'All pages',
selected: route is AllPagesRoute,
onTap: () => go(const AllPagesRoute()),
onMiddle: () => newTab(const AllPagesRoute()),
),
const SizedBox(height: 22),
Expanded(
@@ -312,6 +330,7 @@ class _Sidebar extends StatelessWidget {
selected: route is PageTarget && route.id == p.id,
dense: true,
onTap: () => go(PageTarget(id: p.id)),
onMiddle: () => newTab(PageTarget(id: p.id)),
),
],
),
@@ -329,12 +348,14 @@ class _NavItem extends StatefulWidget {
required this.label,
required this.selected,
required this.onTap,
this.onMiddle,
this.dense = false,
});
final IconData? icon;
final String label;
final bool selected;
final VoidCallback onTap;
final VoidCallback? onMiddle;
final bool dense;
@override
@@ -354,6 +375,9 @@ class _NavItemState extends State<_NavItem> {
onExit: (_) => setState(() => _hover = false),
child: GestureDetector(
onTap: widget.onTap,
onTertiaryTapUp: widget.onMiddle == null
? null
: (_) => widget.onMiddle!(),
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 1),
padding: EdgeInsets.symmetric(
+252
View File
@@ -0,0 +1,252 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import '../state/app_state.dart';
import '../state/keymap.dart';
import '../theme/theme.dart';
const _tabWidth = 168.0;
const _stripHeight = 34.0;
/// Browser-like tab strip: one compact item per tab, close button on hover or when active,
/// a "+" button, horizontal scrolling on overflow and drag to reorder.
class TabStrip extends StatelessWidget {
const TabStrip({super.key});
@override
Widget build(BuildContext context) {
final app = AppScope.of(context);
final colors = context.colors;
final tabs = app.tabs;
return Container(
key: const ValueKey('tab-strip'),
height: _stripHeight,
decoration: BoxDecoration(
color: colors.surface,
border: Border(
top: BorderSide(color: colors.line),
bottom: BorderSide(color: colors.line),
),
),
child: Row(
children: [
Expanded(
child: ReorderableListView.builder(
scrollDirection: Axis.horizontal,
buildDefaultDragHandles: false,
padding: const EdgeInsets.only(left: 6, top: 4),
itemCount: tabs.length,
onReorderItem: app.moveTab,
proxyDecorator: (child, _, _) =>
Material(color: Colors.transparent, child: child),
itemBuilder: (context, i) => ReorderableDragStartListener(
key: ValueKey('tab-${tabs[i].uid}'),
index: i,
child: _TabItem(
tab: tabs[i],
index: i,
active: i == app.activeTab,
),
),
),
),
IconButton(
key: const ValueKey('tab-new'),
tooltip: 'New tab (${app.keymap.of(AppAction.newTab).label})',
iconSize: 16,
visualDensity: VisualDensity.compact,
color: colors.muted,
icon: const Icon(Icons.add_rounded),
onPressed: app.newTab,
),
],
),
);
}
}
class _TabItem extends StatefulWidget {
const _TabItem({
required this.tab,
required this.index,
required this.active,
});
final AppTab tab;
final int index;
final bool active;
@override
State<_TabItem> createState() => _TabItemState();
}
class _TabItemState extends State<_TabItem> {
bool _hover = false;
@override
Widget build(BuildContext context) {
final app = AppScope.read(context);
final colors = context.colors;
final active = widget.active;
return MouseRegion(
onEnter: (_) => setState(() => _hover = true),
onExit: (_) => setState(() => _hover = false),
cursor: SystemMouseCursors.click,
child: Listener(
onPointerDown: (e) {
if (e.buttons == kMiddleMouseButton) app.closeTab(widget.index);
},
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => app.activateTab(widget.index),
child: Container(
width: _tabWidth,
margin: const EdgeInsets.only(right: 2),
padding: const EdgeInsets.only(left: 10, right: 4),
decoration: BoxDecoration(
color: active
? colors.paper
: (_hover ? colors.line.withAlpha(0x66) : null),
border: Border.all(
color: active ? colors.line : Colors.transparent,
),
borderRadius: const BorderRadius.vertical(
top: Radius.circular(8),
),
),
child: Row(
children: [
Icon(
widget.tab.icon,
size: 13,
color: active ? colors.accent : colors.faint,
),
const SizedBox(width: 7),
Expanded(
child: Text(
widget.tab.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12.5,
fontWeight: active ? FontWeight.w600 : FontWeight.w500,
color: active ? colors.ink : colors.muted,
),
),
),
SizedBox(
width: 22,
child: active || _hover
? InkResponse(
key: ValueKey('tab-close-${widget.index}'),
radius: 11,
onTap: () => app.closeTab(widget.index),
child: Icon(
Icons.close_rounded,
size: 14,
color: colors.muted,
),
)
: null,
),
],
),
),
),
),
);
}
}
/// Narrow layouts: a button with the tab count that opens the tab list.
class TabCountButton extends StatelessWidget {
const TabCountButton({super.key});
@override
Widget build(BuildContext context) {
final app = AppScope.of(context);
final colors = context.colors;
return IconButton(
key: const ValueKey('tab-count'),
tooltip: 'Tabs',
onPressed: () => showTabsSheet(context),
icon: Container(
width: 20,
height: 20,
alignment: Alignment.center,
decoration: BoxDecoration(
border: Border.all(color: colors.muted, width: 1.5),
borderRadius: BorderRadius.circular(5),
),
child: Text(
'${app.tabs.length}',
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w700,
color: colors.muted,
),
),
),
);
}
}
Future<void> showTabsSheet(BuildContext context) {
final app = AppScope.read(context);
return showModalBottomSheet<void>(
context: context,
showDragHandle: true,
isScrollControlled: true,
builder: (sheet) => ListenableBuilder(
listenable: app,
builder: (context, _) {
final colors = context.colors;
final tabs = app.tabs;
return SafeArea(
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.sizeOf(context).height * 0.7,
),
child: ListView(
shrinkWrap: true,
children: [
for (var i = 0; i < tabs.length; i++)
ListTile(
key: ValueKey('sheet-tab-$i'),
dense: true,
selected: i == app.activeTab,
selectedColor: colors.accent,
leading: Icon(tabs[i].icon, size: 18),
title: Text(
tabs[i].title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
trailing: IconButton(
key: ValueKey('sheet-tab-close-$i'),
tooltip: 'Close tab',
icon: const Icon(Icons.close_rounded, size: 18),
onPressed: () => app.closeTab(i),
),
onTap: () {
app.activateTab(i);
Navigator.pop(sheet);
},
),
ListTile(
key: const ValueKey('sheet-tab-new'),
dense: true,
leading: const Icon(Icons.add_rounded, size: 18),
title: const Text('New tab'),
onTap: () {
app.newTab();
Navigator.pop(sheet);
},
),
],
),
),
);
},
),
);
}