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