selectable.rs

 1/// A trait for elements that can be selected. Generally used to enable "toggle" or "active" behavior and styles on an element through the [Selection] status.
 2pub trait Selectable {
 3    /// Sets whether the element is selected.
 4    fn selected(self, selected: bool) -> Self;
 5}
 6
 7/// Represents the selection status of an element.
 8#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy)]
 9pub enum Selection {
10    /// The element is not selected.
11    #[default]
12    Unselected,
13    /// The selection state of the element is indeterminate.
14    Indeterminate,
15    /// The element is selected.
16    Selected,
17}
18
19impl Selection {
20    /// Returns the inverse of the current selection status.
21    ///
22    /// Indeterminate states become selected if inverted.
23    pub fn inverse(&self) -> Self {
24        match self {
25            Self::Unselected | Self::Indeterminate => Self::Selected,
26            Self::Selected => Self::Unselected,
27        }
28    }
29}