1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! Where content is displayed, as opposed to where it was laid out: transforms composed
//! by placement while a pass is built, the ones the last pass published (input maps
//! pointer positions back through them), the visual effects being built, and the
//! retained state of subtree effects.
use super::{Context, Id};
use crate::{components::effects::EffectState, Rect, Transform};
use std::collections::HashMap;
#[derive(Default)]
pub(crate) struct Visuals {
/// Nesting of visual effects being built. Inside one, layout positions are not where
/// content is shown, so hover reads the published regions instead.
pub(crate) depth: usize,
/// Clips of the visual effects being built, with the window each one clips.
pub(crate) clips: Vec<(Id, Rect)>,
/// Transforms composed by this pass, by widget id.
current: HashMap<Id, Transform>,
/// Transforms published by the last pass.
input: HashMap<Id, Transform>,
/// Retained state of subtree effects (presence, reveal, motion), by effect id.
pub(crate) effects: HashMap<Id, EffectState>,
}
impl Visuals {
pub(super) fn begin_pass(&mut self) {
self.current.clear();
}
/// The published transform of `id`; identity when it has none.
pub(crate) fn input(&self, id: Id) -> Transform {
self.input.get(&id).copied().unwrap_or_default()
}
/// Map from displayed to layout coordinates of `id`, for input of this pass.
pub(crate) fn to_local(&self, id: Id) -> Transform {
self.input(id).inverse()
}
/// Apply `transform` on top of what this pass composed for `id` so far.
pub(super) fn compose(&mut self, id: Id, transform: Transform) {
let previous = self.current.get(&id).copied().unwrap_or_default();
self.current.insert(id, transform.compose(previous));
}
/// Move `id` by `delta`, giving it a transform when it has none yet.
pub(super) fn translate(&mut self, id: Id, delta: crate::Vec2) {
self.current.entry(id).or_default().translation += delta;
}
/// Move `id` by `delta` only if something already transformed it.
pub(super) fn shift(&mut self, id: Id, delta: crate::Vec2) {
if let Some(transform) = self.current.get_mut(&id) {
transform.translation += delta;
}
}
/// End of pass: the transforms of widgets that `live` accepts route the next input.
pub(super) fn publish(&mut self, live: impl Fn(Id) -> bool) {
self.current.retain(|id, _| live(*id));
self.input = std::mem::take(&mut self.current);
}
/// Subtree effects not built in pass `frame` are dropped.
pub(super) fn retire_effects(&mut self, frame: u64) {
self.effects.retain(|_, state| state.last_frame == frame);
}
/// The current value of the subtree effect `id`, if it is retained.
pub(crate) fn effect_value(&self, id: Id) -> Option<f32> {
self.effects.get(&id).map(|state| state.value)
}
#[doc(hidden)]
pub(super) fn published(&self) -> &HashMap<Id, Transform> {
&self.input
}
}
impl Context {
/// Layout → displayed logical coordinates from the last published pass, which
/// owns the input being delivered now. Available while building deferred content.
/// Map a screen drag vector with `input_transform(id).inverse().vector(delta)`;
/// unlike points, vectors do not include camera/placement translation.
pub fn input_transform(&self, id: Id) -> Transform {
self.visuals.input(id)
}
/// Current visual bounds of a widget's logical allocation. During a pass,
/// call after the enclosing visual/reorder helper has placed its content.
pub fn visual_rect(&self, id: Id, logical: Rect) -> Rect {
self.visual_transform(id).rect(logical)
}
/// Layout → displayed logical coordinates after completed placement. During a pass,
/// use after the enclosing container has finished placing; otherwise use after `run`.
pub fn visual_transform(&self, id: Id) -> Transform {
let transforms = if self.in_pass {
&self.visuals.current
} else {
&self.visuals.input
};
transforms.get(&id).copied().unwrap_or_default()
}
}