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
|
use diaphragm_core::{
types::{Float, ShapeContext},
ComplexShape, DrawResult, DynDrawable, SolverContext,
};
#[derive(Clone)]
pub struct Spacer {
pub margin_left: Float,
pub margin_right: Float,
pub margin_top: Float,
pub margin_bottom: Float,
pub content: DynDrawable,
}
impl Spacer {
pub fn builder<T: Into<DynDrawable>>(drawable: T) -> SpacerBuilder {
SpacerBuilder::new(drawable)
}
}
impl ComplexShape for Spacer {
fn draw(&self, context: &ShapeContext, solver: &mut dyn SolverContext) -> DrawResult {
let mut result = DrawResult::new();
let bounds_left = context.bounds().left(solver);
let bounds_right = context.bounds().right(solver);
let bounds_top = context.bounds().top(solver);
let bounds_bottom = context.bounds().bottom(solver);
let wanted_content_left = solver.float_add(&[bounds_left, self.margin_left]);
let content_left = self.content.bounds().left(solver);
let content_left_constraint = solver.float_eq(content_left, wanted_content_left);
solver.constrain(content_left_constraint);
let wanted_content_right = solver.float_sub(&[bounds_right, self.margin_right]);
let content_right = self.content.bounds().right(solver);
let content_right_constraint = solver.float_eq(content_right, wanted_content_right);
solver.constrain(content_right_constraint);
let wanted_content_top = solver.float_add(&[bounds_top, self.margin_top]);
let content_top = self.content.bounds().top(solver);
let content_top_constraint = solver.float_eq(content_top, wanted_content_top);
solver.constrain(content_top_constraint);
let wanted_content_bottom = solver.float_sub(&[bounds_bottom, self.margin_bottom]);
let content_bottom = self.content.bounds().bottom(solver);
let content_bottom_constraint = solver.float_eq(content_bottom, wanted_content_bottom);
solver.constrain(content_bottom_constraint);
result.push_dyn(self.content.clone());
result
}
}
#[derive(Clone)]
pub struct SpacerBuilder {
pub margin_left: Option<Float>,
pub margin_right: Option<Float>,
pub margin_top: Option<Float>,
pub margin_bottom: Option<Float>,
pub content: DynDrawable,
}
impl SpacerBuilder {
pub fn new<T: Into<DynDrawable>>(drawable: T) -> Self {
SpacerBuilder {
margin_left: None,
margin_right: None,
margin_top: None,
margin_bottom: None,
content: drawable.into(),
}
}
pub fn margin_left(&mut self, margin_left: Float) -> &mut Self {
self.margin_left = Some(margin_left);
self
}
pub fn margin_right(&mut self, margin_right: Float) -> &mut Self {
self.margin_right = Some(margin_right);
self
}
pub fn margin_top(&mut self, margin_top: Float) -> &mut Self {
self.margin_top = Some(margin_top);
self
}
pub fn margin_bottom(&mut self, margin_bottom: Float) -> &mut Self {
self.margin_bottom = Some(margin_bottom);
self
}
pub fn vertical_align_center(&mut self, solver: &mut dyn SolverContext) -> &mut Self {
let vertical_margin = solver.new_free_float();
self.margin_top = Some(vertical_margin);
self.margin_bottom = Some(vertical_margin);
self
}
pub fn vertical_align_center_with(&mut self, margin: Float) -> &mut Self {
self.margin_top = Some(margin);
self.margin_bottom = Some(margin);
self
}
pub fn horizontal_align_center(&mut self, solver: &mut dyn SolverContext) -> &mut Self {
let horizontal_margin = solver.new_free_float();
self.margin_left = Some(horizontal_margin);
self.margin_right = Some(horizontal_margin);
self
}
pub fn horizontal_align_center_with(&mut self, margin: Float) -> &mut Self {
self.margin_left = Some(margin);
self.margin_right = Some(margin);
self
}
pub fn build(&self, solver: &mut dyn SolverContext) -> Spacer {
Spacer {
margin_left: self.margin_left.unwrap_or_else(|| solver.new_free_float()),
margin_right: self.margin_right.unwrap_or_else(|| solver.new_free_float()),
margin_top: self.margin_top.unwrap_or_else(|| solver.new_free_float()),
margin_bottom: self
.margin_bottom
.unwrap_or_else(|| solver.new_free_float()),
content: self.content.clone(),
}
}
}
|