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
use {Color, Colorable, FontSize, Borderable, Labelable, Positionable, Sizeable, Ui};
use position::{self, Align, Dimension, Scalar};
use text;
use widget::{self, Widget};
#[derive(Clone, WidgetCommon_)]
pub struct TitleBar<'a> {
#[conrod(common_builder)]
pub common: widget::CommonBuilder,
pub style: Style,
pub label: &'a str,
}
pub struct State {
ids: Ids,
}
widget_ids! {
struct Ids {
rectangle,
label,
}
}
#[derive(Copy, Clone, Debug, Default, PartialEq, WidgetStyle_)]
pub struct Style {
#[conrod(default = "theme.background_color")]
pub color: Option<Color>,
#[conrod(default = "theme.border_width")]
pub border: Option<Scalar>,
#[conrod(default = "theme.border_color")]
pub border_color: Option<Color>,
#[conrod(default = "theme.label_color")]
pub text_color: Option<Color>,
#[conrod(default = "theme.font_size_medium")]
pub font_size: Option<FontSize>,
#[conrod(default = "Some(widget::text::Wrap::Whitespace)")]
pub maybe_wrap: Option<Option<widget::text::Wrap>>,
#[conrod(default = "1.0")]
pub line_spacing: Option<Scalar>,
#[conrod(default = "text::Justify::Center")]
pub justify: Option<text::Justify>,
#[conrod(default = "position::Relative::Align(Align::Middle)")]
pub label_x: Option<position::Relative>,
#[conrod(default = "position::Relative::Align(Align::Middle)")]
pub label_y: Option<position::Relative>,
#[conrod(default = "theme.font_id")]
pub font_id: Option<Option<text::font::Id>>,
}
const LABEL_PADDING: f64 = 4.0;
impl<'a> TitleBar<'a> {
pub fn new(label: &'a str, id: widget::Id) -> Self {
TitleBar {
common: widget::CommonBuilder::default(),
style: Style::default(),
label: label,
}.w_of(id).mid_top_of(id)
}
pub fn left_justify_label(mut self) -> Self {
self.style.justify = Some(text::Justify::Left);
self
}
pub fn center_justify_label(mut self) -> Self {
self.style.justify = Some(text::Justify::Center);
self
}
pub fn right_justify_label(mut self) -> Self {
self.style.justify = Some(text::Justify::Right);
self
}
pub fn label_x(mut self, x: position::Relative) -> Self {
self.style.label_x = Some(x);
self
}
pub fn label_y(mut self, y: position::Relative) -> Self {
self.style.label_y = Some(y);
self
}
pub fn font_id(mut self, font_id: text::font::Id) -> Self {
self.style.font_id = Some(Some(font_id));
self
}
builder_methods!{
pub line_spacing { style.line_spacing = Some(Scalar) }
}
}
pub fn calc_height(font_size: FontSize) -> Scalar {
font_size as Scalar + LABEL_PADDING * 2.0
}
impl<'a> Widget for TitleBar<'a> {
type State = State;
type Style = Style;
type Event = ();
fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
State {
ids: Ids::new(id_gen),
}
}
fn style(&self) -> Self::Style {
self.style.clone()
}
fn default_y_dimension(&self, ui: &Ui) -> Dimension {
let font_size = self.style.font_size(&ui.theme);
let h = calc_height(font_size);
Dimension::Absolute(h)
}
fn update(self, args: widget::UpdateArgs<Self>) {
let widget::UpdateArgs { id, state, rect, style, ui, .. } = args;
let TitleBar { label, .. } = self;
let dim = rect.dim();
let color = style.color(ui.theme());
let border = style.border(ui.theme());
let border_color = style.border_color(ui.theme());
widget::BorderedRectangle::new(dim)
.color(color)
.border(border)
.border_color(border_color)
.middle_of(id)
.graphics_for(id)
.set(state.ids.rectangle, ui);
let text_color = style.text_color(ui.theme());
let justify = style.justify(ui.theme());
let font_size = style.font_size(ui.theme());
let line_spacing = style.line_spacing(ui.theme());
let maybe_wrap = style.maybe_wrap(ui.theme());
let font_id = style.font_id(&ui.theme).or(ui.fonts.ids().next());
let label_x = style.label_x(&ui.theme);
let label_y = style.label_y(&ui.theme);
widget::Text::new(label)
.and_mut(|text| {
text.style.maybe_wrap = Some(maybe_wrap);
text.style.justify = Some(justify);
})
.and_then(font_id, widget::Text::font_id)
.padded_w_of(state.ids.rectangle, border)
.x_position_relative_to(state.ids.rectangle, label_x)
.y_position_relative_to(state.ids.rectangle, label_y)
.color(text_color)
.font_size(font_size)
.line_spacing(line_spacing)
.graphics_for(id)
.set(state.ids.label, ui);
}
}
impl<'a> Colorable for TitleBar<'a> {
builder_method!(color { style.color = Some(Color) });
}
impl<'a> Borderable for TitleBar<'a> {
builder_methods!{
border { style.border = Some(Scalar) }
border_color { style.border_color = Some(Color) }
}
}
impl<'a> Labelable<'a> for TitleBar<'a> {
builder_methods!{
label { label = &'a str }
label_color { style.text_color = Some(Color) }
label_font_size { style.font_size = Some(FontSize) }
}
}