Commit e4fc94fe authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui

Export: copy the icon as an SVG

parent 0c63b14b
......@@ -15,6 +15,9 @@ pub struct ExportDialog {
icon_size_64: gtk::Image,
}
const TARGET_SVG: u32 = 0;
const TARGET_SVG_XML: u32 = 1;
impl ExportDialog {
pub fn new(icon: &Icon) -> ExportDialog {
let builder = gtk::Builder::new_from_resource("/org/gnome/design/IconLibrary/export_dialog.ui");
......@@ -96,15 +99,30 @@ impl ExportDialog {
// Copy icon to clipboard action: copy-clipboard
let icon_name = icon.name.clone();
let copy_icon = gio::SimpleAction::new("copy-clipboard", None);
copy_icon.connect_activate(move |_, _| {
let display = gdk::Display::get_default().unwrap();
let clipboard = gtk::Clipboard::get_default(&display).unwrap();
let theme = gtk::IconTheme::get_default().unwrap();
let pixbuf = theme.load_icon(&icon_name, 16, gtk::IconLookupFlags::FORCE_SYMBOLIC).unwrap();
let svg_target = gtk::TargetEntry::new("image/svg", gtk::TargetFlags::OTHER_APP, TARGET_SVG);
let svg_xml_target = gtk::TargetEntry::new("image/svg+xml", gtk::TargetFlags::OTHER_APP, TARGET_SVG_XML);
let targets = [svg_target, svg_xml_target];
clipboard.set_text("");
clipboard.set_image(&pixbuf.unwrap());
let icon_name = icon_name.clone();
let svg_targets = targets.clone();
clipboard.set_with_data(&targets, move |_, selection_data, target_id| {
if [TARGET_SVG, TARGET_SVG_XML].contains(&target_id) {
// load the svg content
let theme = gtk::IconTheme::get_default().unwrap();
let icon_info = theme.lookup_icon(&icon_name, 16, gtk::IconLookupFlags::FORCE_SYMBOLIC).unwrap();
let icon_path = icon_info.get_filename().unwrap();
let icon_file = gio::File::new_for_path(icon_path);
let svg = icon_file.load_contents(Some(&gio::Cancellable::new())).unwrap();
// copy the svg content to clipboard when requested
let target = svg_targets.get(target_id as usize).unwrap().get_target();
selection_data.set(&gdk::Atom::intern(target), 8, &svg.0);
}
});
});
actions.add_action(&copy_icon);
......
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