icedwright 0.0.1

The icedwright test harness: run an iced 0.14 program headlessly on virtual time
Documentation
//! Rendering a window headlessly on tiny-skia, at its scale factor.

use iced_core::renderer::Headless;
use iced_core::{Event, Size as IcedSize, renderer, window};
use icedwright_types::units::{Logical, Physical, ScaleFactor};
use icedwright_types::{Frame, Size};

use crate::error::{HarnessError, Result};
use crate::harness::{Harness, TestProgram, with_ui};

impl<P: TestProgram> Harness<P> {
    /// Draws the window as it is now and returns its pixels at its physical size.
    pub fn render(&mut self, window: window::Id) -> Result<Frame> {
        self.render_frame(window, false)
    }

    /// Draws the window with `unfocus` sending `Unfocused` to its widgets before the draw and
    /// `Focused` after it, inside the same interface, so iced's text inputs and editors draw no
    /// caret. No turn of the app runs and every message is dropped, so the app never sees it.
    pub(crate) fn render_frame(&mut self, window: window::Id, unfocus: bool) -> Result<Frame> {
        self.render_frame_at(window, unfocus, None)
    }

    /// [`render_frame`](Self::render_frame) with `system_scale` in place of the window's
    /// system scale factor. The layout and the app are untouched: the same interface is
    /// rasterised at more or fewer pixels, and no `Rescaled` reaches the app.
    pub(crate) fn render_frame_at(
        &mut self,
        window: window::Id,
        unfocus: bool,
        system_scale: Option<ScaleFactor>,
    ) -> Result<Frame> {
        let (rgba, size, scale) = self.draw_window(window, unfocus, system_scale)?;
        let frame = Frame::new(
            rgba,
            Physical::new(Size::new(size.width, size.height)),
            scale,
        )?;
        self.observe(|observer| observer.frame(window, &frame));
        Ok(frame)
    }

    /// The window as `window::screenshot` answers it.
    pub(crate) fn screenshot(&mut self, window: window::Id) -> Result<window::Screenshot> {
        let (rgba, size, scale) = self.draw_window(window, false, None)?;
        Ok(window::Screenshot::new(rgba, size, scale.get()))
    }

    fn draw_window(
        &mut self,
        id: window::Id,
        unfocus: bool,
        system_scale: Option<ScaleFactor>,
    ) -> Result<(Vec<u8>, IcedSize<u32>, ScaleFactor)> {
        let window = self.set.get(id)?;
        let info = window.info.clone();
        let size = info.layout_size();
        let (physical, scale) = match system_scale {
            None => (info.physical_size.get(), info.scale_factor),
            Some(system) => {
                let program = info.scale_factor.get() / window.system_scale.get();
                let scale = ScaleFactor::new(system.get() * program).unwrap_or(system);
                let physical = Logical(icedwright_types::Size::new(size.width, size.height))
                    .to_physical_pixels(scale)
                    .get();
                (physical, scale)
            }
        };
        let physical = IcedSize::new(physical.width, physical.height);
        let now = self.to_iced(self.now());
        let Self {
            program,
            state,
            surfaces,
            tokio,
            ..
        } = self;
        let surface = surfaces
            .get_mut(&id)
            .ok_or(HarnessError::NoSuchWindow(id))?;
        let _enter = tokio.as_ref().map(tokio::runtime::Runtime::enter);
        let style = program.style(state, &surface.theme);
        let cursor = surface.cursor;
        let theme = &surface.theme;
        with_ui(
            program,
            state,
            id,
            size,
            &mut surface.cache,
            &mut surface.renderer,
            |ui, renderer| {
                // Widgets such as `button` and `text_input` pick their status on a redraw and
                // draw as disabled without one, and every render builds them afresh. Messages
                // from this redraw are dropped, so rendering never changes the app.
                let mut dropped = Vec::new();
                let redraw = Event::Window(window::Event::RedrawRequested(now));
                let before = if unfocus {
                    vec![Event::Window(window::Event::Unfocused), redraw]
                } else {
                    vec![redraw]
                };
                let _ = ui.update(
                    &before,
                    cursor,
                    renderer,
                    &mut iced_core::clipboard::Null,
                    &mut dropped,
                );
                ui.draw(
                    renderer,
                    theme,
                    &renderer::Style {
                        text_color: style.text_color,
                    },
                    cursor,
                );
                if unfocus {
                    let _ = ui.update(
                        &[Event::Window(window::Event::Focused)],
                        cursor,
                        renderer,
                        &mut iced_core::clipboard::Null,
                        &mut dropped,
                    );
                }
            },
        );
        let rgba = surface
            .renderer
            .screenshot(physical, scale.get(), style.background_color);
        Ok((rgba, physical, scale))
    }
}