Diff viewer: vim keys, scrolling, find in diff and an all-files view
Left/right and h/l step between files (or headers in the all-files view), up/down and j/k scroll, / opens find-in-diff (highlighted matches across every file, n/N or Enter/Shift+Enter to move, Escape to close), and a top-bar toggle switches between one file at a time and every file stacked with collapsible headers, like a pull request's Files changed tab. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// The diff page's keyboard: left/right and `h`/`l` step between files,
|
||||
/// up/down and `k`/`j` scroll, `/` opens search, `n`/`N` go to the next and
|
||||
/// previous match, Escape closes. While a text field has focus (the search
|
||||
/// box) every key is left to it, so typing an `h` never changes file.
|
||||
class DiffKeyboard extends StatelessWidget {
|
||||
const DiffKeyboard({
|
||||
super.key,
|
||||
required this.focusNode,
|
||||
required this.onPrevious,
|
||||
required this.onNext,
|
||||
required this.onScroll,
|
||||
required this.onSearch,
|
||||
required this.onMatch,
|
||||
required this.onEscape,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final FocusNode focusNode;
|
||||
final VoidCallback onPrevious;
|
||||
final VoidCallback onNext;
|
||||
|
||||
/// Called with +1 (down) or -1 (up).
|
||||
final ValueChanged<int> onScroll;
|
||||
final VoidCallback onSearch;
|
||||
|
||||
/// Called with +1 (next match) or -1 (previous match).
|
||||
final ValueChanged<int> onMatch;
|
||||
final VoidCallback onEscape;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Focus(
|
||||
focusNode: focusNode,
|
||||
autofocus: true,
|
||||
onKeyEvent: _handle,
|
||||
child: child,
|
||||
);
|
||||
|
||||
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<EditableText>() != null)) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
final keyboard = HardwareKeyboard.instance;
|
||||
if (keyboard.isControlPressed ||
|
||||
keyboard.isMetaPressed ||
|
||||
keyboard.isAltPressed) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
final key = event.logicalKey;
|
||||
final shift = keyboard.isShiftPressed;
|
||||
final repeatable = event is KeyDownEvent || event is KeyRepeatEvent;
|
||||
if (!repeatable) return KeyEventResult.ignored;
|
||||
if (key == LogicalKeyboardKey.arrowDown || key == LogicalKeyboardKey.keyJ) {
|
||||
onScroll(1);
|
||||
} else if (key == LogicalKeyboardKey.arrowUp ||
|
||||
key == LogicalKeyboardKey.keyK) {
|
||||
onScroll(-1);
|
||||
} else if (event is! KeyDownEvent) {
|
||||
// Only scrolling repeats while a key is held.
|
||||
return KeyEventResult.ignored;
|
||||
} else if (key == LogicalKeyboardKey.arrowLeft ||
|
||||
key == LogicalKeyboardKey.keyH) {
|
||||
onPrevious();
|
||||
} else if (key == LogicalKeyboardKey.arrowRight ||
|
||||
key == LogicalKeyboardKey.keyL) {
|
||||
onNext();
|
||||
} else if (key == LogicalKeyboardKey.slash || event.character == '/') {
|
||||
onSearch();
|
||||
} else if (key == LogicalKeyboardKey.keyN) {
|
||||
onMatch(shift ? -1 : 1);
|
||||
} else if (key == LogicalKeyboardKey.escape) {
|
||||
onEscape();
|
||||
} else {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user