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/// Widest a settings control grows before it stops tracking the row.
110///
111/// Without a cap, a text input in an 880px content column stretches to
112/// ~590px — far wider than any value typed into it, which reads as an
113/// unfinished form. Windows and macOS both hold settings controls to
114/// roughly this width regardless of window size.
115const CONTROL_MAX_WIDTH: f32 = 340.0;
116
117/// A label paired with its control, as a setting row.
118///
119/// The same row [`setting_row`] draws — label at the left end, control
120/// at the right, at least one row tall — so a page not yet built from
121/// striped groups still lines up with those that are. It is never
122/// striped, because it is not told its place in a list; it used to be a
123/// 2:3 split that put a wide gap between a label and its control.
124///
125/// Takes no `FontScale` of its own, as it never did; it reads the scale
126/// from the active theme instead, so its text scales like every row
127/// beside it rather than staying at 100%.
128pub fn row_field<'a, Message: 'a>(
129    label: impl IntoFragment<'a>,
130    control: impl Into<Element<'a, Message>>,
131) -> Element<'a, Message> {
132    let scale = FontScale(theme::active().font_scale);
133    setting_row(
134        1,
135        label,
136        None,
137        // Fill, capped, and aligned right — not Shrink. A text field
138        // fills whatever it is put in, and inside a Shrink container that
139        // was nearly nothing: every field on the Window rules editor and
140        // Displays' Advanced section drew as a sliver a few pixels wide.
141        // A switch or a button still sits at the row's right end.
142        container(control.into())
143            .max_width(CONTROL_MAX_WIDTH)
144            .width(Length::Fill)
145            .align_x(iced::alignment::Horizontal::Right),
146        scale,
147    )
148}
149
150fn primary_style(theme: &Theme, status: button::Status) -> button::Style {
151    let palette = theme.extended_palette();
152    let base = button::Style {
153        background: Some(Background::Color(palette.primary.base.color)),
154        text_color: palette.primary.base.text,
155        border: Border {
156            radius: 8.0.into(),
157            ..Border::default()
158        },
159        ..button::Style::default()
160    };
161    match status {
162        button::Status::Hovered => button::Style {
163            background: Some(Background::Color(palette.primary.strong.color)),
164            ..base
165        },
166        button::Status::Disabled => button::Style {
167            background: base.background.map(|b| match b {
168                Background::Color(c) => Background::Color(Color { a: 0.4, ..c }),
169                other => other,
170            }),
171            text_color: Color {
172                a: 0.4,
173                ..base.text_color
174            },
175            ..base
176        },
177        _ => base,
178    }
179}
180
181fn secondary_style(_theme: &Theme, status: button::Status) -> button::Style {
182    let base = button::Style {
183        background: Some(Background::Color(surface::row())),
184        text_color: crate::theme::text(),
185        border: Border {
186            radius: 8.0.into(),
187            width: 1.0,
188            color: surface::card_border(),
189        },
190        ..button::Style::default()
191    };
192    match status {
193        button::Status::Hovered => button::Style {
194            background: Some(Background::Color(surface::card_border())),
195            ..base
196        },
197        button::Status::Disabled => button::Style {
198            text_color: Color {
199                a: 0.4,
200                ..base.text_color
201            },
202            ..base
203        },
204        _ => base,
205    }
206}
207
208fn danger_style(theme: &Theme, status: button::Status) -> button::Style {
209    let palette = theme.extended_palette();
210    let base = button::Style {
211        background: Some(Background::Color(surface::row())),
212        text_color: palette.danger.base.color,
213        border: Border {
214            radius: 8.0.into(),
215            width: 1.0,
216            color: palette.danger.base.color,
217        },
218        ..button::Style::default()
219    };
220    match status {
221        // The stronger accent, not the danger colour it used to turn on
222        // hover: red is a state colour, and a primary button is Apply,
223        // Save, Keep — the opposite of danger. Pointing at one must not
224        // make it look like the Delete button.
225        button::Status::Hovered => button::Style {
226            background: Some(Background::Color(palette.primary.strong.color)),
227            text_color: palette.primary.strong.text,
228            ..base
229        },
230        _ => base,
231    }
232}
233
234/// The default action button — filled with the accent color. Use for the
235/// one primary action in a row or dialog (Apply, Save, Confirm).
236pub fn primary_button<'a, Message: Clone + 'a>(
237    label: impl IntoFragment<'a>,
238) -> button::Button<'a, Message> {
239    button(text(label)).style(primary_style)
240}
241
242/// A quiet, outlined button for secondary actions (Rename, Cancel, Edit) —
243/// visible without competing with the primary action in the same row.
244pub fn secondary_button<'a, Message: Clone + 'a>(
245    label: impl IntoFragment<'a>,
246) -> button::Button<'a, Message> {
247    button(text(label)).style(secondary_style)
248}
249
250/// A button styled for destructive/irreversible actions. Reserve for
251/// things that actually destroy data (Delete) — a plain Cancel/Close is
252/// `secondary_button`, not this.
253pub fn danger_button<'a, Message: Clone + 'a>(
254    label: &'a str,
255    on_press: Message,
256) -> Element<'a, Message> {
257    button(text(label))
258        .on_press(on_press)
259        .style(danger_style)
260        .into()
261}
262
263/// A three-way "unset / true / false" selector, for the settings that have
264/// a real difference between "don't care" and "explicitly false" — a window
265/// -rule matcher of `floating = false` selects tiled windows, which a plain
266/// checkbox can't express separately from not matching on it at all.
267pub fn tri_state<'a, Message: Clone + 'a>(
268    value: Option<bool>,
269    on_select: impl Fn(Option<bool>) -> Message,
270) -> Element<'a, Message> {
271    let option = |label: &'a str, choice: Option<bool>| {
272        let button = if value == choice {
273            primary_button(label)
274        } else {
275            secondary_button(label)
276        };
277        button.on_press(on_select(choice))
278    };
279    row![
280        option("Any", None),
281        option("Yes", Some(true)),
282        option("No", Some(false)),
283    ]
284    .spacing(spacing::XS)
285    .into()
286}
287
288/// The standard "this is about to touch a file you own" confirmation dialog
289/// (vision pillar #4: nothing requires faith that a change worked — show the
290/// exact diff before writing).
291pub fn confirm_dialog<'a, Message: Clone + 'a>(
292    title: &'a str,
293    body: &'a str,
294    diff_text: &'a str,
295    on_confirm: Message,
296    on_cancel: Message,
297) -> Element<'a, Message> {
298    let diff = container(text(diff_text).font(crate::theme::mono_font()).size(13))
299        .padding(spacing::SM)
300        .width(Length::Fill)
301        .style(|_theme: &Theme| container::Style {
302            background: Some(Background::Color(surface::row())),
303            border: Border {
304                radius: 6.0.into(),
305                width: 1.0,
306                color: surface::card_border(),
307            },
308            ..container::Style::default()
309        });
310
311    container(
312        column![
313            text(title).size(18),
314            text(body),
315            diff,
316            row![
317                secondary_button("Cancel").on_press(on_cancel),
318                primary_button("Confirm").on_press(on_confirm),
319            ]
320            .spacing(spacing::SM),
321        ]
322        .spacing(spacing::MD)
323        .padding(spacing::LG)
324        .max_width(560.0),
325    )
326    .style(card_style)
327    .into()
328}
329
330
331
332#[cfg(test)]
333mod tests {
334    use super::*;
335
336    /// Pointing at Apply must not paint it the colour of Delete. It did,
337    /// from the first polish pass until the Settings redesign: hover
338    /// turned the primary button the danger red.
339    #[test]
340    fn a_primary_button_stays_the_accent_when_hovered() {
341        let t = theme::app_theme();
342        let palette = t.extended_palette();
343        for status in [button::Status::Active, button::Status::Hovered] {
344            let style = primary_style(&t, status);
345            assert_ne!(
346                style.background,
347                Some(Background::Color(palette.danger.base.color)),
348                "{status:?}"
349            );
350        }
351        assert_eq!(
352            primary_style(&t, button::Status::Hovered).background,
353            Some(Background::Color(palette.primary.strong.color))
354        );
355    }
356}