Skip to content

Commit d74e7ce

Browse files
committed
Added support for NFD_PickFolder
* Notice this currently uses the devel branch of on NFD. I’m not sure we want to use this or not but I wanted to PickFolder support for my own use case.
1 parent 50c0b5d commit d74e7ce

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

examples/pick_folder.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
/*
3+
Copyright (c) 2016 Saurav Sachidanand
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+
*/
23+
24+
extern crate nfd;
25+
26+
use nfd::Response;
27+
28+
fn main() {
29+
let result = nfd::open_pick_folder(None).unwrap_or_else(|e| {
30+
panic!(e);
31+
});
32+
33+
match result {
34+
Response::Okay(file_path) => println!("File path = {:?}", file_path),
35+
Response::Cancel => println!("User canceled"),
36+
_ => (),
37+
}
38+
}
39+

src/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ extern "C" {
5353
pub fn NFD_OpenDialog(filterList: *const nfdchar_t, defaultPath: *const nfdchar_t, outPath: *mut *mut nfdchar_t) -> nfdresult_t;
5454
pub fn NFD_OpenDialogMultiple(filterList: *const nfdchar_t, defaultPath: *const nfdchar_t, outPaths: *mut nfdpathset_t) -> nfdresult_t;
5555
pub fn NFD_SaveDialog(filterList: *const nfdchar_t, defaultPath: *const nfdchar_t, outPath: *mut *mut nfdchar_t) -> nfdresult_t;
56+
pub fn NFD_PickFolder(defaultPath: *const nfdchar_t, outPath: *mut *mut nfdchar_t) -> nfdresult_t;
5657
pub fn NFD_GetError() -> *const raw::c_char;
5758
pub fn NFD_PathSet_GetCount(pathSet: *const nfdpathset_t) -> size_t;
5859
pub fn NFD_PathSet_GetPath(pathSet: *const nfdpathset_t, index: size_t) -> *mut nfdchar_t;

src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub enum DialogType {
4343
SingleFile,
4444
MultipleFiles,
4545
SaveFile,
46+
PickFolder,
4647
}
4748

4849
pub struct DialogBuilder<'a> {
@@ -105,6 +106,11 @@ pub fn open_save_dialog(filter_list: Option<&str>, default_path: Option<&str>) -
105106
open_dialog(filter_list, default_path, DialogType::SaveFile)
106107
}
107108

109+
/// Open save dialog
110+
pub fn open_pick_folder(default_path: Option<&str>) -> Result<Response> {
111+
open_dialog(None, default_path, DialogType::PickFolder)
112+
}
113+
108114
pub fn open_dialog(filter_list: Option<&str>, default_path: Option<&str>, dialog_type: DialogType) -> Result<Response> {
109115
let result;
110116
let filter_list_cstring;
@@ -145,11 +151,16 @@ pub fn open_dialog(filter_list: Option<&str>, default_path: Option<&str>, dialog
145151
DialogType::SaveFile => {
146152
NFD_SaveDialog(filter_list_ptr, default_path_ptr, ptr_out_path)
147153
},
154+
155+
DialogType::PickFolder => {
156+
NFD_PickFolder(default_path_ptr, ptr_out_path)
157+
},
148158
};
149159

150160
match result {
151161
nfdresult_t::NFD_OKAY =>{
152-
if dialog_type == DialogType::SingleFile {
162+
if dialog_type == DialogType::SingleFile ||
163+
dialog_type == DialogType::PickFolder {
153164
Ok(Response::Okay(CStr::from_ptr(out_path).to_string_lossy().into_owned()))
154165
} else {
155166
let count = NFD_PathSet_GetCount(&out_multiple);

0 commit comments

Comments
 (0)