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