|
|
@ -15,7 +15,16 @@ use helix_view::{
|
|
|
|
Editor,
|
|
|
|
Editor,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const MIN_HEIGHT: u16 = 4;
|
|
|
|
const MIN_HEIGHT: u16 = 6;
|
|
|
|
|
|
|
|
const MAX_HEIGHT: u16 = 26;
|
|
|
|
|
|
|
|
const MAX_WIDTH: u16 = 120;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct RenderInfo {
|
|
|
|
|
|
|
|
area: Rect,
|
|
|
|
|
|
|
|
child_height: u16,
|
|
|
|
|
|
|
|
render_borders: bool,
|
|
|
|
|
|
|
|
is_menu: bool,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return
|
|
|
|
// TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return
|
|
|
|
// a width/height hint. maybe Popup(Box<Component>)
|
|
|
|
// a width/height hint. maybe Popup(Box<Component>)
|
|
|
@ -23,7 +32,6 @@ const MIN_HEIGHT: u16 = 4;
|
|
|
|
pub struct Popup<T: Component> {
|
|
|
|
pub struct Popup<T: Component> {
|
|
|
|
contents: T,
|
|
|
|
contents: T,
|
|
|
|
position: Option<Position>,
|
|
|
|
position: Option<Position>,
|
|
|
|
margin: Margin,
|
|
|
|
|
|
|
|
area: Rect,
|
|
|
|
area: Rect,
|
|
|
|
position_bias: Open,
|
|
|
|
position_bias: Open,
|
|
|
|
scroll_half_pages: usize,
|
|
|
|
scroll_half_pages: usize,
|
|
|
@ -38,7 +46,6 @@ impl<T: Component> Popup<T> {
|
|
|
|
Self {
|
|
|
|
Self {
|
|
|
|
contents,
|
|
|
|
contents,
|
|
|
|
position: None,
|
|
|
|
position: None,
|
|
|
|
margin: Margin::none(),
|
|
|
|
|
|
|
|
position_bias: Open::Below,
|
|
|
|
position_bias: Open::Below,
|
|
|
|
area: Rect::new(0, 0, 0, 0),
|
|
|
|
area: Rect::new(0, 0, 0, 0),
|
|
|
|
scroll_half_pages: 0,
|
|
|
|
scroll_half_pages: 0,
|
|
|
@ -71,11 +78,6 @@ impl<T: Component> Popup<T> {
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn margin(mut self, margin: Margin) -> Self {
|
|
|
|
|
|
|
|
self.margin = margin;
|
|
|
|
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn auto_close(mut self, auto_close: bool) -> Self {
|
|
|
|
pub fn auto_close(mut self, auto_close: bool) -> Self {
|
|
|
|
self.auto_close = auto_close;
|
|
|
|
self.auto_close = auto_close;
|
|
|
|
self
|
|
|
|
self
|
|
|
@ -118,38 +120,32 @@ impl<T: Component> Popup<T> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect {
|
|
|
|
pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect {
|
|
|
|
let child_size = self
|
|
|
|
self.render_info(viewport, editor).area
|
|
|
|
.contents
|
|
|
|
|
|
|
|
.required_size((viewport.width, viewport.height))
|
|
|
|
|
|
|
|
.expect("Component needs required_size implemented in order to be embedded in a popup");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.area_internal(viewport, editor, child_size)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn area_internal(
|
|
|
|
fn render_info(&mut self, viewport: Rect, editor: &Editor) -> RenderInfo {
|
|
|
|
&mut self,
|
|
|
|
|
|
|
|
viewport: Rect,
|
|
|
|
|
|
|
|
editor: &Editor,
|
|
|
|
|
|
|
|
child_size: (u16, u16),
|
|
|
|
|
|
|
|
) -> Rect {
|
|
|
|
|
|
|
|
let width = child_size.0.min(viewport.width);
|
|
|
|
|
|
|
|
let height = child_size.1.min(viewport.height.saturating_sub(2)); // add some spacing in the viewport
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let position = self
|
|
|
|
let position = self
|
|
|
|
.position
|
|
|
|
.position
|
|
|
|
.get_or_insert_with(|| editor.cursor().0.unwrap_or_default());
|
|
|
|
.get_or_insert_with(|| editor.cursor().0.unwrap_or_default());
|
|
|
|
|
|
|
|
|
|
|
|
// if there's a orientation preference, use that
|
|
|
|
let is_menu = self
|
|
|
|
// if we're on the top part of the screen, do below
|
|
|
|
.contents
|
|
|
|
// if we're on the bottom part, do above
|
|
|
|
.type_name()
|
|
|
|
|
|
|
|
.starts_with("helix_term::ui::menu::Menu");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut render_borders = if is_menu {
|
|
|
|
|
|
|
|
editor.menu_border()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
editor.popup_border()
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// -- make sure frame doesn't stick out of bounds
|
|
|
|
// -- make sure frame doesn't stick out of bounds
|
|
|
|
let mut rel_x = position.col as u16;
|
|
|
|
let mut rel_x = position.col as u16;
|
|
|
|
let mut rel_y = position.row as u16;
|
|
|
|
let mut rel_y = position.row as u16;
|
|
|
|
if viewport.width <= rel_x + width {
|
|
|
|
|
|
|
|
rel_x = rel_x.saturating_sub((rel_x + width).saturating_sub(viewport.width));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if there's a orientation preference, use that
|
|
|
|
|
|
|
|
// if we're on the top part of the screen, do below
|
|
|
|
|
|
|
|
// if we're on the bottom part, do above
|
|
|
|
let can_put_below = viewport.height > rel_y + MIN_HEIGHT;
|
|
|
|
let can_put_below = viewport.height > rel_y + MIN_HEIGHT;
|
|
|
|
let can_put_above = rel_y.checked_sub(MIN_HEIGHT).is_some();
|
|
|
|
let can_put_above = rel_y.checked_sub(MIN_HEIGHT).is_some();
|
|
|
|
let final_pos = match self.position_bias {
|
|
|
|
let final_pos = match self.position_bias {
|
|
|
@ -163,7 +159,40 @@ impl<T: Component> Popup<T> {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
match final_pos {
|
|
|
|
// compute maximum space available for child
|
|
|
|
|
|
|
|
let mut max_height = match final_pos {
|
|
|
|
|
|
|
|
Open::Above => rel_y,
|
|
|
|
|
|
|
|
Open::Below => viewport.height.saturating_sub(1 + rel_y),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
max_height = max_height.min(MAX_HEIGHT);
|
|
|
|
|
|
|
|
let mut max_width = viewport.width.saturating_sub(2).min(MAX_WIDTH);
|
|
|
|
|
|
|
|
render_borders = render_borders && max_height > 3 && max_width > 3;
|
|
|
|
|
|
|
|
if render_borders {
|
|
|
|
|
|
|
|
max_width -= 2;
|
|
|
|
|
|
|
|
max_height -= 2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// compute required child size and reclamp
|
|
|
|
|
|
|
|
let (mut width, child_height) = self
|
|
|
|
|
|
|
|
.contents
|
|
|
|
|
|
|
|
.required_size((max_width, max_height))
|
|
|
|
|
|
|
|
.expect("Component needs required_size implemented in order to be embedded in a popup");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
width = width.min(MAX_WIDTH);
|
|
|
|
|
|
|
|
let height = if render_borders {
|
|
|
|
|
|
|
|
(child_height + 2).min(MAX_HEIGHT)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
child_height.min(MAX_HEIGHT)
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if render_borders {
|
|
|
|
|
|
|
|
width += 2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if viewport.width <= rel_x + width + 2 {
|
|
|
|
|
|
|
|
rel_x = viewport.width.saturating_sub(width + 2);
|
|
|
|
|
|
|
|
width = viewport.width.saturating_sub(rel_x + 2)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let area = match final_pos {
|
|
|
|
Open::Above => {
|
|
|
|
Open::Above => {
|
|
|
|
rel_y = rel_y.saturating_sub(height);
|
|
|
|
rel_y = rel_y.saturating_sub(height);
|
|
|
|
Rect::new(rel_x, rel_y, width, position.row as u16 - rel_y)
|
|
|
|
Rect::new(rel_x, rel_y, width, position.row as u16 - rel_y)
|
|
|
@ -173,6 +202,12 @@ impl<T: Component> Popup<T> {
|
|
|
|
let y_max = viewport.bottom().min(height + rel_y);
|
|
|
|
let y_max = viewport.bottom().min(height + rel_y);
|
|
|
|
Rect::new(rel_x, rel_y, width, y_max - rel_y)
|
|
|
|
Rect::new(rel_x, rel_y, width, y_max - rel_y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
RenderInfo {
|
|
|
|
|
|
|
|
area,
|
|
|
|
|
|
|
|
child_height,
|
|
|
|
|
|
|
|
render_borders,
|
|
|
|
|
|
|
|
is_menu,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -259,35 +294,16 @@ impl<T: Component> Component for Popup<T> {
|
|
|
|
// tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
|
|
|
|
// tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn required_size(&mut self, _viewport: (u16, u16)) -> Option<(u16, u16)> {
|
|
|
|
|
|
|
|
const MAX_WIDTH: u16 = 120;
|
|
|
|
|
|
|
|
const MAX_HEIGHT: u16 = 26;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let inner = Rect::new(0, 0, MAX_WIDTH, MAX_HEIGHT).inner(&self.margin);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let (width, height) = self
|
|
|
|
|
|
|
|
.contents
|
|
|
|
|
|
|
|
.required_size((inner.width, inner.height))
|
|
|
|
|
|
|
|
.expect("Component needs required_size implemented in order to be embedded in a popup");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let size = (
|
|
|
|
|
|
|
|
(width + self.margin.width()).min(MAX_WIDTH),
|
|
|
|
|
|
|
|
(height + self.margin.height()).min(MAX_HEIGHT),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Some(size)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
|
|
|
|
fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
|
|
|
|
let child_size = self
|
|
|
|
let RenderInfo {
|
|
|
|
.contents
|
|
|
|
area,
|
|
|
|
.required_size((viewport.width, viewport.height))
|
|
|
|
child_height,
|
|
|
|
.expect("Component needs required_size implemented in order to be embedded in a popup");
|
|
|
|
render_borders,
|
|
|
|
|
|
|
|
is_menu,
|
|
|
|
let area = self.area_internal(viewport, cx.editor, child_size);
|
|
|
|
} = self.render_info(viewport, cx.editor);
|
|
|
|
self.area = area;
|
|
|
|
self.area = area;
|
|
|
|
|
|
|
|
|
|
|
|
let max_offset = child_size.1.saturating_sub(area.height) as usize;
|
|
|
|
let max_offset = child_height.saturating_sub(area.height) as usize;
|
|
|
|
let half_page_size = (area.height / 2) as usize;
|
|
|
|
let half_page_size = (area.height / 2) as usize;
|
|
|
|
let scroll = max_offset.min(self.scroll_half_pages * half_page_size);
|
|
|
|
let scroll = max_offset.min(self.scroll_half_pages * half_page_size);
|
|
|
|
if half_page_size > 0 {
|
|
|
|
if half_page_size > 0 {
|
|
|
@ -296,32 +312,30 @@ impl<T: Component> Component for Popup<T> {
|
|
|
|
cx.scroll = Some(scroll);
|
|
|
|
cx.scroll = Some(scroll);
|
|
|
|
|
|
|
|
|
|
|
|
// clear area
|
|
|
|
// clear area
|
|
|
|
let background = cx.editor.theme.get("ui.popup");
|
|
|
|
let background = if is_menu {
|
|
|
|
surface.clear_with(area, background);
|
|
|
|
// TODO: consistently style menu
|
|
|
|
|
|
|
|
cx.editor
|
|
|
|
let render_borders = cx.editor.popup_border();
|
|
|
|
.theme
|
|
|
|
|
|
|
|
.try_get("ui.menu")
|
|
|
|
let inner = if self
|
|
|
|
.unwrap_or_else(|| cx.editor.theme.get("ui.text"))
|
|
|
|
.contents
|
|
|
|
|
|
|
|
.type_name()
|
|
|
|
|
|
|
|
.starts_with("helix_term::ui::menu::Menu")
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
area
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
area.inner(&self.margin)
|
|
|
|
cx.editor.theme.get("ui.popup")
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
surface.clear_with(area, background);
|
|
|
|
|
|
|
|
|
|
|
|
let border = usize::from(render_borders);
|
|
|
|
let mut inner = area;
|
|
|
|
if render_borders {
|
|
|
|
if render_borders {
|
|
|
|
|
|
|
|
inner = area.inner(&Margin::all(1));
|
|
|
|
Widget::render(Block::default().borders(Borders::ALL), area, surface);
|
|
|
|
Widget::render(Block::default().borders(Borders::ALL), area, surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let border = usize::from(render_borders);
|
|
|
|
|
|
|
|
|
|
|
|
self.contents.render(inner, surface, cx);
|
|
|
|
self.contents.render(inner, surface, cx);
|
|
|
|
|
|
|
|
|
|
|
|
// render scrollbar if contents do not fit
|
|
|
|
// render scrollbar if contents do not fit
|
|
|
|
if self.has_scrollbar {
|
|
|
|
if self.has_scrollbar {
|
|
|
|
let win_height = (inner.height as usize).saturating_sub(2 * border);
|
|
|
|
let win_height = (inner.height as usize).saturating_sub(2 * border);
|
|
|
|
let len = (child_size.1 as usize).saturating_sub(2 * border);
|
|
|
|
let len = (child_height as usize).saturating_sub(2 * border);
|
|
|
|
let fits = len <= win_height;
|
|
|
|
let fits = len <= win_height;
|
|
|
|
let scroll_style = cx.editor.theme.get("ui.menu.scroll");
|
|
|
|
let scroll_style = cx.editor.theme.get("ui.menu.scroll");
|
|
|
|
|
|
|
|
|
|
|
|