merge(app): land Templates entry from Settings after retained-run recovery

This commit is contained in:
2026-10-22 00:29:50 +01:00
14 changed files with 1576 additions and 5 deletions
+7 -1
View File
@@ -2,7 +2,7 @@
///
/// The set mirrors [AppDestination] but stays free of widget imports so the
/// parser and serialiser can be exercised without a Flutter host.
enum AppRouteKind { pipelines, runs, chat, search, settings }
enum AppRouteKind { pipelines, runs, chat, search, settings, templates }
/// One page route: a destination plus the run it opens, if any.
///
@@ -18,6 +18,10 @@ class AppRoute {
static const search = AppRoute(AppRouteKind.search);
static const settings = AppRoute(AppRouteKind.settings);
/// The template gallery, reached from Settings. It is a Settings sub-page,
/// not a fifth tab, so Back returns to Settings.
static const templates = AppRoute(AppRouteKind.templates);
static const run = _RunRouteFactory();
/// One chat session, or the chat list when [id] is not a session id.
@@ -43,6 +47,7 @@ class AppRoute {
: '/chat/${Uri.encodeComponent(chatSessionId!)}',
AppRouteKind.search => '/search',
AppRouteKind.settings => '/settings',
AppRouteKind.templates => '/templates',
};
/// Parses a browser pathname (or a full URL) into a route.
@@ -81,6 +86,7 @@ class AppRoute {
'chat' when segments.length == 2 => _chatRoute(segments[1]),
'search' when segments.length == 1 => search,
'settings' when segments.length == 1 => settings,
'templates' when segments.length == 1 => templates,
_ => pipelines,
};
}
+42 -1
View File
@@ -30,14 +30,23 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
required this.onPopToChat,
required this.cupertino,
this.searchBuilder,
this.templatesBuilder,
AppRoute initial = AppRoute.pipelines,
}) : _route = initial,
_searchReturn = initial.kind == AppRouteKind.search
? AppRoute.pipelines
: initial,
_templatesReturn = initial.kind == AppRouteKind.templates
? AppRoute.settings
: initial;
final Widget Function(BuildContext context, AppRoute route) builder;
/// The Templates page above the shell. Templates belongs to Settings, so the
/// shell keeps the Settings destination mounted underneath and its state is
/// restored when this page closes.
final Widget Function(BuildContext context)? templatesBuilder;
/// The Search page above the shell. Search is transient chrome, not a fifth
/// tab: the shell stays mounted underneath so closing restores its loaded
/// state and selection, and the standard navigation bar supplies Back.
@@ -54,16 +63,22 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
final _navigatorKey = GlobalKey<NavigatorState>();
static const _pageKey = ValueKey<String>('pipelines-shell');
static const _searchPageKey = ValueKey<String>('app-search');
static const _templatesPageKey = ValueKey<String>('app-templates');
AppRoute _route;
/// The destination that was open before Search, restored on close.
AppRoute _searchReturn;
/// The Settings route the Templates page closes back to.
AppRoute _templatesReturn;
AppRoute get route => _route;
bool get searchOpen => _route.kind == AppRouteKind.search;
bool get templatesOpen => _route.kind == AppRouteKind.templates;
@override
AppRoute get currentConfiguration => _route;
@@ -80,11 +95,19 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
@override
Widget build(BuildContext context) {
final searching = searchOpen && searchBuilder != null;
final shellRoute = searching ? _searchReturn : _route;
final templates = templatesOpen && templatesBuilder != null;
final shellRoute = searching
? _searchReturn
: templates
? _templatesReturn
: _route;
return Navigator(
key: _navigatorKey,
pages: [
_page(_pageKey, builder(context, shellRoute)),
// Templates is a Settings sub-page: the shell stays mounted underneath,
// so closing restores Settings exactly as it was left.
if (templates) _page(_templatesPageKey, templatesBuilder!(context)),
// Search is a transient full-screen mode, so a full-screen dialog gives
// the standard Close/Cancel action instead of a Back chevron.
if (searching)
@@ -92,10 +115,24 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
],
onDidRemovePage: (page) {
if (page.key == _searchPageKey) closeSearch();
if (page.key == _templatesPageKey) closeTemplates();
},
);
}
/// Open the Templates gallery above Settings. The browser history entry is
/// `/templates`; Back returns to the same Settings screen instance.
void openTemplates() {
if (templatesOpen) return;
_templatesReturn = AppRoute.settings;
goTo(AppRoute.templates);
}
void closeTemplates() {
if (!templatesOpen) return;
goTo(AppRoute.settings);
}
/// Open Search above the current destination; its route carries the browser
/// history entry, and close/back returns to what was open before.
void openSearch() {
@@ -127,6 +164,10 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
closeSearch();
return true;
}
if (templatesOpen) {
closeTemplates();
return true;
}
if (_route.runId != null) {
onPopToRuns();
return true;
+31 -2
View File
@@ -18,6 +18,7 @@ import 'pipelines_destination.dart';
import 'runs_destination.dart';
import 'search_destination.dart';
import 'server_endpoint.dart';
import 'templates_destination.dart';
/// Composes the live application around one [AppSession] and one [Router].
///
@@ -51,6 +52,11 @@ class _PipelinesAppState extends State<PipelinesApp> {
late final RouterConfig<AppRoute> _config;
late final bool _cupertino;
/// Advances when a template create is accepted. The Pipelines destination
/// uses it to re-read through its mounted controller; no row is inserted
/// locally, so the next list reply is the only source of the new pipeline.
int _pipelinesRefresh = 0;
@override
void initState() {
super.initState();
@@ -77,6 +83,7 @@ class _PipelinesAppState extends State<PipelinesApp> {
onPopToRuns: () => _delegate.goTo(AppRoute.runs),
onPopToChat: () => _delegate.goTo(AppRoute.chat),
searchBuilder: _buildSearch,
templatesBuilder: _buildTemplates,
builder: _buildShell,
);
_config = RouterConfig<AppRoute>(
@@ -122,6 +129,22 @@ class _PipelinesAppState extends State<PipelinesApp> {
if (runId != null) _delegate.goTo(AppRoute.run(runId));
}
/// The Templates page above Settings. Closing it restores the same Settings
/// screen instance, so an edited field or an open connection state survives.
Widget _buildTemplates(BuildContext context) => TemplatesDestination(
session: _session,
server: widget.server,
onBack: _delegate.closeTemplates,
onCreated: _templateCreated,
);
/// An accepted create only invalidates Pipelines; the list is re-read and the
/// new row appears from the server's reply, never from an optimistic insert.
void _templateCreated(JsonMap _) {
if (!mounted) return;
setState(() => _pipelinesRefresh++);
}
Widget _buildShell(BuildContext context, AppRoute route) => AdaptiveShell(
cupertino: _cupertino,
destination: _destinationOf(route.kind),
@@ -129,8 +152,11 @@ class _PipelinesAppState extends State<PipelinesApp> {
_delegate.goTo(_routeOf(destination)),
onSearch: _delegate.openSearch,
destinationBodies: {
AppDestination.pipelines: (_) =>
PipelinesDestination(session: _session, server: widget.server),
AppDestination.pipelines: (_) => PipelinesDestination(
session: _session,
server: widget.server,
refreshToken: _pipelinesRefresh,
),
AppDestination.runs: (_) => RunsDestination(
session: _session,
server: widget.server,
@@ -149,6 +175,7 @@ class _PipelinesAppState extends State<PipelinesApp> {
client: _session.client,
initialEndpoint: _session.endpoint,
embedded: true,
onOpenTemplates: _delegate.openTemplates,
),
},
);
@@ -160,6 +187,8 @@ class _PipelinesAppState extends State<PipelinesApp> {
// Search is a transient page above the shell; it never becomes a tab.
AppRouteKind.search => AppDestination.pipelines,
AppRouteKind.settings => AppDestination.settings,
// Templates is a Settings sub-page, not a fifth tab.
AppRouteKind.templates => AppDestination.settings,
};
AppRoute _routeOf(AppDestination destination) => switch (destination) {
@@ -17,11 +17,17 @@ class PipelinesDestination extends StatelessWidget {
super.key,
required this.session,
required this.server,
this.refreshToken = 0,
});
final AppSession session;
final ServerEndpoint server;
/// Requests a fresh list/detail read without remounting the screen or losing
/// the open pipeline. The shell advances it when a template create was
/// accepted; the rows still come only from the server's next reply.
final int refreshToken;
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: session,
@@ -56,6 +62,8 @@ class PipelinesDestination extends StatelessWidget {
Widget _pipelinesScreen() => PipelinesScreen(
gateway: session.gateway,
embedded: true,
syncToken: session.connectionEpoch,
// A reconnect or an accepted template create both advance this counter;
// either change re-reads through the mounted controller.
syncToken: session.connectionEpoch + refreshToken,
);
}
@@ -0,0 +1,122 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../core/api_gateway.dart';
import '../features/templates/screen.dart';
import '../ui/adaptive/host.dart';
import 'app_session.dart';
import 'connection_status.dart';
import 'server_endpoint.dart';
/// The Back control of the app-level Templates page.
const templatesPageBackKey = ValueKey<String>('templates-page-back');
/// The Templates destination: the landed gallery over the app's shared gateway.
///
/// The gallery is rendered in embedded mode, so the shell owns navigation; this
/// destination contributes the page's one native bar (Back + Templates) and the
/// same honest connection states as the other destinations. No socket is opened
/// here: [session] owns the only connection, and a template read, validate or
/// create request travels over it.
///
/// [onCreated] forwards the server-confirmed detail once, so the caller can
/// invalidate the Pipelines list. This screen never inserts a row itself.
class TemplatesDestination extends StatelessWidget {
const TemplatesDestination({
super.key,
required this.session,
required this.server,
required this.onBack,
this.onCreated,
});
final AppSession session;
final ServerEndpoint server;
/// Closes this page and restores the Settings destination underneath.
final VoidCallback onBack;
final ValueChanged<JsonMap>? onCreated;
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
return AnimatedBuilder(
animation: session,
builder: (context, _) => _TemplatesPage(
cupertino: cupertino,
onBack: onBack,
child: _body(cupertino),
),
);
}
Widget _body(bool cupertino) {
if (!session.everConnected) {
return ConnectionGate(
session: session,
server: server,
cupertino: cupertino,
);
}
return Column(
children: [
if (!session.connected)
ConnectionBanner(session: session, cupertino: cupertino),
Expanded(
child: TemplatesScreen(
gateway: session.gateway,
embedded: true,
onCreated: onCreated,
),
),
],
);
}
}
/// The page's standard native bar. Back is named explicitly so it cannot be
/// confused with the gallery's own Back-to-gallery control.
class _TemplatesPage extends StatelessWidget {
const _TemplatesPage({
required this.cupertino,
required this.onBack,
required this.child,
});
final bool cupertino;
final VoidCallback onBack;
final Widget child;
@override
Widget build(BuildContext context) {
if (cupertino) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
leading: CupertinoButton(
key: templatesPageBackKey,
padding: EdgeInsets.zero,
onPressed: onBack,
child: const Icon(
CupertinoIcons.back,
semanticLabel: 'Back to Settings',
),
),
middle: const Text('Templates'),
),
child: SafeArea(child: child),
);
}
return Scaffold(
appBar: AppBar(
leading: IconButton(
key: templatesPageBackKey,
tooltip: 'Back to Settings',
onPressed: onBack,
icon: const Icon(Icons.arrow_back),
),
title: const Text('Templates'),
),
body: SafeArea(child: child),
);
}
}
@@ -9,4 +9,5 @@ abstract final class SettingsKeys {
static const reconnect = ValueKey<String>('settings.reconnect');
static const workspace = ValueKey<String>('settings.workspace');
static const saveWorkspace = ValueKey<String>('settings.saveWorkspace');
static const templates = ValueKey<String>('settings.templates');
}
@@ -10,6 +10,7 @@ import 'settings_controls.dart';
import 'settings_layout.dart';
import 'settings_sections.dart';
import 'settings_store.dart';
import 'settings_templates_section.dart';
/// The Settings destination: the server connection and the server-side
/// workspace, in native Material (native platforms) or Cupertino (iOS) controls.
@@ -27,6 +28,7 @@ class SettingsScreen extends StatefulWidget {
this.store,
this.ping,
this.embedded = false,
this.onOpenTemplates,
});
final transport.PipelinesClient client;
@@ -44,6 +46,10 @@ class SettingsScreen extends StatefulWidget {
/// The application shell already owns the native navigation bar and safe area.
final bool embedded;
/// Opens the Templates gallery. Null keeps Settings standalone/incomplete
/// without a dead row that would go nowhere.
final VoidCallback? onOpenTemplates;
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
@@ -121,6 +127,12 @@ class _SettingsScreenState extends State<SettingsScreen> {
controller: _controller,
workspaceField: _workspaceField,
),
if (widget.onOpenTemplates != null) ...<Widget>[
const SizedBox(height: settingsSectionGap),
const _SectionTitle('Templates'),
const SizedBox(height: settingsTitleGap),
SettingsTemplatesSection(onOpen: widget.onOpenTemplates!),
],
],
),
),
@@ -0,0 +1,50 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'settings_keys.dart';
import 'settings_theme.dart';
/// The Settings entry to the Templates gallery.
///
/// The row wording matches the current web Settings page: `Browse templates`
/// with the server-catalogue secondary line, under its `Templates` heading. It
/// only opens the route; loading the catalogue is the gallery's own read.
const String settingsTemplatesLabel = 'Browse templates';
const String settingsTemplatesSecondary =
'Reusable pipeline definitions offered by the server.';
class SettingsTemplatesSection extends StatelessWidget {
const SettingsTemplatesSection({super.key, required this.onOpen});
final VoidCallback onOpen;
@override
Widget build(BuildContext context) {
if (usesCupertinoControls(context)) {
return CupertinoListSection.insetGrouped(
margin: EdgeInsets.zero,
children: <Widget>[
CupertinoListTile(
key: SettingsKeys.templates,
title: const Text(settingsTemplatesLabel),
subtitle: const Text(settingsTemplatesSecondary),
trailing: const CupertinoListTileChevron(),
onTap: onOpen,
),
],
);
}
return Card(
margin: EdgeInsets.zero,
clipBehavior: Clip.antiAlias,
child: ListTile(
key: SettingsKeys.templates,
leading: const Icon(Icons.widgets_outlined),
title: const Text(settingsTemplatesLabel),
subtitle: const Text(settingsTemplatesSecondary),
trailing: const Icon(Icons.chevron_right),
onTap: onOpen,
),
);
}
}