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
106
107
108
109
110
111
112
113
114
115
116
117
use crate::vec::Vec2;
use crate::view::{Selector, View, ViewWrapper};
use crate::With;
use std::any::Any;
pub struct HideableView<V> {
view: V,
visible: bool,
invalidated: bool,
}
impl<V> HideableView<V> {
pub fn new(view: V) -> Self {
HideableView {
view,
visible: true,
invalidated: true,
}
}
pub fn set_visible(&mut self, visible: bool) {
self.visible = visible;
self.invalidate();
}
pub fn hide(&mut self) {
self.set_visible(false);
}
pub fn unhide(&mut self) {
self.set_visible(true);
}
pub fn hidden(self) -> Self {
self.with(Self::hide)
}
pub fn is_visible(&self) -> bool {
self.visible
}
fn invalidate(&mut self) {
self.invalidated = true;
}
inner_getters!(self.view: V);
}
impl<V: View> ViewWrapper for HideableView<V> {
type V = V;
fn with_view<F, R>(&self, f: F) -> Option<R>
where
F: FnOnce(&Self::V) -> R,
{
if self.visible {
Some(f(&self.view))
} else {
None
}
}
fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
where
F: FnOnce(&mut Self::V) -> R,
{
if self.visible {
Some(f(&mut self.view))
} else {
None
}
}
fn wrap_call_on_any<'a>(
&mut self, selector: &Selector<'_>,
callback: Box<FnMut(&mut dyn Any) + 'a>,
) {
self.view.call_on_any(selector, callback)
}
fn into_inner(self) -> Result<Self::V, Self>
where
Self: Sized,
Self::V: Sized,
{
Ok(self.view)
}
fn wrap_layout(&mut self, size: Vec2) {
self.invalidated = false;
self.with_view_mut(|v| v.layout(size));
}
fn wrap_needs_relayout(&self) -> bool {
self.invalidated || (self.visible && self.view.needs_relayout())
}
}