uzor-framework 1.5.17

uzor app engine: one kernel of single-writer engines driven by a typed input bus
Documentation
//! The paint door shared by [`PanelCx`] and [`OverlayCx`].

use uzor::render::RenderContext;
use uzor::tokens::Tokens;

use crate::handle::{FrameTime, OverlayCx, PanelCx, VisualView, Widgets};
use crate::types::spec::Spec;

/// A content hook context a widget convenience function can paint into.
///
/// Implemented for [`PanelCx`] and [`OverlayCx`]. The closure receives the
/// disjoint pieces of one frame (paint target, registration face, this
/// frame's input truth, tokens) so a widget can register and draw without
/// reaching an engine.
pub trait ContentCx<'a> {
    /// The app vocabulary of the context.
    type Spec: Spec;

    /// Run `f` against this frame's paint target and registration face.
    fn with_paint<R>(
        &mut self,
        f: impl FnOnce(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<Self::Spec>, &Tokens) -> R,
    ) -> R;

    /// Report the focused field's caret rect so the OS IME window opens there.
    fn ime_area(&mut self, _rect: uzor::Rect) {}

    /// Host clock in whole milliseconds. Same unit as
    /// `TextFieldStore::cursor_visible`.
    fn time_ms(&self) -> u64;
}

impl<'a, S: Spec> ContentCx<'a> for PanelCx<'a, S> {
    type Spec = S;

    fn ime_area(&mut self, rect: uzor::Rect) {
        self.out.ime_area(rect);
    }

    fn time_ms(&self) -> u64 {
        frame_ms(self.time)
    }

    fn with_paint<R>(
        &mut self,
        f: impl FnOnce(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<S>, &Tokens) -> R,
    ) -> R {
        f(self.render, &mut self.widgets, self.view, self.tokens)
    }
}

impl<'a, S: Spec> ContentCx<'a> for OverlayCx<'a, S> {
    type Spec = S;

    fn ime_area(&mut self, rect: uzor::Rect) {
        self.out.ime_area(rect);
    }

    fn time_ms(&self) -> u64 {
        frame_ms(self.time)
    }

    fn with_paint<R>(
        &mut self,
        f: impl FnOnce(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<S>, &Tokens) -> R,
    ) -> R {
        f(self.render, &mut self.widgets, self.view, self.tokens)
    }
}

fn frame_ms(time: FrameTime) -> u64 {
    (time.now.get() * 1000.0).round().max(0.0) as u64
}