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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use Scalar;
use color::Color;
use elmesque::Element;
use graphics::character::CharacterCache;
use label::FontSize;
use position::{Dimensions, Point};
use super::canvas::{self, Canvas};
use theme::Theme;
use widget::{self, WidgetId, Widget};
pub struct Tabs<'a> {
tabs: &'a [(WidgetId, &'a str)],
style: Style,
common: widget::CommonBuilder,
maybe_starting_tab_idx: Option<usize>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct State {
tabs: Vec<(WidgetId, String)>,
maybe_selected_tab_idx: Option<usize>,
interaction: Interaction,
tab_bar_dim: Dimensions,
tab_bar_rel_xy: Point,
}
const TAB_BAR_LABEL_PADDING: f64 = 4.0;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Interaction {
Normal,
Highlighted(Elem),
Clicked(Elem),
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Elem {
Tab(usize),
Body,
}
#[derive(Clone, Debug, PartialEq, RustcDecodable, RustcEncodable)]
pub struct Style {
pub maybe_layout: Option<Layout>,
pub maybe_bar_width: Option<Scalar>,
pub maybe_label_color: Option<Color>,
pub maybe_label_font_size: Option<FontSize>,
pub canvas: canvas::Style,
}
#[derive(Copy, Clone, Debug, PartialEq, RustcDecodable, RustcEncodable)]
pub enum Layout {
Horizontal,
Vertical,
}
impl<'a> Tabs<'a> {
pub fn new(tabs: &'a [(WidgetId, &'a str)]) -> Tabs<'a> {
Tabs {
common: widget::CommonBuilder::new(),
tabs: tabs,
style: Style::new(),
maybe_starting_tab_idx: None,
}
}
pub fn bar_width(mut self, width: Scalar) -> Self {
self.style.maybe_bar_width = Some(width);
self
}
pub fn starting_tab_idx(mut self, idx: usize) -> Self {
self.maybe_starting_tab_idx = Some(idx);
self
}
pub fn starting_canvas(mut self, canvas_id: WidgetId) -> Self {
let maybe_idx = self.tabs.iter().enumerate()
.find(|&(_, &(id, _))| canvas_id == id)
.map(|(idx, &(_, _))| idx);
self.maybe_starting_tab_idx = maybe_idx;
self
}
pub fn layout_horizontally(mut self) -> Self {
self.style.maybe_layout = Some(Layout::Horizontal);
self
}
pub fn layout_vertically(mut self) -> Self {
self.style.maybe_layout = Some(Layout::Vertical);
self
}
pub fn label_color(mut self, color: Color) -> Self {
self.style.maybe_label_color = Some(color);
self
}
pub fn label_font_size(mut self, size: FontSize) -> Self {
self.style.maybe_label_font_size = Some(size);
self
}
pub fn canvas_style(mut self, canvas_style: canvas::Style) -> Self {
self.style.canvas = canvas_style;
self
}
pub fn pad_left(mut self, pad: Scalar) -> Tabs<'a> {
self.style.canvas.padding.maybe_left = Some(pad);
self
}
pub fn pad_right(mut self, pad: Scalar) -> Tabs<'a> {
self.style.canvas.padding.maybe_right = Some(pad);
self
}
pub fn pad_top(mut self, pad: Scalar) -> Tabs<'a> {
self.style.canvas.padding.maybe_top = Some(pad);
self
}
pub fn pad_bottom(mut self, pad: Scalar) -> Tabs<'a> {
self.style.canvas.padding.maybe_bottom = Some(pad);
self
}
pub fn pad(self, pad: Scalar) -> Tabs<'a> {
self.pad_left(pad).pad_right(pad).pad_top(pad).pad_bottom(pad)
}
pub fn margin_left(mut self, mgn: Scalar) -> Tabs<'a> {
self.style.canvas.margin.maybe_left = Some(mgn);
self
}
pub fn margin_right(mut self, mgn: Scalar) -> Tabs<'a> {
self.style.canvas.margin.maybe_right = Some(mgn);
self
}
pub fn margin_top(mut self, mgn: Scalar) -> Tabs<'a> {
self.style.canvas.margin.maybe_top = Some(mgn);
self
}
pub fn margin_bottom(mut self, mgn: Scalar) -> Tabs<'a> {
self.style.canvas.margin.maybe_bottom = Some(mgn);
self
}
pub fn margin(self, mgn: Scalar) -> Tabs<'a> {
self.margin_left(mgn).margin_right(mgn).margin_top(mgn).margin_bottom(mgn)
}
}
impl<'a> Widget for Tabs<'a> {
type State = State;
type Style = Style;
fn common(&self) -> &widget::CommonBuilder { &self.common }
fn common_mut(&mut self) -> &mut widget::CommonBuilder { &mut self.common }
fn unique_kind(&self) -> &'static str { "Tabs" }
fn init_state(&self) -> State {
State {
tabs: Vec::new(),
maybe_selected_tab_idx: None,
interaction: Interaction::Normal,
tab_bar_dim: [0.0, 0.0],
tab_bar_rel_xy: [0.0, 0.0],
}
}
fn style(&self) -> Style { self.style.clone() }
fn kid_area(state: &widget::State<State>, style: &Style, theme: &Theme) -> widget::KidArea {
match style.layout(theme) {
Layout::Horizontal => widget::KidArea {
xy: [state.xy[0], state.xy[1] - state.state.tab_bar_dim[1] / 2.0],
dim: [state.dim[0], state.dim[1] - state.state.tab_bar_dim[1]],
pad: style.canvas.padding(theme),
},
Layout::Vertical => widget::KidArea {
xy: [state.xy[0] + state.state.tab_bar_dim[0] / 2.0, state.xy[1]],
dim: [state.dim[0] - state.state.tab_bar_dim[0], state.dim[1]],
pad: style.canvas.padding(theme),
},
}
}
fn update<'b, C>(self, args: widget::UpdateArgs<'b, Self, C>) -> Option<State>
where C: CharacterCache,
{
let widget::UpdateArgs { idx, prev_state, xy, dim, style, mut ui } = args;
let widget::State { ref state, .. } = *prev_state;
let layout = style.layout(ui.theme());
let font_size = style.font_size(ui.theme());
let max_text_width = self.tabs.iter().fold(0.0, |max_w, &(_, string)| {
let w = ui.glyph_cache().width(font_size, &string);
if w > max_w { w } else { max_w }
});
let (tab_bar_dim, tab_bar_rel_xy) =
tab_bar_area(dim, layout, style.maybe_bar_width, font_size as f64, max_text_width);
let num_tabs = self.tabs.len();
let width_multi = if num_tabs == 0 { 1.0 } else { 1.0 / num_tabs as f64 };
let tab_dim = match layout {
Layout::Horizontal =>
[width_multi * tab_bar_dim[0], tab_bar_dim[1]],
Layout::Vertical =>
[tab_bar_dim[0], width_multi * tab_bar_dim[1]],
};
let maybe_mouse = ui.input().maybe_mouse.map(|mouse| mouse.relative_to(xy));
let is_over_elem = || if let Some(mouse) = maybe_mouse {
use utils::is_over_rect;
if is_over_rect([0.0, 0.0], mouse.xy, dim) {
if is_over_rect(tab_bar_rel_xy, mouse.xy, tab_bar_dim) {
match layout {
Layout::Horizontal => {
let start_tab_x = -dim[0] / 2.0 + tab_dim[0] / 2.0;
for i in 0..self.tabs.len() {
let tab_x = start_tab_x + i as f64 * tab_dim[0];
let tab_xy = [tab_x, tab_bar_rel_xy[1]];
if is_over_rect(tab_xy, mouse.xy, tab_dim) {
return Some(Elem::Tab(i));
}
}
Some(Elem::Body)
},
Layout::Vertical => {
let start_tab_y = dim[1] / 2.0 - tab_dim[1] / 2.0;
for i in 0..self.tabs.len() {
let tab_y = start_tab_y - i as f64 * tab_dim[1];
let tab_xy = [tab_bar_rel_xy[0], tab_y];
if is_over_rect(tab_xy, mouse.xy, tab_dim) {
return Some(Elem::Tab(i));
}
}
Some(Elem::Body)
},
}
} else {
Some(Elem::Body)
}
} else {
None
}
} else {
None
};
let new_interaction = if let Some(mouse) = maybe_mouse {
use mouse::ButtonState::{Down, Up};
use self::Interaction::{Normal, Highlighted, Clicked};
match (is_over_elem(), state.interaction, mouse.left) {
(Some(_), Normal, Down) => Normal,
(Some(elem), _, Up) => Highlighted(elem),
(Some(elem), Highlighted(_), Down) => Clicked(elem),
(_, Clicked(elem), Down) => Clicked(elem),
_ => Normal,
}
} else {
Interaction::Normal
};
let maybe_selected_tab_idx = match (state.interaction, new_interaction) {
(Interaction::Clicked(Elem::Tab(prev_idx)), Interaction::Highlighted(Elem::Tab(idx)))
if prev_idx == idx => Some(idx),
_ =>
if let Some(idx) = state.maybe_selected_tab_idx { Some(idx) }
else if let Some(idx) = self.maybe_starting_tab_idx { Some(idx) }
else if self.tabs.len() > 0 { Some(0) }
else { None },
};
if let Some(selected_idx) = maybe_selected_tab_idx {
use position::{Positionable, Sizeable};
let &(child_id, _) = &self.tabs[selected_idx];
let mut canvas = Canvas::new();
let canvas_dim = match style.layout(ui.theme()) {
Layout::Horizontal => [dim[0], dim[1] - tab_bar_dim[1]],
Layout::Vertical => [dim[0] - tab_bar_dim[0], dim[1]],
};
canvas.style = style.canvas.clone();
canvas
.show_title_bar(false)
.dim(canvas_dim)
.floating(false)
.middle_of(idx)
.parent(Some(idx))
.set(child_id, &mut ui);
}
let new_state = || State {
tabs: self.tabs.iter().map(|&(id, s)| (id, s.to_owned())).collect(),
interaction: new_interaction,
maybe_selected_tab_idx: maybe_selected_tab_idx,
tab_bar_dim: tab_bar_dim,
tab_bar_rel_xy: tab_bar_rel_xy,
};
let state_has_changed = state.interaction != new_interaction
|| state.tab_bar_dim != tab_bar_dim
|| state.tab_bar_rel_xy != tab_bar_rel_xy
|| state.maybe_selected_tab_idx != maybe_selected_tab_idx
|| state.tabs.len() != self.tabs.len()
|| state.tabs.iter().zip(self.tabs.iter())
.any(|(&(prev_id, ref prev_label), &(id, label))| {
prev_id != id || &prev_label[..] != label
});
if state_has_changed { Some(new_state()) } else { None }
}
fn draw<'b, C>(args: widget::DrawArgs<'b, Self, C>) -> Element
where C: CharacterCache,
{
use elmesque::form::{collage, rect, text};
use elmesque::text::Text;
let widget::DrawArgs { state, style, theme, .. } = args;
let widget::State { ref state, dim, xy, .. } = *state;
let State {
ref tabs,
ref interaction,
maybe_selected_tab_idx,
tab_bar_dim,
tab_bar_rel_xy,
..
} = *state;
let frame = style.canvas.frame(theme);
let inner_dim = ::vecmath::vec2_sub(dim, [frame * 2.0; 2]);
let color = style.canvas.color(theme);
let frame_color = style.canvas.frame_color(theme);
let frame_form = rect(dim[0], dim[1]).filled(frame_color);
let rect_form = rect(inner_dim[0], inner_dim[1]).filled(color);
let font_size = style.font_size(theme);
let label_color = style.label_color(theme);
let layout = style.layout(theme);
let maybe_highlighted_idx = match *interaction {
Interaction::Highlighted(Elem::Tab(idx)) => Some(idx),
_ => None,
};
let maybe_clicked_idx = match *interaction {
Interaction::Clicked(Elem::Tab(idx)) => Some(idx),
_ => None,
};
let base_forms = Some(frame_form).into_iter().chain(Some(rect_form));
if !tabs.is_empty() {
let num_tabs = tabs.len();
let width_multi = 1.0 / num_tabs as f64;
let rect_form = |dim: Dimensions, color: Color| rect(dim[0], dim[1]).filled(color);
let label_form = |label: &str| text(Text::from_string(label.to_owned())
.color(label_color)
.height(font_size as f64));
let color = |i: usize|
if Some(i) == maybe_selected_tab_idx { color.clicked() }
else if Some(i) == maybe_clicked_idx { color.clicked() }
else if Some(i) == maybe_highlighted_idx { color.highlighted() }
else { color };
match layout {
Layout::Horizontal => {
let tab_dim = [width_multi * tab_bar_dim[0], tab_bar_dim[1]];
let inner_tab_dim = ::vecmath::vec2_sub(tab_dim, [frame * 2.0; 2]);
let start_tab_x = -dim[0] / 2.0 + tab_dim[0] / 2.0;
let tab_bar_forms = tabs.iter().enumerate().flat_map(|(i, &(_, ref label))| {
let tab_x = start_tab_x + i as f64 * tab_dim[0];
let tab_xy = [tab_x, tab_bar_rel_xy[1]];
Some(rect_form(tab_dim, frame_color)).into_iter()
.chain(Some(rect_form(inner_tab_dim, color(i))))
.chain(Some(label_form(label)))
.map(move |form| form.shift(tab_xy[0], tab_xy[1]))
});
let form_chain = base_forms.chain(tab_bar_forms)
.map(move |form| form.shift(xy[0], xy[1]));
collage(dim[0] as i32, dim[1] as i32, form_chain.collect())
},
Layout::Vertical => {
let tab_dim = [tab_bar_dim[0], width_multi * tab_bar_dim[1]];
let inner_tab_dim = ::vecmath::vec2_sub(tab_dim, [frame * 2.0; 2]);
let start_tab_y = dim[1] / 2.0 - tab_dim[1] / 2.0;
let tab_bar_forms = tabs.iter().enumerate().flat_map(|(i, &(_, ref label))| {
let tab_y = start_tab_y - i as f64 * tab_dim[1];
let tab_xy = [tab_bar_rel_xy[0], tab_y];
Some(rect_form(tab_dim, frame_color)).into_iter()
.chain(Some(rect_form(inner_tab_dim, color(i))))
.chain(Some(label_form(label)))
.map(move |form| form.shift(tab_xy[0], tab_xy[1]))
});
let form_chain = base_forms.chain(tab_bar_forms)
.map(move |form| form.shift(xy[0], xy[1]));
collage(dim[0] as i32, dim[1] as i32, form_chain.collect())
},
}
}
else {
let form_chain = base_forms.map(|form| form.shift(xy[0], xy[1]));
collage(dim[0] as i32, dim[1] as i32, form_chain.collect())
}
}
}
fn tab_bar_area(dim: Dimensions,
layout: Layout,
maybe_bar_width: Option<Scalar>,
font_size: f64,
max_text_width: f64) -> (Dimensions, Point)
{
match layout {
Layout::Horizontal => {
let h = maybe_bar_width
.unwrap_or_else(|| font_size + TAB_BAR_LABEL_PADDING * 2.0);
let tab_bar_dim = [dim[0], h];
let y = dim[1] / 2.0 - h / 2.0;
let tab_bar_rel_xy = [0.0, y];
(tab_bar_dim, tab_bar_rel_xy)
},
Layout::Vertical => {
let w = maybe_bar_width
.unwrap_or_else(|| max_text_width + TAB_BAR_LABEL_PADDING * 2.0);
let tab_bar_dim = [w, dim[1]];
let x = -dim[0] / 2.0 + w / 2.0;
let tab_bar_rel_xy = [x, 0.0];
(tab_bar_dim, tab_bar_rel_xy)
},
}
}
impl Style {
pub fn new() -> Style {
Style {
maybe_layout: None,
maybe_bar_width: None,
maybe_label_color: None,
maybe_label_font_size: None,
canvas: canvas::Style::new(),
}
}
pub fn layout(&self, theme: &Theme) -> Layout {
const DEFAULT_LAYOUT: Layout = Layout::Horizontal;
self.maybe_layout.or(theme.maybe_tabs.as_ref().map(|default| {
default.style.maybe_layout.unwrap_or(DEFAULT_LAYOUT)
})).unwrap_or(DEFAULT_LAYOUT)
}
pub fn label_color(&self, theme: &Theme) -> Color {
self.maybe_label_color.or(theme.maybe_tabs.as_ref().map(|default| {
default.style.maybe_label_color.unwrap_or(theme.label_color)
})).unwrap_or(theme.label_color)
}
pub fn font_size(&self, theme: &Theme) -> FontSize {
self.maybe_label_font_size.or(theme.maybe_tabs.as_ref().map(|default| {
default.style.maybe_label_font_size.unwrap_or(theme.font_size_medium)
})).unwrap_or(theme.font_size_medium)
}
}
impl<'a> ::color::Colorable for Tabs<'a> {
fn color(mut self, color: Color) -> Self {
self.style.canvas.maybe_color = Some(color);
self
}
}
impl<'a> ::frame::Frameable for Tabs<'a> {
fn frame(mut self, width: f64) -> Self {
self.style.canvas.maybe_frame = Some(width);
self
}
fn frame_color(mut self, color: Color) -> Self {
self.style.canvas.maybe_frame_color = Some(color);
self
}
}