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();
}
}