1use crate::{Direction, Error, KeyboardInput};
3
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum TreeCheckState {
6 Unchecked,
7 Checked,
8 Mixed,
9}
10impl TreeCheckState {
11 pub fn as_str(self) -> &'static str {
12 match self {
13 Self::Unchecked => "false",
14 Self::Checked => "true",
15 Self::Mixed => "mixed",
16 }
17 }
18}
19
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub struct TreeItem {
22 pub level: u8,
24 pub position: u16,
26 pub count: u16,
27}
28impl TreeItem {
29 pub fn validate(self) -> Result<(), Error> {
30 if self.level == 0 || self.level > 64 || self.count == 0 || self.position >= self.count {
31 Err(Error::InvalidProps(
32 "tree item requires finite level and sibling coordinates",
33 ))
34 } else {
35 Ok(())
36 }
37 }
38}
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
40pub struct TreeNavigation {
41 pub direction: Direction,
42 pub typeahead: bool,
43 pub multiple: bool,
44}
45impl Default for TreeNavigation {
46 fn default() -> Self {
47 Self {
48 direction: Direction::Ltr,
49 typeahead: true,
50 multiple: false,
51 }
52 }
53}
54#[derive(Clone, Debug, PartialEq, Eq)]
55pub enum TreeCommand {
56 Previous,
57 Next,
58 First,
59 Last,
60 Open,
61 Close,
62 Select,
63 Activate,
64 Search { text: String, at_millis: u64 },
65}
66impl TreeCommand {
67 pub fn key(config: TreeNavigation, input: KeyboardInput<'_>, at_millis: u64) -> Option<Self> {
68 if input.composing || input.editable || input.control || input.meta || input.alt {
69 return None;
70 }
71 if !input.shift {
72 let command = match input.key {
73 "ArrowUp" => Some(Self::Previous),
74 "ArrowDown" => Some(Self::Next),
75 "Home" => Some(Self::First),
76 "End" => Some(Self::Last),
77 "ArrowRight" => Some(if config.direction == Direction::Rtl {
78 Self::Close
79 } else {
80 Self::Open
81 }),
82 "ArrowLeft" => Some(if config.direction == Direction::Rtl {
83 Self::Open
84 } else {
85 Self::Close
86 }),
87 " " if !input.repeat => Some(Self::Select),
88 "Enter" if !input.repeat => Some(Self::Activate),
89 _ => None,
90 };
91 if command.is_some() {
92 return command;
93 }
94 }
95 (config.typeahead && input.typeahead(at_millis).is_some()).then(|| Self::Search {
96 text: input.key.to_owned(),
97 at_millis,
98 })
99 }
100 pub fn validate(&self) -> Result<(), Error> {
101 if let Self::Search { text, at_millis } = self {
102 crate::validate_typeahead(text, *at_millis)?;
103 }
104 Ok(())
105 }
106}