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/// One key of a chord, as a cap: `SUPER`, `SHIFT`, `⏎`.
52///
53/// A chord is drawn as a row of these rather than as `SUPER + SHIFT + S`
54/// in a line of text, because the caps are what make a key binding
55/// scannable down a table — the eye finds the shape before reading the
56/// word.
57pub fn keycap<'a, Message: 'a>(label: impl IntoFragment<'a>, scale: FontScale) -> Element<'a, Message> {
58    container(
59        text(label)
60            .font(iced::Font { weight: iced::font::Weight::Semibold, ..theme::mono_font() })
61            .size(scale.apply(density::META_TEXT_BASE * 0.85))
62            .color(theme::text()),
63    )
64    .padding([1.0, scale.apply(6.0)])
65    .style(|_t: &iced::Theme| container::Style {
66        background: Some(Background::Color(surface::sidebar())),
67        border: Border {
68            radius: density::nested_radius().into(),
69            width: 1.0,
70            color: surface::card_border(),
71        },
72        ..container::Style::default()
73    })
74    .into()
75}
76
77/// Text shown exactly as it is written in a config file —
78/// `input:follow_mouse = 1`, `monitor=DP-1,2560x1440@165`.
79///
80/// Mono and dim: it is there to be checked, not read, and it must be
81/// possible to tell `l` from `1` in it.
82pub fn config_line<'a>(content: impl IntoFragment<'a>, scale: FontScale) -> Text<'a> {
83    text(content)
84        .font(theme::mono_font())
85        .size(scale.apply(density::META_TEXT_BASE * 0.9))
86        .color(theme::text_dim())
87}