Skip to main content

hyprforge_ui/
widgets.rs

1//! Every widget the suite shares.
2//!
3//! The functions in this file are the original set; the modules below
4//! arrived with the Settings redesign and Files' path bar and are
5//! re-exported, so every widget is `hyprforge_ui::widgets::<name>`
6//! wherever it happens to live. `DESIGN-SYSTEM.md` beside this crate's
7//! manifest catalogues all of them, and `check.sh` fails if it misses one.
8
9mod anchor;
10mod badges;
11mod controls;
12mod field;
13mod layout;
14mod ring;
15mod selection;
16mod suggestions;
17
18pub use anchor::{anchored, Anchored};
19pub use badges::{chip, config_line, keycap};
20pub use controls::{
21    dropdown_menu_style, dropdown_style, segment_style, segmented, segmented_choice,
22    slider_style, step_index, stepped_slider, toggle, toggle_style, value_slider, SegmentLook,
23};
24pub use field::{inset_field_style, inset_input_style, search_field};
25pub use layout::{
26    hero_card, hint_text, page_header, pending_bar, pending_label, setting_list, setting_row,
27    setting_row_style, status_dot, SETTING_ROW_GAP,
28};
29pub use ring::{countdown_ring, remaining_fraction};
30pub use selection::{section_label, selectable_row_style, spaced_caps, Tint};
31pub use suggestions::{suggestions, Suggestion};
32
33use crate::theme::{self, spacing, surface, FontScale};
34use iced::widget::{button, column, container, row, text, text::IntoFragment, Text};
35use iced::{Background, Border, Color, Element, Length, Theme};
36
37/// `text(content).size(scale.apply(base_size))` — the one place every piece
38/// of Hyprforge UI text should go through, so `FontScale` (vision pillar
39/// #7: accessibility built into the shared layer, not bolted on per-app)
40/// actually reaches the screen instead of being a struct nobody calls.
41pub fn scaled_text<'a>(
42    content: impl IntoFragment<'a>,
43    base_size: f32,
44    scale: FontScale,
45) -> Text<'a> {
46    text(content).size(scale.apply(base_size))
47}
48
49/// Dim, secondary-emphasis text — timestamps, hints, IDs. Distinct from
50/// `scaled_text` alone so "this is metadata, not content" is visible at a
51/// glance rather than relying on size difference only.
52pub fn meta_text<'a>(content: impl IntoFragment<'a>, base_size: f32, scale: FontScale) -> Text<'a> {
53    scaled_text(content, base_size, scale).color(theme::text_dim())
54}
55
56/// The raised-card look used for every section and dialog — a real step
57/// up from the root background plus a hairline border, not an auto-derived
58/// shade a few percent off the background (which reads as "no elevation
59/// at all" on a real monitor).
60fn card_style(_theme: &Theme) -> container::Style {
61    container::Style {
62        background: Some(Background::Color(surface::card())),
63        border: Border {
64            radius: crate::density::card_radius().into(),
65            width: 1.0,
66            color: surface::card_border(),
67        },
68        ..container::Style::default()
69    }
70}
71
72/// A titled group of content — the standard way every Hyprforge settings
73/// screen breaks itself into blocks (vision pillar #2). Sized to its
74/// content; callers that need scrolling should cap a `max_height` on the
75/// `content` they pass in rather than have the card stretch to fill
76/// whatever space happens to be available.
77pub fn section<'a, Message: 'a>(
78    title: impl AsRef<str>,
79    scale: FontScale,
80    content: impl Into<Element<'a, Message>>,
81) -> Element<'a, Message> {
82    column![
83        // The same heading every restyled page uses over its groups, so a
84        // page still built from cards reads as the same app.
85        section_label(title.as_ref(), scale),
86        container(content.into())
87            .padding(spacing::MD)
88            .width(Length::Fill)
89            .style(card_style)
90    ]
91    .spacing(spacing::SM)
92    .into()
93}
94
95/// A thin separator between list rows inside a card — the visual grammar
96/// used everywhere Hyprforge shows a list of things (profiles, rules) so
97/// rows read as distinct items rather than one unbroken block of text.
98pub fn divider<'a, Message: 'a>() -> Element<'a, Message> {
99    container(column![])
100        .width(Length::Fill)
101        .height(Length::Fixed(1.0))
102        .style(|_theme: &Theme| container::Style {
103            background: Some(Background::Color(surface::card_border())),
104            ..container::Style::default()
105        })
106        .into()
107}
108
109/// [`divider`] standing up: the same hairline in the same colour, between
110/// panes that sit side by side — Files' column view draws one between
111/// each folder's pane.
112pub fn vertical_divider<'a, Message: 'a>() -> Element<'a, Message> {
113    container(column![])
114        .width(Length::Fixed(1.0))
115        .height(Length::Fill)
116        .style(|_theme: &Theme| container::Style {
117            background: Some(Background::Color(surface::card_border())),
118            ..container::Style::default()
119        })
120        .into()
121}
122
123/// Widest a settings control grows before it stops tracking the row.
124///
125/// Without a cap, a text input in an 880px content column stretches to
126/// ~590px — far wider than any value typed into it, which reads as an
127/// unfinished form. Windows and macOS both hold settings controls to
128/// roughly this width regardless of window size.
129const CONTROL_MAX_WIDTH: f32 = 340.0;
130
131/// A label paired with its control, as a setting row.
132///
133/// The same row [`setting_row`] draws — label at the left end, control
134/// at the right, at least one row tall — so a page not yet built from
135/// striped groups still lines up with those that are. It is never
136/// striped, because it is not told its place in a list; it used to be a
137/// 2:3 split that put a wide gap between a label and its control.
138///
139/// Takes no `FontScale` of its own, as it never did; it reads the scale
140/// from the active theme instead, so its text scales like every row
141/// beside it rather than staying at 100%.
142pub fn row_field<'a, Message: 'a>(
143    label: impl IntoFragment<'a>,
144    control: impl Into<Element<'a, Message>>,
145) -> Element<'a, Message> {
146    let scale = FontScale(theme::active().font_scale);
147    setting_row(
148        1,
149        label,
150        None,
151        // Fill, capped, and aligned right — not Shrink. A text field
152        // fills whatever it is put in, and inside a Shrink container that
153        // was nearly nothing: every field on the Window rules editor and
154        // Displays' Advanced section drew as a sliver a few pixels wide.
155        // A switch or a button still sits at the row's right end.
156        container(control.into())
157            .max_width(CONTROL_MAX_WIDTH)
158            .width(Length::Fill)
159            .align_x(iced::alignment::Horizontal::Right),
160        scale,
161    )
162}
163
164fn primary_style(theme: &Theme, status: button::Status) -> button::Style {
165    let palette = theme.extended_palette();
166    let base = button::Style {
167        background: Some(Background::Color(palette.primary.base.color)),
168        text_color: palette.primary.base.text,
169        border: Border {
170            radius: 8.0.into(),
171            ..Border::default()
172        },
173        ..button::Style::default()
174    };
175    match status {
176        button::Status::Hovered => button::Style {
177            background: Some(Background::Color(palette.primary.strong.color)),
178            ..base
179        },
180        button::Status::Disabled => button::Style {
181            background: base.background.map(|b| match b {
182                Background::Color(c) => Background::Color(Color { a: 0.4, ..c }),
183                other => other,
184            }),
185            text_color: Color {
186                a: 0.4,
187                ..base.text_color
188            },
189            ..base
190        },
191        _ => base,
192    }
193}
194
195fn secondary_style(_theme: &Theme, status: button::Status) -> button::Style {
196    let base = button::Style {
197        background: Some(Background::Color(surface::row())),
198        text_color: crate::theme::text(),
199        border: Border {
200            radius: 8.0.into(),
201            width: 1.0,
202            color: surface::card_border(),
203        },
204        ..button::Style::default()
205    };
206    match status {
207        button::Status::Hovered => button::Style {
208            background: Some(Background::Color(surface::card_border())),
209            ..base
210        },
211        button::Status::Disabled => button::Style {
212            text_color: Color {
213                a: 0.4,
214                ..base.text_color
215            },
216            ..base
217        },
218        _ => base,
219    }
220}
221
222fn danger_style(theme: &Theme, status: button::Status) -> button::Style {
223    let palette = theme.extended_palette();
224    let base = button::Style {
225        background: Some(Background::Color(surface::row())),
226        text_color: palette.danger.base.color,
227        border: Border {
228            radius: 8.0.into(),
229            width: 1.0,
230            color: palette.danger.base.color,
231        },
232        ..button::Style::default()
233    };
234    match status {
235        // The stronger accent, not the danger colour it used to turn on
236        // hover: red is a state colour, and a primary button is Apply,
237        // Save, Keep — the opposite of danger. Pointing at one must not
238        // make it look like the Delete button.
239        button::Status::Hovered => button::Style {
240            background: Some(Background::Color(palette.primary.strong.color)),
241            text_color: palette.primary.strong.text,
242            ..base
243        },
244        _ => base,
245    }
246}
247
248/// The default action button — filled with the accent color. Use for the
249/// one primary action in a row or dialog (Apply, Save, Confirm).
250pub fn primary_button<'a, Message: Clone + 'a>(
251    label: impl IntoFragment<'a>,
252) -> button::Button<'a, Message> {
253    button(text(label)).style(primary_style)
254}
255
256/// A quiet, outlined button for secondary actions (Rename, Cancel, Edit) —
257/// visible without competing with the primary action in the same row.
258pub fn secondary_button<'a, Message: Clone + 'a>(
259    label: impl IntoFragment<'a>,
260) -> button::Button<'a, Message> {
261    button(text(label)).style(secondary_style)
262}
263
264/// A button styled for destructive/irreversible actions. Reserve for
265/// things that actually destroy data (Delete) — a plain Cancel/Close is
266/// `secondary_button`, not this.
267pub fn danger_button<'a, Message: Clone + 'a>(
268    label: &'a str,
269    on_press: Message,
270) -> Element<'a, Message> {
271    button(text(label))
272        .on_press(on_press)
273        .style(danger_style)
274        .into()
275}
276
277/// A three-way "unset / true / false" selector, for the settings that have
278/// a real difference between "don't care" and "explicitly false" — a window
279/// -rule matcher of `floating = false` selects tiled windows, which a plain
280/// checkbox can't express separately from not matching on it at all.
281pub fn tri_state<'a, Message: Clone + 'a>(
282    value: Option<bool>,
283    on_select: impl Fn(Option<bool>) -> Message,
284) -> Element<'a, Message> {
285    let option = |label: &'a str, choice: Option<bool>| {
286        let button = if value == choice {
287            primary_button(label)
288        } else {
289            secondary_button(label)
290        };
291        button.on_press(on_select(choice))
292    };
293    row![
294        option("Any", None),
295        option("Yes", Some(true)),
296        option("No", Some(false)),
297    ]
298    .spacing(spacing::XS)
299    .into()
300}
301
302/// The standard "this is about to touch a file you own" confirmation dialog
303/// (vision pillar #4: nothing requires faith that a change worked — show the
304/// exact diff before writing).
305pub fn confirm_dialog<'a, Message: Clone + 'a>(
306    title: &'a str,
307    body: &'a str,
308    diff_text: &'a str,
309    on_confirm: Message,
310    on_cancel: Message,
311) -> Element<'a, Message> {
312    let diff = container(text(diff_text).font(crate::theme::mono_font()).size(13))
313        .padding(spacing::SM)
314        .width(Length::Fill)
315        .style(|_theme: &Theme| container::Style {
316            background: Some(Background::Color(surface::row())),
317            border: Border {
318                radius: 6.0.into(),
319                width: 1.0,
320                color: surface::card_border(),
321            },
322            ..container::Style::default()
323        });
324
325    container(
326        column![
327            text(title).size(18),
328            text(body),
329            diff,
330            row![
331                secondary_button("Cancel").on_press(on_cancel),
332                primary_button("Confirm").on_press(on_confirm),
333            ]
334            .spacing(spacing::SM),
335        ]
336        .spacing(spacing::MD)
337        .padding(spacing::LG)
338        .max_width(560.0),
339    )
340    .style(card_style)
341    .into()
342}
343
344
345
346#[cfg(test)]
347mod tests {
348    use super::*;
349
350    /// Pointing at Apply must not paint it the colour of Delete. It did,
351    /// from the first polish pass until the Settings redesign: hover
352    /// turned the primary button the danger red.
353    #[test]
354    fn a_primary_button_stays_the_accent_when_hovered() {
355        let t = theme::app_theme();
356        let palette = t.extended_palette();
357        for status in [button::Status::Active, button::Status::Hovered] {
358            let style = primary_style(&t, status);
359            assert_ne!(
360                style.background,
361                Some(Background::Color(palette.danger.base.color)),
362                "{status:?}"
363            );
364        }
365        assert_eq!(
366            primary_style(&t, button::Status::Hovered).background,
367            Some(Background::Color(palette.primary.strong.color))
368        );
369    }
370}