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