|
@@ -1,3 +1,4 @@
|
|
|
+use anyhow::{Result, bail, anyhow};
|
|
|
use gtk::{
|
|
|
Application,
|
|
|
ApplicationWindow,
|
|
@@ -16,31 +17,34 @@ use std::rc::Rc;
|
|
|
use std::cell::RefCell;
|
|
|
|
|
|
|
|
|
-struct Legend {
|
|
|
+struct AppData {
|
|
|
symbol: char,
|
|
|
color: (f64, f64, f64),
|
|
|
}
|
|
|
|
|
|
-impl Legend {
|
|
|
- fn draw_to_file(&self) {
|
|
|
- let f = std::fs::File::create("samp.svg").unwrap();
|
|
|
+impl AppData {
|
|
|
+ fn draw_to_file(&self) -> Result<()> {
|
|
|
+ let f = std::fs::File::create("samp.svg")?;
|
|
|
let surf = cairo::SvgSurface::for_stream(
|
|
|
100.0,
|
|
|
100.0,
|
|
|
f,
|
|
|
- ).unwrap();
|
|
|
- self.render(&cairo::Context::new(&surf));
|
|
|
- surf.finish_output_stream().unwrap();
|
|
|
+ )?;
|
|
|
+ self.render(&cairo::Context::new(&surf))?;
|
|
|
+ if let Err(err) = surf.finish_output_stream() {
|
|
|
+ bail!(err.error);
|
|
|
+ };
|
|
|
+ Ok(())
|
|
|
}
|
|
|
|
|
|
- fn render(&self, ctx: &cairo::Context) -> () {
|
|
|
+ fn render(&self, ctx: &cairo::Context) -> Result<()> {
|
|
|
ctx.set_source_rgb(self.color.0, self.color.1, self.color.2);
|
|
|
ctx.paint();
|
|
|
|
|
|
ctx.set_source_rgb(0., 0., 0.);
|
|
|
|
|
|
ctx.move_to(0.0, -23.0);
|
|
|
- let layout = pangocairo::functions::create_layout(&ctx).unwrap();
|
|
|
+ let layout = pangocairo::functions::create_layout(&ctx).ok_or_else(|| anyhow!("Could not create layout"))?;
|
|
|
layout.set_width(100 * 1024);
|
|
|
layout.set_alignment(pango::Alignment::Center);
|
|
|
let font = pango::FontDescription::from_string("Fira Sans 92");
|
|
@@ -48,13 +52,10 @@ impl Legend {
|
|
|
|
|
|
layout.set_text(&self.symbol.to_string());
|
|
|
pangocairo::functions::show_layout(&ctx, &layout);
|
|
|
+ Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-struct AppData {
|
|
|
- legend: Legend,
|
|
|
-}
|
|
|
-
|
|
|
#[derive(Clone)]
|
|
|
struct App {
|
|
|
data: Rc<RefCell<AppData>>,
|
|
@@ -62,11 +63,11 @@ struct App {
|
|
|
|
|
|
impl App {
|
|
|
fn new() -> App {
|
|
|
- let legend = Legend {
|
|
|
+ let app_data = AppData {
|
|
|
symbol: 'X',
|
|
|
color: (0.5, 0.5, 0.5),
|
|
|
};
|
|
|
- let data = Rc::new(RefCell::new(AppData { legend }));
|
|
|
+ let data = Rc::new(RefCell::new(app_data));
|
|
|
App { data }
|
|
|
}
|
|
|
}
|
|
@@ -90,14 +91,18 @@ fn mk_app(app: >k::Application) {
|
|
|
|
|
|
let canvas = gtk::DrawingArea::new();
|
|
|
let copy = app.clone();
|
|
|
- canvas.connect_draw(move |cv, ctx| {
|
|
|
- copy.data.borrow_mut().legend.render(ctx);
|
|
|
+ canvas.connect_draw(move |_cv, ctx| {
|
|
|
+ if let Err(err) = copy.data.borrow_mut().render(ctx) {
|
|
|
+ eprintln!("Error in rendering: {}", err);
|
|
|
+ }
|
|
|
gtk::Inhibit(false)
|
|
|
});
|
|
|
flow.pack_start(&canvas, true, true, 0);
|
|
|
|
|
|
button.connect_clicked(move |_| {
|
|
|
- app.data.borrow_mut().legend.draw_to_file();
|
|
|
+ if let Err(err) = app.data.borrow_mut().draw_to_file() {
|
|
|
+ eprintln!("Error in rendering: {}", err);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
window.add(&flow);
|
|
@@ -107,7 +112,7 @@ fn mk_app(app: >k::Application) {
|
|
|
fn mk_icon_choice(choice: char, cell: App) -> gtk::Button {
|
|
|
let button = gtk::Button::with_label(&choice.to_string());
|
|
|
button.connect_clicked(move |_| {
|
|
|
- cell.data.borrow_mut().legend.symbol = choice;
|
|
|
+ cell.data.borrow_mut().symbol = choice;
|
|
|
println!("Choosing '{}'", choice);
|
|
|
});
|
|
|
button
|