icedwright-probe 0.1.4

What an iced app links for icedwright: semantic annotations, typed test ids and named tasks, all pass-through unless a feature turns them on
Documentation
//! A test widget that reports a container and a text, keeps state, and remembers the cursor
//! it was last handed.

#![allow(
    dead_code,
    reason = "each test binary uses a different part of this module"
)]

use std::sync::{Arc, Mutex};

use iced_core::layout::{self, Layout};
use iced_core::widget::{Operation, Tree, tree};
use iced_core::{
    Clipboard, Element, Event, Length, Rectangle, Shell, Size, Widget, mouse, renderer,
};

pub type Message = ();
pub type El<'a> = Element<'a, Message, (), ()>;

#[derive(Debug, Default)]
pub struct Seen {
    pub cursors: Vec<mouse::Cursor>,
    pub interactions: Vec<mouse::Cursor>,
}

/// A widget with a `u32` of state and two child trees.
#[derive(Debug, Default, Clone)]
pub struct Leaf {
    pub label: &'static str,
    pub seen: Arc<Mutex<Seen>>,
}

impl Leaf {
    pub fn new(label: &'static str) -> Self {
        Self {
            label,
            seen: Arc::default(),
        }
    }
}

impl Widget<Message, (), ()> for Leaf {
    fn size(&self) -> Size<Length> {
        Size::new(Length::Fixed(40.0), Length::Fixed(20.0))
    }

    fn layout(
        &mut self,
        _tree: &mut Tree,
        _renderer: &(),
        _limits: &layout::Limits,
    ) -> layout::Node {
        layout::Node::new(Size::new(40.0, 20.0))
    }

    fn draw(
        &self,
        _tree: &Tree,
        _renderer: &mut (),
        _theme: &(),
        _style: &renderer::Style,
        _layout: Layout<'_>,
        _cursor: mouse::Cursor,
        _viewport: &Rectangle,
    ) {
    }

    fn tag(&self) -> tree::Tag {
        tree::Tag::of::<u32>()
    }

    fn state(&self) -> tree::State {
        tree::State::new(7_u32)
    }

    fn children(&self) -> Vec<Tree> {
        vec![Tree::empty(), Tree::empty()]
    }

    fn diff(&self, tree: &mut Tree) {
        tree.children.truncate(1);
    }

    fn operate(
        &mut self,
        _tree: &mut Tree,
        layout: Layout<'_>,
        _renderer: &(),
        operation: &mut dyn Operation,
    ) {
        operation.container(None, layout.bounds());
        operation.traverse(&mut |operation| {
            operation.text(None, layout.bounds(), self.label);
        });
    }

    fn update(
        &mut self,
        _tree: &mut Tree,
        _event: &Event,
        _layout: Layout<'_>,
        cursor: mouse::Cursor,
        _renderer: &(),
        _clipboard: &mut dyn Clipboard,
        _shell: &mut Shell<'_, Message>,
        _viewport: &Rectangle,
    ) {
        if let Ok(mut seen) = self.seen.lock() {
            seen.cursors.push(cursor);
        }
    }

    fn mouse_interaction(
        &self,
        _tree: &Tree,
        _layout: Layout<'_>,
        cursor: mouse::Cursor,
        _viewport: &Rectangle,
        _renderer: &(),
    ) -> mouse::Interaction {
        if let Ok(mut seen) = self.seen.lock() {
            seen.interactions.push(cursor);
        }
        mouse::Interaction::Pointer
    }
}

impl<'a> From<Leaf> for El<'a> {
    fn from(leaf: Leaf) -> Self {
        Element::new(leaf)
    }
}

/// Runs `operation` over `element` laid out at the origin.
pub fn operate(element: &mut El<'_>, operation: &mut dyn Operation) {
    let mut tree = Tree::new(element.as_widget());
    let node = element.as_widget_mut().layout(
        &mut tree,
        &(),
        &layout::Limits::new(Size::ZERO, Size::INFINITE),
    );
    element
        .as_widget_mut()
        .operate(&mut tree, Layout::new(&node), &(), operation);
}

/// Sends `event` to `element` with `cursor`.
pub fn update(element: &mut El<'_>, tree: &mut Tree, event: &Event, cursor: mouse::Cursor) {
    let node =
        element
            .as_widget_mut()
            .layout(tree, &(), &layout::Limits::new(Size::ZERO, Size::INFINITE));
    let mut messages = Vec::new();
    let mut shell = Shell::new(&mut messages);
    let mut clipboard = iced_core::clipboard::Null;
    element.as_widget_mut().update(
        tree,
        event,
        Layout::new(&node),
        cursor,
        &(),
        &mut clipboard,
        &mut shell,
        &Rectangle::with_size(Size::INFINITE),
    );
}

/// Asks `element` for its interaction with `cursor`.
pub fn interaction(element: &El<'_>, tree: &Tree, cursor: mouse::Cursor) -> mouse::Interaction {
    let node = layout::Node::new(Size::new(40.0, 20.0));
    element.as_widget().mouse_interaction(
        tree,
        Layout::new(&node),
        cursor,
        &Rectangle::with_size(Size::INFINITE),
        &(),
    )
}