Skip to main content

hyprforge_ui/widgets/
panel.rs

1//! The pieces of a docked side panel: tabs across its top, and the
2//! label-and-value lines a panel about one thing is mostly made of.
3//!
4//! Written for Files' Properties inspector (mockup `1g`), and here rather
5//! than in Files because neither is about files: Settings' pages describe
6//! a monitor or a battery with the same "Where · Created · Owner" lines,
7//! and any panel with more than one view of its subject wants the same
8//! tabs. See `DESIGN-SYSTEM.md`'s rule about the second user arriving
9//! with a copy of its own.
10
11use super::{meta_text, scaled_text};
12use crate::density;
13use crate::theme::{self, spacing, FontScale};
14use iced::widget::{button, column, container, row, text::IntoFragment, Row, Space};
15use iced::{Background, Element, Length};
16
17/// How wide a fact's label column is at 100% — room for "Accessed" and
18/// "Permissions" in the dim metadata size, so every value in a panel
19/// starts at one edge and the eye can run down them.
20const FACT_LABEL_BASE: f32 = 86.0;
21
22/// The underline under the selected tab.
23const TAB_UNDERLINE: f32 = 2.0;
24
25/// Tabs across the top of a panel: each a word, the selected one in the
26/// body colour over an accent underline, the rest dim.
27///
28/// The underline is the accent because a tab *is* a selection — which of
29/// the panel's views you picked — and that is the one thing the accent
30/// means. Hover only lifts the text to the body colour; it never borrows
31/// the underline, for the reason `selectable_row_style` gives.
32///
33/// Every tab keeps a transparent underline of the same height, so
34/// switching tabs moves nothing: the row is the same height whichever
35/// one is lit.
36pub fn panel_tabs<'a, T, Message>(
37    tabs: &[(T, &'a str)],
38    selected: &T,
39    on_select: impl Fn(T) -> Message,
40    scale: FontScale,
41) -> Element<'a, Message>
42where
43    T: Clone + PartialEq,
44    Message: Clone + 'a,
45{
46    let mut strip = Row::new().spacing(spacing::MD).align_y(iced::Alignment::End);
47    for (tab, label) in tabs {
48        let lit = tab == selected;
49        let word = button(scaled_text(*label, density::META_TEXT_BASE, scale))
50            .padding([2.0, 0.0])
51            .on_press(on_select(tab.clone()))
52            .style(move |_t: &iced::Theme, status| button::Style {
53                background: None,
54                text_color: if lit || matches!(status, button::Status::Hovered) {
55                    theme::text()
56                } else {
57                    theme::text_dim()
58                },
59                ..button::Style::default()
60            });
61        let underline = container(Space::new())
62            .width(Length::Fill)
63            .height(Length::Fixed(TAB_UNDERLINE))
64            .style(move |t: &iced::Theme| iced::widget::container::Style {
65                background: lit.then(|| Background::Color(t.extended_palette().primary.base.color)),
66                ..iced::widget::container::Style::default()
67            });
68        strip = strip.push(column![word, underline].width(Length::Shrink));
69    }
70    strip.into()
71}
72
73/// One fact about the thing a panel describes: a dim label on the left,
74/// at a fixed width so every value in the panel lines up, and the value
75/// beside it — `Where   ~/projects/hyprsuite/src`.
76///
77/// The value is an element rather than a string so a caller can set a
78/// path or a mode in the mono font (`config_line`), add a chip, or put a
79/// button beside it. A long value wraps under itself, never back under
80/// the label.
81pub fn fact_row<'a, Message: 'a>(
82    label: impl IntoFragment<'a>,
83    value: impl Into<Element<'a, Message>>,
84    scale: FontScale,
85) -> Element<'a, Message> {
86    row![
87        container(meta_text(label, density::META_TEXT_BASE, scale))
88            .width(Length::Fixed(scale.apply(FACT_LABEL_BASE))),
89        container(value.into()).width(Length::Fill),
90    ]
91    .spacing(spacing::SM)
92    .align_y(iced::Alignment::Start)
93    .into()
94}
95
96/// [`fact_row`] for the common case of a plain value, wrapped by word —
97/// or by glyph, for a name with no spaces to break at.
98pub fn fact<'a, Message: 'a>(
99    label: impl IntoFragment<'a>,
100    value: impl IntoFragment<'a>,
101    scale: FontScale,
102) -> Element<'a, Message> {
103    fact_row(
104        label,
105        scaled_text(value, density::META_TEXT_BASE, scale)
106            .color(theme::text())
107            .wrapping(iced::widget::text::Wrapping::WordOrGlyph),
108        scale,
109    )
110}