on master help sign in
ui: Calculate popup rendering and sizing.
commit11c4e0b05325d8fd8046a764ec4b9e655486313d authorBlaž Hrastnik <blaz@mxxn.io> dateTue 2 Mar 2021, 17:58 (6 years ago) parent8f4ff4c
2 files changed+29−9
helix-term/src/commands.rs−1 helix-term/src/ui/popup.rs+29−8
helix-term/src/commands.rs−1
@@ -1181,7 +1181,6 @@ pub fn hover(cx: &mut Context) {
11811181 cx.callback = Some(Box::new(
11821182 move |compositor: &mut Compositor, editor: &mut Editor| {
11831183 if let Some(mut pos) = editor.cursor_position() {
1184− pos.row += 1; // shift down by one row
11851184 popup.set_position(pos);
11861185 };
11871186
helix-term/src/ui/popup.rs+29−8
@@ -12,6 +12,9 @@ use std::borrow::Cow;
1212use helix_core::Position;
1313use helix_view::Editor;
1414
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>)
17+
1518pub struct Popup {
1619 contents: String,
1720 position: Position,
@@ -64,11 +67,31 @@ impl Component for Popup {
6467 // EventResult::Consumed(None)
6568 EventResult::Consumed(None)
6669 }
67− fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
68− // render a box at x, y. Width equal to max width of item.
69− const MAX: usize = 15;
70− let rows = std::cmp::min(self.contents.lines().count(), MAX) as u16; // inefficient
71− let area = Rect::new(self.position.col as u16, self.position.row as u16, 80, rows);
70+ fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
71+ use tui::text::Text;
72+ use tui::widgets::{Paragraph, Widget, Wrap};
73+
74+ let contents = Text::from(self.contents.clone());
75+
76+ let width = contents.width().min(150) as u16;
77+ let height = contents.height().min(13) as u16;
78+
79+ // -- 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;
82+ if viewport.width <= rel_x + width {
83+ rel_x -= ((rel_x + width) - viewport.width)
84+ };
85+
86+ if height <= rel_y {
87+ rel_y -= height // position above point
88+ } else {
89+ rel_y += 1 // position below point
90+ }
91+
92+ let area = Rect::new(rel_x, rel_y, width, height);
93+ // clip to viewport
94+ let area = viewport.intersection(Rect::new(rel_x, rel_y, width, height));
7295
7396 // clear area
7497 let background = cx.editor.theme.get("ui.popup");
@@ -85,10 +108,8 @@ impl Component for Popup {
85108
86109 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
87110
88− use tui::text::Text;
89− use tui::widgets::{Paragraph, Widget, Wrap};
90− let contents = Text::from(self.contents.clone());
91111 let par = Paragraph::new(contents).wrap(Wrap { trim: false });
112+ // .scroll(x, y) offsets
92113
93114 par.render(area, surface);
94115 }