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