feat(app): on a phone an open pipeline, run or chat is its own page over the tabs

This commit is contained in:
2026-10-23 22:58:49 +01:00
parent 700ba6f820
commit 7cbfbbbb61
8 changed files with 418 additions and 76 deletions
+52 -32
View File
@@ -1,3 +1,5 @@
import '../../shell/destination.dart';
import '../../shell/detail_page.dart';
import 'dart:async';
import 'package:flutter/material.dart';
@@ -163,49 +165,67 @@ class _ChatScreenState extends State<ChatScreen> {
}
}
/// The open chat is its own page over the tabs on a phone.
final DetailPageHost _page = DetailPageHost();
@override
void dispose() {
_page.dispose();
_sessions.dispose();
super.dispose();
}
Widget _thread(String selected, VoidCallback onBack) {
final row = _rowFor(selected);
final start = _startNotice;
return ChatThreadView(
key: ValueKey<String>('chat-session-$selected'),
gateway: widget.gateway,
sessionId: selected,
title: '${row?['title'] ?? ''}'.trim(),
session: row,
syncToken: widget.syncToken,
scrollPositions: _scrollPositions,
initialNotice: start != null && start.id == selected
? start.notice
: null,
onBack: onBack,
onModelChanged: (applied) => _modelChanged(selected, applied),
onCreatedSession: _openCreated,
onSessionRenamed: (title) => _renamed(selected, title),
onSessionClosed: _closed,
);
}
@override
Widget build(BuildContext context) {
final selected = _selected;
final row = selected == null ? null : _rowFor(selected);
final start = _startNotice;
// On a phone an open chat is its own page over the tabs; a wide window
// keeps it in the tab, beside the destination rail.
final narrow = MediaQuery.sizeOf(context).width < kDetailPageMaxWidth;
_page.sync(
context,
selected: selected != null && narrow,
destination: AppDestination.chat,
onClosed: _back,
builder: (context) => selected == null
? const SizedBox.shrink()
: _thread(selected, () => Navigator.of(context).maybePop()),
);
final list = ChatSessionsScreen(
gateway: widget.gateway,
embedded: true,
syncToken: widget.syncToken,
controller: _sessions,
onOpen: _open,
onSessions: _adoptRows,
activeSessionId: narrow ? null : selected,
);
if (narrow) return list;
return Stack(
children: [
Offstage(
offstage: selected != null,
child: ChatSessionsScreen(
gateway: widget.gateway,
embedded: true,
syncToken: widget.syncToken,
controller: _sessions,
onOpen: _open,
onSessions: _adoptRows,
activeSessionId: selected,
),
),
if (selected != null)
ChatThreadView(
key: ValueKey<String>('chat-session-$selected'),
gateway: widget.gateway,
sessionId: selected,
title: '${row?['title'] ?? ''}'.trim(),
session: row,
syncToken: widget.syncToken,
scrollPositions: _scrollPositions,
initialNotice: start != null && start.id == selected
? start.notice
: null,
onBack: _back,
onModelChanged: (applied) => _modelChanged(selected, applied),
onCreatedSession: _openCreated,
onSessionRenamed: (title) => _renamed(selected, title),
onSessionClosed: _closed,
),
Offstage(offstage: selected != null, child: list),
if (selected != null) _thread(selected, _back),
],
);
}
+31 -15
View File
@@ -1,3 +1,5 @@
import '../../shell/destination.dart';
import '../../shell/detail_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
@@ -78,6 +80,9 @@ class PipelinesScreen extends StatefulWidget {
class _PipelinesScreenState extends State<PipelinesScreen> {
late PipelinesController _controller;
/// The open pipeline is its own page over the tabs on a phone.
final DetailPageHost _page = DetailPageHost();
/// Counts route stage requests so a re-read detail cannot replay an old one.
int _stageRequest = 0;
@@ -132,6 +137,7 @@ class _PipelinesScreenState extends State<PipelinesScreen> {
@override
void dispose() {
_page.dispose();
_controller.dispose();
super.dispose();
}
@@ -180,21 +186,31 @@ class _PipelinesScreenState extends State<PipelinesScreen> {
final body = LayoutBuilder(
builder: (context, constraints) {
final wide = constraints.maxWidth >= kPipelinesWideBreakpoint;
if (!wide) {
return _controller.hasSelection
? PipelineDetailPanel(
controller: _controller,
gateway: widget.gateway,
showBack: true,
onRunStarted: widget.onRunStarted,
onViewPipelineRuns: widget.onViewPipelineRuns,
stageKey: stageKey,
stagePipelineId: widget.initialPipelineId,
stageVersion: widget.initialVersion,
stageRequest: _stageRequest,
)
: PipelineListPanel(controller: _controller);
}
// On a phone an open pipeline is its own page over the tabs.
_page.sync(
context,
selected: _controller.hasSelection && !wide,
destination: AppDestination.pipelines,
onClosed: _controller.clearSelection,
builder: (context) => AnimatedBuilder(
animation: _controller,
builder: (context, _) => ShellBar(
title: _controller.detail?.displayName,
child: PipelineDetailPanel(
controller: _controller,
gateway: widget.gateway,
showBack: false,
onRunStarted: widget.onRunStarted,
onViewPipelineRuns: widget.onViewPipelineRuns,
stageKey: stageKey,
stagePipelineId: widget.initialPipelineId,
stageVersion: widget.initialVersion,
stageRequest: _stageRequest,
),
),
),
);
if (!wide) return PipelineListPanel(controller: _controller);
return Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
+42 -26
View File
@@ -3,6 +3,8 @@ import 'dart:async';
import 'package:flutter/widgets.dart';
import '../../core/api_gateway.dart';
import '../../shell/destination.dart';
import '../../shell/detail_page.dart';
import '../../ui/adaptive/host.dart';
import '../attention/notify/notification_controller.dart';
import '../attention/runtime/attention_controller.dart';
@@ -98,6 +100,9 @@ class _RunsScreenState extends State<RunsScreen> {
/// named run consumes it, whether or not that run is waiting.
bool _decideConsumed = false;
/// The open run is its own page over the tabs.
final DetailPageHost _page = DetailPageHost();
@override
void initState() {
super.initState();
@@ -224,41 +229,52 @@ class _RunsScreenState extends State<RunsScreen> {
controller.removeListener(_selectionChanged);
controller.removeListener(_pipelineFilterChanged);
controller.removeListener(_detailChanged);
_page.dispose();
controller.dispose();
super.dispose();
}
Widget _detail(VoidCallback onBack) => RunDetailView(
controller: controller,
gateway: widget.gateway,
onBack: onBack,
onRunOpened: _select,
onOpenPipeline: widget.onOpenPipeline,
initialStageKey: widget.initialStageKey,
initialVisit: widget.initialVisit,
);
@override
Widget build(BuildContext context) {
final selected = controller.selectedRunId != null;
// On a phone an open run is its own page over the tabs; a wide window
// keeps it in the tab, beside the destination rail.
final narrow = MediaQuery.sizeOf(context).width < kDetailPageMaxWidth;
_page.sync(
context,
selected: selected && narrow,
destination: AppDestination.runs,
onClosed: _back,
builder: (context) => _detail(() => Navigator.of(context).maybePop()),
);
final list = AnimatedBuilder(
animation: controller,
builder: (context, _) => RunsList(
active: !selected,
controller: controller,
onSelected: _select,
syncToken: widget.syncToken,
attentionStore: widget.attentionStore,
attentionNow: widget.attentionNow,
notifications: widget.notifications,
attention: widget.attention,
),
);
if (narrow) return list;
return Stack(
children: [
AnimatedBuilder(
animation: controller,
builder: (context, _) => Offstage(
offstage: selected,
child: RunsList(
active: !selected,
controller: controller,
onSelected: _select,
syncToken: widget.syncToken,
attentionStore: widget.attentionStore,
attentionNow: widget.attentionNow,
notifications: widget.notifications,
attention: widget.attention,
),
),
),
if (selected)
RunDetailView(
controller: controller,
gateway: widget.gateway,
onBack: _back,
onRunOpened: _select,
onOpenPipeline: widget.onOpenPipeline,
initialStageKey: widget.initialStageKey,
initialVisit: widget.initialVisit,
),
Offstage(offstage: selected, child: list),
if (selected) _detail(_back),
],
);
}
+157
View File
@@ -0,0 +1,157 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../ui/adaptive/host.dart';
import 'destination.dart';
import 'shell_bar.dart';
/// Narrower than this, an open item is its own page; wider windows keep it
/// beside the destination rail.
const double kDetailPageMaxWidth = 600;
/// Keeps one full-screen detail page (an open run, pipeline or chat) in step
/// with a screen's selection: selecting pushes the page over the tabs, like a
/// new activity, and closing it (its Back, the system Back) clears the
/// selection. A selection cleared elsewhere (a route change) removes the page.
class DetailPageHost {
Route<void>? _route;
/// The owner's latest build of the page's content, so the open page
/// follows the owner's state (a renamed title, a newer reply).
final ValueNotifier<WidgetBuilder?> _content = ValueNotifier(null);
/// Whether the page is pushed (or about to be).
bool get open => _route != null;
void sync(
BuildContext context, {
required bool selected,
required AppDestination destination,
required WidgetBuilder builder,
required VoidCallback onClosed,
}) {
if (selected && _route != null) {
// Another route cannot be rebuilt mid-build; the page follows next frame.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_route != null) _content.value = builder;
});
}
if (selected && _route == null) {
_content.value = builder;
final route = detailPageRoute(
context,
destination: destination,
builder: (context) => ValueListenableBuilder<WidgetBuilder?>(
valueListenable: _content,
builder: (context, build, _) =>
build == null ? const SizedBox.shrink() : build(context),
),
);
_route = route;
// Pushing during a build is not allowed; the page follows this frame.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!context.mounted || _route != route) return;
Navigator.of(context).push(route).then((_) {
if (_route != route) return;
_route = null;
onClosed();
});
});
} else if (!selected && _route != null) {
final route = _route!;
_route = null;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (route.isActive) route.navigator?.removeRoute(route);
});
}
}
/// Removes the page with its owner.
void dispose() {
// The notifier is left to the page: a page still animating out keeps
// listening to it until it is gone.
final route = _route;
_route = null;
if (route != null && route.isActive) route.navigator?.removeRoute(route);
}
}
/// A full-screen page whose bar shows what the page's content publishes
/// through [ShellBar] (title and actions), with the platform's Back.
Route<void> detailPageRoute(
BuildContext context, {
required AppDestination destination,
required WidgetBuilder builder,
}) {
final page = _DetailPage(destination: destination, builder: builder);
return usesCupertinoHost(context)
? CupertinoPageRoute<void>(builder: (_) => page)
: MaterialPageRoute<void>(builder: (_) => page);
}
class _DetailPage extends StatefulWidget {
const _DetailPage({required this.destination, required this.builder});
final AppDestination destination;
final WidgetBuilder builder;
@override
State<_DetailPage> createState() => _DetailPageState();
}
class _DetailPageState extends State<_DetailPage> {
final ShellBarRegistry _bar = ShellBarRegistry();
@override
void dispose() {
_bar.dispose();
super.dispose();
}
void _back() => Navigator.of(context).maybePop();
@override
Widget build(BuildContext context) {
final body = ShellBarScope(
registry: _bar,
destination: widget.destination,
child: Builder(builder: widget.builder),
);
return ListenableBuilder(
listenable: _bar,
builder: (context, _) {
final spec = _bar.specFor(widget.destination);
final title = spec?.title ?? '';
final actions = spec?.actions ?? const <Widget>[];
if (usesCupertinoHost(context)) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
leading: CupertinoNavigationBarBackButton(
key: const ValueKey('shell.back'),
onPressed: _back,
),
automaticallyImplyLeading: false,
middle: Text(title),
trailing: actions.isEmpty
? null
: Row(mainAxisSize: MainAxisSize.min, children: actions),
),
child: SafeArea(child: body),
);
}
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
leading: BackButton(
key: const ValueKey('shell.back'),
onPressed: _back,
),
title: Text(title),
actions: actions,
),
body: SafeArea(top: false, child: body),
);
},
);
}
}
@@ -243,7 +243,7 @@ void main() {
await tester.pageBack();
await tester.pumpAndSettle();
await tester.tap(find.byTooltip('Back to pipelines'));
await tester.tap(find.byKey(const ValueKey('shell.back')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('pipeline-list')), findsOneWidget);
@@ -282,7 +282,7 @@ void main() {
await tester.pageBack();
await tester.pumpAndSettle();
await tester.tap(find.byTooltip('Back to pipelines'));
await tester.tap(find.byKey(const ValueKey('shell.back')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('pipeline-row-1')));
await tester.pumpAndSettle();
@@ -33,9 +33,12 @@ void main() {
expect(find.byKey(const Key('pipeline-list')), findsNothing);
expect(find.byKey(const Key('pipeline-detail')), findsOneWidget);
await tester.tap(find.byTooltip('Back to pipelines'));
// The open pipeline is its own page: its bar names it and Back closes it.
expect(find.text('Alpha review'), findsWidgets);
await tester.tap(find.byKey(const ValueKey('shell.back')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('pipeline-list')), findsOneWidget);
expect(find.byKey(const Key('pipeline-detail')), findsNothing);
});
testWidgets(
@@ -298,4 +298,36 @@ void main() {
},
variant: TargetPlatformVariant.only(TargetPlatform.iOS),
);
testWidgets('on a phone an open run is its own page and Back returns', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
int? selected;
var backs = 0;
await tester.pumpWidget(
_materialScreen(
gateway,
onRunSelected: (id) => selected = id,
onBack: (_) => backs++,
),
);
await _mountList(tester, gateway);
await tester.tap(find.text('Beta run'));
await tester.pump();
expect(selected, 2);
gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'});
await tester.pumpAndSettle();
// The run covers the list as a page with its own Back.
expect(find.text('Alpha run'), findsNothing);
expect(find.byKey(const ValueKey('shell.back')), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('shell.back')));
await tester.pumpAndSettle();
expect(backs, 1);
expect(find.text('Alpha run'), findsOneWidget);
});
}
+98
View File
@@ -0,0 +1,98 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/shell/detail_page.dart';
import 'package:pipelines_app/shell/shell_bar.dart';
class _Owner extends StatefulWidget {
const _Owner({required this.selection});
final ValueNotifier<String?> selection;
@override
State<_Owner> createState() => _OwnerState();
}
class _OwnerState extends State<_Owner> {
final DetailPageHost page = DetailPageHost();
int closed = 0;
@override
void dispose() {
page.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => ValueListenableBuilder<String?>(
valueListenable: widget.selection,
builder: (context, selected, _) {
page.sync(
context,
selected: selected != null,
destination: AppDestination.runs,
onClosed: () {
closed++;
widget.selection.value = null;
},
builder: (context) =>
ShellBar(title: 'Run $selected', child: Text('detail $selected')),
);
return const Text('the list');
},
);
}
void main() {
testWidgets('a selection opens its page; Back closes it and clears it', (
tester,
) async {
final selection = ValueNotifier<String?>(null);
await tester.pumpWidget(MaterialApp(home: _Owner(selection: selection)));
expect(find.text('the list'), findsOneWidget);
selection.value = '7';
await tester.pumpAndSettle();
// A page of its own over the list, titled by its content.
expect(find.text('detail 7'), findsOneWidget);
expect(find.text('Run 7'), findsOneWidget);
expect(find.text('the list'), findsNothing);
// The open page follows the owner's later state.
selection.value = '8';
await tester.pumpAndSettle();
expect(find.text('detail 8'), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('shell.back')));
await tester.pumpAndSettle();
expect(find.text('the list'), findsOneWidget);
expect(selection.value, isNull);
final state = tester.state<_OwnerState>(find.byType(_Owner));
expect(state.closed, 1);
});
testWidgets('a selection cleared elsewhere removes the page', (tester) async {
final selection = ValueNotifier<String?>('3');
await tester.pumpWidget(MaterialApp(home: _Owner(selection: selection)));
await tester.pumpAndSettle();
expect(find.text('detail 3'), findsOneWidget);
selection.value = null;
await tester.pumpAndSettle();
expect(find.text('detail 3'), findsNothing);
expect(find.text('the list'), findsOneWidget);
// Removed by the owner, so nothing is reported back.
expect(tester.state<_OwnerState>(find.byType(_Owner)).closed, 0);
});
testWidgets('the system Back closes the page first', (tester) async {
final selection = ValueNotifier<String?>('5');
await tester.pumpWidget(MaterialApp(home: _Owner(selection: selection)));
await tester.pumpAndSettle();
final navigator = tester.state<NavigatorState>(find.byType(Navigator));
expect(await navigator.maybePop(), isTrue);
await tester.pumpAndSettle();
expect(find.text('the list'), findsOneWidget);
expect(selection.value, isNull);
});
}