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
118
119
120
121
122
123
124
125
126
127
128
use crate::draw::Primitive;
use crate::event::Event;
use crate::layout::{Rectangle, Size};
use crate::stylesheet::Stylesheet;
use crate::widget::{Context, IntoNode, Node, Widget};
#[allow(missing_docs)]
pub enum Anchor {
TopLeft,
TopRight,
BottomLeft,
BottomRight,
}
pub struct Panel<'a, T> {
offset: (f32, f32),
anchor: Anchor,
content: Node<'a, T>,
}
impl<'a, T: 'a> Panel<'a, T> {
pub fn new(offset: (f32, f32), anchor: Anchor, content: impl IntoNode<'a, T>) -> Self {
Self {
offset,
anchor,
content: content.into_node(),
}
}
fn layout(&self, layout: Rectangle) -> Option<Rectangle> {
let (content_width, content_height) = self.content.size();
let (left, top) = match self.anchor {
Anchor::TopLeft => (true, true),
Anchor::TopRight => (false, true),
Anchor::BottomLeft => (true, false),
Anchor::BottomRight => (false, false),
};
let available = Rectangle {
left: if left { layout.left + self.offset.0 } else { layout.left },
right: if left {
layout.right
} else {
layout.right - self.offset.0
},
top: if top { layout.top + self.offset.1 } else { layout.top },
bottom: if top {
layout.bottom
} else {
layout.bottom - self.offset.1
},
};
if available.left < available.right && available.top < available.bottom {
let width = match content_width {
Size::Exact(width) => width.min(available.width()),
Size::Fill(_) => available.width(),
Size::Shrink => 0.0,
};
let height = match content_height {
Size::Exact(height) => height.min(available.height()),
Size::Fill(_) => available.height(),
Size::Shrink => 0.0,
};
Some(Rectangle {
left: if left { available.left } else { available.right - width },
right: if left { available.left + width } else { available.right },
top: if top { available.top } else { available.bottom - height },
bottom: if top { available.top + height } else { available.bottom },
})
} else {
None
}
}
}
impl<'a, T: 'a> Widget<'a, T> for Panel<'a, T> {
fn widget(&self) -> &'static str {
"panel"
}
fn len(&self) -> usize {
1
}
fn visit_children(&mut self, visitor: &mut dyn FnMut(&mut Node<'a, T>)) {
visitor(&mut self.content);
}
fn size(&self, style: &Stylesheet) -> (Size, Size) {
(style.width, style.height)
}
fn hit(&self, layout: Rectangle, clip: Rectangle, _: &Stylesheet, x: f32, y: f32) -> bool {
if layout.point_inside(x, y) && clip.point_inside(x, y) {
self.layout(layout)
.map(|layout| layout.point_inside(x, y))
.unwrap_or(false)
} else {
false
}
}
fn focused(&self) -> bool {
self.content.focused()
}
fn event(&mut self, layout: Rectangle, clip: Rectangle, _: &Stylesheet, event: Event, context: &mut Context<T>) {
if let Some(layout) = self.layout(layout) {
self.content.event(layout, clip, event, context)
}
}
fn draw(&mut self, layout: Rectangle, clip: Rectangle, _: &Stylesheet) -> Vec<Primitive<'a>> {
if let Some(layout) = self.layout(layout) {
self.content.draw(layout, clip)
} else {
Vec::new()
}
}
}
impl<'a, T: 'a> IntoNode<'a, T> for Panel<'a, T> {
fn into_node(self) -> Node<'a, T> {
Node::new(self)
}
}