Skip to main content

grafix_toolbox/gui/elements/
selector.rs

1use super::*;
2
3#[derive(Default, Debug)]
4pub struct Selector {
5	button: Button,
6	edit: LineEdit,
7	choices: Vec<Button>,
8	open: bool,
9	pub choice: usize,
10}
11impl Selector {
12	pub fn draw<'s: 'l, 'l>(&'s mut self, r: &mut RenderLock<'l>, t: &'l Theme, layout: Surf, options: &'s mut [String]) -> &'s str {
13		if options.is_empty() {
14			return "";
15		}
16
17		let (len, Self { button, edit, choices, open, choice }) = (options.len(), self);
18
19		*choice = (*choice).min(len - 1);
20		choices.resize_with(len, Def);
21
22		if !*open {
23			let text = &options[*choice];
24
25			if button.draw(r, t, layout, text) {
26				(*open, (*edit, *choices)) = (true, Def());
27				edit.text = text.into();
28			}
29
30			return text;
31		}
32
33		for (n, c) in choices.iter_mut().enumerate() {
34			if c.draw(r, t, layout.y_self(n + 1), &options[n]) {
35				(*choice, (*open, *button)) = (n, Def());
36				return &options[n];
37			}
38		}
39
40		let text = options.at_mut(*choice);
41
42		if let Some(edit) = edit.draw(r, t, layout, None) {
43			(*text, *open) = (edit.into(), false);
44		}
45
46		*open &= r.hovers_in(layout.h_scale(len + 1));
47
48		options.at(*choice)
49	}
50}
51
52impl<'s: 'l, 'l> Lock::Selector<'s, 'l, '_> {
53	pub fn draw(self, g: impl Into<Surf>, o: &'s mut [String]) -> &'s str {
54		let Self { s, r, t } = self;
55		s.draw(r, t, g.into(), o)
56	}
57}