Skip to content

Commit

Permalink
Initial get_results_iter implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wooster0 committed May 17, 2021
1 parent a980e60 commit 9036798
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 33 deletions.
22 changes: 12 additions & 10 deletions src/backend/gtk3/file_dialog/dialog_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ impl GtkFileDialog {
}

pub fn get_results(&self) -> Vec<PathBuf> {
self.get_results_iter().collect()
}

pub fn get_results_iter(&self) -> impl Iterator<Item = PathBuf> {
#[derive(Debug)]
struct FileList(*mut glib_sys::GSList);

Expand All @@ -123,17 +127,15 @@ impl GtkFileDialog {
let chosen_filenames =
unsafe { gtk_sys::gtk_file_chooser_get_filenames(self.ptr as *mut _) };

let paths: Vec<PathBuf> = FileList(chosen_filenames)
.filter_map(|item| {
let cstr = unsafe { CStr::from_ptr(item.data as _).to_str() };
let paths = FileList(chosen_filenames).filter_map(|item| {
let cstr = unsafe { CStr::from_ptr(item.data as _).to_str() };

if let Ok(cstr) = cstr {
Some(PathBuf::from(cstr.to_owned()))
} else {
None
}
})
.collect();
if let Ok(cstr) = cstr {
Some(PathBuf::from(cstr.to_owned()))
} else {
None
}
});

paths
}
Expand Down
43 changes: 37 additions & 6 deletions src/backend/macos/file_dialog/panel_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,47 @@ impl Panel {
}

pub fn get_results(&self) -> Vec<PathBuf> {
get_results_iter().collect()
}

pub fn get_results_iter(&self) -> impl Iterator<Item = PathBuf> {
struct FileList {
count: usize,
urls: Id<NSArray<NSURL>>,
}

impl FileList {
fn new(urls: Vec<Id<NSArray<NSURL>>>) {
FileList {
count: urls.len(),
urls,
}
}
}

impl Iterator for FileList {
type Item = Id<NSArray<NSURL>>;

fn next(&mut self) -> Option<PathBuf> {
let curr_ptr = self.0;

if !curr_ptr.is_null() {
let curr = unsafe { *curr_ptr };

self.0 = curr.next;

Some(curr)
} else {
None
}
}
}

unsafe {
let urls = msg_send![self.panel, URLs];
let urls: Id<NSArray<NSURL>> = Id::from_ptr(urls);

let mut res = Vec::new();
for url in urls.to_vec() {
res.push(url.to_path_buf());
}

res
FileList::new(urls.to_vec())
}
}
}
Expand Down
56 changes: 39 additions & 17 deletions src/backend/win_cid/file_dialog/dialog_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,55 @@ impl IDialog {
Ok(())
}

pub fn get_results(&self) -> Result<Vec<PathBuf>, HRESULT> {
unsafe {
let mut res_items: *mut IShellItemArray = ptr::null_mut();
(*(self.0 as *mut IFileOpenDialog))
.GetResults(&mut res_items)
.check()?;
pub fn get_results_iter(&self) -> Result<impl Iterator<Item = PathBuf>, HRESULT> {
struct FileList {
items: *mut IShellItemArray,
count: usize,
}

impl FileList {
fn new(item_array: *mut IFileDialog) -> Result<Self, HRESULT> {
let mut res_items: *mut IShellItemArray = ptr::null_mut();
(*(item_array.0 as *mut IFileOpenDialog))
.GetResults(&mut res_items)
.check()?;

let items = &*res_items;
let mut count = 0;
items.GetCount(&mut count);

Self { items, count }
}
}

impl Iterator for FileList {
type Item = *mut IShellItem;

let items = &*res_items;
let mut count = 0;
items.GetCount(&mut count);
let mut paths = Vec::new();
for id in 0..count {
fn next(&mut self) -> Option<PathBuf> {
let mut res_item: *mut IShellItem = ptr::null_mut();
items.GetItemAt(id, &mut res_item).check()?;
items.GetItemAt(id, &mut res_item).check().ok()?;
let mut display_name: LPWSTR = ptr::null_mut();
(*res_item)
.GetDisplayName(SIGDN_FILESYSPATH, &mut display_name)
.check()?;
.check()
.ok()?;
let filename = to_os_string(&display_name);
CoTaskMemFree(display_name as LPVOID);
let path = PathBuf::from(filename);
paths.push(path);
Some(PathBuf::from(filename))
}
items.Release();
}

Ok(paths)
impl Drop for FileList {
fn drop(&mut self) {
self.items.Release();
}
}

FileList::new(self.0)?
}

pub fn get_results(&self) -> Result<Vec<PathBuf>, HRESULT> {
get_results_iter()?.collect()
}

pub fn get_result(&self) -> Result<PathBuf, HRESULT> {
Expand Down

0 comments on commit 9036798

Please sign in to comment.