use super::{config_line, primary_button, scaled_text, secondary_button, Tint};
use crate::density;
use crate::theme::{self, spacing, surface, FontScale};
use iced::widget::{column, container, row, text::IntoFragment, Column, Container, Space};
use iced::{Background, Border, Element, Length};
const PAGE_TITLE_BASE: f32 = 20.0;
pub fn page_header<'a, Message: 'a>(
title: impl IntoFragment<'a>,
subtitle: Option<String>,
scale: FontScale,
) -> Element<'a, Message> {
let title = scaled_text(title, PAGE_TITLE_BASE, scale)
.font(iced::Font { weight: iced::font::Weight::Semibold, ..iced::Font::DEFAULT })
.color(theme::text());
let mut header = row![title].spacing(spacing::SM + spacing::XS).align_y(iced::Alignment::Center);
if let Some(subtitle) = subtitle {
header = header.push(config_line(subtitle, scale));
}
header.into()
}
pub const SETTING_ROW_GAP: f32 = 2.0;
pub fn setting_list<'a, Message: 'a>(
rows: impl IntoIterator<Item = Element<'a, Message>>,
) -> Column<'a, Message> {
column(rows).spacing(SETTING_ROW_GAP).width(Length::Fill)
}
pub fn setting_row_style(index: usize) -> container::Style {
container::Style {
background: index.is_multiple_of(2).then(|| Background::Color(theme::row_tint())),
border: Border { radius: density::card_radius().into(), ..Border::default() },
..container::Style::default()
}
}
pub fn hint_text<'a>(content: impl IntoFragment<'a>, scale: FontScale) -> iced::widget::Text<'a> {
super::meta_text(content, density::META_TEXT_BASE * 0.9, scale)
}
pub fn setting_row<'a, Message: 'a>(
index: usize,
label: impl IntoFragment<'a>,
hint: Option<Element<'a, Message>>,
control: impl Into<Element<'a, Message>>,
scale: FontScale,
) -> Element<'a, Message> {
let mut left = column![scaled_text(label, density::ROW_TEXT_BASE * 0.9, scale).color(theme::text())]
.spacing(2.0);
if let Some(hint) = hint {
left = left.push(hint);
}
let strut = Space::new()
.width(Length::Fixed(1.0))
.height(Length::Fixed(density::setting_row_height(scale)));
let label = row![strut, left.width(Length::Fill)].align_y(iced::Alignment::Center);
container(
row![label.width(Length::Fill), control.into()]
.spacing(spacing::MD)
.align_y(iced::Alignment::Center),
)
.padding([0.0, scale.apply(14.0)])
.width(Length::Fill)
.style(move |_t: &iced::Theme| setting_row_style(index))
.into()
}
const HERO_FILL_ALPHA: f32 = 0.10;
const HERO_BORDER_ALPHA: f32 = 0.40;
pub fn hero_card<'a, Message: 'a>(
tint: Tint,
content: impl Into<Element<'a, Message>>,
) -> Container<'a, Message> {
let color = tint.iced();
container(content)
.padding(spacing::MD)
.width(Length::Fill)
.style(move |_t: &iced::Theme| container::Style {
background: Some(Background::Color(iced::Color { a: HERO_FILL_ALPHA, ..color })),
border: Border {
radius: density::card_radius().into(),
width: 1.0,
color: iced::Color { a: HERO_BORDER_ALPHA, ..color },
},
..container::Style::default()
})
}
pub fn status_dot<'a, Message: 'a>(tint: Tint, scale: FontScale) -> Element<'a, Message> {
let side = scale.apply(7.0);
let color = tint.iced();
container(Space::new())
.width(Length::Fixed(side))
.height(Length::Fixed(side))
.style(move |_t: &iced::Theme| container::Style {
background: Some(Background::Color(color)),
border: Border { radius: (side / 2.0).into(), ..Border::default() },
..container::Style::default()
})
.into()
}
pub fn pending_label(count: usize) -> String {
match count {
1 => "1 pending change".to_string(),
n => format!("{n} pending changes"),
}
}
pub fn pending_bar<'a, Message: Clone + 'a>(
summary: String,
preview: Option<String>,
on_discard: Option<Message>,
on_apply: Message,
scale: FontScale,
) -> Element<'a, Message> {
let mut info = row![
status_dot(Tint::Warning, scale),
scaled_text(summary, density::META_TEXT_BASE, scale).color(theme::text()),
]
.spacing(spacing::SM)
.align_y(iced::Alignment::Center);
if let Some(preview) = preview {
info = info.push(config_line(preview, scale));
}
let mut actions = row![].spacing(spacing::SM).align_y(iced::Alignment::Center);
if let Some(discard) = on_discard {
actions = actions.push(secondary_button("Discard").on_press(discard));
}
actions = actions.push(primary_button("Apply").on_press(on_apply));
container(
row![info.width(Length::Fill), actions]
.spacing(spacing::SM)
.align_y(iced::Alignment::Center),
)
.padding([spacing::SM, spacing::MD])
.width(Length::Fill)
.style(|_t: &iced::Theme| container::Style {
background: Some(Background::Color(surface::sidebar())),
border: Border { width: 1.0, color: surface::card_border(), ..Border::default() },
..container::Style::default()
})
.into()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn alternating_rows_differ_only_in_fill() {
let striped = setting_row_style(0);
let clear = setting_row_style(1);
assert_eq!(striped.border, clear.border);
assert_eq!(striped.text_color, clear.text_color);
assert_eq!(striped.shadow, clear.shadow);
assert_eq!(striped.background, Some(Background::Color(theme::row_tint())));
assert_eq!(clear.background, None);
assert_eq!(setting_row_style(2), striped, "the pattern repeats");
}
#[test]
fn a_single_pending_change_is_not_plural() {
assert_eq!(pending_label(1), "1 pending change");
assert_eq!(pending_label(2), "2 pending changes");
assert_eq!(pending_label(0), "0 pending changes");
}
}