2021-11-10 21:28:46 +05:30
|
|
|
use crate::{
|
2022-02-23 04:46:12 +01:00
|
|
|
compositor::{Callback, Component, Context, EventResult},
|
2021-11-10 21:28:46 +05:30
|
|
|
ctrl, key,
|
|
|
|
};
|
|
|
|
use crossterm::event::Event;
|
2021-06-24 20:58:15 -07:00
|
|
|
use tui::buffer::Buffer as Surface;
|
2021-02-25 18:07:47 +09:00
|
|
|
|
|
|
|
use helix_core::Position;
|
2022-01-31 13:42:32 +09:00
|
|
|
use helix_view::graphics::{Margin, Rect};
|
2021-02-25 18:07:47 +09:00
|
|
|
|
2021-03-02 17:58:15 +09:00
|
|
|
// 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>)
|
|
|
|
|
2021-03-27 12:06:40 +09:00
|
|
|
pub struct Popup<T: Component> {
|
|
|
|
contents: T,
|
2021-03-02 18:24:24 +09:00
|
|
|
position: Option<Position>,
|
2022-01-31 13:42:32 +09:00
|
|
|
margin: Margin,
|
2021-03-08 17:00:32 +09:00
|
|
|
size: (u16, u16),
|
2021-12-10 19:23:34 +09:00
|
|
|
child_size: (u16, u16),
|
2021-03-08 17:32:13 +09:00
|
|
|
scroll: usize,
|
2022-02-23 04:46:12 +01:00
|
|
|
auto_close: bool,
|
2021-12-08 02:11:18 -05:00
|
|
|
id: &'static str,
|
2021-02-25 18:07:47 +09:00
|
|
|
}
|
|
|
|
|
2021-03-27 12:06:40 +09:00
|
|
|
impl<T: Component> Popup<T> {
|
2021-12-08 02:11:18 -05:00
|
|
|
pub fn new(id: &'static str, contents: T) -> Self {
|
2021-02-25 18:07:47 +09:00
|
|
|
Self {
|
|
|
|
contents,
|
2021-03-02 18:24:24 +09:00
|
|
|
position: None,
|
2022-01-31 13:42:32 +09:00
|
|
|
margin: Margin {
|
|
|
|
vertical: 0,
|
|
|
|
horizontal: 0,
|
|
|
|
},
|
2021-03-08 17:00:32 +09:00
|
|
|
size: (0, 0),
|
2021-12-10 19:23:34 +09:00
|
|
|
child_size: (0, 0),
|
2021-03-08 17:32:13 +09:00
|
|
|
scroll: 0,
|
2022-02-23 04:46:12 +01:00
|
|
|
auto_close: false,
|
2021-12-08 02:11:18 -05:00
|
|
|
id,
|
2021-02-25 18:07:47 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 18:24:24 +09:00
|
|
|
pub fn set_position(&mut self, pos: Option<Position>) {
|
2021-02-25 18:07:47 +09:00
|
|
|
self.position = pos;
|
|
|
|
}
|
2021-03-08 17:32:13 +09:00
|
|
|
|
2022-01-31 13:42:32 +09:00
|
|
|
pub fn margin(mut self, margin: Margin) -> Self {
|
|
|
|
self.margin = margin;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-02-23 04:46:12 +01:00
|
|
|
pub fn auto_close(mut self, auto_close: bool) -> Self {
|
|
|
|
self.auto_close = auto_close;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-09-08 07:33:59 +00:00
|
|
|
pub fn get_rel_position(&mut self, viewport: Rect, cx: &Context) -> (u16, u16) {
|
|
|
|
let position = self
|
|
|
|
.position
|
|
|
|
.get_or_insert_with(|| cx.editor.cursor().0.unwrap_or_default());
|
|
|
|
|
|
|
|
let (width, height) = self.size;
|
|
|
|
|
2021-09-09 12:35:14 +09:00
|
|
|
// 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
|
|
|
|
|
2021-09-08 07:33:59 +00:00
|
|
|
// -- make sure frame doesn't stick out of bounds
|
|
|
|
let mut rel_x = position.col as u16;
|
2021-09-09 12:35:14 +09:00
|
|
|
let mut rel_y = position.row as u16;
|
2021-09-08 07:33:59 +00:00
|
|
|
if viewport.width <= rel_x + width {
|
|
|
|
rel_x = rel_x.saturating_sub((rel_x + width).saturating_sub(viewport.width));
|
2021-09-09 12:35:14 +09:00
|
|
|
}
|
2021-09-08 07:33:59 +00:00
|
|
|
|
|
|
|
// TODO: be able to specify orientation preference. We want above for most popups, below
|
|
|
|
// for menus/autocomplete.
|
2021-09-09 12:35:14 +09:00
|
|
|
if viewport.height > rel_y + height {
|
|
|
|
rel_y += 1 // position below point
|
2021-09-08 07:33:59 +00:00
|
|
|
} else {
|
2021-09-09 12:35:14 +09:00
|
|
|
rel_y = rel_y.saturating_sub(height) // position above point
|
2021-09-08 07:33:59 +00:00
|
|
|
}
|
2021-09-09 12:35:14 +09:00
|
|
|
|
|
|
|
(rel_x, rel_y)
|
2021-09-08 07:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_size(&self) -> (u16, u16) {
|
|
|
|
(self.size.0, self.size.1)
|
|
|
|
}
|
|
|
|
|
2021-03-08 17:32:13 +09:00
|
|
|
pub fn scroll(&mut self, offset: usize, direction: bool) {
|
|
|
|
if direction {
|
|
|
|
self.scroll += offset;
|
2021-12-10 19:23:34 +09:00
|
|
|
|
|
|
|
let max_offset = self.child_size.1.saturating_sub(self.size.1);
|
|
|
|
self.scroll = (self.scroll + offset).min(max_offset as usize);
|
2021-03-08 17:32:13 +09:00
|
|
|
} else {
|
|
|
|
self.scroll = self.scroll.saturating_sub(offset);
|
|
|
|
}
|
|
|
|
}
|
2021-03-27 12:06:40 +09:00
|
|
|
|
2021-04-05 18:23:37 +09:00
|
|
|
pub fn contents(&self) -> &T {
|
|
|
|
&self.contents
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contents_mut(&mut self) -> &mut T {
|
2021-03-27 12:06:40 +09:00
|
|
|
&mut self.contents
|
|
|
|
}
|
2021-02-25 18:07:47 +09:00
|
|
|
}
|
|
|
|
|
2021-03-27 12:06:40 +09:00
|
|
|
impl<T: Component> Component for Popup<T> {
|
2021-02-25 18:07:47 +09:00
|
|
|
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
2021-03-02 18:24:24 +09:00
|
|
|
let key = match event {
|
2021-02-25 18:07:47 +09:00
|
|
|
Event::Key(event) => event,
|
2021-07-01 11:57:12 -07:00
|
|
|
Event::Resize(_, _) => {
|
2021-03-03 17:28:50 +09:00
|
|
|
// TODO: calculate inner area, call component's handle_event with that area
|
2022-02-23 04:46:12 +01:00
|
|
|
return EventResult::Ignored(None);
|
2021-03-03 17:28:50 +09:00
|
|
|
}
|
2022-02-23 04:46:12 +01:00
|
|
|
_ => return EventResult::Ignored(None),
|
2021-02-25 18:07:47 +09:00
|
|
|
};
|
|
|
|
|
2022-02-23 04:46:12 +01:00
|
|
|
let close_fn: Callback = Box::new(|compositor, _| {
|
2021-05-09 18:02:31 +09:00
|
|
|
// remove the layer
|
|
|
|
compositor.pop();
|
2022-02-23 04:46:12 +01:00
|
|
|
});
|
2021-02-25 18:07:47 +09:00
|
|
|
|
2021-11-10 21:28:46 +05:30
|
|
|
match key.into() {
|
2021-02-25 18:07:47 +09:00
|
|
|
// esc or ctrl-c aborts the completion and closes the menu
|
2022-02-23 04:46:12 +01:00
|
|
|
key!(Esc) | ctrl!('c') => EventResult::Consumed(Some(close_fn)),
|
2021-11-10 21:28:46 +05:30
|
|
|
ctrl!('d') => {
|
2021-03-08 17:32:13 +09:00
|
|
|
self.scroll(self.size.1 as usize / 2, true);
|
2021-03-11 16:15:54 +09:00
|
|
|
EventResult::Consumed(None)
|
2021-03-08 17:32:13 +09:00
|
|
|
}
|
2021-11-10 21:28:46 +05:30
|
|
|
ctrl!('u') => {
|
2021-03-08 17:32:13 +09:00
|
|
|
self.scroll(self.size.1 as usize / 2, false);
|
2021-03-11 16:15:54 +09:00
|
|
|
EventResult::Consumed(None)
|
2021-03-08 17:32:13 +09:00
|
|
|
}
|
2022-02-23 04:46:12 +01:00
|
|
|
_ => {
|
|
|
|
let contents_event_result = self.contents.handle_event(event, cx);
|
|
|
|
|
|
|
|
if self.auto_close {
|
|
|
|
if let EventResult::Ignored(None) = contents_event_result {
|
|
|
|
return EventResult::Ignored(Some(close_fn));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
contents_event_result
|
|
|
|
}
|
2021-02-25 18:07:47 +09:00
|
|
|
}
|
|
|
|
// for some events, we want to process them but send ignore, specifically all input except
|
|
|
|
// tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
|
|
|
|
}
|
2021-03-03 17:28:50 +09:00
|
|
|
|
2021-12-10 19:23:34 +09:00
|
|
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
|
|
|
let max_width = 120.min(viewport.0);
|
|
|
|
let max_height = 26.min(viewport.1.saturating_sub(2)); // add some spacing in the viewport
|
|
|
|
|
2022-01-31 13:42:32 +09:00
|
|
|
let inner = Rect::new(0, 0, max_width, max_height).inner(&self.margin);
|
|
|
|
|
2021-03-08 17:00:32 +09:00
|
|
|
let (width, height) = self
|
|
|
|
.contents
|
2022-01-31 13:42:32 +09:00
|
|
|
.required_size((inner.width, inner.height))
|
2021-03-08 17:00:32 +09:00
|
|
|
.expect("Component needs required_size implemented in order to be embedded in a popup");
|
|
|
|
|
2021-12-10 19:23:34 +09:00
|
|
|
self.child_size = (width, height);
|
2022-01-31 13:42:32 +09:00
|
|
|
self.size = (
|
|
|
|
(width + self.margin.horizontal * 2).min(max_width),
|
|
|
|
(height + self.margin.vertical * 2).min(max_height),
|
|
|
|
);
|
2021-12-10 19:23:34 +09:00
|
|
|
|
|
|
|
// re-clamp scroll offset
|
|
|
|
let max_offset = self.child_size.1.saturating_sub(self.size.1);
|
|
|
|
self.scroll = self.scroll.min(max_offset as usize);
|
2021-03-08 17:00:32 +09:00
|
|
|
|
|
|
|
Some(self.size)
|
|
|
|
}
|
|
|
|
|
2021-08-12 12:30:42 +05:30
|
|
|
fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
|
2021-09-09 12:35:14 +09:00
|
|
|
// trigger required_size so we recalculate if the child changed
|
|
|
|
self.required_size((viewport.width, viewport.height));
|
|
|
|
|
2021-03-08 17:32:13 +09:00
|
|
|
cx.scroll = Some(self.scroll);
|
|
|
|
|
2021-09-08 07:33:59 +00:00
|
|
|
let (rel_x, rel_y) = self.get_rel_position(viewport, cx);
|
2021-03-02 17:58:15 +09:00
|
|
|
|
|
|
|
// clip to viewport
|
2021-09-08 07:33:59 +00:00
|
|
|
let area = viewport.intersection(Rect::new(rel_x, rel_y, self.size.0, self.size.1));
|
2021-02-25 18:07:47 +09:00
|
|
|
|
|
|
|
// clear area
|
|
|
|
let background = cx.editor.theme.get("ui.popup");
|
2021-05-09 18:13:50 +09:00
|
|
|
surface.clear_with(area, background);
|
2021-02-25 18:07:47 +09:00
|
|
|
|
2022-01-31 13:42:32 +09:00
|
|
|
let inner = area.inner(&self.margin);
|
|
|
|
self.contents.render(inner, surface, cx);
|
2021-02-25 18:07:47 +09:00
|
|
|
}
|
2021-12-08 02:11:18 -05:00
|
|
|
|
|
|
|
fn id(&self) -> Option<&'static str> {
|
|
|
|
Some(self.id)
|
|
|
|
}
|
2021-02-25 18:07:47 +09:00
|
|
|
}
|