icedwright 0.0.1

The icedwright test harness: run an iced 0.14 program headlessly on virtual time
Documentation
//! Widget operations that act on the n-th focusable or scrollable, counted in traversal order
//! as the semantic tree counts them, so widgets without a `widget::Id` can be reached.

use iced_core::widget::operation::scrollable::AbsoluteOffset;
use iced_core::widget::operation::{Focusable, Outcome, Scrollable, TextInput};
use iced_core::widget::{Id, Operation};
use iced_core::{Rectangle, Vector};

/// Focuses the focusable with this ordinal and unfocuses every other, as iced's `focus(id)`
/// does. Its outcome is whether the target was met.
#[derive(Debug)]
pub(crate) struct FocusOrdinal {
    target: u32,
    seen: u32,
    found: bool,
}

impl FocusOrdinal {
    pub(crate) fn new(target: u32) -> Self {
        Self {
            target,
            seen: 0,
            found: false,
        }
    }
}

impl Operation<bool> for FocusOrdinal {
    fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<bool>)) {
        operate(self);
    }

    fn focusable(&mut self, _id: Option<&Id>, _bounds: Rectangle, state: &mut dyn Focusable) {
        if self.seen == self.target {
            self.found = true;
            if !state.is_focused() {
                state.focus();
            }
        } else if state.is_focused() {
            state.unfocus();
        }
        self.seen += 1;
    }

    fn finish(&self) -> Outcome<bool> {
        Outcome::Some(self.found)
    }
}

/// Selects all of the text input whose focusable has this ordinal. A text input reports
/// `text_input` just before `focusable`, so the ordinal it will get is the count so far. Its
/// outcome is whether a text input was there.
#[derive(Debug)]
pub(crate) struct SelectAllOrdinal {
    target: u32,
    seen: u32,
    selected: bool,
}

impl SelectAllOrdinal {
    pub(crate) fn new(target: u32) -> Self {
        Self {
            target,
            seen: 0,
            selected: false,
        }
    }
}

impl Operation<bool> for SelectAllOrdinal {
    fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<bool>)) {
        operate(self);
    }

    fn text_input(&mut self, _id: Option<&Id>, _bounds: Rectangle, state: &mut dyn TextInput) {
        if self.seen == self.target {
            state.select_all();
            self.selected = true;
        }
    }

    fn focusable(&mut self, _id: Option<&Id>, _bounds: Rectangle, _state: &mut dyn Focusable) {
        self.seen += 1;
    }

    fn finish(&self) -> Outcome<bool> {
        Outcome::Some(self.selected)
    }
}

/// Scrolls the scrollable with this ordinal to an absolute offset, and reports the
/// translation it had before.
#[derive(Debug)]
pub(crate) struct ScrollOrdinal {
    target: u32,
    seen: u32,
    offset: Option<Vector>,
    translation: Option<Vector>,
}

impl ScrollOrdinal {
    /// Scrolls to `offset`.
    pub(crate) fn to(target: u32, offset: Vector) -> Self {
        Self {
            target,
            seen: 0,
            offset: Some(offset),
            translation: None,
        }
    }

    /// Only reads the translation.
    pub(crate) fn read(target: u32) -> Self {
        Self {
            target,
            seen: 0,
            offset: None,
            translation: None,
        }
    }
}

impl Operation<Option<Vector>> for ScrollOrdinal {
    fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<Option<Vector>>)) {
        operate(self);
    }

    fn scrollable(
        &mut self,
        _id: Option<&Id>,
        _bounds: Rectangle,
        _content_bounds: Rectangle,
        translation: Vector,
        state: &mut dyn Scrollable,
    ) {
        if self.seen == self.target {
            self.translation = Some(translation);
            if let Some(offset) = self.offset {
                state.scroll_to(AbsoluteOffset {
                    x: Some(offset.x),
                    y: Some(offset.y),
                });
            }
        }
        self.seen += 1;
    }

    fn finish(&self) -> Outcome<Option<Vector>> {
        Outcome::Some(self.translation)
    }
}