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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::direction::Direction;
use crate::event::{AnyCb, Event, EventResult};
use crate::view::{scroll, ScrollStrategy, Selector, View};
use crate::{Printer, Rect, Vec2, With};
pub struct ScrollView<V> {
inner: V,
core: scroll::Core,
}
impl_scroller!(ScrollView<V>::core);
impl<V> ScrollView<V>
where
V: View,
{
pub fn new(inner: V) -> Self {
ScrollView {
inner,
core: scroll::Core::new(),
}
}
pub fn content_viewport(&self) -> Rect {
self.core.content_viewport()
}
pub fn inner_size(&self) -> Vec2 {
self.core.inner_size()
}
pub fn is_at_top(&self) -> bool {
self.content_viewport().top() == 0
}
pub fn is_at_bottom(&self) -> bool {
(1 + self.content_viewport().bottom()) >= self.inner_size().y
}
pub fn is_at_left_edge(&self) -> bool {
self.content_viewport().left() == 0
}
pub fn is_at_right_edge(&self) -> bool {
(1 + self.content_viewport().right()) >= self.inner_size().x
}
pub fn set_scroll_strategy(&mut self, strategy: ScrollStrategy) {
self.core.set_scroll_strategy(strategy);
}
pub fn scroll_strategy(self, strategy: ScrollStrategy) -> Self {
self.with(|s| s.set_scroll_strategy(strategy))
}
pub fn set_show_scrollbars(&mut self, show_scrollbars: bool) {
self.core.set_show_scrollbars(show_scrollbars);
}
pub fn show_scrollbars(self, show_scrollbars: bool) -> Self {
self.with(|s| s.set_show_scrollbars(show_scrollbars))
}
pub fn set_offset<S>(&mut self, offset: S)
where
S: Into<Vec2>,
{
self.core.set_offset(offset);
}
pub fn set_scroll_y(&mut self, enabled: bool) {
self.core.set_scroll_y(enabled);
}
pub fn set_scroll_x(&mut self, enabled: bool) {
self.core.set_scroll_x(enabled);
}
pub fn scroll_y(self, enabled: bool) -> Self {
self.with(|s| s.set_scroll_y(enabled))
}
pub fn scroll_x(self, enabled: bool) -> Self {
self.with(|s| s.set_scroll_x(enabled))
}
pub fn scroll_to_top(&mut self) {
self.core.scroll_to_top();
}
pub fn scroll_to_bottom(&mut self) {
self.core.scroll_to_bottom();
}
pub fn scroll_to_left(&mut self) {
self.core.scroll_to_left();
}
pub fn scroll_to_right(&mut self) {
self.core.scroll_to_right();
}
pub fn scroll_to_important_area(&mut self) {
let important_area = self.inner.important_area(self.core.last_size());
self.core.scroll_to_rect(important_area);
}
pub fn into_inner(self) -> V {
self.inner
}
inner_getters!(self.inner: V);
}
impl<V> View for ScrollView<V>
where
V: View,
{
fn draw(&self, printer: &Printer<'_, '_>) {
scroll::draw(self, printer, |s, p| s.inner.draw(p));
}
fn on_event(&mut self, event: Event) -> EventResult {
scroll::on_event(
self,
event,
|s, e| s.inner.on_event(e),
|s, si| s.inner.important_area(si),
)
}
fn layout(&mut self, size: Vec2) {
scroll::layout(
self,
size,
self.inner.needs_relayout(),
|s, si| s.inner.layout(si),
|s, c| s.inner.required_size(c),
);
}
fn needs_relayout(&self) -> bool {
self.core.needs_relayout() || self.inner.needs_relayout()
}
fn required_size(&mut self, constraint: Vec2) -> Vec2 {
scroll::required_size(
self,
constraint,
self.inner.needs_relayout(),
|s, c| s.inner.required_size(c),
)
}
fn call_on_any<'a>(&mut self, selector: &Selector<'_>, cb: AnyCb<'a>) {
self.inner.call_on_any(selector, cb)
}
fn focus_view(&mut self, selector: &Selector<'_>) -> Result<(), ()> {
self.inner.focus_view(selector).map(|()| {
self.scroll_to_important_area();
})
}
fn take_focus(&mut self, source: Direction) -> bool {
if self.inner.take_focus(source) {
self.scroll_to_important_area();
true
} else {
self.core.is_scrolling().any()
}
}
fn important_area(&self, size: Vec2) -> Rect {
scroll::important_area(self, size, |s, si| s.inner.important_area(si))
}
}