Debian Codegraph @

helix-term/src/ui/select.rs

raw symbols SCIP
1 use std::borrow::Cow;
2
3 use helix_view::{graphics::Rect, Editor};
4 use tui::{
5 buffer::Buffer as Surface,
6 widgets::{Block, Widget as _},
7 };
8
9 use crate::compositor::{Component, Context, Event, EventResult};
10
11 use super::{menu::Item, Menu, PromptEvent, Text};
12
13 pub struct Select<T: Item> {
14 message: Text,
15 options: Menu<T>,
16 }
17
18 impl<T: Item> Select<T> {
19 pub fn new<M, I, F>(message: M, options: I, data: T::Data, callback: F) -> Self
20 where
21 M: Into<Cow<'static, str>>,
22 I: IntoIterator<Item = T>,
23 F: Fn(&mut Editor, &T, PromptEvent) + 'static,
24 {
25 let message = tui::text::Text::from(message.into()).into();
26 let options: Vec<_> = options.into_iter().collect();
27 assert!(!options.is_empty());
28 let mut menu = Menu::new(options, data, move |editor, option, event| {
29 // Options are non-empty (asserted above) and an option is selected by default,
30 // so `option` must be Some here.
31 let option = &option.unwrap();
32 callback(editor, option, event)
33 })
34 .auto_close(true);
35 // Select the first option by default.
36 menu.move_down();
37
38 Self {
39 message,
40 options: menu,
41 }
42 }
43 }
44
45 impl<T: Item> Component for Select<T> {
46 fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
47 self.options.handle_event(event, cx)
48 }
49
50 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
51 let (message_width, message_height) = self.message.required_size(viewport).unwrap();
52 let (menu_width, menu_height) = self.options.required_size(viewport).unwrap();
53 Some((
54 menu_width.max(message_width + 2),
55 message_height + menu_height + 2,
56 ))
57 }
58
59 fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
60 const BLOCK: Block<'_> = Block::bordered();
61
62 // +---------------------+
63 // | message |
64 // +---------------------+
65 // options menu
66 //
67 //
68
69 // Limit the text width to 80% of the screen or 80 columns, whichever is
70 // smaller.
71 let max_width = 80.min(((area.width as u32) * 80u32 / 100) as u16);
72 let (message_width, message_height) =
73 super::text::required_size(&self.message.contents, max_width);
74 let (_, menu_height) = self
75 .options
76 .required_size((max_width, area.height))
77 .unwrap();
78 // + 2 for borders and another + 2 for horizontal padding
79 let width = message_width + 4;
80 let height = message_height + 2 + menu_height;
81 let area = Rect {
82 x: (area.width / 2) - width / 2,
83 y: (area.height / 2) - height / 2,
84 width,
85 height,
86 };
87
88 // Message
89 let background = cx.editor.theme.get("ui.background");
90 let text = cx.editor.theme.get("ui.text");
91 let message_box = area.with_height(message_height + 2);
92 surface.clear_with(message_box, background.patch(text));
93 BLOCK.render(message_box, surface);
94 // Add horizontal padding so the message isn't too close to the border.
95 let message_area = BLOCK.inner(message_box).clip_left(1).clip_right(1);
96 self.message.render(message_area, surface, cx);
97
98 // Options menu
99 let menu_area = area.clip_top(message_height + 2);
100 self.options.render(menu_area, surface, cx);
101 }
102 }