pull/13430/merge
Em Zhan 2025-06-15 13:15:54 +02:00 committed by GitHub
commit 3ba0e5b6e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 40 additions and 15 deletions

View File

@ -57,6 +57,7 @@ use self::handlers::{DynamicQueryChange, DynamicQueryHandler, PreviewHighlightHa
pub const ID: &str = "picker"; pub const ID: &str = "picker";
pub const MIN_AREA_WIDTH_FOR_PREVIEW: u16 = 72; pub const MIN_AREA_WIDTH_FOR_PREVIEW: u16 = 72;
pub const MIN_AREA_HEIGHT_FOR_PREVIEW: u16 = 24;
/// Biggest file size to preview in bytes /// Biggest file size to preview in bytes
pub const MAX_FILE_SIZE_FOR_PREVIEW: u64 = 10 * 1024 * 1024; pub const MAX_FILE_SIZE_FOR_PREVIEW: u64 = 10 * 1024 * 1024;
@ -987,31 +988,58 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
); );
} }
} }
fn get_layout(&self, area: Rect) -> (bool, bool) {
let render_preview = self.show_preview
&& self.file_fn.is_some()
&& area.width >= MIN_AREA_WIDTH_FOR_PREVIEW
&& area.height >= MIN_AREA_HEIGHT_FOR_PREVIEW;
let stack_vertically = area.width / 2 < MIN_AREA_WIDTH_FOR_PREVIEW;
(render_preview, stack_vertically)
}
} }
impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I, D> { impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I, D> {
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
// default render
// +---------+ +---------+ // +---------+ +---------+
// |prompt | |preview | // |prompt | |preview |
// +---------+ | | // +---------+ | |
// |picker | | | // |picker | | |
// | | | | // | | | |
// +---------+ +---------+ // +---------+ +---------+
//
// stack vertically
// +---------+
// |prompt |
// +---------+
// |picker |
// | |
// +---------+
// |preview |
// | |
// | |
// +---------+
let render_preview = let (render_preview, stack_vertically) = self.get_layout(area);
self.show_preview && self.file_fn.is_some() && area.width > MIN_AREA_WIDTH_FOR_PREVIEW; let picker_area = if render_preview {
if stack_vertically {
let picker_width = if render_preview { area.with_height(area.height / 3)
area.width / 2 } else {
area.with_width(area.width / 2)
}
} else { } else {
area.width area
}; };
let picker_area = area.with_width(picker_width);
self.render_picker(picker_area, surface, cx); self.render_picker(picker_area, surface, cx);
if render_preview { if render_preview {
let preview_area = area.clip_left(picker_width); let preview_area = if stack_vertically {
area.clip_top(picker_area.height)
} else {
area.clip_left(picker_area.width)
};
self.render_preview(preview_area, surface, cx); self.render_preview(preview_area, surface, cx);
} }
} }
@ -1137,18 +1165,15 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I,
// calculate the inner area inside the box // calculate the inner area inside the box
let inner = block.inner(area); let inner = block.inner(area);
// prompt area let (render_preview, stack_vertically) = self.get_layout(area);
let render_preview = let picker_width = if render_preview && !stack_vertically {
self.show_preview && self.file_fn.is_some() && area.width > MIN_AREA_WIDTH_FOR_PREVIEW;
let picker_width = if render_preview {
area.width / 2 area.width / 2
} else { } else {
area.width area.width
}; };
let area = inner.clip_left(1).with_height(1).with_width(picker_width); let prompt_area = inner.clip_left(1).with_height(1).with_width(picker_width);
self.prompt.cursor(area, editor) self.prompt.cursor(prompt_area, editor)
} }
fn required_size(&mut self, (width, height): (u16, u16)) -> Option<(u16, u16)> { fn required_size(&mut self, (width, height): (u16, u16)) -> Option<(u16, u16)> {