Skip to content

Commit

Permalink
Get uri strings in drop target
Browse files Browse the repository at this point in the history
  • Loading branch information
nik012003 committed Aug 14, 2023
1 parent 26ef60d commit 662363c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/compact_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{glib, CssProvider, DragSource, Label, Widget};

use crate::util::{generate_file_model, setup_drag_source_all, setup_drop_target, ListWidget, drag_source_and_exit};
use crate::util::{
drag_source_and_exit, generate_file_model, setup_drag_source_all, setup_drop_target, ListWidget,
};

pub fn generate_compact_view() -> ListWidget {
let file_model = generate_file_model();
Expand Down
36 changes: 24 additions & 12 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn setup_drag_source_all(drag_source: &DragSource, list_model: &ListStore) {
fn create_tmp_file(file: &File) -> Option<FileObject> {
let print_err = |err| eprintln!("{}", err);
if file.path().is_some() {
println!("It's some {:?}", file.path());
Some(FileObject::new(file))
} else {
let info = file.query_info(
Expand Down Expand Up @@ -116,29 +117,40 @@ pub fn setup_drop_target(model: &ListStore, widget: &Widget) {
.name("file-drop-target")
.actions(DragAction::COPY)
.build();
drop_target.set_types(&[FileList::static_type()]);
drop_target.set_types(&[FileList::static_type(), glib::types::Type::STRING]);

drop_target.connect_drop(
clone!(@weak model => @default-return false, move |_, value, _, _|
{
if let Ok(files) = value.get::<gdk::FileList>() {
let files = files.files();
if files.is_empty() {
return false;
}
let mut files_vec: Vec<File> = vec![];

let vec: Vec<FileObject> = files.iter()
if let Ok(file_uris) = value.get::<&str>(){
files_vec = file_uris.split('\n')
.collect::<Vec<&str>>()
.iter()
.filter_map(|uri| glib::Uri::parse(uri, glib::UriFlags::PARSE_RELAXED).ok())
.map(|uri| File::for_uri(uri.to_str().as_str()))
.collect();
}
else if let Ok(files) = value.get::<gdk::FileList>() {
files_vec = files.files();
}

if files_vec.is_empty(){
return false;
}

let file_objs:Vec<FileObject> = files_vec.iter()
.filter_map(|item| {
println!("{}", item.parse_name());
create_tmp_file(item)
}).collect();

if ARGS.get().unwrap().keep {
model.extend_from_slice(&vec);
}
return true
if ARGS.get().unwrap().keep {
model.extend_from_slice(&file_objs);
}
false

true
}),
);

Expand Down

0 comments on commit 662363c

Please sign in to comment.