brain: Scrolling improvements, and `z` key
e46aa3a0f1391e421270ec9d9676ec9cbaf0106c
1 parent
5d0eb389
crates/brain/src/session.rs
+7 -0
| 877 | 877 | } |
|
| 878 | 878 | Event::Key(Key::Char('j')) => self.apply(DiffAction::NextHunk, area), |
|
| 879 | 879 | Event::Key(Key::Char('k')) => self.apply(DiffAction::PreviousHunk, area), |
|
| 880 | 880 | Event::Key(Key::Char('n')) => self.apply(DiffAction::FocusNext, area), |
|
| 881 | 881 | Event::Key(Key::Char('p')) => self.apply(DiffAction::FocusPrevious, area), |
|
| 882 | + | Event::Key(Key::Char('z')) => { |
|
| 883 | + | if self.current()?.viewer.center_focused_hunk(area) { |
|
| 884 | + | Ok(Control::Redraw) |
|
| 885 | + | } else { |
|
| 886 | + | Ok(Control::Continue) |
|
| 887 | + | } |
|
| 888 | + | } |
|
| 882 | 889 | Event::Key(Key::Char('c')) => { |
|
| 883 | 890 | self.enter_hunk_view(area)?; |
|
| 884 | 891 | Ok(Control::Redraw) |
|
| 885 | 892 | } |
|
| 886 | 893 | Event::Key(Key::Enter) => { |
a.rs
+0 -0
crates/diff/src/viewer.rs
+30 -3
| 93 | 93 | /// Number of source columns moved by horizontal-scroll inputs. |
|
| 94 | 94 | const HORIZONTAL_SCROLL_COLS: usize = 8; |
|
| 95 | 95 | /// Number of body rows moved by mouse-wheel inputs. |
|
| 96 | 96 | const WHEEL_SCROLL_ROWS: isize = 3; |
|
| 97 | 97 | /// Central viewport percentage where hunk navigation preserves the scroll offset. |
|
| 98 | - | const HUNK_SCROLL_COMFORT_PERCENT: usize = 80; |
|
| 98 | + | const HUNK_SCROLL_COMFORT_PERCENT: usize = 60; |
|
| 99 | 99 | /// Local row of a file card's top border. |
|
| 100 | 100 | const FILE_TOP_ROW: usize = 0; |
|
| 101 | 101 | /// Local row of a file card's header. |
|
| 102 | 102 | const FILE_HEADER_ROW: usize = 1; |
|
| 103 | 103 | /// Local row of the divider between a file header and its hunk body. |
| 1123 | 1123 | self.sync_file_tree(active); |
|
| 1124 | 1124 | } |
|
| 1125 | 1125 | self.state != before |
|
| 1126 | 1126 | } |
|
| 1127 | 1127 | ||
| 1128 | + | /// Places the focused hunk in the viewport. |
|
| 1129 | + | pub fn center_focused_hunk(&mut self, area: Rect) -> bool { |
|
| 1130 | + | let Some(focus @ DiffFocus::Hunk(_)) = self.state.focus else { |
|
| 1131 | + | return false; |
|
| 1132 | + | }; |
|
| 1133 | + | let visible_rows = self.layout(area).diff.rows; |
|
| 1134 | + | let Some(target) = self |
|
| 1135 | + | .focus_targets(FocusFilter::HunkNavigation) |
|
| 1136 | + | .into_iter() |
|
| 1137 | + | .find(|target| target.focus == focus) |
|
| 1138 | + | else { |
|
| 1139 | + | return false; |
|
| 1140 | + | }; |
|
| 1141 | + | let offset = |
|
| 1142 | + | target.scroll_offset(self.state.scroll.offset, visible_rows, HunkScroll::Center); |
|
| 1143 | + | let previous = self.state.scroll.offset; |
|
| 1144 | + | self.state |
|
| 1145 | + | .scroll |
|
| 1146 | + | .set_offset(offset, self.scroll_rows(visible_rows), visible_rows); |
|
| 1147 | + | self.state.scroll.offset != previous |
|
| 1148 | + | } |
|
| 1149 | + | ||
| 1128 | 1150 | /// Returns the file containing the current focus. |
|
| 1129 | 1151 | pub fn focused_file(&self) -> Option<FileId> { |
|
| 1130 | 1152 | match self.state.focus? { |
|
| 1131 | 1153 | DiffFocus::Commit => None, |
|
| 1132 | 1154 | DiffFocus::File(file) => Some(file), |
| 1507 | 1529 | } |
|
| 1508 | 1530 | Event::Key(Key::Char('J')) => DiffAction::MoveFileTree(1), |
|
| 1509 | 1531 | Event::Key(Key::Char('K')) => DiffAction::MoveFileTree(-1), |
|
| 1510 | 1532 | Event::Key(Key::Char('H')) => DiffAction::ExpandFileTreeFolder, |
|
| 1511 | 1533 | Event::Key(Key::Char('L')) => DiffAction::CollapseFileTreeFolder, |
|
| 1512 | - | Event::Key(Key::Char(' ') | Key::PageDown) => DiffAction::Scroll(Scroll::Pages(1)), |
|
| 1534 | + | Event::Key(Key::Char(' ')) => { |
|
| 1535 | + | let rows = self.layout(area).diff.rows; |
|
| 1536 | + | DiffAction::Scroll(Scroll::Lines((rows / 2).max(1) as isize)) |
|
| 1537 | + | } |
|
| 1538 | + | Event::Key(Key::PageDown) => DiffAction::Scroll(Scroll::Pages(1)), |
|
| 1513 | 1539 | Event::Key(Key::PageUp) => DiffAction::Scroll(Scroll::Pages(-1)), |
|
| 1514 | 1540 | Event::Key(Key::Ctrl('d')) => { |
|
| 1515 | 1541 | DiffAction::Scroll(Scroll::Lines((area.rows / 2).max(1) as isize)) |
|
| 1516 | 1542 | } |
|
| 1517 | 1543 | Event::Key(Key::Ctrl('u')) => { |
| 2649 | 2675 | return top; |
|
| 2650 | 2676 | } |
|
| 2651 | 2677 | ||
| 2652 | 2678 | match placement { |
|
| 2653 | 2679 | HunkScroll::Center => { |
|
| 2654 | - | let screen_row = leading + available.saturating_sub(height) / 2; |
|
| 2680 | + | let screen_row = visible_rows.saturating_sub(height) / 2; |
|
| 2681 | + | let screen_row = screen_row.max(leading); |
|
| 2655 | 2682 | ||
| 2656 | 2683 | self.row.saturating_sub(screen_row) |
|
| 2657 | 2684 | } |
|
| 2658 | 2685 | HunkScroll::Preserve => { |
|
| 2659 | 2686 | let inset = visible_rows.saturating_mul(100 - HUNK_SCROLL_COMFORT_PERCENT) / 200; |
a.rs
+0 -0
image.png
+0 -0
image.png
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0
src/app/a.rs
+0 -0
src/lib/b.rs
+0 -0
README.md
+0 -0
a.rs
+0 -0
b.rs
+0 -0
first.rs
+0 -0
second.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs\n",
+0 -0
new.rs\n",
+0 -0