export: use a native file chooser

fixes #60
parent c0bff8d9
Pipeline #183098 passed with stage
in 41 minutes and 34 seconds
......@@ -114,7 +114,7 @@ pub fn is_valid_app_id(app_id: &str) -> bool {
app_id.contains('.') && !app_id.ends_with('.') && !app_id.starts_with('.')
}
pub fn clean_svg(file: PathBuf) -> anyhow::Result<()> {
pub fn clean_svg(svg: &str) -> anyhow::Result<Vec<u8>> {
let options = svgcleaner::CleaningOptions {
remove_unused_defs: true,
convert_shapes: false,
......@@ -155,14 +155,10 @@ pub fn clean_svg(file: PathBuf) -> anyhow::Result<()> {
paths_coordinates_precision: 8,
transforms_precision: 8,
};
let data = svgcleaner::cleaner::load_file(file.to_str().unwrap())?;
let mut document = svgcleaner::cleaner::parse_data(&data, &svgcleaner::ParseOptions::default()).unwrap();
let mut document = svgcleaner::cleaner::parse_data(svg, &svgcleaner::ParseOptions::default()).unwrap();
let _ = svgcleaner::cleaner::clean_doc(&mut document, &options, &svgcleaner::WriteOptions::default());
let mut buf = vec![];
svgcleaner::cleaner::write_buffer(&document, &svgcleaner::WriteOptions::default(), &mut buf);
svgcleaner::cleaner::save_file(&buf, file.to_str().unwrap())?;
Ok(())
Ok(buf)
}
......@@ -93,12 +93,7 @@ impl Project {
_ => (),
};
let dialog = gtk::FileChooserDialog::with_buttons(
Some(&gettext("Export")),
Some(parent),
gtk::FileChooserAction::Save,
&[(&gettext("_Save"), gtk::ResponseType::Accept), (&gettext("_Cancel"), gtk::ResponseType::Cancel)],
);
let dialog = gtk::FileChooserNative::new(Some(&gettext("Export")), Some(parent), gtk::FileChooserAction::Save, Some(&gettext("_Save")), Some(&gettext("_Cancel")));
dialog.set_modal(true);
dialog.set_current_name(&icon_name);
......@@ -108,13 +103,17 @@ impl Project {
svg_filter.add_mime_type("image/svg+xml");
dialog.add_filter(&svg_filter);
dialog.connect_response(clone!(@strong gicon => move |dialog, response| {
dialog.connect_response(clone!(@strong gicon, @strong dialog => move |_, response| {
if response == gtk::ResponseType::Accept {
let dest = dialog.get_file().unwrap();
if let Some(source) = &gicon {
let save = move || -> anyhow::Result<()> {
source.copy(&dest, gio::FileCopyFlags::OVERWRITE, gio::NONE_CANCELLABLE, None)?;
common::clean_svg(dest.get_path().unwrap())?;
let (svg, _) = source.load_contents(gio::NONE_CANCELLABLE)?;
let cleaned_svg = common::clean_svg(std::str::from_utf8(&svg)?)?;
dest.replace_contents(&cleaned_svg, None, false,
gio::FileCreateFlags::REPLACE_DESTINATION,
gio::NONE_CANCELLABLE)?;
Ok(())
};
if save().is_err() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment