accept dnd drops from app library prototype
This commit is contained in:
parent
b9247e37c4
commit
83b6f3da5e
9 changed files with 215 additions and 47 deletions
|
|
@ -2,6 +2,8 @@ use gtk::glib;
|
|||
use gtk::prelude::*;
|
||||
use gtk::subclass::prelude::*;
|
||||
use gtk4 as gtk;
|
||||
use gtk4::DragSource;
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
use gtk::CompositeTemplate;
|
||||
|
||||
|
|
@ -10,6 +12,7 @@ use gtk::CompositeTemplate;
|
|||
pub struct DockItem {
|
||||
#[template_child]
|
||||
pub image: TemplateChild<gtk::Image>,
|
||||
pub drag_controller: OnceCell<DragSource>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
use crate::icon_source;
|
||||
use gdk4::ContentProvider;
|
||||
use gdk4::Display;
|
||||
use gio::File;
|
||||
use glib::FromVariant;
|
||||
use glib::Variant;
|
||||
use gio::Icon;
|
||||
use gio::ListStore;
|
||||
use gtk4 as gtk;
|
||||
use gtk4::DragSource;
|
||||
use gtk4::IconTheme;
|
||||
use gtk4::TreeIter;
|
||||
mod imp;
|
||||
|
||||
use crate::ApplicationObject;
|
||||
use gtk::glib;
|
||||
use gtk::prelude::*;
|
||||
use gtk::subclass::prelude::*;
|
||||
|
|
@ -27,44 +26,94 @@ impl Default for DockItem {
|
|||
|
||||
impl DockItem {
|
||||
pub fn new() -> Self {
|
||||
glib::Object::new(&[]).expect("Failed to create DockItem")
|
||||
let dc = DragSource::builder()
|
||||
.name("dock drag source")
|
||||
.actions(gdk4::DragAction::MOVE)
|
||||
.build();
|
||||
|
||||
let self_: DockItem = glib::Object::new(&[]).expect("Failed to create DockItem");
|
||||
self_.add_controller(&dc);
|
||||
imp::DockItem::from_instance(&self_)
|
||||
.drag_controller
|
||||
.set(dc)
|
||||
.expect("Failed to set dock item");
|
||||
self_
|
||||
}
|
||||
|
||||
pub fn set_app_info(&self, app_info: &gio::DesktopAppInfo) {
|
||||
pub fn drag_controller(&self) -> &DragSource {
|
||||
let imp = imp::DockItem::from_instance(self);
|
||||
imp.drag_controller
|
||||
.get()
|
||||
.expect("Could not get drag_controller")
|
||||
}
|
||||
|
||||
// TODO current method seems very messy...
|
||||
// refactor to emit event for removing the item?
|
||||
pub fn set_app_info(
|
||||
&self,
|
||||
app_info: &gio::DesktopAppInfo,
|
||||
i: u32,
|
||||
saved_app_model: &ListStore,
|
||||
) {
|
||||
dbg!("setting app info");
|
||||
let self_ = imp::DockItem::from_instance(self);
|
||||
self_.image.set_tooltip_text(Some(&app_info.name()));
|
||||
|
||||
let drag = DragSource::builder()
|
||||
.name("application library drag source")
|
||||
.actions(gdk4::DragAction::COPY)
|
||||
// .content()
|
||||
.build();
|
||||
self.add_controller(&drag);
|
||||
if let Some(file) = app_info.filename() {
|
||||
let file = File::for_path(file);
|
||||
let provider = ContentProvider::for_value(&file.to_value());
|
||||
drag.set_content(Some(&provider));
|
||||
}
|
||||
if let Some(icon) = app_info.icon() {
|
||||
dbg!("setting icon {}", app_info.name());
|
||||
self_.image.set_from_gicon(&icon);
|
||||
// set drag source icon if possible...
|
||||
// gio Icon is not easily converted to a Paintable, but this seems to be the correct method
|
||||
if let Some(default_display) = &Display::default() {
|
||||
if let Some(icon_theme) = IconTheme::for_display(default_display) {
|
||||
if let Some(paintable_icon) = icon_theme.lookup_by_gicon(
|
||||
&icon,
|
||||
64,
|
||||
1,
|
||||
gtk4::TextDirection::None,
|
||||
gtk4::IconLookupFlags::empty(),
|
||||
) {
|
||||
drag.set_icon(Some(&paintable_icon), 32, 32);
|
||||
if let Some(drag_controller) = self_.drag_controller.get() {
|
||||
// if let Some(file) = app_info.filename() {
|
||||
// let file = File::for_path(file);
|
||||
let provider = ContentProvider::for_value(&app_info.to_value());
|
||||
drag_controller.set_content(Some(&provider));
|
||||
// }
|
||||
drag_controller.connect_drag_end(move |_self, _drag, delete_data| {
|
||||
dbg!("removing", delete_data);
|
||||
});
|
||||
drag_controller.connect_drag_cancel(
|
||||
glib::clone!(@weak saved_app_model => @default-return true, move |_self, _drag, _delete_data| {
|
||||
dbg!("removing {}", i);
|
||||
if saved_app_model.n_items() > i {
|
||||
saved_app_model.remove(i);
|
||||
}
|
||||
true
|
||||
}),
|
||||
);
|
||||
drag_controller.connect_drag_end(
|
||||
glib::clone!(@weak saved_app_model => move |_self, _drag, delete_data| {
|
||||
dbg!("removing {}", i);
|
||||
if delete_data && saved_app_model.n_items() > i {
|
||||
saved_app_model.remove(i);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let icon = app_info
|
||||
.icon()
|
||||
.unwrap_or(Icon::for_string("image-missing").expect("Failed to set default icon"));
|
||||
drag_controller.connect_drag_begin(glib::clone!(@weak icon, => move |_self, drag| {
|
||||
drag.set_selected_action(gdk4::DragAction::MOVE);
|
||||
// set drag source icon if possible...
|
||||
// gio Icon is not easily converted to a Paintable, but this seems to be the correct method
|
||||
if let Some(default_display) = &Display::default() {
|
||||
if let Some(icon_theme) = IconTheme::for_display(default_display) {
|
||||
if let Some(paintable_icon) = icon_theme.lookup_by_gicon(
|
||||
&icon,
|
||||
64,
|
||||
1,
|
||||
gtk4::TextDirection::None,
|
||||
gtk4::IconLookupFlags::empty(),
|
||||
) {
|
||||
_self.set_icon(Some(&paintable_icon), 32, 32);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
let icon = app_info
|
||||
.icon()
|
||||
.unwrap_or(Icon::for_string("image-missing").expect("Failed to set default icon"));
|
||||
|
||||
self_.image.set_from_gicon(&icon);
|
||||
}
|
||||
|
||||
// pub fn set_app_info(&self, app_obj: ApplicationObject) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue