1/// A trait for elements that can be selected.
2///
3/// Generally used to enable "toggle" or "active" behavior and styles on an element through the [`Selection`] status.
4pub trait Selectable {
5 /// Sets whether the element is selected.
6 fn selected(self, selected: bool) -> Self;
7}
8
9/// Represents the selection status of an element.
10#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]
11pub enum Selection {
12 /// The element is not selected.
13 #[default]
14 Unselected,
15 /// The selection state of the element is indeterminate.
16 Indeterminate,
17 /// The element is selected.
18 Selected,
19}
20
21impl Selection {
22 /// Returns the inverse of the current selection status.
23 ///
24 /// Indeterminate states become selected if inverted.
25 pub fn inverse(&self) -> Self {
26 match self {
27 Self::Unselected | Self::Indeterminate => Self::Selected,
28 Self::Selected => Self::Unselected,
29 }
30 }
31}
32
33impl From<bool> for Selection {
34 fn from(selected: bool) -> Self {
35 if selected {
36 Self::Selected
37 } else {
38 Self::Unselected
39 }
40 }
41}
42
43impl From<Option<bool>> for Selection {
44 fn from(selected: Option<bool>) -> Self {
45 match selected {
46 Some(true) => Self::Selected,
47 Some(false) => Self::Unselected,
48 None => Self::Unselected,
49 }
50 }
51}