diff --git a/app/lib/features/changes/widgets/capture_section.dart b/app/lib/features/changes/widgets/capture_section.dart index 5b577d96..b973b425 100644 --- a/app/lib/features/changes/widgets/capture_section.dart +++ b/app/lib/features/changes/widgets/capture_section.dart @@ -32,33 +32,10 @@ class StageCaptureSection extends StatelessWidget { final status = stage.stepStatus.trim().toLowerCase(); final pending = capture == null && (status == 'running' || status == 'waiting'); - final meta = [ - 'Visit ${stage.visit}', - if (stage.stepStatus.isNotEmpty) stage.stepStatus, - ].join(' · '); return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ - Padding( - padding: const EdgeInsets.fromLTRB(16, 16, 16, 8), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text(meta, style: Theme.of(context).textTheme.bodySmall), - if (capture != null) ...[ - Text( - fileChangesSummary(capture), - style: Theme.of(context).textTheme.bodySmall, - ), - if (capture.scope.isNotEmpty) - Text( - 'Scope: ${capture.scope}', - style: Theme.of(context).textTheme.bodySmall, - ), - ], - ], - ), - ), + StageCaptureHeading(stage: stage), const Divider(height: 1), if (capture == null) _CaptureNote( @@ -99,6 +76,38 @@ class StageCaptureSection extends StatelessWidget { } } +/// A stage/visit capture's heading: the visit and step status, the capture's +/// summary and its scope. +class StageCaptureHeading extends StatelessWidget { + const StageCaptureHeading({super.key, required this.stage}); + + final StageCapture stage; + + @override + Widget build(BuildContext context) { + final capture = stage.capture; + final small = Theme.of(context).textTheme.bodySmall; + final meta = [ + 'Visit ${stage.visit}', + if (stage.stepStatus.isNotEmpty) stage.stepStatus, + ].join(' · '); + return Padding( + padding: const EdgeInsets.fromLTRB(16, 16, 16, 8), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(meta, style: small), + if (capture != null) ...[ + Text(fileChangesSummary(capture), style: small), + if (capture.scope.isNotEmpty) + Text('Scope: ${capture.scope}', style: small), + ], + ], + ), + ); + } +} + /// One changed file: its kind, path and line counts; tapping opens its diff. /// [dense] draws it on one compact line (the path, then the counts), for a /// card that lists many files in little space. diff --git a/app/lib/features/changes/widgets/changes_split_view.dart b/app/lib/features/changes/widgets/changes_split_view.dart new file mode 100644 index 00000000..a6cba454 --- /dev/null +++ b/app/lib/features/changes/widgets/changes_split_view.dart @@ -0,0 +1,248 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; + +import '../domain/file_change.dart'; +import '../domain/unified_patch.dart'; +import '../domain/change_kind.dart'; +import '../domain/change_wording.dart'; +import 'capture_section.dart' show changeKindIcon; +import 'change_style.dart'; +import 'file_diff_viewer.dart'; + +/// The window width from which the Changes surface shows its files beside +/// the selected file's diff instead of one row per file. +const double changesSplitMinWidth = 1000; + +/// The narrowest pane that still fits the file list beside a readable diff; +/// a desktop window's detail pane beside the run list is usually narrower +/// than the window threshold. +const double changesSplitMinPaneWidth = 720; + +/// Whether a Changes pane [paneWidth] wide splits into list and diff. +bool changesSplitFits(BuildContext context, double paneWidth) => + MediaQuery.sizeOf(context).width >= changesSplitMinWidth && + paneWidth >= changesSplitMinPaneWidth; + +/// A desktop review layout: the changed files as a compact list on the left +/// and the selected file's diff on the right, like a code review tool. +/// +/// While the view has focus, `j`/ArrowDown select the next file and +/// `k`/ArrowUp the previous one; Enter opens the selected file on its own +/// page. Keys typed into a text field are never taken. +class ChangesSplitView extends StatefulWidget { + const ChangesSplitView({super.key, required this.files, this.heading}); + + /// The files to review, in list order. Must not be empty. + final List files; + + /// Shown above the file list (a visit's heading). + final Widget? heading; + + @override + State createState() => _ChangesSplitViewState(); +} + +class _ChangesSplitViewState extends State { + final _focus = FocusNode(debugLabel: 'changes split'); + final _listScroll = ScrollController(); + List _rowKeys = const []; + int _index = 0; + + @override + void initState() { + super.initState(); + _rowKeys = _keysFor(widget.files); + } + + @override + void didUpdateWidget(ChangesSplitView oldWidget) { + super.didUpdateWidget(oldWidget); + if (identical(oldWidget.files, widget.files)) return; + // Keep the same file selected across a refreshed reply when it is still + // listed; otherwise stay at the same position. + final path = oldWidget.files.isEmpty + ? null + : oldWidget.files[_index.clamp(0, oldWidget.files.length - 1)].path; + final kept = widget.files.indexWhere((file) => file.path == path); + _index = kept >= 0 + ? kept + : _index.clamp(0, widget.files.isEmpty ? 0 : widget.files.length - 1); + _rowKeys = _keysFor(widget.files); + } + + static List _keysFor(List files) => [ + for (final file in files) GlobalKey(debugLabel: 'change row ${file.path}'), + ]; + + @override + void dispose() { + _focus.dispose(); + _listScroll.dispose(); + super.dispose(); + } + + void _select(int index, {bool focus = false}) { + if (focus) _focus.requestFocus(); + if (index < 0 || index >= widget.files.length) return; + final forward = index > _index; + if (index != _index) setState(() => _index = index); + // Keep the selected row in view once it is laid out at its new state. + WidgetsBinding.instance.addPostFrameCallback((_) { + final row = _rowKeys[index].currentContext; + if (row == null || !row.mounted) return; + Scrollable.ensureVisible( + row, + alignmentPolicy: forward + ? ScrollPositionAlignmentPolicy.keepVisibleAtEnd + : ScrollPositionAlignmentPolicy.keepVisibleAtStart, + ); + }); + } + + void _step(int delta) => + _select((_index + delta).clamp(0, widget.files.length - 1)); + + KeyEventResult _handle(FocusNode node, KeyEvent event) { + if (event is KeyUpEvent) return KeyEventResult.ignored; + final primary = FocusManager.instance.primaryFocus; + final focused = primary?.context; + if (primary != node && + focused != null && + (focused.widget is EditableText || + focused.findAncestorWidgetOfExactType() != null)) { + return KeyEventResult.ignored; + } + final keyboard = HardwareKeyboard.instance; + if (keyboard.isControlPressed || + keyboard.isMetaPressed || + keyboard.isAltPressed) { + return KeyEventResult.ignored; + } + final key = event.logicalKey; + if (key == LogicalKeyboardKey.arrowDown || key == LogicalKeyboardKey.keyJ) { + _step(1); + } else if (key == LogicalKeyboardKey.arrowUp || + key == LogicalKeyboardKey.keyK) { + _step(-1); + } else if (event is KeyDownEvent && + (key == LogicalKeyboardKey.enter || + key == LogicalKeyboardKey.numpadEnter)) { + _openPage(); + } else { + return KeyEventResult.ignored; + } + return KeyEventResult.handled; + } + + void _openPage() => + showFileDiffViewer(context, widget.files[_index], files: widget.files); + + @override + Widget build(BuildContext context) { + final files = widget.files; + final index = _index.clamp(0, files.length - 1); + final file = files[index]; + return LayoutBuilder( + builder: (context, constraints) { + final listWidth = (constraints.maxWidth * 0.28).clamp(260.0, 380.0); + return Focus( + focusNode: _focus, + onKeyEvent: _handle, + child: Row( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + SizedBox( + width: listWidth, + child: Semantics( + container: true, + identifier: 'changes-file-list', + label: 'Changed files', + explicitChildNodes: true, + child: SingleChildScrollView( + key: const ValueKey('changes-split-list'), + controller: _listScroll, + padding: const EdgeInsets.only(bottom: 16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + ?widget.heading, + for (var i = 0; i < files.length; i++) + _SplitFileRow( + key: _rowKeys[i], + file: files[i], + selected: i == index, + onTap: () => _select(i, focus: true), + ), + ], + ), + ), + ), + ), + const VerticalDivider(width: 1), + Expanded( + child: FileDiffViewer( + key: ValueKey('changes-split-diff-${file.path}'), + file: file, + ), + ), + ], + ), + ); + }, + ); + } +} + +/// One file in the split list: its name with its folder below, so a long +/// path never hides the file name, and its line counts. +class _SplitFileRow extends StatelessWidget { + const _SplitFileRow({ + super.key, + required this.file, + required this.selected, + required this.onTap, + }); + + final FileChange file; + final bool selected; + final VoidCallback onTap; + + @override + Widget build(BuildContext context) { + final theme = Theme.of(context); + final mono = nativeDiffText(DefaultTextStyle.of(context).style); + final slash = file.path.lastIndexOf('/'); + final name = slash < 0 ? file.path : file.path.substring(slash + 1); + final folder = slash < 0 ? '' : file.path.substring(0, slash); + return Semantics( + container: true, + button: true, + selected: selected, + label: + 'Show diff for ${file.path}. ' + '${changeKindTitle(file.kind)} · ${changeFileMeta(file)}', + excludeSemantics: true, + onTap: onTap, + child: ListTile( + dense: true, + visualDensity: VisualDensity.compact, + minLeadingWidth: 20, + selected: selected, + selectedTileColor: theme.colorScheme.secondaryContainer, + selectedColor: theme.colorScheme.onSecondaryContainer, + leading: Icon(changeKindIcon(file.kind), size: 20), + title: Text( + name, + style: mono, + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + subtitle: folder.isEmpty + ? null + : Text(folder, maxLines: 1, overflow: TextOverflow.ellipsis), + trailing: ChangeStatText(changeStat(file.added, file.removed)), + onTap: onTap, + ), + ); + } +} diff --git a/app/lib/features/changes/widgets/changes_viewer.dart b/app/lib/features/changes/widgets/changes_viewer.dart index 6247e356..d93b4ce3 100644 --- a/app/lib/features/changes/widgets/changes_viewer.dart +++ b/app/lib/features/changes/widgets/changes_viewer.dart @@ -6,6 +6,7 @@ import '../domain/change_wording.dart'; import '../domain/file_change.dart'; import '../domain/run_changes.dart'; import 'capture_section.dart'; +import 'changes_split_view.dart'; import 'change_style.dart'; import 'file_diff_viewer.dart'; import 'visit_pager.dart'; @@ -97,7 +98,14 @@ class _ChangesViewerState extends State { static const _query = ''; @override - Widget build(BuildContext context) { + Widget build(BuildContext context) => LayoutBuilder( + builder: (context, constraints) => + _build(context, split: changesSplitFits(context, constraints.maxWidth)), + ); + + /// [split] shows the files beside the selected file's diff (a wide pane) + /// rather than one row per file opening its own page. + Widget _build(BuildContext context, {required bool split}) { final run = _run; final captures = _captures; final showSummary = widget.showSummary; @@ -106,6 +114,7 @@ class _ChangesViewerState extends State { ); final note = showSummary ? runChangesNote(run) : ''; final visit = captures.isEmpty ? null : captures[_visitIndex]; + final visitFiles = visit?.capture?.files ?? const []; final hasVisitPager = !widget.finalOnly && captures.length > 1; final pager = hasVisitPager ? ChangesVisitPager( @@ -158,7 +167,17 @@ class _ChangesViewerState extends State { ), if (showSummary) const Divider(height: 1), // The run's final changes: every changed path once, as it ended. - if (widget.finalOnly) + if (widget.finalOnly && split && run.files.isNotEmpty) + Expanded( + child: Padding( + padding: withBarClearance(context, EdgeInsets.zero), + child: ChangesSplitView( + key: const ValueKey('changes-final-split'), + files: run.files, + ), + ), + ) + else if (widget.finalOnly) Expanded( child: run.files.isEmpty ? SingleChildScrollView( @@ -184,7 +203,31 @@ class _ChangesViewerState extends State { ], ), ), - if (!widget.finalOnly) + if (!widget.finalOnly && + split && + visit != null && + visitFiles.isNotEmpty) + Expanded( + child: Padding( + padding: hasVisitPager + ? withBarClearance(context, EdgeInsets.zero) + : withTopBarClearance(context, EdgeInsets.zero), + child: ChangesSplitView( + key: ValueKey( + 'changes-visit-split-${visit.stage}-${visit.sequence}', + ), + files: visitFiles, + heading: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + StageCaptureHeading(stage: visit), + const Divider(height: 1), + ], + ), + ), + ), + ) + else if (!widget.finalOnly) Expanded( child: visit == null ? SingleChildScrollView( diff --git a/app/test/features/changes/changes_split_view_test.dart b/app/test/features/changes/changes_split_view_test.dart new file mode 100644 index 00000000..18fe4cbb --- /dev/null +++ b/app/test/features/changes/changes_split_view_test.dart @@ -0,0 +1,148 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/changes/changes.dart'; + +import 'fixtures.dart'; +import 'widget_hosts.dart'; + +String _patch(String path, String line) => + '--- "a/$path"\n' + '+++ "b/$path"\n' + '@@ -1,1 +1,1 @@\n' + '-old $line\n' + '+new $line\n'; + +List _steps() => [ + stepFixture( + fileChanges: captureFixture( + files: [ + fileChangeFixture(path: 'lib/a.dart', patch: _patch('lib/a.dart', 'A')), + fileChangeFixture(path: 'lib/b.dart', patch: _patch('lib/b.dart', 'B')), + fileChangeFixture(path: 'lib/c.dart', patch: _patch('lib/c.dart', 'C')), + ], + ), + ), +]; + +Future _pumpAt( + WidgetTester tester, + Size size, { + bool finalOnly = true, +}) async { + tester.view.physicalSize = size; + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + await tester.pumpWidget( + materialHost(ChangesViewer(steps: _steps(), finalOnly: finalOnly)), + ); + await tester.pumpAndSettle(); +} + +bool _shows(String text) => + find.textContaining(text, findRichText: true).evaluate().isNotEmpty; + +void main() { + testWidgets('a wide pane lists the files beside the first file\'s diff', ( + tester, + ) async { + await _pumpAt(tester, const Size(1400, 900)); + expect(find.byKey(const ValueKey('changes-final-split')), findsOneWidget); + expect(find.byType(FileDiffViewer), findsOneWidget); + expect( + find.byKey(const ValueKey('changes-split-diff-lib/a.dart')), + findsOneWidget, + ); + expect(_shows('new A'), isTrue); + expect(_shows('new B'), isFalse); + }); + + testWidgets('tapping a file shows its diff without leaving the pane', ( + tester, + ) async { + await _pumpAt(tester, const Size(1400, 900)); + await tester.tap(find.bySemanticsLabel(RegExp('^Show diff for lib/c'))); + await tester.pumpAndSettle(); + expect(find.byType(FileDiffPage), findsNothing); + expect( + find.byKey(const ValueKey('changes-split-diff-lib/c.dart')), + findsOneWidget, + ); + expect(_shows('new C'), isTrue); + }); + + testWidgets('j/k and the arrows move between files once focused', ( + tester, + ) async { + await _pumpAt(tester, const Size(1400, 900)); + await tester.tap(find.bySemanticsLabel(RegExp('^Show diff for lib/a'))); + await tester.pumpAndSettle(); + Future press(LogicalKeyboardKey key) async { + await tester.sendKeyEvent(key); + await tester.pumpAndSettle(); + } + + await press(LogicalKeyboardKey.keyJ); + expect(_shows('new B'), isTrue); + await press(LogicalKeyboardKey.arrowDown); + expect(_shows('new C'), isTrue); + // The last file stays selected rather than wrapping. + await press(LogicalKeyboardKey.keyJ); + expect(_shows('new C'), isTrue); + await press(LogicalKeyboardKey.keyK); + expect(_shows('new B'), isTrue); + await press(LogicalKeyboardKey.arrowUp); + expect(_shows('new A'), isTrue); + // Enter opens the selected file on its own page. + await press(LogicalKeyboardKey.enter); + expect(find.byType(FileDiffPage), findsOneWidget); + }); + + testWidgets('a visit page splits too, under the visit heading', ( + tester, + ) async { + await _pumpAt(tester, const Size(1400, 900), finalOnly: false); + expect( + find.byKey(const ValueKey('changes-visit-split-implementer-1')), + findsOneWidget, + ); + expect(find.textContaining('Visit 1'), findsOneWidget); + expect(_shows('new A'), isTrue); + }); + + testWidgets('a phone keeps one row per file opening its own page', ( + tester, + ) async { + await _pumpAt(tester, const Size(560, 1000)); + expect(find.byKey(const ValueKey('changes-final-split')), findsNothing); + expect(find.byType(FileDiffViewer), findsNothing); + await tester.tap(find.bySemanticsLabel(RegExp('^Open diff for lib/b'))); + await tester.pumpAndSettle(); + expect(find.byType(FileDiffPage), findsOneWidget); + }); + + testWidgets( + 'a detail pane beside a list splits from 720 px in a wide window', + (tester) async { + tester.view.physicalSize = const Size(1440, 900); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + Widget pane(double width) => materialHost( + Align( + alignment: Alignment.topRight, + child: SizedBox( + width: width, + child: ChangesViewer(steps: _steps(), finalOnly: true), + ), + ), + ); + await tester.pumpWidget(pane(900)); + await tester.pumpAndSettle(); + expect(find.byKey(const ValueKey('changes-final-split')), findsOneWidget); + await tester.pumpWidget(pane(600)); + await tester.pumpAndSettle(); + expect(find.byKey(const ValueKey('changes-final-split')), findsNothing); + }, + ); +}