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

feat: file and status tab support pageup and pagedown #2496

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

* Files tab support pageUp and pageDown

## [0.27.0] - 2024-01-14

**new: manage remotes**
Expand Down
56 changes: 53 additions & 3 deletions filetreelist/src/filetree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
error::Result, filetreeitems::FileTreeItems,
tree_iter::TreeIterator, TreeItemInfo,
};
use std::{collections::BTreeSet, path::Path};
use std::{cell::RefCell, collections::BTreeSet, path::Path};

///
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -30,6 +30,7 @@ pub struct FileTree {
selection: Option<usize>,
// caches the absolute selection translated to visual index
visual_selection: Option<VisualSelection>,
pub show_height: RefCell<Option<usize>>,
}

impl FileTree {
Expand All @@ -42,6 +43,7 @@ impl FileTree {
items: FileTreeItems::new(list, collapsed)?,
selection: if list.is_empty() { None } else { Some(0) },
visual_selection: None,
show_height: None.into(),
};
new_self.visual_selection = new_self.calc_visual_selection();

Expand Down Expand Up @@ -112,6 +114,49 @@ impl FileTree {
}
}

fn selection_page_updown(
&self,
current_index: usize,
up: bool,
) -> Option<usize> {
let mut index = current_index;

let mut count = 0;
loop {
index = {
let new_index = if up {
index.saturating_sub(1)
} else {
index.saturating_add(1)
};

if new_index == index {
break;
}

if new_index >= self.items.len() {
break;
}

new_index
};

if self.is_visible_index(index) {
count += 1;
}

if count >= self.show_height.borrow().unwrap_or(0) {
break;
}
}

if index == current_index {
None
} else {
Some(index)
}
}

///
pub fn move_selection(&mut self, dir: MoveSelection) -> bool {
self.selection.is_some_and(|selection| {
Expand All @@ -130,8 +175,13 @@ impl FileTree {
Self::selection_start(selection)
}
MoveSelection::End => self.selection_end(selection),
MoveSelection::PageDown | MoveSelection::PageUp => {
None
MoveSelection::PageUp => {
Self::selection_page_updown(self, selection, true)
}
MoveSelection::PageDown => {
Self::selection_page_updown(
self, selection, false,
)
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/components/revision_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ impl RevisionFilesComponent {
let tree_height = usize::from(area.height.saturating_sub(2));
let tree_width = usize::from(area.width);

self.tree
.show_height
.borrow_mut()
.replace(tree_height.saturating_sub(1));

self.tree.visual_selection().map_or_else(
|| {
self.scroll.reset();
Expand Down
10 changes: 10 additions & 0 deletions src/components/status_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ impl DrawableComponent for StatusTreeComponent {
.map(|idx| idx.saturating_sub(selection_offset))
.unwrap_or_default();
let tree_height = r.height.saturating_sub(2) as usize;
self.tree.window_height.borrow_mut().replace(tree_height);

self.scroll_top.set(ui::calc_scroll_top(
self.scroll_top.get(),
Expand Down Expand Up @@ -504,6 +505,15 @@ impl Component for StatusTreeComponent {
|| key_match(e, self.key_config.keys.shift_down)
{
Ok(self.move_selection(MoveSelection::End).into())
} else if key_match(e, self.key_config.keys.page_up) {
Ok(self
.move_selection(MoveSelection::PageUp)
.into())
} else if key_match(e, self.key_config.keys.page_down)
{
Ok(self
.move_selection(MoveSelection::PageDown)
.into())
} else if key_match(e, self.key_config.keys.move_left)
{
Ok(self
Expand Down
43 changes: 42 additions & 1 deletion src/components/utils/statustree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::filetree::{
};
use anyhow::Result;
use asyncgit::StatusItem;
use std::{cmp, collections::BTreeSet};
use std::{cell::RefCell, cmp, collections::BTreeSet};

//TODO: use new `filetreelist` crate

Expand All @@ -16,6 +16,8 @@ pub struct StatusTree {
// some folders may be folded up, this allows jumping
// over folders which are folded into their parent
pub available_selections: Vec<usize>,

pub window_height: RefCell<Option<usize>>,
}

///
Expand All @@ -27,6 +29,8 @@ pub enum MoveSelection {
Right,
Home,
End,
PageDown,
PageUp,
}

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -143,6 +147,12 @@ impl StatusTree {
}
MoveSelection::Home => SelectionChange::new(0, false),
MoveSelection::End => self.selection_end(),
MoveSelection::PageUp => {
self.selection_page_updown(selection, true)
}
MoveSelection::PageDown => {
self.selection_page_updown(selection, false)
}
};

let changed_index =
Expand Down Expand Up @@ -283,6 +293,37 @@ impl StatusTree {
SelectionChange::new(new_index, false)
}

fn selection_page_updown(
&self,
current_index: usize,
up: bool,
) -> SelectionChange {
let mut new_index = current_index;
let mut count = 0;

loop {
if up {
new_index = new_index.saturating_sub(1);
} else {
new_index = new_index.saturating_add(1);
}

if self.is_visible_index(new_index) {
count += 1;
}

if count == self.window_height.borrow().unwrap_or(0) {
break;
}

if new_index == 0 || new_index == self.tree.len() - 1 {
break;
}
}

SelectionChange::new(new_index, false)
}

fn is_visible_index(&self, idx: usize) -> bool {
self.tree[idx].info.visible
}
Expand Down