feat(app): finish native run Changes wiring and scaled form menus

This commit is contained in:
2026-10-21 20:48:45 +01:00
parent 7b327f012b
commit 77c71eb45e
18 changed files with 1956 additions and 159 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ export 'domain/change_wording.dart';
export 'domain/file_change.dart';
export 'domain/run_changes.dart';
export 'domain/unified_patch.dart';
export 'widgets/change_style.dart' show DiffForm, usesCupertino;
export 'widgets/change_style.dart' show DiffForm, hostTextTheme, usesCupertino;
export 'widgets/changes_viewer.dart';
export 'widgets/file_diff_viewer.dart';
export 'widgets/patch_view.dart';
@@ -44,6 +44,13 @@ class RunChanges {
RunChanges runChanges(List<JsonMap> steps) =>
runChangesFor(stageCaptures(steps));
/// Whether the run-wide final-state aggregate may be presented. Ported from
/// `web/src/changes.js` `showRunChangesSummary`: only a settled succeeded run
/// has a final workspace state. A failed or running run keeps its evidence in
/// each visit's own captured section instead of claiming a settled summary.
bool showRunChangesSummary(Object? status) =>
status != null && '$status'.trim().toLowerCase() == 'succeeded';
/// The same aggregate over already-derived [stageCaptures], so a caller that
/// shows both the summary and the sections parses each capture only once.
RunChanges runChangesFor(List<StageCapture> visits) {
@@ -2,104 +2,52 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../domain/unified_patch.dart';
import 'diff_form_control.dart';
/// Which form of a recorded patch the viewer is showing.
enum DiffForm { rendered, raw }
export 'diff_form_control.dart';
/// The app's established platform switch: Material for Android/desktop/web,
/// Cupertino for iOS and macOS. Feature widgets stay portable across all three
/// targets instead of hard-coding one control set.
bool usesCupertino(BuildContext context) {
final platform = Theme.of(context).platform;
return platform == TargetPlatform.iOS || platform == TargetPlatform.macOS;
}
/// Patch text inherits the host's typography, including its font and scaler.
/// A diff is not an exception to the native-defaults requirement.
/// Patch text inherits the host's typography without a diff-specific font.
TextStyle nativeDiffText(TextStyle base) => base;
/// Native semantic colours, never a bespoke diff palette. The recorded +/-
/// markers carry meaning independently of colour. No background tint is added.
/// Keep Material-derived metadata roles readable inside a Cupertino host.
ThemeData hostTextTheme(BuildContext context) => ThemeData(
brightness: CupertinoTheme.brightnessOf(context),
platform: Theme.of(context).platform,
);
/// Native semantic roles, never a bespoke palette or added row tint. The exact
/// recorded +/- markers also distinguish changes without relying on colour.
({Color text, Color? background}) diffLineColors(
BuildContext context,
ChangeLineKind kind,
) {
return switch (kind) {
ChangeLineKind.add => (
text: usesCupertino(context)
? CupertinoColors.systemGreen.resolveFrom(context)
: Theme.of(context).colorScheme.primary,
background: null,
),
ChangeLineKind.remove => (
text: usesCupertino(context)
? CupertinoColors.destructiveRed.resolveFrom(context)
: Theme.of(context).colorScheme.error,
background: null,
),
ChangeLineKind.context => (text: labelColor(context), background: null),
};
}
) => switch (kind) {
ChangeLineKind.add => (
text: usesCupertino(context)
? CupertinoColors.systemGreen.resolveFrom(context)
: Theme.of(context).colorScheme.primary,
background: null,
),
ChangeLineKind.remove => (
text: usesCupertino(context)
? CupertinoColors.destructiveRed.resolveFrom(context)
: Theme.of(context).colorScheme.error,
background: null,
),
ChangeLineKind.context => (text: labelColor(context), background: null),
};
/// The plain body/label colour for non-diff text inside the viewer.
Color labelColor(BuildContext context) => usesCupertino(context)
? CupertinoColors.label.resolveFrom(context)
: Theme.of(context).colorScheme.onSurface;
/// The muted colour for gutters, notes and secondary metadata.
Color mutedColor(BuildContext context) => usesCupertino(context)
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
/// The hunk header's fill.
Color hunkHeaderColor(BuildContext context) => usesCupertino(context)
? CupertinoColors.tertiarySystemFill.resolveFrom(context)
: Theme.of(context).colorScheme.surfaceContainerHighest;
/// The viewer's shared Rendered/Raw control: a native segmented control on each
/// platform, with the same two words the browser uses.
Widget diffFormControl(
BuildContext context, {
required DiffForm form,
required ValueChanged<DiffForm> onChanged,
}) {
if (usesCupertino(context)) {
return CupertinoSlidingSegmentedControl<DiffForm>(
groupValue: form,
onValueChanged: (value) {
if (value != null) onChanged(value);
},
children: const {
DiffForm.rendered: _SegmentLabel('Rendered'),
DiffForm.raw: _SegmentLabel('Raw patch'),
},
);
}
return SegmentedButton<DiffForm>(
segments: const [
ButtonSegment(value: DiffForm.rendered, label: Text('Rendered')),
ButtonSegment(value: DiffForm.raw, label: Text('Raw patch')),
],
selected: {form},
showSelectedIcon: false,
onSelectionChanged: (selection) {
if (selection.isNotEmpty) onChanged(selection.first);
},
);
}
class _SegmentLabel extends StatelessWidget {
const _SegmentLabel(this.label);
final String label;
@override
Widget build(BuildContext context) =>
Padding(padding: const EdgeInsets.all(6), child: Text(label));
}
/// Copy the server's raw patch exactly as recorded. Disabled when the entry has
/// no readable patch, so the action never copies a fabricated empty diff.
Widget copyPatchControl(
BuildContext context, {
required bool enabled,
@@ -128,7 +76,6 @@ Widget copyPatchControl(
);
}
/// The standard close action, labelled exactly on both platforms.
Widget closeDiffControl(BuildContext context, VoidCallback onClose) {
if (usesCupertino(context)) {
return Semantics(
@@ -17,7 +17,11 @@ import 'file_diff_viewer.dart';
/// Scaffold body, tab body or Expanded child). It never opens its own socket or
/// requests data; the caller passes the run detail's `steps`.
class ChangesViewer extends StatefulWidget {
const ChangesViewer({super.key, required this.steps});
const ChangesViewer({
super.key,
required this.steps,
this.showSummary = true,
});
/// The run detail's steps exactly as the server returned them. Each step's
/// `file_changes` may be null (no capture), a complete/partial/unavailable
@@ -27,6 +31,11 @@ class ChangesViewer extends StatefulWidget {
/// captures and the summary are derived from it rather than on every rebuild.
final List<JsonMap> steps;
/// Whether the run-wide final-state header and filter are shown. The web
/// client gates its aggregate on [showRunChangesSummary]; the per-stage
/// sections stay the evidence for a run without a settled final state.
final bool showSummary;
@override
State<ChangesViewer> createState() => _ChangesViewerState();
}
@@ -67,49 +76,51 @@ class _ChangesViewerState extends State<ChangesViewer> {
Widget build(BuildContext context) {
final run = _run;
final captures = _captures;
final showSummary = widget.showSummary;
final hasFiles = captures.any(
(visit) => visit.capture?.files.isNotEmpty ?? false,
);
final note = runChangesNote(run);
final note = showSummary ? runChangesNote(run) : '';
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
runChangesHeader(run),
style: Theme.of(context).textTheme.titleMedium,
),
if (note.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
note,
style: Theme.of(context).textTheme.bodySmall,
),
if (showSummary)
Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
runChangesHeader(run),
style: Theme.of(context).textTheme.titleMedium,
),
if (hasFiles)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
'The summary counts each path once, as the last recorded '
'visit left it. Each stage/visit below lists what its own '
'capture recorded.',
style: Theme.of(context).textTheme.bodySmall,
if (note.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
note,
style: Theme.of(context).textTheme.bodySmall,
),
),
),
],
if (hasFiles)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
'The summary counts each path once, as the last recorded '
'visit left it. Each stage/visit below lists what its own '
'capture recorded.',
style: Theme.of(context).textTheme.bodySmall,
),
),
],
),
),
),
if (hasFiles)
if (showSummary && hasFiles)
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: _filterField(context, run),
),
const Divider(height: 1),
if (showSummary) const Divider(height: 1),
Expanded(
child: captures.isEmpty
? SingleChildScrollView(
@@ -0,0 +1,138 @@
import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
enum DiffForm { rendered, raw }
bool usesCupertino(BuildContext context) => usesCupertinoHost(context);
const diffFormLabels = <DiffForm, String>{
DiffForm.rendered: 'Rendered',
DiffForm.raw: 'Raw patch',
};
/// Conservatively choose between two stock controls using the current native
/// label role and text scaler. This does not size or restyle either control.
bool diffFormSegmentsFit(BuildContext context, double maxWidth) {
final style = usesCupertino(context)
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.labelLarge ??
DefaultTextStyle.of(context).style;
var widest = 0.0;
for (final label in diffFormLabels.values) {
final painter = TextPainter(
text: TextSpan(text: label, style: style),
textDirection: Directionality.of(context),
textScaler: MediaQuery.textScalerOf(context),
maxLines: 1,
)..layout();
widest = math.max(widest, painter.width);
painter.dispose();
}
return (widest + 48) * 2 <= maxWidth;
}
Widget diffFormControl(
BuildContext context, {
required DiffForm form,
required ValueChanged<DiffForm> onChanged,
}) {
if (usesCupertino(context)) {
return CupertinoSlidingSegmentedControl<DiffForm>(
groupValue: form,
onValueChanged: (value) {
if (value != null) onChanged(value);
},
children: {
for (final entry in diffFormLabels.entries)
entry.key: Padding(
padding: const EdgeInsets.all(6),
child: Text(entry.value),
),
},
);
}
return SegmentedButton<DiffForm>(
segments: [
for (final entry in diffFormLabels.entries)
ButtonSegment(value: entry.key, label: Text(entry.value)),
],
selected: {form},
showSelectedIcon: false,
onSelectionChanged: (selection) {
if (selection.isNotEmpty) onChanged(selection.first);
},
);
}
/// Native action sheet/bottom sheet when fixed segment labels cannot fit.
Widget diffFormMenu(
BuildContext context, {
required DiffForm form,
required ValueChanged<DiffForm> onChanged,
}) {
if (usesCupertino(context)) {
return Semantics(
label: 'Diff form',
button: true,
child: CupertinoButton(
padding: EdgeInsets.zero,
onPressed: () => showCupertinoModalPopup<void>(
context: context,
builder: (sheetContext) => CupertinoActionSheet(
title: const Text('Diff form'),
actions: [
for (final entry in diffFormLabels.entries)
CupertinoActionSheetAction(
isDefaultAction: entry.key == form,
onPressed: () {
Navigator.of(sheetContext).pop();
onChanged(entry.key);
},
child: Text(entry.value),
),
],
),
),
child: const Icon(CupertinoIcons.ellipsis),
),
);
}
return IconButton(
tooltip: 'Diff form',
icon: const Icon(Icons.more_vert),
onPressed: () => showModalBottomSheet<void>(
context: context,
showDragHandle: true,
builder: (sheetContext) => SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: Align(
alignment: AlignmentDirectional.centerStart,
child: Text(
'Diff form',
style: Theme.of(sheetContext).textTheme.titleMedium,
),
),
),
for (final entry in diffFormLabels.entries)
ListTile(
title: Text(entry.value),
selected: entry.key == form,
onTap: () {
Navigator.of(sheetContext).pop();
onChanged(entry.key);
},
),
],
),
),
),
);
}
@@ -10,6 +10,7 @@ import '../domain/file_change.dart';
import '../domain/unified_patch.dart';
import 'change_style.dart';
import 'patch_view.dart';
import 'raw_patch_view.dart';
/// The reusable viewer for one recorded file change: its kind, path, stat,
/// tags, the rendered or raw unified patch, Copy patch (the server's exact
@@ -38,6 +39,8 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
super.dispose();
}
void _selectForm(DiffForm value) => setState(() => _form = value);
void _close() {
final close = widget.onClose;
if (close != null) {
@@ -147,16 +150,33 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
crossAxisAlignment: WrapCrossAlignment.center,
children: [
if (file.hasPatch)
diffFormControl(
context,
form: _form,
onChanged: (value) => setState(() => _form = value),
LayoutBuilder(
builder: (context, constraints) {
// Two native segmented-control segments cannot show both
// fixed words at 2x text on a narrow host; their menu
// shows the same labels whole, one per row.
final control =
diffFormSegmentsFit(context, constraints.maxWidth)
? diffFormControl(
context,
form: _form,
onChanged: _selectForm,
)
: diffFormMenu(
context,
form: _form,
onChanged: _selectForm,
);
return control;
},
),
_scrollsWhenTooWide(
copyPatchControl(
context,
enabled: file.hasPatch,
copied: _copied,
onPressed: _copy,
),
copyPatchControl(
context,
enabled: file.hasPatch,
copied: _copied,
onPressed: _copy,
),
],
),
@@ -167,13 +187,13 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
? CupertinoColors.separator.resolveFrom(context)
: Theme.of(context).dividerColor,
),
Flexible(child: _body(context, cupertino, mono)),
Flexible(child: _body(context, mono)),
],
),
);
}
Widget _body(BuildContext context, bool cupertino, TextStyle mono) {
Widget _body(BuildContext context, TextStyle mono) {
final file = widget.file;
if (file.binary) return _message(context, binaryDiffMessage);
if (!file.hasPatch) {
@@ -184,24 +204,7 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
container: true,
label: 'Raw patch for ${file.path}',
value: file.patch,
child: SingleChildScrollView(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
child: SingleChildScrollView(
// Raw patch bytes are one line each: scroll them sideways like the
// web view instead of wrapping a continuation under a new line.
scrollDirection: Axis.horizontal,
child: ExcludeSemantics(
child: SelectionArea(
child: Text(
file.patch!,
style: mono,
softWrap: false,
overflow: TextOverflow.visible,
),
),
),
),
),
child: RawPatchView(patch: file.patch!, style: mono),
);
}
return SingleChildScrollView(
@@ -218,6 +221,15 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
}
}
/// Gives a native control its intrinsic width and lets it scroll sideways when
/// the dialog is narrower than the control, instead of letting the control clip
/// or break one of its fixed labels.
///
/// The scroll view shrink-wraps when the control already fits, so the action
/// keeps its usual layout until scaled text or a narrow phone demands more room.
Widget _scrollsWhenTooWide(Widget control) =>
SingleChildScrollView(scrollDirection: Axis.horizontal, child: control);
/// Open [file] in the platform's standard modal: a Material dialog on
/// Android/desktop/web, a Cupertino dialog with a popup surface on iOS/macOS.
Future<void> showFileDiffViewer(BuildContext context, FileChange file) {
@@ -231,15 +243,20 @@ Future<void> showFileDiffViewer(BuildContext context, FileChange file) {
// A popup surface spans the window unless it is inset; on a phone the
// screen edge would clip its native rounded corners.
final width = MediaQuery.sizeOf(dialogContext).width;
return Padding(
padding: EdgeInsets.symmetric(
horizontal: width < 600 ? 16 : 40,
vertical: 24,
),
child: Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 900),
child: CupertinoPopupSurface(child: SafeArea(child: viewer)),
return Theme(
// Dialogs open on the app Navigator, above the Changes page's own
// brightness-aware theme, so the popup needs its own.
data: hostTextTheme(dialogContext),
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: width < 600 ? 16 : 40,
vertical: 24,
),
child: Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 900),
child: CupertinoPopupSurface(child: SafeArea(child: viewer)),
),
),
),
);
@@ -0,0 +1,75 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'change_style.dart';
/// The recorded raw patch bytes, one recorded line per line, in the host's own
/// selectable body.
///
/// Material uses [SelectionArea]; a Cupertino host has no
/// [MaterialLocalizations], so it renders the same text in the read-only
/// [CupertinoTextField] the run detail already uses for plain selectable text.
/// Both forms keep their intrinsic line width, so long lines scroll sideways
/// instead of wrapping a continuation under a new line.
class RawPatchView extends StatefulWidget {
const RawPatchView({super.key, required this.patch, required this.style});
final String patch;
final TextStyle style;
@override
State<RawPatchView> createState() => _RawPatchViewState();
}
class _RawPatchViewState extends State<RawPatchView> {
late final TextEditingController _text = TextEditingController(
text: widget.patch,
);
@override
void didUpdateWidget(RawPatchView oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.patch != _text.text) _text.text = widget.patch;
}
@override
void dispose() {
_text.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final cupertino = usesCupertino(context);
return SingleChildScrollView(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: cupertino
? IntrinsicWidth(
child: ExcludeSemantics(
child: CupertinoTextField(
key: const ValueKey('raw-patch-field'),
controller: _text,
readOnly: true,
maxLines: null,
padding: EdgeInsets.zero,
decoration: const BoxDecoration(),
style: widget.style,
),
),
)
: ExcludeSemantics(
child: SelectionArea(
child: Text(
widget.patch,
style: widget.style,
softWrap: false,
overflow: TextOverflow.visible,
),
),
),
),
);
}
}