use std::borrow::Cow;
use crate::model::DialogButtonOrder;
#[derive(Clone, Debug)]
pub struct ResolutionContext {
pub font_dpi: f32,
pub button_order: DialogButtonOrder,
pub icon_theme: Option<Cow<'static, str>>,
}
impl ResolutionContext {
#[must_use]
pub fn from_system() -> Self {
Self::with_detected_icon_theme(crate::detect::system_icon_theme())
}
pub(crate) fn with_detected_icon_theme(detected: crate::Result<String>) -> Self {
Self::with_font_dpi(crate::detect::system_font_dpi(), detected)
}
pub(crate) fn with_font_dpi(font_dpi: f32, detected: crate::Result<String>) -> Self {
Self {
font_dpi,
button_order: crate::resolve::inheritance::platform_button_order(),
icon_theme: detected.ok().map(Cow::Owned),
}
}
#[must_use]
pub fn for_tests() -> Self {
Self {
font_dpi: 96.0,
button_order: DialogButtonOrder::PrimaryRight,
icon_theme: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn with_font_dpi_is_its_arguments() {
let ctx = ResolutionContext::with_font_dpi(
123.3,
Err(crate::Error::PlatformUnsupported {
platform: "the test's failing detection",
}),
);
assert_eq!(ctx.font_dpi, 123.3);
assert!(ctx.icon_theme.is_none());
assert_eq!(
ctx.button_order,
crate::resolve::inheritance::platform_button_order()
);
let ctx = ResolutionContext::with_font_dpi(144.0, Ok("detected-theme".to_string()));
assert_eq!(ctx.font_dpi, 144.0);
assert_eq!(ctx.icon_theme.as_deref(), Some("detected-theme"));
}
}