on 2158366 help sign in
141 lines 4.5 kB history raw
1use crate::compositor::{Component, Compositor, Context, EventResult};
2use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
3use tui::buffer::Buffer as Surface;
5use helix_core::Position;
6use helix_view::graphics::Rect;
8// TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return
9// a width/height hint. maybe Popup(Box<Component>)
11pub struct Popup<T: Component> {
12 contents: T,
13 position: Option<Position>,
14 size: (u16, u16),
15 scroll: usize,
18impl<T: Component> Popup<T> {
19 // TODO: it's like a slimmed down picker, share code? (picker = menu + prompt with different
20 // rendering)
21 pub fn new(contents: T) -> Self {
22 Self {
23 contents,
24 position: None,
25 size: (0, 0),
26 scroll: 0,
27 }
28 }
30 pub fn set_position(&mut self, pos: Option<Position>) {
31 self.position = pos;
32 }
34 pub fn scroll(&mut self, offset: usize, direction: bool) {
35 if direction {
36 self.scroll += offset;
37 } else {
38 self.scroll = self.scroll.saturating_sub(offset);
39 }
40 }
42 pub fn contents(&self) -> &T {
43 &self.contents
44 }
46 pub fn contents_mut(&mut self) -> &mut T {
47 &mut self.contents
48 }
51impl<T: Component> Component for Popup<T> {
52 fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
53 let key = match event {
54 Event::Key(event) => event,
55 Event::Resize(_, _) => {
56 // TODO: calculate inner area, call component's handle_event with that area
57 return EventResult::Ignored;
58 }
59 _ => return EventResult::Ignored,
60 };
62 let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| {
63 // remove the layer
64 compositor.pop();
65 })));
67 match key {
68 // esc or ctrl-c aborts the completion and closes the menu
69 KeyEvent {
70 code: KeyCode::Esc, ..
71 }
72 | KeyEvent {
73 code: KeyCode::Char('c'),
74 modifiers: KeyModifiers::CONTROL,
75 } => close_fn,
77 KeyEvent {
78 code: KeyCode::Char('d'),
79 modifiers: KeyModifiers::CONTROL,
80 } => {
81 self.scroll(self.size.1 as usize / 2, true);
82 EventResult::Consumed(None)
83 }
84 KeyEvent {
85 code: KeyCode::Char('u'),
86 modifiers: KeyModifiers::CONTROL,
87 } => {
88 self.scroll(self.size.1 as usize / 2, false);
89 EventResult::Consumed(None)
90 }
91 _ => self.contents.handle_event(event, cx),
92 }
93 // for some events, we want to process them but send ignore, specifically all input except
94 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
95 }
97 fn required_size(&mut self, _viewport: (u16, u16)) -> Option<(u16, u16)> {
98 let (width, height) = self
99 .contents
100 .required_size((120, 26)) // max width, max height
101 .expect("Component needs required_size implemented in order to be embedded in a popup");
103 self.size = (width, height);
105 Some(self.size)
106 }
108 fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
109 cx.scroll = Some(self.scroll);
111 let position = self
112 .position
113 .get_or_insert_with(|| cx.editor.cursor().0.unwrap_or_default());
115 let (width, height) = self.size;
117 // -- make sure frame doesn't stick out of bounds
118 let mut rel_x = position.col as u16;
119 let mut rel_y = position.row as u16;
120 if viewport.width <= rel_x + width {
121 rel_x = rel_x.saturating_sub((rel_x + width).saturating_sub(viewport.width));
122 };
124 // TODO: be able to specify orientation preference. We want above for most popups, below
125 // for menus/autocomplete.
126 if height <= rel_y {
127 rel_y = rel_y.saturating_sub(height) // position above point
128 } else {
129 rel_y += 1 // position below point
130 }
132 // clip to viewport
133 let area = viewport.intersection(Rect::new(rel_x, rel_y, width, height));
135 // clear area
136 let background = cx.editor.theme.get("ui.popup");
137 surface.clear_with(area, background);
139 self.contents.render(area, surface, cx);
140 }