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    /// The frame clock in milliseconds, unrounded: the stamp scroll containers share
33    /// within one frame.
34    fn frame_now_ms(&self) -> f64 {
35        self.time_ms() as f64
36    }
37
38    /// Ask for another frame (an animation or a scroll is still moving).
39    fn request_frame(&mut self) {}
40
41    /// Painting into an overlay (a modal makes the page's scroll containers inert to keys).
42    fn is_overlay(&self) -> bool {
43        false
44    }
45}
46
47impl<'a, S: Spec> ContentCx<'a> for PanelCx<'a, S> {
48    type Spec = S;
49
50    fn frame_now_ms(&self) -> f64 {
51        self.time.now.0 * 1000.0
52    }
53
54    fn request_frame(&mut self) {
55        self.out.request_frame();
56    }
57
58    fn is_overlay(&self) -> bool {
59        false
60    }
61
62    fn ime_area(&mut self, rect: uzor::Rect) {
63        self.out.ime_area(rect);
64    }
65
66    fn time_ms(&self) -> u64 {
67        frame_ms(self.time)
68    }
69
70    fn with_paint<R>(
71        &mut self,
72        f: impl FnOnce(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<S>, &Tokens) -> R,
73    ) -> R {
74        let mut f = Some(f);
75        with_paint_dyn(self.render, &mut self.widgets, self.view, self.tokens, &mut |r, w, v, tok| {
76            f.take().expect("with_paint once")(r, w, v, tok)
77        })
78    }
79}
80
81impl<'a, S: Spec> ContentCx<'a> for OverlayCx<'a, S> {
82    type Spec = S;
83
84    fn frame_now_ms(&self) -> f64 {
85        self.time.now.0 * 1000.0
86    }
87
88    fn request_frame(&mut self) {
89        self.out.request_frame();
90    }
91
92    fn is_overlay(&self) -> bool {
93        true
94    }
95
96    fn ime_area(&mut self, rect: uzor::Rect) {
97        self.out.ime_area(rect);
98    }
99
100    fn time_ms(&self) -> u64 {
101        frame_ms(self.time)
102    }
103
104    fn with_paint<R>(
105        &mut self,
106        f: impl FnOnce(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<S>, &Tokens) -> R,
107    ) -> R {
108        let mut f = Some(f);
109        with_paint_dyn(self.render, &mut self.widgets, self.view, self.tokens, &mut |r, w, v, tok| {
110            f.take().expect("with_paint once")(r, w, v, tok)
111        })
112    }
113}
114
115fn with_paint_dyn<'a, S: Spec, R>(
116    render: &mut dyn RenderContext,
117    widgets: &mut Widgets<'a>,
118    view: &VisualView<S>,
119    tokens: &Tokens,
120    f: &mut dyn FnMut(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<S>, &Tokens) -> R,
121) -> R {
122    f(render, widgets, view, tokens)
123}
124
125fn frame_ms(time: FrameTime) -> u64 {
126    (time.now.get() * 1000.0).round().max(0.0) as u64
127}