Skip to content

Commit b29cb6a

Browse files
committed
move out internal error handling
1 parent 60df6e9 commit b29cb6a

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

druid/examples/svg.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ fn main() {
2323
eprintln!("cargo run --example svg --features \"svg\"");
2424
}
2525

26+
#[cfg(feature = "svg")]
27+
use log::error;
28+
2629
#[cfg(feature = "svg")]
2730
use druid::{
28-
widget::{Flex, Svg, WidgetExt},
31+
widget::{Flex, Svg, SvgData, WidgetExt},
2932
AppLauncher, Widget, WindowDesc,
3033
};
34+
3135
#[cfg(feature = "svg")]
3236
fn main() {
3337
let main_window = WindowDesc::new(ui_builder);
@@ -40,10 +44,18 @@ fn main() {
4044

4145
#[cfg(feature = "svg")]
4246
fn ui_builder() -> impl Widget<u32> {
43-
let tiger_svg = include_str!("tiger.svg");
47+
let tiger_svg = match include_str!("tiger.svg").parse::<SvgData>() {
48+
Ok(svg) => svg,
49+
Err(err) => {
50+
error!("{}", err);
51+
error!("Using an empty SVG instead.");
52+
SvgData::default()
53+
}
54+
};
55+
4456
let mut col = Flex::column();
4557

46-
col.add_child(Svg::new(tiger_svg).fix_width(100.0).center(), 1.0);
58+
col.add_child(Svg::new(tiger_svg.clone()).fix_width(100.0).center(), 1.0);
4759
col.add_child(Svg::new(tiger_svg), 1.0);
4860
col
4961
}

druid/src/widget/svg.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@
1414

1515
//! An SVG widget.
1616
17+
use std::error::Error;
18+
use std::marker::PhantomData;
19+
use std::str::FromStr;
20+
use std::sync::Arc;
21+
22+
use log::error;
23+
24+
use usvg;
25+
1726
use crate::{
1827
kurbo::BezPath,
1928
piet::{Color, RenderContext},
2029
BaseState, BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, PaintCtx, Point, Size,
2130
UpdateCtx, Widget,
2231
};
2332

24-
use log::error;
25-
use std::error::Error;
26-
use std::marker::PhantomData;
27-
use std::str::FromStr;
28-
29-
use usvg;
30-
3133
/// A widget that renders a SVG
3234
pub struct Svg<T> {
3335
// On construction the SVG string is parsed into a SvgData.
@@ -36,25 +38,13 @@ pub struct Svg<T> {
3638
}
3739

3840
impl<T: Data> Svg<T> {
39-
/// Create an SVG-drawing widget from a valid SVG string literal.
41+
/// Create an SVG-drawing widget from SvgData.
4042
///
4143
/// The SVG will scale to fit its box constraints.
42-
/// If SVG is invalid a blank SVG will be rendered instead.
43-
pub fn new(svg_str: &str) -> impl Widget<T> {
44-
let svg_data = SvgData::from_str(svg_str);
45-
46-
match svg_data {
47-
Ok(data) => Svg {
48-
svg_data: data,
49-
phantom: Default::default(),
50-
},
51-
Err(err) => {
52-
error!("{}", err);
53-
Svg {
54-
svg_data: SvgData::empty(),
55-
phantom: Default::default(),
56-
}
57-
}
44+
pub fn new(svg_data: SvgData) -> impl Widget<T> {
45+
Svg {
46+
svg_data,
47+
phantom: Default::default(),
5848
}
5949
}
6050

@@ -109,8 +99,9 @@ impl<T: Data> Widget<T> for Svg<T> {
10999

110100
/// Stored SVG data.
111101
/// Implements `FromStr` and can be converted to piet draw instructions.
102+
#[derive(Clone)]
112103
pub struct SvgData {
113-
tree: usvg::Tree,
104+
tree: Arc<usvg::Tree>,
114105
}
115106

116107
impl SvgData {
@@ -122,14 +113,14 @@ impl SvgData {
122113
};
123114

124115
let empty_svg = r###"
125-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 0 0">
116+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
126117
<g fill="none">
127118
</g>
128119
</svg>
129120
"###;
130121

131122
SvgData {
132-
tree: usvg::Tree::from_str(empty_svg, &re_opt).unwrap(),
123+
tree: Arc::new(usvg::Tree::from_str(empty_svg, &re_opt).unwrap()),
133124
}
134125
}
135126

@@ -203,6 +194,12 @@ impl SvgData {
203194
}
204195
}
205196

197+
impl Default for SvgData {
198+
fn default() -> Self {
199+
SvgData::empty()
200+
}
201+
}
202+
206203
impl FromStr for SvgData {
207204
type Err = Box<dyn Error>;
208205

@@ -213,7 +210,9 @@ impl FromStr for SvgData {
213210
};
214211

215212
match usvg::Tree::from_str(svg_str, &re_opt) {
216-
Ok(tree) => Ok(SvgData { tree }),
213+
Ok(tree) => Ok(SvgData {
214+
tree: Arc::new(tree),
215+
}),
217216
Err(err) => Err(err.into()),
218217
}
219218
}

0 commit comments

Comments
 (0)