fix(explorer/overlay): prompt overflow

- Previously the prompt appears within the float, which has very limited
  space
- Now, the prompt will be rendered at the editor command area
pull/9/head
wongjiahau 2 years ago
parent c2e2f050da
commit a4943a7226

@ -2439,9 +2439,9 @@ fn open_or_focus_explorer(cx: &mut Context) {
|compositor: &mut Compositor, cx: &mut compositor::Context| { |compositor: &mut Compositor, cx: &mut compositor::Context| {
if let Some(editor) = compositor.find::<ui::EditorView>() { if let Some(editor) = compositor.find::<ui::EditorView>() {
match editor.explorer.as_mut() { match editor.explorer.as_mut() {
Some(explore) => explore.content.focus(), Some(explore) => explore.focus(),
None => match ui::Explorer::new(cx) { None => match ui::Explorer::new(cx) {
Ok(explore) => editor.explorer = Some(overlayed(explore)), Ok(explore) => editor.explorer = Some(explore),
Err(err) => cx.editor.set_error(format!("{}", err)), Err(err) => cx.editor.set_error(format!("{}", err)),
}, },
} }
@ -2455,11 +2455,13 @@ fn reveal_current_file(cx: &mut Context) {
|compositor: &mut Compositor, cx: &mut compositor::Context| { |compositor: &mut Compositor, cx: &mut compositor::Context| {
if let Some(editor) = compositor.find::<ui::EditorView>() { if let Some(editor) = compositor.find::<ui::EditorView>() {
(|| match editor.explorer.as_mut() { (|| match editor.explorer.as_mut() {
Some(explore) => explore.content.reveal_current_file(cx), Some(explore) => explore.reveal_current_file(cx),
None => { None => {
editor.explorer = Some(overlayed(ui::Explorer::new(cx)?)); editor.explorer = Some(ui::Explorer::new(cx)?);
let explorer = editor.explorer.as_mut().unwrap(); if let Some(explorer) = editor.explorer.as_mut() {
explorer.content.reveal_current_file(cx) explorer.reveal_current_file(cx)?;
}
Ok(())
} }
})() })()
.unwrap_or_else(|err| cx.editor.set_error(err.to_string())) .unwrap_or_else(|err| cx.editor.set_error(err.to_string()))

@ -124,7 +124,6 @@ pub trait Component: Any + AnyComponent {
let mut jobs = Context::dummy_jobs(); let mut jobs = Context::dummy_jobs();
let mut cx = Context::dummy(&mut jobs, &mut editor); let mut cx = Context::dummy(&mut jobs, &mut editor);
for event in parse_macro(events)? { for event in parse_macro(events)? {
println!("Event = {}", event);
self.handle_event(&Event::Key(event), &mut cx); self.handle_event(&Event::Key(event), &mut cx);
} }
Ok(()) Ok(())

@ -6,7 +6,6 @@ use crate::{
keymap::{KeymapResult, Keymaps}, keymap::{KeymapResult, Keymaps},
ui::{ ui::{
document::{render_document, LinePos, TextRenderer, TranslatedPosition}, document::{render_document, LinePos, TextRenderer, TranslatedPosition},
overlay::Overlay,
Completion, Explorer, ProgressSpinners, Completion, Explorer, ProgressSpinners,
}, },
}; };
@ -43,7 +42,7 @@ pub struct EditorView {
last_insert: (commands::MappableCommand, Vec<InsertEvent>), last_insert: (commands::MappableCommand, Vec<InsertEvent>),
pub(crate) completion: Option<Completion>, pub(crate) completion: Option<Completion>,
spinners: ProgressSpinners, spinners: ProgressSpinners,
pub(crate) explorer: Option<Overlay<Explorer>>, pub(crate) explorer: Option<Explorer>,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -1360,8 +1359,8 @@ impl Component for EditorView {
}; };
let editor_area = if let Some(explorer) = &self.explorer { let editor_area = if let Some(explorer) = &self.explorer {
let explorer_column_width = if explorer.content.is_opened() { let explorer_column_width = if explorer.is_opened() {
explorer.content.column_width().saturating_add(2) explorer.column_width().saturating_add(2)
} else { } else {
0 0
}; };
@ -1381,14 +1380,14 @@ impl Component for EditorView {
cx.editor.resize(editor_area); cx.editor.resize(editor_area);
if let Some(explorer) = self.explorer.as_mut() { if let Some(explorer) = self.explorer.as_mut() {
if !explorer.content.is_focus() { if !explorer.is_focus() {
if let Some(position) = config.explorer.is_embed() { if let Some(position) = config.explorer.is_embed() {
let area = if use_bufferline { let area = if use_bufferline {
area.clip_top(1) area.clip_top(1)
} else { } else {
area area
}; };
explorer.content.render_embed(area, surface, cx, &position); explorer.render_embed(area, surface, cx, &position);
} }
} }
} }
@ -1473,14 +1472,14 @@ impl Component for EditorView {
} }
if let Some(explore) = self.explorer.as_mut() { if let Some(explore) = self.explorer.as_mut() {
if explore.content.is_focus() { if explore.is_focus() {
if let Some(position) = config.explorer.is_embed() { if let Some(position) = config.explorer.is_embed() {
let area = if use_bufferline { let area = if use_bufferline {
area.clip_top(1) area.clip_top(1)
} else { } else {
area area
}; };
explore.content.render_embed(area, surface, cx, &position); explore.render_embed(area, surface, cx, &position);
} else { } else {
explore.render(area, surface, cx); explore.render(area, surface, cx);
} }
@ -1490,11 +1489,11 @@ impl Component for EditorView {
fn cursor(&self, _area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) { fn cursor(&self, _area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) {
if let Some(explore) = &self.explorer { if let Some(explore) = &self.explorer {
if explore.content.is_focus() { if explore.is_focus() {
if editor.config().explorer.is_overlay() { if editor.config().explorer.is_overlay() {
return explore.cursor(_area, editor); return explore.cursor(_area, editor);
} }
let cursor = explore.content.cursor(_area, editor); let cursor = explore.cursor(_area, editor);
if cursor.0.is_some() { if cursor.0.is_some() {
return cursor; return cursor;
} }

@ -443,17 +443,14 @@ impl Explorer {
} }
fn render_float(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { fn render_float(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
let float_area_box = area.overlayed();
let background = cx.editor.theme.get("ui.background"); let background = cx.editor.theme.get("ui.background");
surface.clear_with(area, background); surface.clear_with(float_area_box, background);
let area = render_block(area, surface, Borders::ALL); let float_area = render_block(float_area_box, surface, Borders::ALL);
let mut preview_area = area.clip_left(self.column_width + 1); let preview_area = float_area.clip_left(self.column_width + 1);
if let Some((_, prompt)) = self.prompt.as_mut() { if let Some((_, prompt)) = self.prompt.as_mut() {
let area = preview_area.clip_bottom(2); prompt.render(area, surface, cx);
let promp_area =
render_block(preview_area.clip_top(area.height), surface, Borders::TOP);
prompt.render(promp_area, surface, cx);
preview_area = area;
} }
if self.show_help { if self.show_help {
self.render_help(preview_area, surface, cx); self.render_help(preview_area, surface, cx);
@ -461,7 +458,12 @@ impl Explorer {
self.render_preview(preview_area, surface, cx.editor); self.render_preview(preview_area, surface, cx.editor);
} }
let list_area = render_block(area.clip_right(preview_area.width), surface, Borders::RIGHT); let list_area = render_block(
float_area.clip_right(preview_area.width),
surface,
Borders::RIGHT,
);
self.render_tree(list_area, surface, cx) self.render_tree(list_area, surface, cx)
} }
@ -841,17 +843,7 @@ impl Component for Explorer {
Some((_, prompt)) => prompt, Some((_, prompt)) => prompt,
None => return (None, CursorKind::Hidden), None => return (None, CursorKind::Hidden),
}; };
let config = &editor.config().explorer; let (x, y) = (area.x, area.y + area.height.saturating_sub(1));
let (x, y) = if config.is_overlay() {
let colw = self.column_width as u16;
if area.width > colw {
(area.x + colw + 2, area.y + area.height.saturating_sub(2))
} else {
return (None, CursorKind::Hidden);
}
} else {
(area.x, area.y + area.height.saturating_sub(1))
};
prompt.cursor(Rect::new(x, y, area.width, 1), editor) prompt.cursor(Rect::new(x, y, area.width, 1), editor)
} }
} }

@ -19,26 +19,7 @@ pub struct Overlay<T> {
pub fn overlayed<T>(content: T) -> Overlay<T> { pub fn overlayed<T>(content: T) -> Overlay<T> {
Overlay { Overlay {
content, content,
calc_child_size: Box::new(|rect: Rect| clip_rect_relative(rect.clip_bottom(2), 90, 90)), calc_child_size: Box::new(|rect: Rect| rect.overlayed()),
}
}
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,
} }
} }

@ -249,7 +249,7 @@ impl Default for ExplorerConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
position: ExplorerPosition::Left, position: ExplorerPosition::Left,
column_width: 30, column_width: 36,
} }
} }
} }

@ -248,6 +248,34 @@ impl Rect {
&& self.y < other.y + other.height && self.y < other.y + other.height
&& self.y + self.height > other.y && self.y + self.height > other.y
} }
/// Returns a smaller `Rect` with a margin of 5% on each side, and an additional 2 rows at the bottom
pub fn overlayed(self) -> Rect {
self.clip_bottom(2).clip_relative(90, 90)
}
/// Returns a smaller `Rect` with width and height clipped to the given `percent_horizontal`
/// and `percent_vertical`.
///
/// Value of `percent_horizontal` and `percent_vertical` is from 0 to 100.
pub fn clip_relative(self, 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(self.width, percent_horizontal);
let inner_h = mul_and_cast(self.height, percent_vertical);
let offset_x = self.width.saturating_sub(inner_w) / 2;
let offset_y = self.height.saturating_sub(inner_h) / 2;
Rect {
x: self.x + offset_x,
y: self.y + offset_y,
width: inner_w,
height: inner_h,
}
}
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]

Loading…
Cancel
Save