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> {
13 position: Option<Position>,
18impl<T: Component> Popup<T> {
19 // TODO: it's like a slimmed down picker, share code? (picker = menu + prompt with different
21 pub fn new(contents: T) -> Self {
30 pub fn set_position(&mut self, pos: Option<Position>) {
34 pub fn scroll(&mut self, offset: usize, direction: bool) {
36 self.scroll += offset;
38 self.scroll = self.scroll.saturating_sub(offset);
42 pub fn contents(&self) -> &T {
46 pub fn contents_mut(&mut self) -> &mut T {
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;
59 _ => return EventResult::Ignored,
62 let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| {
68 // esc or ctrl-c aborts the completion and closes the menu
70 code: KeyCode::Esc, ..
73 code: KeyCode::Char('c'),
74 modifiers: KeyModifiers::CONTROL,
78 code: KeyCode::Char('d'),
79 modifiers: KeyModifiers::CONTROL,
81 self.scroll(self.size.1 as usize / 2, true);
82 EventResult::Consumed(None)
85 code: KeyCode::Char('u'),
86 modifiers: KeyModifiers::CONTROL,
88 self.scroll(self.size.1 as usize / 2, false);
89 EventResult::Consumed(None)
91 _ => self.contents.handle_event(event, cx),
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.
97 fn required_size(&mut self, _viewport: (u16, u16)) -> Option<(u16, u16)> {
98 let (width, height) = self
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);
108 fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
109 cx.scroll = Some(self.scroll);
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));
124 // TODO: be able to specify orientation preference. We want above for most popups, below
125 // for menus/autocomplete.
127 rel_y = rel_y.saturating_sub(height) // position above point
129 rel_y += 1 // position below point
133 let area = viewport.intersection(Rect::new(rel_x, rel_y, width, height));
136 let background = cx.editor.theme.get("ui.popup");
137 surface.clear_with(area, background);
139 self.contents.render(area, surface, cx);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list