use std::fmt;
use iced_core::theme;
use icedwright_types::Size as LogicalSize;
use icedwright_types::units::{Logical, ScaleFactor};
use crate::config::HarnessConfig;
use crate::harness::TestProgram;
use crate::testing::matrix::{Size, Theme};
#[must_use]
pub fn theme_name(theme: Theme) -> &'static str {
match theme {
Theme::Light => "light",
Theme::Dark => "dark",
}
}
#[must_use]
pub fn size_name(size: Size) -> String {
match size {
Size::Phone => "phone".to_owned(),
Size::Tablet => "tablet".to_owned(),
Size::Desktop => "desktop".to_owned(),
Size::Custom(width, height) => format!("w{width}h{height}"),
}
}
#[must_use]
pub fn scale_name(scale: ScaleFactor) -> String {
format!("{}x", scale.get())
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[must_use]
pub struct Variant {
theme: Option<Theme>,
size: Option<Size>,
scale: Option<ScaleFactor>,
}
impl Variant {
pub fn new() -> Self {
Self::default()
}
pub fn theme(mut self, theme: Theme) -> Self {
self.theme = Some(theme);
self
}
pub fn size(mut self, size: Size) -> Self {
self.size = Some(size);
self
}
pub fn scale(mut self, scale: ScaleFactor) -> Self {
self.scale = Some(scale);
self
}
#[must_use]
pub fn theme_axis(&self) -> Option<Theme> {
self.theme
}
#[must_use]
pub fn size_axis(&self) -> Option<Size> {
self.size
}
#[must_use]
pub fn scale_axis(&self) -> Option<ScaleFactor> {
self.scale
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.theme.is_none() && self.size.is_none() && self.scale.is_none()
}
pub fn apply<P: TestProgram>(self, config: HarnessConfig<P>) -> HarnessConfig<P> {
config.variant(self)
}
pub(crate) fn configure(
&self,
system_theme: &mut theme::Mode,
size: &mut Option<Logical<LogicalSize>>,
scale: &mut ScaleFactor,
) {
if let Some(axis) = self.theme {
*system_theme = axis.into();
}
if let Some(axis) = self.size {
*size = Some(axis.into());
}
if let Some(axis) = self.scale {
*scale = axis;
}
}
#[must_use]
pub fn semantic_suffix(&self, per_theme: bool) -> String {
let theme = self
.theme
.filter(|_| per_theme)
.map(|t| theme_name(t).to_owned());
let size = self.size.map(size_name);
[theme, size]
.into_iter()
.flatten()
.collect::<Vec<_>>()
.join("-")
}
#[must_use]
pub fn file_stem(&self, name: &str) -> String {
if self.is_empty() {
return name.to_owned();
}
format!("{name}@{self}")
}
}
impl fmt::Display for Variant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let parts = [
self.theme.map(|t| theme_name(t).to_owned()),
self.size.map(size_name),
self.scale.map(scale_name),
];
let joined: Vec<String> = parts.into_iter().flatten().collect();
f.write_str(&joined.join("-"))
}
}