use super::{meta_text, scaled_text};
use crate::density;
use crate::theme::{self, spacing, FontScale};
use iced::widget::{button, column, container, row, text::IntoFragment, Row, Space};
use iced::{Background, Element, Length};
const FACT_LABEL_BASE: f32 = 86.0;
const TAB_UNDERLINE: f32 = 2.0;
pub fn panel_tabs<'a, T, Message>(
tabs: &[(T, &'a str)],
selected: &T,
on_select: impl Fn(T) -> Message,
scale: FontScale,
) -> Element<'a, Message>
where
T: Clone + PartialEq,
Message: Clone + 'a,
{
let mut strip = Row::new().spacing(spacing::MD).align_y(iced::Alignment::End);
for (tab, label) in tabs {
let lit = tab == selected;
let word = button(scaled_text(*label, density::META_TEXT_BASE, scale))
.padding([2.0, 0.0])
.on_press(on_select(tab.clone()))
.style(move |_t: &iced::Theme, status| button::Style {
background: None,
text_color: if lit || matches!(status, button::Status::Hovered) {
theme::text()
} else {
theme::text_dim()
},
..button::Style::default()
});
let underline = container(Space::new())
.width(Length::Fill)
.height(Length::Fixed(TAB_UNDERLINE))
.style(move |t: &iced::Theme| iced::widget::container::Style {
background: lit.then(|| Background::Color(t.extended_palette().primary.base.color)),
..iced::widget::container::Style::default()
});
strip = strip.push(column![word, underline].width(Length::Shrink));
}
strip.into()
}
pub fn fact_row<'a, Message: 'a>(
label: impl IntoFragment<'a>,
value: impl Into<Element<'a, Message>>,
scale: FontScale,
) -> Element<'a, Message> {
row![
container(meta_text(label, density::META_TEXT_BASE, scale))
.width(Length::Fixed(scale.apply(FACT_LABEL_BASE))),
container(value.into()).width(Length::Fill),
]
.spacing(spacing::SM)
.align_y(iced::Alignment::Start)
.into()
}
pub fn fact<'a, Message: 'a>(
label: impl IntoFragment<'a>,
value: impl IntoFragment<'a>,
scale: FontScale,
) -> Element<'a, Message> {
fact_row(
label,
scaled_text(value, density::META_TEXT_BASE, scale)
.color(theme::text())
.wrapping(iced::widget::text::Wrapping::WordOrGlyph),
scale,
)
}