mod anchor;
mod badges;
mod controls;
mod field;
mod layout;
mod ring;
mod selection;
mod suggestions;
pub use anchor::{anchored, Anchored};
pub use badges::{chip, config_line, keycap};
pub use controls::{
dropdown_menu_style, dropdown_style, segment_style, segmented, segmented_choice,
slider_style, step_index, stepped_slider, toggle, toggle_style, value_slider, SegmentLook,
};
pub use field::{inset_field_style, inset_input_style, search_field};
pub use layout::{
hero_card, hint_text, page_header, pending_bar, pending_label, setting_list, setting_row,
setting_row_style, status_dot, SETTING_ROW_GAP,
};
pub use ring::{countdown_ring, remaining_fraction};
pub use selection::{section_label, selectable_row_style, spaced_caps, Tint};
pub use suggestions::{suggestions, Suggestion};
use crate::theme::{self, spacing, surface, FontScale};
use iced::widget::{button, column, container, row, text, text::IntoFragment, Text};
use iced::{Background, Border, Color, Element, Length, Theme};
pub fn scaled_text<'a>(
content: impl IntoFragment<'a>,
base_size: f32,
scale: FontScale,
) -> Text<'a> {
text(content).size(scale.apply(base_size))
}
pub fn meta_text<'a>(content: impl IntoFragment<'a>, base_size: f32, scale: FontScale) -> Text<'a> {
scaled_text(content, base_size, scale).color(theme::text_dim())
}
fn card_style(_theme: &Theme) -> container::Style {
container::Style {
background: Some(Background::Color(surface::card())),
border: Border {
radius: crate::density::card_radius().into(),
width: 1.0,
color: surface::card_border(),
},
..container::Style::default()
}
}
pub fn section<'a, Message: 'a>(
title: impl AsRef<str>,
scale: FontScale,
content: impl Into<Element<'a, Message>>,
) -> Element<'a, Message> {
column![
section_label(title.as_ref(), scale),
container(content.into())
.padding(spacing::MD)
.width(Length::Fill)
.style(card_style)
]
.spacing(spacing::SM)
.into()
}
pub fn divider<'a, Message: 'a>() -> Element<'a, Message> {
container(column![])
.width(Length::Fill)
.height(Length::Fixed(1.0))
.style(|_theme: &Theme| container::Style {
background: Some(Background::Color(surface::card_border())),
..container::Style::default()
})
.into()
}
const CONTROL_MAX_WIDTH: f32 = 340.0;
pub fn row_field<'a, Message: 'a>(
label: impl IntoFragment<'a>,
control: impl Into<Element<'a, Message>>,
) -> Element<'a, Message> {
let scale = FontScale(theme::active().font_scale);
setting_row(
1,
label,
None,
container(control.into())
.max_width(CONTROL_MAX_WIDTH)
.width(Length::Fill)
.align_x(iced::alignment::Horizontal::Right),
scale,
)
}
fn primary_style(theme: &Theme, status: button::Status) -> button::Style {
let palette = theme.extended_palette();
let base = button::Style {
background: Some(Background::Color(palette.primary.base.color)),
text_color: palette.primary.base.text,
border: Border {
radius: 8.0.into(),
..Border::default()
},
..button::Style::default()
};
match status {
button::Status::Hovered => button::Style {
background: Some(Background::Color(palette.primary.strong.color)),
..base
},
button::Status::Disabled => button::Style {
background: base.background.map(|b| match b {
Background::Color(c) => Background::Color(Color { a: 0.4, ..c }),
other => other,
}),
text_color: Color {
a: 0.4,
..base.text_color
},
..base
},
_ => base,
}
}
fn secondary_style(_theme: &Theme, status: button::Status) -> button::Style {
let base = button::Style {
background: Some(Background::Color(surface::row())),
text_color: crate::theme::text(),
border: Border {
radius: 8.0.into(),
width: 1.0,
color: surface::card_border(),
},
..button::Style::default()
};
match status {
button::Status::Hovered => button::Style {
background: Some(Background::Color(surface::card_border())),
..base
},
button::Status::Disabled => button::Style {
text_color: Color {
a: 0.4,
..base.text_color
},
..base
},
_ => base,
}
}
fn danger_style(theme: &Theme, status: button::Status) -> button::Style {
let palette = theme.extended_palette();
let base = button::Style {
background: Some(Background::Color(surface::row())),
text_color: palette.danger.base.color,
border: Border {
radius: 8.0.into(),
width: 1.0,
color: palette.danger.base.color,
},
..button::Style::default()
};
match status {
button::Status::Hovered => button::Style {
background: Some(Background::Color(palette.primary.strong.color)),
text_color: palette.primary.strong.text,
..base
},
_ => base,
}
}
pub fn primary_button<'a, Message: Clone + 'a>(
label: impl IntoFragment<'a>,
) -> button::Button<'a, Message> {
button(text(label)).style(primary_style)
}
pub fn secondary_button<'a, Message: Clone + 'a>(
label: impl IntoFragment<'a>,
) -> button::Button<'a, Message> {
button(text(label)).style(secondary_style)
}
pub fn danger_button<'a, Message: Clone + 'a>(
label: &'a str,
on_press: Message,
) -> Element<'a, Message> {
button(text(label))
.on_press(on_press)
.style(danger_style)
.into()
}
pub fn tri_state<'a, Message: Clone + 'a>(
value: Option<bool>,
on_select: impl Fn(Option<bool>) -> Message,
) -> Element<'a, Message> {
let option = |label: &'a str, choice: Option<bool>| {
let button = if value == choice {
primary_button(label)
} else {
secondary_button(label)
};
button.on_press(on_select(choice))
};
row![
option("Any", None),
option("Yes", Some(true)),
option("No", Some(false)),
]
.spacing(spacing::XS)
.into()
}
pub fn confirm_dialog<'a, Message: Clone + 'a>(
title: &'a str,
body: &'a str,
diff_text: &'a str,
on_confirm: Message,
on_cancel: Message,
) -> Element<'a, Message> {
let diff = container(text(diff_text).font(crate::theme::mono_font()).size(13))
.padding(spacing::SM)
.width(Length::Fill)
.style(|_theme: &Theme| container::Style {
background: Some(Background::Color(surface::row())),
border: Border {
radius: 6.0.into(),
width: 1.0,
color: surface::card_border(),
},
..container::Style::default()
});
container(
column![
text(title).size(18),
text(body),
diff,
row![
secondary_button("Cancel").on_press(on_cancel),
primary_button("Confirm").on_press(on_confirm),
]
.spacing(spacing::SM),
]
.spacing(spacing::MD)
.padding(spacing::LG)
.max_width(560.0),
)
.style(card_style)
.into()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_primary_button_stays_the_accent_when_hovered() {
let t = theme::app_theme();
let palette = t.extended_palette();
for status in [button::Status::Active, button::Status::Hovered] {
let style = primary_style(&t, status);
assert_ne!(
style.background,
Some(Background::Color(palette.danger.base.color)),
"{status:?}"
);
}
assert_eq!(
primary_style(&t, button::Status::Hovered).background,
Some(Background::Color(palette.primary.strong.color))
);
}
}