-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add PageUp
, PageDown
, Ctrl-u
, Ctrl-d
, Home
, End
keyboard shortcuts to file picker
#1612
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dfd7dbf
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard …
Aloso ce8bc19
Refactor file picker paging logic
Aloso 256cad6
change key mapping
Aloso 915cfb1
Add overlay component
Aloso a59dc11
Use closure instead of margin to calculate size
Aloso 66f7bc9
Don't wrap file picker in `Overlay` automatically
Aloso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crossterm::event::Event; | ||
use helix_core::Position; | ||
use helix_view::{ | ||
graphics::{CursorKind, Rect}, | ||
Editor, | ||
}; | ||
use tui::buffer::Buffer; | ||
|
||
use crate::compositor::{Component, Context, EventResult}; | ||
|
||
/// Contains a component placed in the center of the parent component | ||
pub struct Overlay<T> { | ||
/// Child component | ||
pub content: T, | ||
/// Function to compute the size and position of the child component | ||
pub calc_child_size: Box<dyn Fn(Rect) -> Rect>, | ||
} | ||
|
||
/// Surrounds the component with a margin of 5% on each side, and an additional 2 rows at the bottom | ||
pub fn overlayed<T>(content: T) -> Overlay<T> { | ||
Overlay { | ||
content, | ||
calc_child_size: Box::new(|rect: Rect| clip_rect_relative(rect.clip_bottom(2), 90, 90)), | ||
} | ||
} | ||
|
||
fn clip_rect_relative(rect: Rect, percent_horizontal: u8, percent_vertical: u8) -> Rect { | ||
fn mul_and_cast(size: u16, factor: u8) -> u16 { | ||
((size as u32) * (factor as u32) / 100).try_into().unwrap() | ||
} | ||
|
||
let inner_w = mul_and_cast(rect.width, percent_horizontal); | ||
let inner_h = mul_and_cast(rect.height, percent_vertical); | ||
|
||
let offset_x = rect.width.saturating_sub(inner_w) / 2; | ||
let offset_y = rect.height.saturating_sub(inner_h) / 2; | ||
|
||
Rect { | ||
x: rect.x + offset_x, | ||
y: rect.y + offset_y, | ||
width: inner_w, | ||
height: inner_h, | ||
} | ||
} | ||
|
||
impl<T: Component + 'static> Component for Overlay<T> { | ||
fn render(&mut self, area: Rect, frame: &mut Buffer, ctx: &mut Context) { | ||
let dimensions = (self.calc_child_size)(area); | ||
self.content.render(dimensions, frame, ctx) | ||
} | ||
|
||
fn required_size(&mut self, (width, height): (u16, u16)) -> Option<(u16, u16)> { | ||
let area = Rect { | ||
x: 0, | ||
y: 0, | ||
width, | ||
height, | ||
}; | ||
let dimensions = (self.calc_child_size)(area); | ||
let viewport = (dimensions.width, dimensions.height); | ||
let _ = self.content.required_size(viewport)?; | ||
Some((width, height)) | ||
} | ||
|
||
fn handle_event(&mut self, event: Event, ctx: &mut Context) -> EventResult { | ||
self.content.handle_event(event, ctx) | ||
} | ||
|
||
fn cursor(&self, area: Rect, ctx: &Editor) -> (Option<Position>, CursorKind) { | ||
let dimensions = (self.calc_child_size)(area); | ||
self.content.cursor(dimensions, ctx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please separate this out,
Popup
with some calculation should be enough to replace this type altogether.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(definitely avoid floats and use a percentage value between 0 and 100)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better if a type that can only guarantee the value range but I am not sure if we have something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@archseer I refactored the code again, it is now much simpler. Also, I'm not sure I understand what you mean with your first comment.