@@ -12,6 +12,9 @@ use std::borrow::Cow;
12use helix_core::Position;
12use helix_core::Position;
13use helix_view::Editor;
13use helix_view::Editor;
15+// TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return
16+// a width/height hint. maybe Popup(Box<Component>)
15pub struct Popup {
18pub struct Popup {
16 contents: String,
19 contents: String,
17 position: Position,
20 position: Position,
@@ -64,11 +67,31 @@ impl Component for Popup {
64 // EventResult::Consumed(None)
67 // EventResult::Consumed(None)
65 EventResult::Consumed(None)
68 EventResult::Consumed(None)
67− fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
70+ fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
68− // render a box at x, y. Width equal to max width of item.
71+ use tui::text::Text;
69− const MAX: usize = 15;
72+ use tui::widgets::{Paragraph, Widget, Wrap};
70− let rows = std::cmp::min(self.contents.lines().count(), MAX) as u16; // inefficient
73+
74+ let contents = Text::from(self.contents.clone());
76+ let width = contents.width().min(150) as u16;
77+ let height = contents.height().min(13) as u16;
79+ // -- make sure frame doesn't stick out of bounds
71− let area = Rect::new(self.position.col as u16, self.position.row as u16, 80, rows);
80+ let mut rel_x = self.position.col as u16;
81+ let mut rel_y = self.position.row as u16;
82+ if viewport.width <= rel_x + width {
83+ rel_x -= ((rel_x + width) - viewport.width)
87+ rel_y -= height // position above point
89+ rel_y += 1 // position below point
92+ let area = Rect::new(rel_x, rel_y, width, height);
94+ let area = viewport.intersection(Rect::new(rel_x, rel_y, width, height));
73 // clear area
96 // clear area
74 let background = cx.editor.theme.get("ui.popup");
97 let background = cx.editor.theme.get("ui.popup");
@@ -85,10 +108,8 @@ impl Component for Popup {
86 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
109 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
89− use tui::widgets::{Paragraph, Widget, Wrap};
90− let contents = Text::from(self.contents.clone());
91 let par = Paragraph::new(contents).wrap(Wrap { trim: false });
111 let par = Paragraph::new(contents).wrap(Wrap { trim: false });
112+ // .scroll(x, y) offsets
93 par.render(area, surface);
114 par.render(area, surface);