Skip to main content

hyprforge_ui/widgets/
selection.rs

1//! Selection, and the labels that group what can be selected.
2//!
3//! Moved here from Files when Settings adopted the same design. These
4//! are the pieces that carry the suite's one colour rule — purple means
5//! *selected*, and nothing else is ever purple — so there must be exactly
6//! one copy of each.
7
8use crate::density;
9use crate::theme::{self, surface, FontScale};
10use iced::widget::{button, Text};
11use iced::{Background, Border};
12
13/// A clickable row's look: the accent when selected, a plain elevation
14/// step when merely hovered, nothing otherwise.
15///
16/// Hover must never share the selected colour: a row the pointer happens
17/// to be over looking like the one you picked makes "is this selected?"
18/// ambiguous the instant the mouse moves.
19/// `tests::only_the_selected_row_ever_uses_the_accent_colour` pins
20/// this.
21///
22/// One function for every list of rows — Files' sidebar, its entry list,
23/// its application chooser, and Settings' sidebar — because it is one
24/// rule, and two copies of "purple means selected" is two places for it
25/// to stop being true.
26pub fn selectable_row_style(
27    theme: &iced::Theme,
28    status: button::Status,
29    selected: bool,
30) -> button::Style {
31    let palette = theme.extended_palette();
32    let background = if selected {
33        Some(Background::Color(palette.primary.weak.color))
34    } else {
35        match status {
36            button::Status::Hovered => Some(Background::Color(surface::row())),
37            _ => None,
38        }
39    };
40    button::Style {
41        background,
42        text_color: theme::text(),
43        // The design's 6px inner radius, taken from the Theme — see
44        // `density::inner_radius`'s doc.
45        border: Border { radius: density::inner_radius().into(), ..Border::default() },
46        ..button::Style::default()
47    }
48}
49
50/// Which of the theme's roles a mark, a chip or a hero card is drawn in.
51///
52/// A closed set of *roles* rather than a colour, so nothing drawn with it
53/// can pick a colour outside the theme. `Accent` is selection; `Info`,
54/// `Success`, `Warning` and `Error` are state; `Dim` is neither.
55///
56/// Files' sidebar borrows the state roles for *identity* — `Warning` on
57/// the Pictures row does not mean anything is wrong — which is a real
58/// cost worth naming. It is defensible there because a sidebar place
59/// carries no state for the colour to be confused with; the same trick
60/// on a row that *does* carry state would be a mistake.
61#[derive(Debug, Clone, Copy, PartialEq, Eq)]
62pub enum Tint {
63    Accent,
64    Info,
65    Success,
66    Warning,
67    Error,
68    Dim,
69}
70
71impl Tint {
72    /// Resolves to the live theme's colour for this role.
73    pub fn color(self) -> hyprforge_look::Color {
74        let t = theme::active();
75        match self {
76            Tint::Accent => t.accent,
77            Tint::Info => t.info,
78            Tint::Success => t.success,
79            Tint::Warning => t.warning,
80            Tint::Error => t.error,
81            Tint::Dim => t.surfaces.text_dim,
82        }
83    }
84
85    /// [`Tint::color`], ready for iced.
86    pub fn iced(self) -> iced::Color {
87        crate::color::to_iced(self.color())
88    }
89}
90
91/// `title` in capitals with a thin space between the letters.
92///
93/// iced has no letter-spacing, and the design's heading depends on it:
94/// small uppercase text without it reads as a cramped word rather than
95/// as a label. Inserting U+2009 THIN SPACE between characters is the
96/// approximation available — coarser than real tracking, but it buys
97/// most of the effect for a heading that is never more than two words.
98pub fn spaced_caps(title: &str) -> String {
99    title
100        .to_uppercase()
101        .chars()
102        .map(|c| c.to_string())
103        .collect::<Vec<_>>()
104        .join("\u{2009}")
105}
106
107/// A group's heading in a sidebar or above a stack of rows — `PLACES`,
108/// `HYPRLAND`, `BORDERS & FOCUS`.
109///
110/// Small and dim, because a heading is a signpost rather than something
111/// to read: findable when looked for, invisible when not.
112pub fn section_label<'a>(title: &str, scale: FontScale) -> Text<'a> {
113    super::meta_text(spaced_caps(title), density::SECTION_LABEL_BASE, scale)
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119
120    /// The design reserves purple for exactly one meaning: the current
121    /// selection. This pins that the accent (`palette.primary.weak`,
122    /// what `selectable_row_style` selects on) never appears for any
123    /// non-selected status, hover included.
124    #[test]
125    fn only_the_selected_row_ever_uses_the_accent_colour() {
126        let theme = theme::app_theme();
127        let accent_bg = theme.extended_palette().primary.weak.color;
128
129        let selected = selectable_row_style(&theme, button::Status::Active, true);
130        assert_eq!(selected.background, Some(Background::Color(accent_bg)));
131
132        for status in [
133            button::Status::Active,
134            button::Status::Hovered,
135            button::Status::Pressed,
136            button::Status::Disabled,
137        ] {
138            let unselected = selectable_row_style(&theme, status, false);
139            assert_ne!(
140                unselected.background,
141                Some(Background::Color(accent_bg)),
142                "a non-selected row must never render the accent colour ({status:?})"
143            );
144        }
145    }
146
147    /// Hover has to be visibly different from *both* "plain" and
148    /// "selected" — a hover that read the same as selection would make a
149    /// row the user is merely pointing at look like one they picked.
150    #[test]
151    fn hover_is_a_distinct_elevation_from_both_plain_and_selected() {
152        let theme = theme::app_theme();
153        let plain = selectable_row_style(&theme, button::Status::Active, false);
154        let hovered = selectable_row_style(&theme, button::Status::Hovered, false);
155        let selected = selectable_row_style(&theme, button::Status::Active, true);
156        assert_ne!(plain.background, hovered.background);
157        assert_ne!(hovered.background, selected.background);
158    }
159
160    /// The selected fill derives from the accent, never from a state
161    /// colour — the mockup's rule that purple is selection and
162    /// cyan/green/orange/red are state only.
163    #[test]
164    fn a_state_colour_is_never_used_for_selection() {
165        let theme = theme::app_theme();
166        let Some(Background::Color(selected)) =
167            selectable_row_style(&theme, button::Status::Active, true).background
168        else {
169            panic!("a selected row has a fill");
170        };
171        for state in [Tint::Info, Tint::Success, Tint::Warning, Tint::Error] {
172            let c = state.iced();
173            assert_ne!(
174                (selected.r, selected.g, selected.b),
175                (c.r, c.g, c.b),
176                "selection drawn in {state:?}"
177            );
178        }
179    }
180
181    /// Every role is a different colour, or a chip in one could not be
182    /// told from a chip in another.
183    #[test]
184    fn every_tint_is_its_own_colour() {
185        let all = [Tint::Accent, Tint::Info, Tint::Success, Tint::Warning, Tint::Error, Tint::Dim];
186        for (i, a) in all.iter().enumerate() {
187            for b in &all[i + 1..] {
188                assert_ne!(a.color(), b.color(), "{a:?} and {b:?}");
189            }
190        }
191    }
192
193    #[test]
194    fn a_heading_is_capitals_with_thin_spaces_between_the_letters() {
195        assert_eq!(spaced_caps("Wi-Fi"), "W\u{2009}I\u{2009}-\u{2009}F\u{2009}I");
196        assert_eq!(spaced_caps(""), "");
197    }
198}