-
-
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
Custom file picker width max min and factor #1089
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ use tui::widgets::Widget; | |
|
||
use std::{ | ||
borrow::Cow, | ||
cmp, | ||
collections::HashMap, | ||
io::Read, | ||
path::{Path, PathBuf}, | ||
|
@@ -28,7 +29,6 @@ use helix_view::{ | |
Document, Editor, | ||
}; | ||
|
||
pub const MIN_SCREEN_WIDTH_FOR_PREVIEW: u16 = 80; | ||
/// Biggest file size to preview in bytes | ||
pub const MAX_FILE_SIZE_FOR_PREVIEW: u64 = 10 * 1024 * 1024; | ||
|
||
|
@@ -159,16 +159,32 @@ impl<T: 'static> Component for FilePicker<T> { | |
// |picker | | | | ||
// | | | | | ||
// +---------+ +---------+ | ||
let render_preview = area.width > MIN_SCREEN_WIDTH_FOR_PREVIEW; | ||
let area = inner_rect(area); | ||
let area = inner_rect( | ||
area, | ||
cx.editor.config.file_picker.width_factor, | ||
cx.editor.config.file_picker.height_factor, | ||
Comment on lines
+164
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass in |
||
); | ||
let render_preview = area.width | ||
> cx.editor.config.file_picker.preview_min_width | ||
+ cx.editor.config.file_picker.file_min_width; | ||
Comment on lines
+168
to
+169
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit repetitive, add |
||
// -- Render the frame: | ||
// clear area | ||
let background = cx.editor.theme.get("ui.background"); | ||
let text = cx.editor.theme.get("ui.text"); | ||
surface.clear_with(area, background); | ||
|
||
let picker_width = if render_preview { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be less confusing to have the config as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then I think we can have two different options - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done :-) |
||
area.width / 2 | ||
let preview_width = (area.width as f32 | ||
* cx.editor.config.file_picker.preview_width_factor) | ||
.round() as u16; | ||
pickfire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cmp::max( | ||
area.width | ||
- cmp::max( | ||
preview_width, | ||
cx.editor.config.file_picker.preview_min_width, | ||
), | ||
cx.editor.config.file_picker.file_min_width, | ||
) | ||
Comment on lines
+180
to
+187
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than |
||
} else { | ||
area.width | ||
}; | ||
|
@@ -388,10 +404,10 @@ impl<T> Picker<T> { | |
// - on input change: | ||
// - score all the names in relation to input | ||
|
||
fn inner_rect(area: Rect) -> Rect { | ||
fn inner_rect(area: Rect, width_factor: f32, height_factor: f32) -> Rect { | ||
let margin = Margin { | ||
vertical: area.height * 10 / 100, | ||
horizontal: area.width * 10 / 100, | ||
vertical: (area.height as f32 * (1.0 - width_factor)).round() as u16, | ||
horizontal: (area.width as f32 * (1.0 - height_factor)).round() as u16, | ||
Comment on lines
+409
to
+410
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using floats feels wrong to me, why not specify a percentage from |
||
}; | ||
area.inner(&margin) | ||
} | ||
|
@@ -453,7 +469,11 @@ impl<T: 'static> Component for Picker<T> { | |
|
||
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { | ||
let area = if self.render_centered { | ||
inner_rect(area) | ||
inner_rect( | ||
area, | ||
cx.editor.config.file_picker.width_factor, | ||
cx.editor.config.file_picker.height_factor, | ||
) | ||
} else { | ||
area | ||
}; | ||
|
@@ -534,7 +554,11 @@ impl<T: 'static> Component for Picker<T> { | |
|
||
fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) { | ||
// TODO: this is mostly duplicate code | ||
let area = inner_rect(area); | ||
let area = inner_rect( | ||
area, | ||
editor.config.file_picker.width_factor, | ||
editor.config.file_picker.height_factor, | ||
); | ||
let block = Block::default().borders(Borders::ALL); | ||
// calculate the inner area inside the box | ||
let inner = block.inner(area); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,16 @@ pub struct FilePickerConfig { | |
/// WalkBuilder options | ||
/// Maximum Depth to recurse directories in file picker and global search. Defaults to `None`. | ||
pub max_depth: Option<usize>, | ||
/// File picker width, treated as a factor. Defaults to 0.9. | ||
pub width_factor: f32, | ||
/// File picker height, treated as a factor. Defaults to 0.9. | ||
pub height_factor: f32, | ||
/// File picker preview width, treated as a factor. Defaults to 0.5. | ||
pub preview_width_factor: f32, | ||
/// File picker preview minimum width in columns. Default to 60. | ||
pub preview_min_width: u16, | ||
/// File picker filenames minimum width in columns when preview is showed. Default to 20. | ||
pub file_min_width: u16, | ||
} | ||
|
||
impl Default for FilePickerConfig { | ||
|
@@ -72,6 +82,13 @@ impl Default for FilePickerConfig { | |
git_global: true, | ||
git_exclude: true, | ||
max_depth: None, | ||
width_factor: 0.9, | ||
height_factor: 0.9, | ||
preview_width_factor: 0.5, | ||
// Default values `preview_min_width` and `file_min_width` | ||
// should total up to 80 to fit 80 column terminals. | ||
preview_min_width: 60, | ||
file_min_width: 20, | ||
Comment on lines
+90
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a comment that both of this should total up to 80. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Out of curiosity, what is the reason? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that's why you put 80? Like previously I saw a number 80 being removed, and you replaced it with the sum of these two. At the very least I think we should still make it look nice for 80x24 terminals. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done :-) |
||
} | ||
} | ||
} | ||
|
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.
filenames minimum width seemed confusing to me?
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.
i know my English sucks ;-), any suggestion?
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.
File picker preview minimum width in columns?
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.
file-min-width
is the other side.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.
Not sure how to explain this too, hopefully someone can help.