Skip to content

Commit

Permalink
Custom file picker width
Browse files Browse the repository at this point in the history
  • Loading branch information
Rational-Curiosity committed Nov 22, 2021
1 parent 4238a84 commit 61be1fd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
3 changes: 3 additions & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ To override global configuration parameters, create a `config.toml` file located
|`git-global` | Enables reading global .gitignore, whose path is specified in git's config: `core.excludefile` option. | true
|`git-exclude` | Enables reading `.git/info/exclude` files. | true
|`max-depth` | Set with an integer value for maximum depth to recurse. | Defaults to `None`.
|`width-factor` | File picker width, treated as a factor. Defaults to 0.9 | `0.9` |
|`height-factor` | File picker height, treated as a factor. Defaults to 0.9 | `0.9` |
|`preview-width` | File picker preview width, from 0.0 to 1.0 treated as a factor, > 1 width in characters. Defaults to 0.5 | `0.5` |

## LSP

Expand Down
31 changes: 24 additions & 7 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,24 @@ impl<T: 'static> Component for FilePicker<T> {
// | | | |
// +---------+ +---------+
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,
);
// -- 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 {
area.width / 2
match cx.editor.config.file_picker.preview_width {
w if w <= 0.0 => area.width / 2,
w if w <= 1.0 => (area.width as f32 * (1.0 - w)).round() as u16,
w if w < area.width as f32 => area.width - w.round() as u16,
_ => area.width,
}
} else {
area.width
};
Expand Down Expand Up @@ -388,10 +397,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,
};
area.inner(&margin)
}
Expand Down Expand Up @@ -453,7 +462,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
};
Expand Down Expand Up @@ -534,7 +547,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);
Expand Down
9 changes: 9 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ 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, from 0.0 to 1.0 treated as a factor, > 1 width in characters. Defaults to 0.5.
pub preview_width: f32,
}

impl Default for FilePickerConfig {
Expand All @@ -71,6 +77,9 @@ impl Default for FilePickerConfig {
git_global: true,
git_exclude: true,
max_depth: None,
width_factor: 0.9,
height_factor: 0.9,
preview_width: 0.5,
}
}
}
Expand Down

0 comments on commit 61be1fd

Please sign in to comment.