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
//! The `semantic(..)` wrapper apps put around an element to describe it.

use iced_core::Element;
use icedwright_types::role::{AnyRole, RoleKind, RoleType, Untyped};

use crate::annotation::Annotation;
use crate::test_id::IntoTestId;

/// An element with an annotation. Convert it into an [`Element`] to use it.
///
/// With the `semantics` feature on it is a widget that forwards everything to its content and
/// reports the annotation to the recorder. With the feature off, converting it gives back the
/// content itself.
#[must_use = "a Semantic does nothing until it is converted into an Element"]
pub struct Semantic<'a, Message, Theme, Renderer, R: RoleKind = AnyRole> {
    pub(crate) content: Element<'a, Message, Theme, Renderer>,
    pub(crate) annotation: Annotation<'a, R>,
    #[cfg(feature = "semantics")]
    pub(crate) cursor: Option<icedwright_types::CursorSeen>,
}

impl<Message, Theme, Renderer, R: RoleKind> std::fmt::Debug
    for Semantic<'_, Message, Theme, Renderer, R>
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Semantic")
            .field("annotation", &self.annotation)
            .finish_non_exhaustive()
    }
}

/// Wraps an element so it can be described: `semantic(button("Delete")).role(role::Button)`.
pub fn semantic<'a, Message, Theme, Renderer>(
    content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Semantic<'a, Message, Theme, Renderer, AnyRole> {
    Semantic {
        content: content.into(),
        annotation: Annotation::new(),
        #[cfg(feature = "semantics")]
        cursor: None,
    }
}

/// Wraps an element with only a test id.
pub fn test_id<'a, Message, Theme, Renderer>(
    id: impl IntoTestId<'a>,
    content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Semantic<'a, Message, Theme, Renderer, AnyRole> {
    semantic(content).test_id(id)
}

/// `.semantic()` on anything that converts into an [`Element`], for builder chains.
pub trait SemanticExt<'a, Message, Theme, Renderer>:
    Into<Element<'a, Message, Theme, Renderer>>
{
    /// Wraps the element so it can be described.
    fn semantic(self) -> Semantic<'a, Message, Theme, Renderer, AnyRole> {
        semantic(self)
    }
}

impl<'a, Message, Theme, Renderer, T> SemanticExt<'a, Message, Theme, Renderer> for T where
    T: Into<Element<'a, Message, Theme, Renderer>>
{
}

impl<'a, Message, Theme, Renderer, R: RoleKind> Semantic<'a, Message, Theme, Renderer, R> {
    /// Gives the element a role. Only once: a second call does not compile.
    #[inline]
    pub fn role<R2: RoleType>(self, role: R2) -> Semantic<'a, Message, Theme, Renderer, R2>
    where
        R: Untyped,
    {
        Semantic {
            content: self.content,
            annotation: self.annotation.role(role),
            #[cfg(feature = "semantics")]
            cursor: self.cursor,
        }
    }

    #[inline]
    pub(crate) fn map_annotation(
        self,
        change: impl FnOnce(Annotation<'a, R>) -> Annotation<'a, R>,
    ) -> Self {
        Self {
            content: self.content,
            annotation: change(self.annotation),
            #[cfg(feature = "semantics")]
            cursor: self.cursor,
        }
    }
}

impl<'a, Message, Theme, Renderer, R> From<Semantic<'a, Message, Theme, Renderer, R>>
    for Element<'a, Message, Theme, Renderer>
where
    Message: 'a,
    Theme: 'a,
    Renderer: iced_core::Renderer + 'a,
    R: RoleKind,
{
    #[inline]
    fn from(semantic: Semantic<'a, Message, Theme, Renderer, R>) -> Self {
        #[cfg(feature = "semantics")]
        {
            Element::new(semantic)
        }
        #[cfg(not(feature = "semantics"))]
        {
            semantic.content
        }
    }
}

#[cfg(feature = "semantics")]
mod widget {
    use iced_core::layout::{self, Layout};
    use iced_core::widget::{Operation, Tree, tree};
    use iced_core::{
        Clipboard, Event, Length, Rectangle, Shell, Size, Vector, mouse, overlay, renderer,
    };
    use icedwright_types::CursorSeen;
    use icedwright_types::role::RoleKind;

    use super::Semantic;
    use crate::annotation::report;

    fn seen(cursor: mouse::Cursor) -> CursorSeen {
        match cursor {
            mouse::Cursor::Available(_) => CursorSeen::Available,
            mouse::Cursor::Levitating(_) => CursorSeen::Levitating,
            mouse::Cursor::Unavailable => CursorSeen::Unavailable,
        }
    }

    impl<Message, Theme, Renderer, R> iced_core::Widget<Message, Theme, Renderer>
        for Semantic<'_, Message, Theme, Renderer, R>
    where
        Renderer: iced_core::Renderer,
        R: RoleKind,
    {
        fn size(&self) -> Size<Length> {
            self.content.as_widget().size()
        }

        fn size_hint(&self) -> Size<Length> {
            self.content.as_widget().size_hint()
        }

        fn layout(
            &mut self,
            tree: &mut Tree,
            renderer: &Renderer,
            limits: &layout::Limits,
        ) -> layout::Node {
            self.content.as_widget_mut().layout(tree, renderer, limits)
        }

        fn draw(
            &self,
            tree: &Tree,
            renderer: &mut Renderer,
            theme: &Theme,
            style: &renderer::Style,
            layout: Layout<'_>,
            cursor: mouse::Cursor,
            viewport: &Rectangle,
        ) {
            self.content
                .as_widget()
                .draw(tree, renderer, theme, style, layout, cursor, viewport);
        }

        fn tag(&self) -> tree::Tag {
            self.content.as_widget().tag()
        }

        fn state(&self) -> tree::State {
            self.content.as_widget().state()
        }

        fn children(&self) -> Vec<Tree> {
            self.content.as_widget().children()
        }

        fn diff(&self, tree: &mut Tree) {
            self.content.as_widget().diff(tree);
        }

        fn operate(
            &mut self,
            tree: &mut Tree,
            layout: Layout<'_>,
            renderer: &Renderer,
            operation: &mut dyn Operation,
        ) {
            let cursor = self.cursor;
            let annotation = &self.annotation;
            report(operation, layout.bounds(), || {
                annotation.to_static().cursor_seen(cursor)
            });
            let content = &mut self.content;
            operation.traverse(&mut |operation| {
                content
                    .as_widget_mut()
                    .operate(tree, layout, renderer, operation);
            });
        }

        fn update(
            &mut self,
            tree: &mut Tree,
            event: &Event,
            layout: Layout<'_>,
            cursor: mouse::Cursor,
            renderer: &Renderer,
            clipboard: &mut dyn Clipboard,
            shell: &mut Shell<'_, Message>,
            viewport: &Rectangle,
        ) {
            if matches!(event, Event::Mouse(_)) {
                self.cursor = Some(seen(cursor));
            }
            self.content.as_widget_mut().update(
                tree, event, layout, cursor, renderer, clipboard, shell, viewport,
            );
        }

        fn mouse_interaction(
            &self,
            tree: &Tree,
            layout: Layout<'_>,
            cursor: mouse::Cursor,
            viewport: &Rectangle,
            renderer: &Renderer,
        ) -> mouse::Interaction {
            self.content
                .as_widget()
                .mouse_interaction(tree, layout, cursor, viewport, renderer)
        }

        fn overlay<'b>(
            &'b mut self,
            tree: &'b mut Tree,
            layout: Layout<'b>,
            renderer: &Renderer,
            viewport: &Rectangle,
            translation: Vector,
        ) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
            self.content
                .as_widget_mut()
                .overlay(tree, layout, renderer, viewport, translation)
        }
    }
}