use crate::density;
use crate::theme::{self, surface, FontScale};
use iced::widget::{button, Text};
use iced::{Background, Border};
pub fn selectable_row_style(
theme: &iced::Theme,
status: button::Status,
selected: bool,
) -> button::Style {
let palette = theme.extended_palette();
let background = if selected {
Some(Background::Color(palette.primary.weak.color))
} else {
match status {
button::Status::Hovered => Some(Background::Color(surface::row())),
_ => None,
}
};
button::Style {
background,
text_color: theme::text(),
border: Border { radius: density::inner_radius().into(), ..Border::default() },
..button::Style::default()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Tint {
Accent,
Info,
Success,
Warning,
Error,
Dim,
}
impl Tint {
pub fn color(self) -> hyprforge_look::Color {
let t = theme::active();
match self {
Tint::Accent => t.accent,
Tint::Info => t.info,
Tint::Success => t.success,
Tint::Warning => t.warning,
Tint::Error => t.error,
Tint::Dim => t.surfaces.text_dim,
}
}
pub fn iced(self) -> iced::Color {
crate::color::to_iced(self.color())
}
}
pub fn spaced_caps(title: &str) -> String {
title
.to_uppercase()
.chars()
.map(|c| c.to_string())
.collect::<Vec<_>>()
.join("\u{2009}")
}
pub fn section_label<'a>(title: &str, scale: FontScale) -> Text<'a> {
super::meta_text(spaced_caps(title), density::SECTION_LABEL_BASE, scale)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_the_selected_row_ever_uses_the_accent_colour() {
let theme = theme::app_theme();
let accent_bg = theme.extended_palette().primary.weak.color;
let selected = selectable_row_style(&theme, button::Status::Active, true);
assert_eq!(selected.background, Some(Background::Color(accent_bg)));
for status in [
button::Status::Active,
button::Status::Hovered,
button::Status::Pressed,
button::Status::Disabled,
] {
let unselected = selectable_row_style(&theme, status, false);
assert_ne!(
unselected.background,
Some(Background::Color(accent_bg)),
"a non-selected row must never render the accent colour ({status:?})"
);
}
}
#[test]
fn hover_is_a_distinct_elevation_from_both_plain_and_selected() {
let theme = theme::app_theme();
let plain = selectable_row_style(&theme, button::Status::Active, false);
let hovered = selectable_row_style(&theme, button::Status::Hovered, false);
let selected = selectable_row_style(&theme, button::Status::Active, true);
assert_ne!(plain.background, hovered.background);
assert_ne!(hovered.background, selected.background);
}
#[test]
fn a_state_colour_is_never_used_for_selection() {
let theme = theme::app_theme();
let Some(Background::Color(selected)) =
selectable_row_style(&theme, button::Status::Active, true).background
else {
panic!("a selected row has a fill");
};
for state in [Tint::Info, Tint::Success, Tint::Warning, Tint::Error] {
let c = state.iced();
assert_ne!(
(selected.r, selected.g, selected.b),
(c.r, c.g, c.b),
"selection drawn in {state:?}"
);
}
}
#[test]
fn every_tint_is_its_own_colour() {
let all = [Tint::Accent, Tint::Info, Tint::Success, Tint::Warning, Tint::Error, Tint::Dim];
for (i, a) in all.iter().enumerate() {
for b in &all[i + 1..] {
assert_ne!(a.color(), b.color(), "{a:?} and {b:?}");
}
}
}
#[test]
fn a_heading_is_capitals_with_thin_spaces_between_the_letters() {
assert_eq!(spaced_caps("Wi-Fi"), "W\u{2009}I\u{2009}-\u{2009}F\u{2009}I");
assert_eq!(spaced_caps(""), "");
}
}