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