Skip to main content

hyprforge_ui/widgets/
badges.rs

1//! Small inline marks: a chip, a keycap, a config line.
2
3use super::Tint;
4use crate::density;
5use crate::theme::{self, surface, FontScale};
6use iced::widget::{container, text, text::IntoFragment, Text};
7use iced::{Background, Border, Element};
8
9/// How strongly a chip's own colour tints its fill and its outline.
10///
11/// Faint on purpose: the text carries the colour, and the fill only
12/// separates the chip from the row it sits in. A chip at full strength
13/// would outshout the label it annotates.
14const CHIP_FILL_ALPHA: f32 = 0.10;
15const CHIP_BORDER_ALPHA: f32 = 0.45;
16
17/// A short fact about the row it sits in — `WPA3`, `saved`,
18/// `captive portal`, `suite default` — in the mono font, in `tint`.
19///
20/// Mono because a chip is usually something the system said rather than
21/// something written for a person, and setting it in the same font as
22/// config lines says so.
23pub fn chip<'a, Message: 'a>(
24    label: impl IntoFragment<'a>,
25    tint: Tint,
26    scale: FontScale,
27) -> Element<'a, Message> {
28    let color = tint.iced();
29    container(
30        text(label)
31            .font(theme::mono_font())
32            .size(scale.apply(density::META_TEXT_BASE * 0.9))
33            .color(color)
34            // Never wrapped: a chip broken over two lines stops reading
35            // as one mark and doubles the height of the row it sits in.
36            .wrapping(iced::widget::text::Wrapping::None),
37    )
38    .padding([1.0, scale.apply(6.0)])
39    .style(move |_t: &iced::Theme| container::Style {
40        background: Some(Background::Color(iced::Color { a: CHIP_FILL_ALPHA, ..color })),
41        border: Border {
42            radius: density::nested_radius().into(),
43            width: 1.0,
44            color: iced::Color { a: CHIP_BORDER_ALPHA, ..color },
45        },
46        ..container::Style::default()
47    })
48    .into()
49}
50
51/// A token the user wrote and can take back — Files' `ext:rs` in its
52/// search field: the label in the mono font, and a × that sends
53/// `on_remove`.
54///
55/// Neutral, not tinted like [`chip`]: a [`chip`] is something the system
56/// says about a row, in a state colour; this is something the person
57/// typed, and colouring it would claim a state it does not have. It
58/// takes the raised row surface — "a live control" in this suite's
59/// three-level idiom — with the hairline the keycaps have.
60pub fn removable_chip<'a, Message: Clone + 'a>(
61    label: impl IntoFragment<'a>,
62    on_remove: Message,
63    scale: FontScale,
64) -> Element<'a, Message> {
65    let size = scale.apply(density::META_TEXT_BASE * 0.9);
66    let cross = iced::widget::button(text("\u{00d7}").size(size).color(theme::text_dim()))
67        .padding([0.0, scale.apply(2.0)])
68        .on_press(on_remove)
69        .style(|_t: &iced::Theme, status| iced::widget::button::Style {
70            background: matches!(status, iced::widget::button::Status::Hovered)
71                .then(|| Background::Color(surface::card())),
72            text_color: theme::text(),
73            border: Border { radius: density::nested_radius().into(), ..Border::default() },
74            ..iced::widget::button::Style::default()
75        });
76    container(
77        iced::widget::row![
78            text(label)
79                .font(theme::mono_font())
80                .size(size)
81                .color(theme::text())
82                .wrapping(iced::widget::text::Wrapping::None),
83            cross,
84        ]
85        .spacing(scale.apply(2.0))
86        .align_y(iced::Alignment::Center),
87    )
88    .padding([0.0, scale.apply(4.0)])
89    .style(|_t: &iced::Theme| container::Style {
90        background: Some(Background::Color(surface::row())),
91        border: Border { radius: density::nested_radius().into(), width: 1.0, color: surface::card_border() },
92        ..container::Style::default()
93    })
94    .into()
95}
96
97/// One key of a chord, as a cap: `SUPER`, `SHIFT`, `⏎`.
98///
99/// A chord is drawn as a row of these rather than as `SUPER + SHIFT + S`
100/// in a line of text, because the caps are what make a key binding
101/// scannable down a table — the eye finds the shape before reading the
102/// word.
103pub fn keycap<'a, Message: 'a>(label: impl IntoFragment<'a>, scale: FontScale) -> Element<'a, Message> {
104    container(
105        text(label)
106            .font(iced::Font { weight: iced::font::Weight::Semibold, ..theme::mono_font() })
107            .size(scale.apply(density::META_TEXT_BASE * 0.85))
108            .color(theme::text()),
109    )
110    .padding([1.0, scale.apply(6.0)])
111    .style(|_t: &iced::Theme| container::Style {
112        background: Some(Background::Color(surface::sidebar())),
113        border: Border {
114            radius: density::nested_radius().into(),
115            width: 1.0,
116            color: surface::card_border(),
117        },
118        ..container::Style::default()
119    })
120    .into()
121}
122
123/// Text shown exactly as it is written in a config file —
124/// `input:follow_mouse = 1`, `monitor=DP-1,2560x1440@165`.
125///
126/// Mono and dim: it is there to be checked, not read, and it must be
127/// possible to tell `l` from `1` in it.
128pub fn config_line<'a>(content: impl IntoFragment<'a>, scale: FontScale) -> Text<'a> {
129    text(content)
130        .font(theme::mono_font())
131        .size(scale.apply(density::META_TEXT_BASE * 0.9))
132        .color(theme::text_dim())
133}