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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use color::Color;
use graphics::math::Scalar;
use position::{self, Dimensions, Direction, Point};
use theme::Theme;
use ui::Ui;
use widget::Custom as CustomWidget;
use super::{CanvasId, Kind};
pub type Length = Scalar;
#[derive(Clone, Debug, PartialEq)]
pub struct State;
pub struct Split<'a> {
id: CanvasId,
maybe_splits: Option<(Direction, &'a [Split<'a>])>,
maybe_length: Option<f64>,
style: Style,
}
#[derive(Clone, Debug, PartialEq, RustcDecodable, RustcEncodable)]
pub struct Style {
maybe_frame: Option<f64>,
maybe_frame_color: Option<Color>,
maybe_color: Option<Color>,
padding: Padding,
margin: Margin,
}
#[derive(Clone, Debug, PartialEq, RustcDecodable, RustcEncodable)]
pub struct Padding {
maybe_top: Option<f64>,
maybe_bottom: Option<f64>,
maybe_left: Option<f64>,
maybe_right: Option<f64>,
}
#[derive(Clone, Debug, PartialEq, RustcDecodable, RustcEncodable)]
pub struct Margin {
maybe_top: Option<f64>,
maybe_bottom: Option<f64>,
maybe_left: Option<f64>,
maybe_right: Option<f64>,
}
impl Style {
pub fn color(&self, theme: &Theme) -> Color {
self.maybe_color.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.maybe_color.unwrap_or(theme.background_color)
})).unwrap_or(theme.background_color)
}
pub fn frame(&self, theme: &Theme) -> f64 {
self.maybe_frame.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.maybe_frame.unwrap_or(theme.frame_width)
})).unwrap_or(theme.frame_width)
}
pub fn frame_color(&self, theme: &Theme) -> Color {
self.maybe_frame_color.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.maybe_frame_color.unwrap_or(theme.frame_color)
})).unwrap_or(theme.frame_color)
}
pub fn padding(&self, theme: &Theme) -> position::Padding {
position::Padding {
top: self.padding.maybe_top.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.padding.maybe_top.unwrap_or(theme.padding.top)
})).unwrap_or(theme.padding.top),
bottom: self.padding.maybe_bottom.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.padding.maybe_bottom.unwrap_or(theme.padding.bottom)
})).unwrap_or(theme.padding.bottom),
left: self.padding.maybe_left.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.padding.maybe_left.unwrap_or(theme.padding.left)
})).unwrap_or(theme.padding.left),
right: self.padding.maybe_right.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.padding.maybe_right.unwrap_or(theme.padding.right)
})).unwrap_or(theme.padding.right),
}
}
pub fn margin(&self, theme: &Theme) -> position::Margin {
position::Margin {
top: self.margin.maybe_top.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.margin.maybe_top.unwrap_or(theme.margin.top)
})).unwrap_or(theme.margin.top),
bottom: self.margin.maybe_bottom.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.margin.maybe_bottom.unwrap_or(theme.margin.bottom)
})).unwrap_or(theme.margin.bottom),
left: self.margin.maybe_left.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.margin.maybe_left.unwrap_or(theme.margin.left)
})).unwrap_or(theme.margin.left),
right: self.margin.maybe_right.or(theme.maybe_canvas_split.as_ref().map(|style| {
style.margin.maybe_right.unwrap_or(theme.margin.right)
})).unwrap_or(theme.margin.right),
}
}
}
impl<'a> Split<'a> {
pub fn new(id: CanvasId) -> Split<'a> {
Split {
id: id,
maybe_splits: None,
maybe_length: None,
style: Style::new(),
}
}
pub fn length(mut self, length: Length) -> Split<'a> {
self.maybe_length = Some(length);
self
}
pub fn flow(mut self, dir: Direction, splits: &'a [Split<'a>]) -> Split<'a> {
self.maybe_splits = Some((dir, splits));
self
}
pub fn flow_down(self, splits: &'a [Split<'a>]) -> Split<'a> {
self.flow(Direction::Down, splits)
}
pub fn flow_up(self, splits: &'a [Split<'a>]) -> Split<'a> {
self.flow(Direction::Up, splits)
}
pub fn flow_right(self, splits: &'a [Split<'a>]) -> Split<'a> {
self.flow(Direction::Right, splits)
}
pub fn flow_left(self, splits: &'a [Split<'a>]) -> Split<'a> {
self.flow(Direction::Left, splits)
}
pub fn pad_left(mut self, pad: Scalar) -> Split<'a> {
self.style.padding.maybe_left = Some(pad);
self
}
pub fn pad_right(mut self, pad: Scalar) -> Split<'a> {
self.style.padding.maybe_right = Some(pad);
self
}
pub fn pad_top(mut self, pad: Scalar) -> Split<'a> {
self.style.padding.maybe_top = Some(pad);
self
}
pub fn pad_bottom(mut self, pad: Scalar) -> Split<'a> {
self.style.padding.maybe_bottom = Some(pad);
self
}
pub fn pad(self, pad: Scalar) -> Split<'a> {
self.pad_left(pad).pad_right(pad).pad_top(pad).pad_bottom(pad)
}
pub fn margin_left(mut self, pad: Scalar) -> Split<'a> {
self.style.margin.maybe_left = Some(pad);
self
}
pub fn margin_right(mut self, pad: Scalar) -> Split<'a> {
self.style.margin.maybe_right = Some(pad);
self
}
pub fn margin_top(mut self, pad: Scalar) -> Split<'a> {
self.style.margin.maybe_top = Some(pad);
self
}
pub fn margin_bottom(mut self, pad: Scalar) -> Split<'a> {
self.style.margin.maybe_bottom = Some(pad);
self
}
pub fn margin(self, pad: Scalar) -> Split<'a> {
self.margin_left(pad).margin_right(pad).margin_top(pad).margin_bottom(pad)
}
pub fn set<C, W>(self, ui: &mut Ui<C, W>) where W: CustomWidget {
let dim = [ui.win_w as f64, ui.win_h as f64];
self.into_ui(dim, [0.0, 0.0], ui);
}
fn into_ui<C, W>(&self, dim: Dimensions, xy: Point, ui: &mut Ui<C, W>) where W: CustomWidget {
use elmesque::form::{rect, collage};
use vecmath::{vec2_add, vec2_sub, vec2_scale};
let Split { id, ref maybe_splits, ref style, .. } = *self;
let color = style.color(&ui.theme);
let frame_color = style.frame_color(&ui.theme);
let frame = style.frame(&ui.theme);
let pad = style.padding(&ui.theme);
let mgn = style.margin(&ui.theme);
let mgn_offset = [(mgn.left - mgn.right), (mgn.bottom - mgn.top)];
let dim = vec2_sub(dim, [mgn.left + mgn.right, mgn.top + mgn.bottom]);
let frame_dim = vec2_sub(dim, [frame * 2.0; 2]);
let pad_offset = [(pad.bottom - pad.top), (pad.left - pad.right)];
let pad_dim = vec2_sub(frame_dim, [pad.left + pad.right, pad.top + pad.bottom]);
let xy = vec2_add(xy, mgn_offset);
if let Some((direction, splits)) = *maybe_splits {
use position::{align_top_of, align_bottom_of, align_left_of, align_right_of};
let xy = vec2_add(xy, pad_offset);
let num = splits.len() as f64;
let split_dim = match splits.len() {
0 => [0.0, 0.0],
1 => pad_dim,
_ => match direction {
Direction::Up | Direction::Down => [pad_dim[0], pad_dim[1] / num],
Direction::Left | Direction::Right => [pad_dim[0] / num, pad_dim[1]],
},
};
let split_offset = match direction {
Direction::Up => [0.0, split_dim[1]],
Direction::Down => [0.0, -split_dim[1]],
Direction::Left => [-split_dim[0], 0.0],
Direction::Right => [split_dim[0], 0.0],
};
let first_xy = match direction {
Direction::Up => [xy[0], xy[1] + align_bottom_of(pad_dim[1], split_dim[1])],
Direction::Down => [xy[0], xy[1] + align_top_of(pad_dim[1], split_dim[1])],
Direction::Left => [xy[0] + align_right_of(pad_dim[0], split_dim[0]), xy[1]],
Direction::Right => [xy[0] + align_left_of(pad_dim[0], split_dim[0]), xy[1]],
};
for (i, split) in splits.iter().enumerate() {
let split_xy = vec2_add(first_xy, vec2_scale(split_offset, i as f64));
split.into_ui(split_dim, split_xy, ui)
}
}
let frame_form = rect(dim[0], dim[1]).filled(frame_color);
let inner_form = rect(frame_dim[0], frame_dim[1]).filled(color);
let form_chain = Some(frame_form).into_iter()
.chain(Some(inner_form).into_iter())
.map(|form| form.shift(xy[0], xy[1]));
let element = collage(frame_dim[0] as i32, frame_dim[1] as i32, form_chain.collect());
ui.update_canvas(id, Kind::Split(State), xy, pad, Some(element));
}
}
impl Style {
pub fn new() -> Style {
Style {
maybe_frame: None,
maybe_frame_color: None,
maybe_color: None,
padding: Padding::new(),
margin: Margin::new(),
}
}
}
impl Padding {
pub fn new() -> Padding {
Padding {
maybe_top: None,
maybe_bottom: None,
maybe_left: None,
maybe_right: None,
}
}
}
impl Margin {
pub fn new() -> Margin {
Margin {
maybe_top: None,
maybe_bottom: None,
maybe_left: None,
maybe_right: None,
}
}
}
impl<'a> ::color::Colorable for Split<'a> {
fn color(mut self, color: Color) -> Self {
self.style.maybe_color = Some(color);
self
}
}
impl<'a> ::frame::Frameable for Split<'a> {
fn frame(mut self, width: f64) -> Self {
self.style.maybe_frame = Some(width);
self
}
fn frame_color(mut self, color: Color) -> Self {
self.style.maybe_frame_color = Some(color);
self
}
}