uzor_framework/widgets/
host.rs1use uzor::render::RenderContext;
4use uzor::tokens::Tokens;
5
6use crate::handle::{FrameTime, OverlayCx, PanelCx, VisualView, Widgets};
7use crate::types::spec::Spec;
8
9pub trait ContentCx<'a> {
16 type Spec: Spec;
18
19 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 fn ime_area(&mut self, _rect: uzor::Rect) {}
27
28 fn time_ms(&self) -> u64;
31
32 fn frame_now_ms(&self) -> f64 {
35 self.time_ms() as f64
36 }
37
38 fn request_frame(&mut self) {}
40
41 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}