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
use crate::{
Color, DrawWithTooltips, EventCtx, GeomBatch, JustDraw, RewriteColor, ScreenDims, ScreenPt,
Text, Widget,
};
pub struct Image<'a> {
filename: &'a str,
tooltip: Option<Text>,
color: Option<RewriteColor>,
}
impl<'a> Image<'a> {
pub fn icon(filename: &'a str) -> Self {
Self {
filename,
tooltip: None,
color: None,
}
}
pub fn untinted(filename: &'a str) -> Self {
Self::icon(filename).color(RewriteColor::NoOp)
}
pub fn tooltip(mut self, tooltip: Text) -> Self {
self.tooltip = Some(tooltip);
self
}
pub fn color<RWC: Into<RewriteColor>>(mut self, color: RWC) -> Self {
self.color = Some(color.into());
self
}
pub fn into_widget(self, ctx: &EventCtx) -> Widget {
let (mut batch, bounds) = crate::svg::load_svg(ctx.prerender, self.filename);
let color = self
.color
.unwrap_or(RewriteColor::ChangeAll(ctx.style.icon_fg));
batch = batch.color(color);
batch.push(Color::CLEAR, bounds.get_rectangle());
if let Some(tooltip) = self.tooltip {
DrawWithTooltips::new(
ctx,
batch,
vec![(bounds.get_rectangle(), tooltip)],
Box::new(|_| GeomBatch::new()),
)
} else {
Widget::new(Box::new(JustDraw {
dims: ScreenDims::new(bounds.width(), bounds.height()),
draw: ctx.upload(batch),
top_left: ScreenPt::new(0.0, 0.0),
}))
}
}
}