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::{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
29impl<'a, S: Spec> ContentCx<'a> for PanelCx<'a, S> {
30    type Spec = S;
31
32    fn ime_area(&mut self, rect: uzor::Rect) {
33        self.out.ime_area(rect);
34    }
35
36    fn with_paint<R>(
37        &mut self,
38        f: impl FnOnce(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<S>, &Tokens) -> R,
39    ) -> R {
40        f(self.render, &mut self.widgets, self.view, self.tokens)
41    }
42}
43
44impl<'a, S: Spec> ContentCx<'a> for OverlayCx<'a, S> {
45    type Spec = S;
46
47    fn ime_area(&mut self, rect: uzor::Rect) {
48        self.out.ime_area(rect);
49    }
50
51    fn with_paint<R>(
52        &mut self,
53        f: impl FnOnce(&mut dyn RenderContext, &mut Widgets<'a>, &VisualView<S>, &Tokens) -> R,
54    ) -> R {
55        f(self.render, &mut self.widgets, self.view, self.tokens)
56    }
57}