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