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
use std::f64::consts::PI;
use anyhow::Result;
use csscolorparser::Color;
use crate::{
parser, remove_color_from_config, remove_float_from_config,
remove_string_from_config,
};
/// The configuration options for a panel background
#[derive(Debug, Clone, Default, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum Bg {
/// No background will be drawn for the panel.
#[default]
None,
/// A bubble will be drawn around the panel.
Bubble {
/// `radius` describes how sharp the corners are. A radius of zero will
/// result in a rectangle being drawn.
radius: f64,
/// How far past the left and right edges the bubble will extend.
border: f64,
/// The color of the background. See [`csscolorparser::parse`] for
/// parsing options.
color: Color,
},
/// The panel's background will have curved corners on the left and sharp
/// corners on the right. This can be used to visually combine panels.
BubbleLeft {
/// `radius` describes how sharp the corners are. A radius of zero will
/// result in a rectangle being drawn.
radius: f64,
/// How far past the left and right edges the bubble will extend.
border: f64,
/// The color of the background. See [`csscolorparser::parse`] for
/// parsing options.
color: Color,
},
/// The panel's background will have curved corners on the right and sharp
/// corners on the left. This can be used to visually combine panels.
BubbleRight {
/// `radius` describes how sharp the corners are. A radius of zero will
/// result in a rectangle being drawn.
radius: f64,
/// How far past the left and right edges the bubble will extend.
border: f64,
/// The color of the background. See [`csscolorparser::parse`] for
/// parsing options.
color: Color,
},
/// A bubble will be drawn around the panel. A proportional `border` will
/// be inferred from the text height, which may not give the expected
/// results (e.g. when using large fonts)
BubbleProp {
/// `radius` describes how sharp the corners are. A radius of zero will
/// result in a rectangle being drawn.
radius: f64,
/// The color of the background. See [`csscolorparser::parse`] for
/// parsing options.
color: Color,
},
}
impl Bg {
/// Removes the appropriate values from a given config table and returns an
/// attempt at parsing them into a [`Bg`].
///
/// Configuration options:
/// - `style`: One of `none`, `bubble`, `bubble_prop`
/// - default: `none`
/// - `radius`: radius of corners, ignored if `style` is `none`.
/// - default: 0.0
/// - `border`: how far to extend edges, ignored if `style` is not `bubble`.
/// - default: 0.0
/// - `color`: the background color. See [csscolorparser] for parsing
/// options.
pub fn parse(name: impl AsRef<str>) -> Option<Self> {
let bgs_table = parser::BGS.get().unwrap();
let mut bg_table =
bgs_table.get(name.as_ref())?.clone().into_table().ok()?;
remove_string_from_config("style", &mut bg_table).and_then(|style| {
match style.as_str() {
which @ ("bubble" | "bubble_left" | "bubble_right") => {
let radius =
remove_float_from_config("radius", &mut bg_table)
.unwrap_or_default();
let border =
remove_float_from_config("border", &mut bg_table)
.unwrap_or_default();
let color =
remove_color_from_config("color", &mut bg_table)
.unwrap_or_default();
Some(match which {
"bubble" => Self::Bubble {
radius,
border,
color,
},
"bubble_left" => Self::BubbleLeft {
radius,
border,
color,
},
"bubble_right" => Self::BubbleRight {
radius,
border,
color,
},
_ => unreachable!(),
})
}
"bubble_prop" => {
let radius =
remove_float_from_config("radius", &mut bg_table)
.unwrap_or_default();
let color =
remove_color_from_config("color", &mut bg_table)
.unwrap_or_default();
Some(Self::BubbleProp { radius, color })
}
"none" => Some(Self::None),
_ => None,
}
})
}
pub(crate) fn draw(
&self,
cr: &cairo::Context,
width: f64,
text_height: f64,
max_height: f64,
) -> Result<f64> {
if width == 0.0 {
return Ok(0.0);
}
cr.save()?;
let offset = match self {
Self::None => 0.0,
Self::Bubble {
radius,
border,
color,
} => {
let total_width = width + 2.0 * border;
cr.save()?;
cr.move_to(*radius, 0.0);
cr.rel_line_to(total_width - 2.0 * radius, 0.0);
cr.arc(total_width - radius, *radius, *radius, -PI / 2.0, 0.0);
cr.rel_line_to(0.0, max_height - 2.0 * radius);
cr.arc(
total_width - radius,
max_height - radius,
*radius,
0.0,
PI / 2.0,
);
cr.rel_line_to(2.0 * radius - total_width, 0.0);
cr.arc(*radius, max_height - radius, *radius, PI / 2.0, PI);
cr.rel_line_to(0.0, 2.0 * radius - max_height);
cr.arc(*radius, *radius, *radius, PI, -PI / 2.0);
cr.set_source_rgba(
color.r.into(),
color.g.into(),
color.b.into(),
color.a.into(),
);
cr.fill()?;
cr.restore()?;
border.max(0.0)
}
Self::BubbleLeft {
radius,
border,
color,
} => {
let total_width = width + 2.0 * border;
cr.save()?;
cr.move_to(*radius, 0.0);
cr.rel_line_to(total_width - radius, 0.0);
cr.rel_line_to(0.0, max_height);
cr.rel_line_to(radius - total_width, 0.0);
cr.arc(*radius, max_height - radius, *radius, PI / 2.0, PI);
cr.rel_line_to(0.0, 2.0 * radius - max_height);
cr.arc(*radius, *radius, *radius, PI, -PI / 2.0);
cr.set_source_rgba(
color.r.into(),
color.g.into(),
color.b.into(),
color.a.into(),
);
cr.fill()?;
cr.restore()?;
border.max(0.0)
}
Self::BubbleRight {
radius,
border,
color,
} => {
let total_width = width + 2.0 * border;
cr.save()?;
cr.move_to(0.0, 0.0);
cr.rel_line_to(total_width - radius, 0.0);
cr.arc(total_width - radius, *radius, *radius, -PI / 2.0, 0.0);
cr.rel_line_to(0.0, max_height - 2.0 * radius);
cr.arc(
total_width - radius,
max_height - radius,
*radius,
0.0,
PI / 2.0,
);
cr.rel_line_to(radius - total_width, 0.0);
cr.rel_line_to(0.0, max_height);
cr.set_source_rgba(
color.r.into(),
color.g.into(),
color.b.into(),
color.a.into(),
);
cr.fill()?;
cr.restore()?;
border.max(0.0)
}
Self::BubbleProp { radius, color } => {
let border = (max_height - text_height) / 2.0;
let total_width = 2.0f64.mul_add(border, width);
cr.save()?;
cr.move_to(*radius, 0.0);
cr.rel_line_to(total_width - 2.0 * radius, 0.0);
cr.arc(total_width - radius, *radius, *radius, -PI / 2.0, 0.0);
cr.rel_line_to(0.0, max_height - 2.0 * radius);
cr.arc(
total_width - radius,
max_height - radius,
*radius,
0.0,
PI / 2.0,
);
cr.rel_line_to(2.0 * radius - total_width, 0.0);
cr.arc(*radius, max_height - radius, *radius, PI / 2.0, PI);
cr.rel_line_to(0.0, 2.0 * radius - max_height);
cr.arc(*radius, *radius, *radius, PI, -PI / 2.0);
cr.set_source_rgba(
color.r.into(),
color.g.into(),
color.b.into(),
color.a.into(),
);
cr.fill()?;
cr.restore()?;
border.max(0.0)
}
};
cr.restore()?;
Ok(offset)
}
/// Gets the offset in the x direction from where the background was drawn
/// to where the text should start.
#[must_use]
pub fn get_offset(&self, text_height: f64, max_height: f64) -> f64 {
match self {
Self::None => 0.0,
Self::Bubble {
radius: _,
border,
color: _,
}
| Self::BubbleLeft {
radius: _,
border,
color: _,
}
| Self::BubbleRight {
radius: _,
border,
color: _,
} => *border,
Self::BubbleProp {
radius: _,
color: _,
} => (max_height - text_height) / 2.0,
}
}
/// Adjusts the dimensions of a panel to account for the background.
#[must_use]
pub fn adjust_dims(&self, dims: (i32, i32), max_height: i32) -> (i32, i32) {
let out = match self {
Self::None => dims,
Self::Bubble {
radius: _,
border,
color: _,
}
| Self::BubbleLeft {
radius: _,
border,
color: _,
}
| Self::BubbleRight {
radius: _,
border,
color: _,
} => (dims.0 + (2.0 * border).round() as i32, max_height),
Self::BubbleProp {
radius: _,
color: _,
} => (dims.0 + max_height - dims.1, max_height),
};
(out.0.max(dims.0), out.1.max(dims.1))
}
}