Implement drag source on images

Using a 128x128 thumbnail.
This commit is contained in:
Maximiliano Sandoval R 2022-01-08 16:21:27 +01:00 • committed by Christopher Davis
parent fc6d3668c0
commit 13d7a21501
5 changed files with 135 additions and 0 deletions

View file

@ -210,4 +210,22 @@ impl LpImage {
let imp = self.imp();
imp.texture.borrow().clone()
}
pub fn content_provider(&self) -> anyhow::Result<gdk::ContentProvider> {
let imp = self.imp();
let mut contents = vec![];
if let Some(file) = imp.file.borrow().as_ref() {
let content = gdk::ContentProvider::for_value(&file.to_value());
contents.push(content);
}
if let Some(texture) = imp.texture.borrow().as_ref() {
let bytes = texture.save_to_png_bytes();
let content = gdk::ContentProvider::for_bytes("image/png", &bytes);
contents.push(content);
}
Ok(gdk::ContentProvider::new_union(contents.as_slice()))
}
}

View file

@ -31,6 +31,7 @@ use ashpd::WindowIdentifier;
use once_cell::sync::Lazy;
use std::cell::{Cell, RefCell};
use crate::thumbnail::Thumbnail;
use crate::util;
use crate::widgets::LpImage;
@ -131,6 +132,30 @@ mod imp {
.connect_pressed(clone!(@weak obj => move |_, x, y| {
obj.show_popover_at(x, y);
}));
let source = gtk::DragSource::new();
source.connect_prepare(
glib::clone!(@weak obj => @default-return None, move |_, _, _| {
match obj.imp().picture.content_provider() {
Ok(content) => Some(content),
Err(e) => {
log::error!("Could not get content provider: {:?}", e);
None
}
}
}),
);
source.connect_drag_begin(glib::clone!(@weak obj => move |source, _| {
let imp = obj.imp();
if let Some(texture) = imp.picture.texture() {
let thumbnail = Thumbnail::new(&texture);
source.set_icon(Some(&thumbnail), 0, 0);
};
}));
obj.add_controller(&source);
}
}