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