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