2021-02-25 10:07:47 +01:00
|
|
|
use crate::compositor::{Component, Compositor, Context, EventResult};
|
|
|
|
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
|
|
|
use tui::{
|
2021-03-22 04:40:07 +01:00
|
|
|
buffer::Buffer as Surface,
|
2021-02-25 10:07:47 +01:00
|
|
|
layout::Rect,
|
|
|
|
style::{Color, Style},
|
|
|
|
widgets::{Block, Borders},
|
|
|
|
};
|
|
|
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
use helix_core::Position;
|
|
|
|
use helix_view::Editor;
|
|
|
|
|
2021-03-02 09:58:15 +01: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 04:06:40 +01:00
|
|
|
pub struct Popup<T: Component> {
|
|
|
|
contents: T,
|
2021-03-02 10:24:24 +01:00
|
|
|
position: Option<Position>,
|
2021-03-08 09:00:32 +01:00
|
|
|
size: (u16, u16),
|
2021-03-08 09:32:13 +01:00
|
|
|
scroll: usize,
|
2021-02-25 10:07:47 +01:00
|
|
|
}
|
|
|
|
|
2021-03-27 04:06:40 +01:00
|
|
|
impl<T: Component> Popup<T> {
|
2021-02-25 10:07:47 +01:00
|
|
|
// TODO: it's like a slimmed down picker, share code? (picker = menu + prompt with different
|
|
|
|
// rendering)
|
2021-03-27 04:06:40 +01:00
|
|
|
pub fn new(contents: T) -> Self {
|
2021-02-25 10:07:47 +01:00
|
|
|
Self {
|
|
|
|
contents,
|
2021-03-02 10:24:24 +01:00
|
|
|
position: None,
|
2021-03-08 09:00:32 +01:00
|
|
|
size: (0, 0),
|
2021-03-08 09:32:13 +01:00
|
|
|
scroll: 0,
|
2021-02-25 10:07:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:24:24 +01:00
|
|
|
pub fn set_position(&mut self, pos: Option<Position>) {
|
2021-02-25 10:07:47 +01:00
|
|
|
self.position = pos;
|
|
|
|
}
|
2021-03-08 09:32:13 +01:00
|
|
|
|
|
|
|
pub fn scroll(&mut self, offset: usize, direction: bool) {
|
|
|
|
if direction {
|
|
|
|
self.scroll += offset;
|
|
|
|
} else {
|
|
|
|
self.scroll = self.scroll.saturating_sub(offset);
|
|
|
|
}
|
|
|
|
}
|
2021-03-27 04:06:40 +01:00
|
|
|
|
2021-04-05 11:23:37 +02:00
|
|
|
pub fn contents(&self) -> &T {
|
|
|
|
&self.contents
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contents_mut(&mut self) -> &mut T {
|
2021-03-27 04:06:40 +01:00
|
|
|
&mut self.contents
|
|
|
|
}
|
2021-02-25 10:07:47 +01:00
|
|
|
}
|
|
|
|
|
2021-03-27 04:06:40 +01:00
|
|
|
impl<T: Component> Component for Popup<T> {
|
2021-02-25 10:07:47 +01:00
|
|
|
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
2021-03-02 10:24:24 +01:00
|
|
|
let key = match event {
|
2021-02-25 10:07:47 +01:00
|
|
|
Event::Key(event) => event,
|
2021-03-03 09:28:50 +01:00
|
|
|
Event::Resize(width, height) => {
|
|
|
|
// TODO: calculate inner area, call component's handle_event with that area
|
|
|
|
return EventResult::Ignored;
|
|
|
|
}
|
2021-02-25 10:07:47 +01:00
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
};
|
|
|
|
|
|
|
|
let close_fn = EventResult::Consumed(Some(Box::new(
|
|
|
|
|compositor: &mut Compositor, editor: &mut Editor| {
|
|
|
|
// remove the layer
|
|
|
|
compositor.pop();
|
|
|
|
},
|
|
|
|
)));
|
|
|
|
|
2021-03-02 10:24:24 +01:00
|
|
|
match key {
|
2021-02-25 10:07:47 +01:00
|
|
|
// esc or ctrl-c aborts the completion and closes the menu
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Esc, ..
|
|
|
|
}
|
|
|
|
| KeyEvent {
|
|
|
|
code: KeyCode::Char('c'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
2021-03-02 10:25:40 +01:00
|
|
|
} => close_fn,
|
2021-03-08 09:32:13 +01:00
|
|
|
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('d'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => {
|
|
|
|
self.scroll(self.size.1 as usize / 2, true);
|
2021-03-11 08:15:54 +01:00
|
|
|
EventResult::Consumed(None)
|
2021-03-08 09:32:13 +01:00
|
|
|
}
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('u'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => {
|
|
|
|
self.scroll(self.size.1 as usize / 2, false);
|
2021-03-11 08:15:54 +01:00
|
|
|
EventResult::Consumed(None)
|
2021-03-08 09:32:13 +01:00
|
|
|
}
|
2021-03-02 10:24:24 +01:00
|
|
|
_ => self.contents.handle_event(event, cx),
|
2021-02-25 10:07:47 +01: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 09:28:50 +01:00
|
|
|
|
2021-03-08 09:00:32 +01:00
|
|
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
|
|
|
let (width, height) = self
|
|
|
|
.contents
|
|
|
|
.required_size((120, 26)) // max width, max height
|
|
|
|
.expect("Component needs required_size implemented in order to be embedded in a popup");
|
|
|
|
|
|
|
|
self.size = (width, height);
|
|
|
|
|
|
|
|
Some(self.size)
|
|
|
|
}
|
|
|
|
|
2021-03-02 09:58:15 +01:00
|
|
|
fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
|
2021-03-22 04:40:07 +01:00
|
|
|
use tui::{
|
|
|
|
text::Text,
|
|
|
|
widgets::{Paragraph, Widget, Wrap},
|
|
|
|
};
|
2021-03-02 09:58:15 +01:00
|
|
|
|
2021-03-08 09:32:13 +01:00
|
|
|
cx.scroll = Some(self.scroll);
|
|
|
|
|
2021-03-02 10:24:24 +01:00
|
|
|
let position = self
|
|
|
|
.position
|
|
|
|
.or_else(|| cx.editor.cursor_position())
|
|
|
|
.unwrap_or_default();
|
2021-03-02 09:58:15 +01:00
|
|
|
|
2021-03-08 09:00:32 +01:00
|
|
|
let (width, height) = self.size;
|
2021-03-02 09:58:15 +01:00
|
|
|
|
|
|
|
// -- make sure frame doesn't stick out of bounds
|
2021-03-02 10:24:24 +01:00
|
|
|
let mut rel_x = position.col as u16;
|
|
|
|
let mut rel_y = position.row as u16;
|
2021-03-02 09:58:15 +01:00
|
|
|
if viewport.width <= rel_x + width {
|
|
|
|
rel_x -= ((rel_x + width) - viewport.width)
|
|
|
|
};
|
|
|
|
|
2021-03-02 10:24:24 +01:00
|
|
|
// TODO: be able to specify orientation preference. We want above for most popups, below
|
|
|
|
// for menus/autocomplete.
|
2021-03-02 09:58:15 +01:00
|
|
|
if height <= rel_y {
|
|
|
|
rel_y -= height // position above point
|
|
|
|
} else {
|
|
|
|
rel_y += 1 // position below point
|
|
|
|
}
|
|
|
|
|
|
|
|
let area = Rect::new(rel_x, rel_y, width, height);
|
|
|
|
// clip to viewport
|
|
|
|
let area = viewport.intersection(Rect::new(rel_x, rel_y, width, height));
|
2021-02-25 10:07:47 +01:00
|
|
|
|
|
|
|
// clear area
|
|
|
|
let background = cx.editor.theme.get("ui.popup");
|
|
|
|
for y in area.top()..area.bottom() {
|
|
|
|
for x in area.left()..area.right() {
|
|
|
|
let cell = surface.get_mut(x, y);
|
|
|
|
cell.reset();
|
|
|
|
// cell.symbol.clear();
|
|
|
|
cell.set_style(background);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:24:24 +01:00
|
|
|
self.contents.render(area, surface, cx);
|
2021-02-25 10:07:47 +01:00
|
|
|
}
|
|
|
|
}
|