@@ -16,28 +16,28 @@ use helix_view::Editor;
1616// a width/height hint. maybe Popup(Box<Component>)
1717
1818pub struct Popup {
19− contents: String,
20− position: Position,
19+ contents: Box<dyn Component>,
20+ position: Option<Position>,
2121}
2222
2323impl Popup {
2424 // TODO: it's like a slimmed down picker, share code? (picker = menu + prompt with different
2525 // rendering)
26− pub fn new(contents: String) -> Self {
26+ pub fn new(contents: Box<dyn Component>) -> Self {
2727 Self {
2828 contents,
29− position: Position::default(),
29+ position: None,
3030 }
3131 }
3232
33− pub fn set_position(&mut self, pos: Position) {
33+ pub fn set_position(&mut self, pos: Option<Position>) {
3434 self.position = pos;
3535 }
3636}
3737
3838impl Component for Popup {
3939 fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
40− let event = match event {
40+ let key = match event {
4141 Event::Key(event) => event,
4242 _ => return EventResult::Ignored,
4343 };
@@ -49,7 +49,7 @@ impl Component for Popup {
4949 },
5050 )));
5151
52− match event {
52+ match key {
5353 // esc or ctrl-c aborts the completion and closes the menu
5454 KeyEvent {
5555 code: KeyCode::Esc, ..
@@ -60,29 +60,37 @@ impl Component for Popup {
6060 } => {
6161 return close_fn;
6262 }
63− _ => (),
63+ _ => self.contents.handle_event(event, cx),
6464 }
6565 // for some events, we want to process them but send ignore, specifically all input except
6666 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
67− // EventResult::Consumed(None)
68− EventResult::Consumed(None)
6967 }
7068 fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
7169 use tui::text::Text;
7270 use tui::widgets::{Paragraph, Widget, Wrap};
7371
74− let contents = Text::from(self.contents.clone());
72+ let position = self
73+ .position
74+ .or_else(|| cx.editor.cursor_position())
75+ .unwrap_or_default();
7576
76− let width = contents.width().min(150) as u16;
77− let height = contents.height().min(13) as u16;
77+ let (width, height) = self
78+ .contents
79+ .size_hint(viewport)
80+ .expect("Component needs size_hint implemented in order to be embedded in a popup");
81+
82+ let width = width.min(150) as u16;
83+ let height = height.min(13) as u16;
7884
7985 // -- make sure frame doesn't stick out of bounds
80− let mut rel_x = self.position.col as u16;
81− let mut rel_y = self.position.row as u16;
86+ let mut rel_x = position.col as u16;
87+ let mut rel_y = position.row as u16;
8288 if viewport.width <= rel_x + width {
8389 rel_x -= ((rel_x + width) - viewport.width)
8490 };
8591
92+ // TODO: be able to specify orientation preference. We want above for most popups, below
93+ // for menus/autocomplete.
8694 if height <= rel_y {
8795 rel_y -= height // position above point
8896 } else {
@@ -104,13 +112,6 @@ impl Component for Popup {
104112 }
105113 }
106114
107− // -- Render the contents:
108−
109− let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
110−
111− let par = Paragraph::new(contents).wrap(Wrap { trim: false });
112− // .scroll(x, y) offsets
113−
114− par.render(area, surface);
115+ self.contents.render(area, surface, cx);
115116 }
116117}