~vpzom/savg

2670797412c156c5085624824e932f8217d5e836 — Colin Reeder 5 years ago 5a28ee1
Add save dialog
2 files changed, 30 insertions(+), 3 deletions(-)

M src/main.rs
M src/save.rs
M src/main.rs => src/main.rs +29 -3
@@ 14,7 14,7 @@ use types::*;

use gtk::prelude::*;

use std::ops::Deref;
use std::borrow::Cow;
use std::sync::Arc;
use std::sync::RwLock;
use std::sync::atomic::AtomicBool;


@@ 108,9 108,35 @@ fn main() {

    {
        let instance = instance.clone();
        let window = window.clone();
        save_item.connect_activate(move |_| {
            let document = instance.document.read().unwrap();
            save::save_svg(&document, std::fs::File::create("/tmp/savg.svg").expect("jhdsjkghsdfkj"));
            let dialog = gtk::FileChooserDialog::with_buttons(Some("Save File"), Some(&window), gtk::FileChooserAction::Save, &[(&gtk::STOCK_CANCEL, gtk::ResponseType::Cancel), (&gtk::STOCK_SAVE, gtk::ResponseType::Accept)]);
            let filter = gtk::FileFilter::new();
            filter.add_pattern("*.svg");
            gtk::FileFilterExt::set_name(&filter, "SVG");
            dialog.add_filter(&filter);
            dialog.set_current_name("untitled.svg");
            let res = dialog.run();
            dialog.hide();
            if gtk::ResponseType::Accept == res.into() {
                let result = dialog.get_filename()
                    .ok_or(Cow::Borrowed("Unable to get filename"))
                    .and_then(|filename| {
                        std::fs::File::create(filename)
                            .map_err(|e| format!("Unable to open output file: {:?}", e).into())
                    })
                    .and_then(|file| {
                        let document = instance.document.read().unwrap();
                        save::save_svg(&document, file)
                            .map_err(|e| format!("{:?}", e).into())
                    });
                if let Err(err) = result {
                    let error_dialog = gtk::MessageDialog::new(Some(&window), gtk::DialogFlags::MODAL & gtk::DialogFlags::DESTROY_WITH_PARENT, gtk::MessageType::Error, gtk::ButtonsType::Ok, &format!("Failed to save file: {}", err));
                    error_dialog.run();
                    error_dialog.destroy();
                }
            }
            dialog.destroy();
        });
    }


M src/save.rs => src/save.rs +1 -0
@@ 6,6 6,7 @@ use document;

use std::borrow::Cow;

#[derive(Debug)]
pub enum Error {
    XML(quick_xml::Error),
    IO(std::io::Error),