Open Search artifact hits in the viewer over the run's Artifacts tab

On a phone-width window the open run is its own page pushed on the app
navigator, so RunDetailTabs was not a descendant of the shell's
RunSearchSelectionScope (which wraps only the Runs tab body). The field or
artifact selection was never seen and the run opened at its plain Summary.
The previous widget tests injected the scope directly above RunDetailView at
900 px, where the detail is an inline pane, so they could not catch it.

- RunsScreen relays the shell's search scope into the detail it builds, so
  the pushed page sees the selection too (also fixes field hits on phones).
- An artifact hit now opens the run's Artifacts tab and the gallery opens
  that artifact exactly as a tap there does: selected, scrolled into view
  (ArtifactVisitReveal) and in the viewer. Back returns to the tab with the
  artifact highlighted. An unknown URL leaves the tab open with the existing
  "That artifact is no longer available." notice.
- New test/application/search_artifact_open_test.dart drives the real Search
  tab -> typed query -> result tap -> run read -> viewer on a 390x844 phone,
  Material and Cupertino; it fails without the scope relay.
This commit is contained in:
2026-11-02 18:34:53 +00:00
parent 37b67c84df
commit 8d2ca95057
9 changed files with 519 additions and 147 deletions
@@ -56,6 +56,8 @@ class ArtifactGalleryPage extends StatefulWidget {
this.fileBridge = const ArtifactFileBridge(),
this.embedded = false,
this.initiallyCollapsed = false,
this.openArtifactUrl,
this.onOpenArtifactHandled,
});
final List<JsonMap> steps;
@@ -78,6 +80,12 @@ class ArtifactGalleryPage extends StatefulWidget {
/// preserve their established open layout.
final bool initiallyCollapsed;
/// A recorded artifact URL to open as if tapped here (a search hit): it is
/// selected, scrolled into view and opened in the viewer. The page reports
/// [onOpenArtifactHandled] with whether its groups record that URL.
final String? openArtifactUrl;
final ValueChanged<bool>? onOpenArtifactHandled;
@override
State<ArtifactGalleryPage> createState() => _ArtifactGalleryPageState();
}
@@ -91,6 +99,56 @@ class _ArtifactGalleryPageState extends State<ArtifactGalleryPage> {
String? _status;
bool _showTurnDocuments = false;
/// The requested artifact the list is scrolling to before it opens.
({ArtifactGroup group, ArtifactRecord artifact})? _opening;
@override
void initState() {
super.initState();
_requestOpen(widget.openArtifactUrl);
}
@override
void didUpdateWidget(covariant ArtifactGalleryPage oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.openArtifactUrl != oldWidget.openArtifactUrl) {
_requestOpen(widget.openArtifactUrl);
}
}
/// Finds [url] among every recorded group, turn documents included, and
/// lets the list reveal it; [_revealed] then opens it.
void _requestOpen(String? url) {
if (url == null) return;
for (final group in _groups) {
for (final document in group.documents) {
final record = document.record;
if (record == null || record.url != url) continue;
// A turn document is listed only while the debug toggle shows them.
_showTurnDocuments = true;
_opening = (group: group, artifact: record);
}
for (final artifact in group.artifacts) {
if (artifact.url == url) _opening = (group: group, artifact: artifact);
}
final opening = _opening;
if (opening != null) {
_selectedIdentity = artifactVisitIdentity(group, opening.artifact);
return;
}
}
final handled = widget.onOpenArtifactHandled;
WidgetsBinding.instance.addPostFrameCallback((_) => handled?.call(false));
}
void _revealed() {
final opening = _opening;
if (!mounted || opening == null) return;
setState(() => _opening = null);
widget.onOpenArtifactHandled?.call(true);
_select(opening.group, opening.artifact);
}
List<ArtifactGroup> _groupsOf(int? focusIndex) {
final allGroups = _withRepairDocuments(galleryGroups(widget.steps));
if (focusIndex == null) return allGroups;
@@ -555,6 +613,8 @@ class _ArtifactGalleryPageState extends State<ArtifactGalleryPage> {
fetch: widget.fetch,
initiallyCollapsed: widget.initiallyCollapsed,
showTurnDocuments: _showTurnDocuments,
revealIdentity: _opening == null ? null : _selectedIdentity,
onRevealed: _revealed,
);
if (widget.embedded) return body;
if (usesCupertinoHost(context)) {
@@ -12,6 +12,7 @@ import 'artifact_record_row.dart';
import 'artifact_tile.dart';
import 'artifact_viewer_fetch.dart';
import 'artifact_row_labels.dart';
import 'artifact_visit_reveal.dart';
import '../../../ui/adaptive/floating_bar_layout.dart';
export 'artifact_row_labels.dart'
@@ -47,6 +48,8 @@ class ArtifactVisitList extends StatefulWidget {
this.fetch = fetchArtifact,
this.initiallyCollapsed = true,
this.showTurnDocuments = false,
this.revealIdentity,
this.onRevealed,
});
final Widget? header;
@@ -67,6 +70,12 @@ class ArtifactVisitList extends StatefulWidget {
/// Whether debugging documents are visible below their visit's artifacts.
final bool showTurnDocuments;
/// An artifact to open its section for and scroll into view, by
/// [artifactVisitIdentity]; [onRevealed] follows once it is (or once the
/// list has no further to scroll).
final String? revealIdentity;
final VoidCallback? onRevealed;
/// The layout last chosen, kept for the app's lifetime.
static ArtifactGalleryMode lastMode = ArtifactGalleryMode.grid;
@@ -81,6 +90,8 @@ class _ArtifactVisitListState extends State<ArtifactVisitList> {
/// so a run with many visits does not open every file list at once.
late final Set<String> _closed;
final ArtifactVisitReveal _reveal = ArtifactVisitReveal();
@override
void initState() {
super.initState();
@@ -90,11 +101,42 @@ class _ArtifactVisitListState extends State<ArtifactVisitList> {
if (group.artifacts.isNotEmpty)
'${group.stageKey}\u0000${group.visit}\u0000artifacts',
};
_startReveal();
}
@override
void dispose() {
_reveal.cancel();
super.dispose();
}
/// Opens the requested artifact's section and scrolls the artifact into view.
void _startReveal() {
final identity = widget.revealIdentity;
if (identity == null) return;
for (final group in widget.groups) {
final records = [
...group.artifacts,
for (final document in group.documents) ?document.record,
];
if (records.any((r) => artifactVisitIdentity(group, r) == identity)) {
_closed.remove('${group.stageKey}\u0000${group.visit}\u0000artifacts');
}
}
_reveal.reveal(() => widget.onRevealed?.call());
}
/// [child] carries the reveal target when it shows the requested artifact.
Widget _revealable(bool requested, Widget child) =>
requested ? KeyedSubtree(key: _reveal.target, child: child) : child;
@override
void didUpdateWidget(covariant ArtifactVisitList oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.revealIdentity != oldWidget.revealIdentity) {
_reveal.cancel();
_startReveal();
}
// Embedded galleries begin with their visits collapsed. Debug must reveal
// the documents immediately rather than require a second tap on the visit
// heading.
@@ -129,6 +171,7 @@ class _ArtifactVisitListState extends State<ArtifactVisitList> {
// On the page colour (that of the pinned section headings), not a
// grouped band.
child: ColoredBox(
key: _reveal.anchor,
color: SectionHeadingStyle.of(context).background,
child: _modeSwitch(context),
),
@@ -155,13 +198,21 @@ class _ArtifactVisitListState extends State<ArtifactVisitList> {
: _list(group),
if (widget.showTurnDocuments && group.documents.isNotEmpty)
SliverToBoxAdapter(
child: _inkSurface(
context,
ArtifactDocumentSection(
group: group,
selectedIdentity: widget.selectedIdentity,
onSelected: widget.onSelected,
showHeader: false,
child: _revealable(
group.documents.any(
(document) =>
document.record != null &&
artifactVisitIdentity(group, document.record!) ==
widget.revealIdentity,
),
_inkSurface(
context,
ArtifactDocumentSection(
group: group,
selectedIdentity: widget.selectedIdentity,
onSelected: widget.onSelected,
showHeader: false,
),
),
),
),
@@ -262,22 +313,25 @@ class _ArtifactVisitListState extends State<ArtifactVisitList> {
itemBuilder: (context, index) {
final artifact = group.artifacts[index];
final identity = artifactVisitIdentity(group, artifact);
return _inkSurface(
context,
ArtifactRecordRow(
key: ValueKey<String>('$identity\u0000$index'),
group: group,
artifact: artifact,
selected: widget.selectedIdentity == identity,
onSelected: widget.onSelected,
leading: ClipRRect(
borderRadius: BorderRadius.circular(6),
child: SizedBox.square(
dimension: 44,
child: ArtifactThumbnail(
artifact: artifact,
configuredEndpoint: widget.configuredEndpoint,
fetch: widget.fetch,
return _revealable(
widget.revealIdentity == identity,
_inkSurface(
context,
ArtifactRecordRow(
key: ValueKey<String>('$identity\u0000$index'),
group: group,
artifact: artifact,
selected: widget.selectedIdentity == identity,
onSelected: widget.onSelected,
leading: ClipRRect(
borderRadius: BorderRadius.circular(6),
child: SizedBox.square(
dimension: 44,
child: ArtifactThumbnail(
artifact: artifact,
configuredEndpoint: widget.configuredEndpoint,
fetch: widget.fetch,
),
),
),
),
@@ -312,19 +366,23 @@ class _ArtifactVisitListState extends State<ArtifactVisitList> {
if (index > first) const SizedBox(width: gap),
SizedBox(
width: tile,
child: ArtifactTile(
key: ValueKey<String>(
'${artifactVisitIdentity(group, artifacts[index])}'
'\u0000$index',
),
group: group,
artifact: artifacts[index],
selected:
widget.selectedIdentity ==
child: _revealable(
widget.revealIdentity ==
artifactVisitIdentity(group, artifacts[index]),
configuredEndpoint: widget.configuredEndpoint,
fetch: widget.fetch,
onSelected: widget.onSelected,
ArtifactTile(
key: ValueKey<String>(
'${artifactVisitIdentity(group, artifacts[index])}'
'\u0000$index',
),
group: group,
artifact: artifacts[index],
selected:
widget.selectedIdentity ==
artifactVisitIdentity(group, artifacts[index]),
configuredEndpoint: widget.configuredEndpoint,
fetch: widget.fetch,
onSelected: widget.onSelected,
),
),
),
],
@@ -0,0 +1,58 @@
import 'package:flutter/widgets.dart';
/// Scrolls one artifact of a lazy gallery into view.
///
/// The gallery builds only the rows on screen, so a requested artifact far
/// down a run may not exist yet. [reveal] scrolls by most of a viewport each
/// frame until the row keyed by [target] is built, then centres it. It stops
/// at the end of the list either way and always reports [onDone] exactly once.
class ArtifactVisitReveal {
/// Wraps the requested row so its position can be found once it is built.
final GlobalKey target = GlobalKey(debugLabel: 'artifact-reveal-target');
/// Any widget that is always built inside the gallery's scroll view.
final GlobalKey anchor = GlobalKey(debugLabel: 'artifact-reveal-anchor');
int _generation = 0;
/// Abandons a reveal in progress (a new request, or the list going away).
void cancel() => _generation++;
void reveal(VoidCallback onDone) {
final generation = ++_generation;
// Generous for any recorded run, yet bounded so a row that never builds
// cannot keep scheduling frames.
var remaining = 200;
void step() {
if (generation != _generation) return;
final row = target.currentContext;
if (row != null && row.mounted) {
Scrollable.ensureVisible(row, alignment: 0.5);
onDone();
return;
}
final anchorContext = anchor.currentContext;
final position = anchorContext == null
? null
: Scrollable.maybeOf(anchorContext)?.position;
if (position == null ||
!position.hasContentDimensions ||
position.pixels >= position.maxScrollExtent ||
--remaining <= 0) {
onDone();
return;
}
position.jumpTo(
(position.pixels + position.viewportDimension * 0.8).clamp(
position.minScrollExtent,
position.maxScrollExtent,
),
);
WidgetsBinding.instance.addPostFrameCallback((_) => step());
WidgetsBinding.instance.scheduleFrame();
}
WidgetsBinding.instance.addPostFrameCallback((_) => step());
WidgetsBinding.instance.scheduleFrame();
}
}
+29 -9
View File
@@ -18,6 +18,7 @@ import 'data/runs_sort_store.dart';
import 'domain/run_decisions.dart';
import 'ui/run_pipeline_link.dart';
import 'ui/run_detail_view.dart';
import 'ui/run_search_selection.dart';
import 'ui/runs_controller.dart';
import 'ui/runs_list.dart';
@@ -248,18 +249,37 @@ class _RunsScreenState extends State<RunsScreen> {
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,
);
/// The shell's pending search selection, read in [build] so this screen
/// rebuilds (and refreshes an open detail page) whenever it changes.
RunSearchSelectionScope? _searchScope;
/// The run detail, under the shell's search selection.
///
/// On a narrow window the detail is its own page on the app's navigator, so
/// it is not a descendant of the shell's [RunSearchSelectionScope]; relaying
/// the scope keeps a field or artifact hit's selection reachable there too.
Widget _detail(VoidCallback onBack) {
final view = RunDetailView(
controller: controller,
gateway: widget.gateway,
onBack: onBack,
onRunOpened: _select,
onOpenPipeline: widget.onOpenPipeline,
initialStageKey: widget.initialStageKey,
initialVisit: widget.initialVisit,
);
final scope = _searchScope;
if (scope == null) return view;
return RunSearchSelectionScope(
selection: scope.selection,
onConsumed: scope.onConsumed,
child: view,
);
}
@override
Widget build(BuildContext context) {
_searchScope = RunSearchSelectionScope.maybeOf(context);
final selected = controller.selectedRunId != null;
// The same adaptive list/detail layout as Pipelines and Chat.
final narrow = !showsListBesideDetail(context);
+26 -61
View File
@@ -13,10 +13,6 @@ import '../../../core/api_gateway.dart';
import '../../graph/widgets/run_graph_view.dart';
import '../../artifacts/ui/artifact_gallery_action.dart';
import '../../artifacts/ui/artifact_gallery_page.dart';
import '../../artifacts/domain/artifact_groups.dart';
import '../../artifacts/domain/artifact_record.dart';
import '../../artifacts/domain/gallery_navigation.dart';
import '../../artifacts/ui/artifact_viewer_page.dart';
import '../../changes/changes.dart';
import 'run_changes_action.dart';
import 'run_detail_content.dart';
@@ -116,9 +112,8 @@ class _RunDetailTabsState extends State<RunDetailTabs> {
/// request never reopens a pane the operator closed in the meantime.
RunSearchSelection? _appliedSearchSelection;
/// Search artifacts open above their visit, so the normal visit-page sync
/// must wait for the search callback to install both routes.
bool _searchArtifactVisitPending = false;
/// The artifact a search hit asked the Artifacts tab to open, until it has.
String? _searchArtifactUrl;
@override
void initState() {
@@ -249,62 +244,30 @@ class _RunDetailTabsState extends State<RunDetailTabs> {
_selectedStage = stageKey;
_selectedVisit = timelineIndex;
_tab = RunDetailTab.timeline;
case RunArtifactTarget(:final visit, :final url):
if (detailVisitStepAt(widget.run, visit.timelineIndex) == null) return;
// The viewer is opened above the visit. Prevent build() from opening
// that visit first in its own callback; the search callback installs
// the visit route and then the viewer route in that order.
_selectedStage = visit.stageKey;
_selectedVisit = visit.timelineIndex;
_tab = RunDetailTab.timeline;
_searchArtifactVisitPending = true;
WidgetsBinding.instance.addPostFrameCallback(
(_) => mounted ? _openSearchArtifact(url, visit.timelineIndex) : null,
);
case RunArtifactTarget(:final url):
_selectedStage = null;
_selectedVisit = null;
if (!_tabsFor(widget.run).contains(RunDetailTab.artifacts)) {
// A run with no recorded steps has no gallery to open it in.
_tab = RunDetailTab.summary;
WidgetsBinding.instance.addPostFrameCallback(
(_) => mounted ? _showSearchArtifactMissingNotice() : null,
);
return;
}
// The gallery opens it as if tapped there: highlighted, scrolled into
// view and in the viewer, so Back returns to the Artifacts tab.
_tab = RunDetailTab.artifacts;
_searchArtifactUrl = url;
}
}
void _openSearchArtifact(String url, int visitIndex) {
// _pushVisit reaches Navigator.push before its first await. Install the
// visit first so it remains the run-detail destination on Back, including
// when the recorded artifact has disappeared.
unawaited(_pushVisit(visitIndex));
// The visit page is now the shown one (_pushVisit records it before its
// first await), so later visit selections sync normally again.
_searchArtifactVisitPending = false;
final steps = detailRunSteps(widget.run);
final groups = galleryGroups(steps);
ArtifactGroup? selectedGroup;
ArtifactRecord? selectedArtifact;
for (final group in groups) {
for (final artifact in [
...group.artifacts,
...group.documents.map((document) => document.record),
]) {
if (artifact == null || artifact.url != url) continue;
selectedGroup = group;
selectedArtifact = artifact;
break;
}
if (selectedArtifact != null) break;
}
if (selectedGroup == null || selectedArtifact == null) {
_showSearchArtifactMissingNotice();
return;
}
final group = selectedGroup;
final artifact = selectedArtifact;
final page = ArtifactViewerPage(
groups: groups,
selectedIdentity: artifactVisitIdentityOf(group, artifact),
includeExternalDocuments: true,
configuredEndpoint: widget.configuredEndpoint,
);
Navigator.of(context).push<void>(
widget.cupertino
? CupertinoPageRoute<void>(builder: (_) => page)
: MaterialPageRoute<void>(builder: (_) => page),
);
/// The Artifacts tab handled the search request: it opened the artifact, or
/// found no recorded file with that URL.
void _searchArtifactHandled(bool found) {
if (!mounted) return;
setState(() => _searchArtifactUrl = null);
if (!found) _showSearchArtifactMissingNotice();
}
void _showSearchArtifactMissingNotice() {
@@ -439,7 +402,7 @@ class _RunDetailTabsState extends State<RunDetailTabs> {
@override
Widget build(BuildContext context) {
// A selected visit opens on its own page; the tab keeps the list.
if (_selectedVisit != _shownVisit && !_searchArtifactVisitPending) {
if (_selectedVisit != _shownVisit) {
_syncVisitPage();
}
final tabs = _tabsFor(widget.run);
@@ -558,6 +521,8 @@ class _RunDetailTabsState extends State<RunDetailTabs> {
executing: runDetailExecuting(widget.run),
configuredEndpoint: widget.configuredEndpoint,
embedded: true,
openArtifactUrl: _searchArtifactUrl,
onOpenArtifactHandled: _searchArtifactHandled,
);
case RunDetailTab.changes:
final viewer = ChangesViewer(
@@ -66,12 +66,12 @@ final class RunVisitTarget extends RunSearchTarget {
final int timelineIndex;
}
/// A recorded artifact hit selects its visit first, then opens the artifact
/// viewer on the exact recorded file.
/// A recorded artifact hit opens the run's Artifacts tab and that tab opens
/// the exact recorded file, exactly as tapping it in the gallery does.
final class RunArtifactTarget extends RunSearchTarget {
const RunArtifactTarget({required this.visit, required this.url});
const RunArtifactTarget({required this.url});
final RunVisitTarget visit;
/// The recorded artifact URL, matched against the gallery's own records.
final String url;
}
@@ -82,7 +82,8 @@ final class RunArtifactTarget extends RunSearchTarget {
/// unknown field leaves the plain Summary instead of guessing a section. A
/// visit hit selects the step with the recorded sequence; a sequence the run did
/// not record falls back to the plain Summary, as the browser does, so a stale
/// hit never leaves an unrelated visit pane open.
/// hit never leaves an unrelated visit pane open. An artifact hit names the
/// Artifacts tab and its recorded URL.
RunSearchTarget resolveRunSearchSelection(
RunSearchSelection selection,
JsonMap run,
@@ -97,13 +98,11 @@ RunSearchTarget resolveRunSearchSelection(
},
);
}
// The URL is the artifact's identity; the gallery matches it among every
// recorded visit, so a sequence the detail numbers differently still opens
// the file, and a file the run no longer lists reports itself as gone.
final artifactUrl = selection.artifactUrl?.trim() ?? '';
if (artifactUrl.isNotEmpty) {
final visit = _visitTarget(selection, run);
return visit == null
? const RunSummaryTarget()
: RunArtifactTarget(visit: visit, url: artifactUrl);
}
if (artifactUrl.isNotEmpty) return RunArtifactTarget(url: artifactUrl);
final visit = _visitTarget(selection, run);
if (visit != null) return visit;
return const RunSummaryTarget();