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
use crate::event::{Event, EventResult};
use crate::vec::Vec2;
use crate::view::{Margins, View, ViewWrapper};
use crate::Printer;
pub struct PaddedView<V> {
view: V,
margins: Margins,
}
impl<V: View> PaddedView<V> {
pub fn new<M: Into<Margins>>(margins: M, view: V) -> Self {
let margins = margins.into();
PaddedView { view, margins }
}
pub fn set_margins<M: Into<Margins>>(&mut self, margins: M) {
self.margins = margins.into();
}
}
impl<V: View> ViewWrapper for PaddedView<V> {
wrap_impl!(self.view: V);
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
let margins = self.margins.combined();
self.view.required_size(req.saturating_sub(margins)) + margins
}
fn wrap_layout(&mut self, size: Vec2) {
let margins = self.margins.combined();
self.view.layout(size.saturating_sub(margins));
}
fn wrap_on_event(&mut self, event: Event) -> EventResult {
let padding = self.margins.top_left();
self.view.on_event(event.relativized(padding))
}
fn wrap_draw(&self, printer: &Printer<'_, '_>) {
let top_left = self.margins.top_left();
let bot_right = self.margins.bot_right();
let printer = &printer.offset(top_left).shrinked(bot_right);
self.view.draw(printer);
}
}