use iced_core::window;
use icedwright_check::snapshot::{DiffOptions, PixelMask, Rgba};
use icedwright_locator::{AnyRole, IntoLocator, Locator, RoleKind};
use icedwright_types::units::{Logical, Ratio, ScaleFactor};
use icedwright_types::{Rect, VirtualDuration};
use super::Variant;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Caret {
#[default]
Hide,
Show,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[must_use]
pub struct RenderOptions {
pub caret: Caret,
pub scale: Option<ScaleFactor>,
}
impl RenderOptions {
pub fn new() -> Self {
Self::default()
}
pub fn caret(mut self, caret: Caret) -> Self {
self.caret = caret;
self
}
pub fn scale(mut self, factor: ScaleFactor) -> Self {
self.scale = Some(factor);
self
}
}
const DEFAULT_THRESHOLD: Ratio = match Ratio::new(0.1) {
Ok(ratio) => ratio,
Err(_) => Ratio::ZERO,
};
#[derive(Debug, Clone)]
#[must_use]
pub struct Screenshot {
pub(crate) threshold: Ratio,
pub(crate) max_diff_pixels: u32,
pub(crate) max_diff_ratio: Ratio,
pub(crate) include_anti_aliased: bool,
pub(crate) mask: Vec<Locator<AnyRole>>,
pub(crate) mask_colour: Rgba,
pub(crate) caret: Caret,
pub(crate) full_frame: bool,
pub(crate) clip: Option<Logical<Rect>>,
pub(crate) window: Option<window::Id>,
pub(crate) timeout: Option<VirtualDuration>,
pub(crate) only: Option<fn(&Variant) -> bool>,
pub(crate) scale: Option<ScaleFactor>,
}
impl Default for Screenshot {
fn default() -> Self {
Self {
threshold: DEFAULT_THRESHOLD,
max_diff_pixels: 0,
max_diff_ratio: Ratio::ZERO,
include_anti_aliased: false,
mask: Vec::new(),
mask_colour: Rgba::MASK,
caret: Caret::Hide,
full_frame: false,
clip: None,
window: None,
timeout: None,
only: None,
scale: None,
}
}
}
impl Screenshot {
pub fn new() -> Self {
Self::default()
}
pub fn threshold(mut self, threshold: Ratio) -> Self {
self.threshold = threshold;
self
}
pub fn max_diff_pixels(mut self, pixels: u32) -> Self {
self.max_diff_pixels = pixels;
self
}
pub fn max_diff_ratio(mut self, ratio: Ratio) -> Self {
self.max_diff_ratio = ratio;
self
}
pub fn include_anti_aliased(mut self, on: bool) -> Self {
self.include_anti_aliased = on;
self
}
pub fn mask<R: RoleKind>(mut self, locator: impl IntoLocator<R>) -> Self {
self.mask.push(locator.into_locator().any_role());
self
}
pub fn mask_colour(mut self, colour: Rgba) -> Self {
self.mask_colour = colour;
self
}
pub fn caret(mut self, caret: Caret) -> Self {
self.caret = caret;
self
}
pub fn full_frame(mut self, on: bool) -> Self {
self.full_frame = on;
self
}
pub fn clip(mut self, region: Logical<Rect>) -> Self {
self.clip = Some(region);
self
}
pub fn window(mut self, window: window::Id) -> Self {
self.window = Some(window);
self
}
pub fn timeout(mut self, timeout: impl Into<VirtualDuration>) -> Self {
self.timeout = Some(timeout.into());
self
}
pub fn only(mut self, keep: fn(&Variant) -> bool) -> Self {
self.only = Some(keep);
self
}
pub fn scale(mut self, factor: ScaleFactor) -> Self {
self.scale = Some(factor);
self
}
pub(crate) fn naming_variant(&self, variant: &Variant) -> Variant {
match self.scale {
Some(scale) => variant.scale(scale),
None => *variant,
}
}
pub(crate) fn diff_options(&self, mask: PixelMask) -> DiffOptions {
DiffOptions {
threshold: self.threshold,
include_anti_aliased: self.include_anti_aliased,
max_diff_pixels: self.max_diff_pixels,
max_diff_ratio: self.max_diff_ratio,
mask,
mask_colour: self.mask_colour,
}
}
}