Skip to content

Make file preview scrollable #2206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
(WIP) feat: scrollable file preview in Log
  • Loading branch information
TD-Sky committed Jun 10, 2024
commit 3a8cf4aa749b5988f9c29795ba717ebd9d8b4a84
5 changes: 4 additions & 1 deletion src/components/commit_details/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ impl DetailsComponent {
})
}

fn move_scroll_top(&mut self, move_type: ScrollType) -> bool {
pub(in crate::components) fn move_scroll_top(
&mut self,
move_type: ScrollType,
) -> bool {
if self.data.is_some() {
self.scroll.move_top(move_type)
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/components/commit_details/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
app::Environment,
keys::{key_match, SharedKeyConfig},
strings,
ui::seek_scroll,
};
use anyhow::Result;
use asyncgit::{
Expand Down Expand Up @@ -221,6 +222,16 @@ impl Component for CommitDetailsComponent {
self.file_tree.focus(false);
self.set_details_focus(true);
Ok(EventState::Consumed)
} else if let Some(scroll) =
seek_scroll(e, &self.key_config)
{
let state = if self.file_tree.focused() {
self.single_details.move_scroll_top(scroll);
EventState::Consumed
} else {
EventState::NotConsumed
};
Ok(state)
} else {
Ok(EventState::NotConsumed)
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn command_pump(
}
}

#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub enum ScrollType {
Up,
Down,
Expand Down
5 changes: 3 additions & 2 deletions src/components/revision_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
queue::{InternalEvent, Queue, StackablePopupOpen},
strings::{self, order, symbol},
try_or_popup,
ui::{self, common_nav, key2seek, style::SharedTheme},
ui::{self, common_nav, seek_move, style::SharedTheme},
AsyncNotification,
};
use anyhow::Result;
Expand Down Expand Up @@ -489,7 +489,8 @@ impl Component for RevisionFilesComponent {
|| key_match(key, self.key_config.keys.seek_down)
{
if is_tree_focused {
if let Some(nav) = key2seek(key, &self.key_config)
if let Some(nav) =
seek_move(key, &self.key_config)
{
return Ok(
if self.current_file.scroll(nav) {
Expand Down
5 changes: 4 additions & 1 deletion src/components/syntax_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ impl SyntaxTextComponent {
}
}

pub(in crate::components) fn scroll(&self, nav: MoveSelection) -> bool {
pub(in crate::components) fn scroll(
&self,
nav: MoveSelection,
) -> bool {
let state = self.paragraph_state.get();

let new_scroll_pos = match nav {
Expand Down
20 changes: 18 additions & 2 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ pub use stateful_paragraph::{
};
pub use syntax_text::{AsyncSyntaxJob, SyntaxText};

use crate::keys::{key_match, SharedKeyConfig};
use crate::{
components::ScrollType,
keys::{key_match, SharedKeyConfig},
};

/// return the scroll position (line) necessary to have the `selection` in view if it is not already
pub const fn calc_scroll_top(
Expand Down Expand Up @@ -153,7 +156,7 @@ pub fn common_nav(
}
}

pub fn key2seek(
pub fn seek_move(
key: &crossterm::event::KeyEvent,
key_config: &SharedKeyConfig,
) -> Option<MoveSelection> {
Expand All @@ -166,6 +169,19 @@ pub fn key2seek(
}
}

pub fn seek_scroll(
key: &crossterm::event::KeyEvent,
key_config: &SharedKeyConfig,
) -> Option<ScrollType> {
if key_match(key, key_config.keys.seek_up) {
Some(ScrollType::Up)
} else if key_match(key, key_config.keys.seek_down) {
Some(ScrollType::Down)
} else {
None
}
}

#[cfg(test)]
mod test {
use super::{rect_inside, Size};
Expand Down