Skip to main content

uzor_framework/widgets/
host.rs

1//! The paint door shared by [`PanelCx`] and [`OverlayCx`].
2
3use uzor::render::RenderContext;
4use uzor::tokens::Tokens;
5
6use crate::handle::{FrameTime, OverlayCx, PanelCx, VisualView, Widgets};
7use crate::types::spec::Spec;
8
9/// A content hook context a widget convenience function can paint into.
10///
11/// Implemented for [`PanelCx`] and [`OverlayCx`]. The closure receives the
12/// disjoint pieces of one frame (paint target, registration face, this
13/// frame's input truth, tokens) so a widget can register and draw without
14/// reaching an engine.
15pub trait ContentCx<'a> {
16    /// The app vocabulary of the context.
17    type Spec: Spec;
18
19    /// Run `f` against this frame's paint target and registration face.
20    fn with_paint<R>(
21        &mut self,
22        f: impl FnOnce(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<Self::Spec>, &Tokens) -> R,
23    ) -> R;
24
25    /// Report the focused field's caret rect so the OS IME window opens there.
26    fn ime_area(&mut self, _rect: uzor::Rect) {}
27
28    /// Host clock in whole milliseconds. Same unit as
29    /// `TextFieldStore::cursor_visible`.
30    fn time_ms(&self) -> u64;
31}
32
33impl<'a, S: Spec> ContentCx<'a> for PanelCx<'a, S> {
34    type Spec = S;
35
36    fn ime_area(&mut self, rect: uzor::Rect) {
37        self.out.ime_area(rect);
38    }
39
40    fn time_ms(&self) -> u64 {
41        frame_ms(self.time)
42    }
43
44    fn with_paint<R>(
45        &mut self,
46        f: impl FnOnce(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<S>, &Tokens) -> R,
47    ) -> R {
48        f(self.render, &mut self.widgets, self.view, self.tokens)
49    }
50}
51
52impl<'a, S: Spec> ContentCx<'a> for OverlayCx<'a, S> {
53    type Spec = S;
54
55    fn ime_area(&mut self, rect: uzor::Rect) {
56        self.out.ime_area(rect);
57    }
58
59    fn time_ms(&self) -> u64 {
60        frame_ms(self.time)
61    }
62
63    fn with_paint<R>(
64        &mut self,
65        f: impl FnOnce(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<S>, &Tokens) -> R,
66    ) -> R {
67        f(self.render, &mut self.widgets, self.view, self.tokens)
68    }
69}
70
71fn frame_ms(time: FrameTime) -> u64 {
72    (time.now.get() * 1000.0).round().max(0.0) as u64
73}