Skip to content
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

Make file preview scrollable #2206

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
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
23 changes: 22 additions & 1 deletion 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, style::SharedTheme},
ui::{self, common_nav, seek_move, style::SharedTheme},
AsyncNotification,
};
use anyhow::Result;
Expand Down Expand Up @@ -444,6 +444,11 @@ impl Component for RevisionFilesComponent {
)
.order(order::RARE_ACTION),
);
out.push(CommandInfo::new(
strings::commands::seek(&self.key_config),
self.tree.selected_file().is_some(),
true,
));
tree_nav_cmds(&self.tree, &self.key_config, out);
} else {
self.current_file.commands(out, force_all);
Expand Down Expand Up @@ -480,6 +485,22 @@ impl Component for RevisionFilesComponent {
self.hide();
return Ok(EventState::Consumed);
}
} else if key_match(key, self.key_config.keys.seek_up)
|| key_match(key, self.key_config.keys.seek_down)
{
if is_tree_focused {
if let Some(nav) =
seek_move(key, &self.key_config)
{
return Ok(
if self.current_file.scroll(nav) {
EventState::Consumed
} else {
EventState::NotConsumed
},
);
}
}
} else if key_match(key, self.key_config.keys.move_right)
{
if is_tree_focused {
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 {
}
}

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
4 changes: 4 additions & 0 deletions src/keys/key_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub struct KeysList {
pub move_right: GituiKeyEvent,
pub move_up: GituiKeyEvent,
pub move_down: GituiKeyEvent,
pub seek_up: GituiKeyEvent,
pub seek_down: GituiKeyEvent,
pub tree_collapse_recursive: GituiKeyEvent,
pub tree_expand_recursive: GituiKeyEvent,
pub home: GituiKeyEvent,
Expand Down Expand Up @@ -152,6 +154,8 @@ impl Default for KeysList {
end: GituiKeyEvent::new(KeyCode::End, KeyModifiers::empty()),
move_up: GituiKeyEvent::new(KeyCode::Up, KeyModifiers::empty()),
move_down: GituiKeyEvent::new(KeyCode::Down, KeyModifiers::empty()),
seek_up: GituiKeyEvent::new(KeyCode::Up, KeyModifiers::ALT),
seek_down: GituiKeyEvent::new(KeyCode::Down, KeyModifiers::ALT),
popup_up: GituiKeyEvent::new(KeyCode::Up, KeyModifiers::empty()),
popup_down: GituiKeyEvent::new(KeyCode::Down, KeyModifiers::empty()),
page_down: GituiKeyEvent::new(KeyCode::PageDown, KeyModifiers::empty()),
Expand Down
11 changes: 11 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,17 @@ pub mod commands {
CMD_GROUP_GENERAL,
)
}
pub fn seek(key_config: &SharedKeyConfig) -> CommandText {
CommandText::new(
format!(
"Seek [{}{}]",
key_config.get_hint(key_config.keys.seek_up),
key_config.get_hint(key_config.keys.seek_down)
),
"Seek up or down in selected file",
CMD_GROUP_GENERAL,
)
}
pub fn commit_list_mark(
key_config: &SharedKeyConfig,
marked: bool,
Expand Down
31 changes: 30 additions & 1 deletion 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,6 +156,32 @@ pub fn common_nav(
}
}

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

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