Skip to content

Commit 87531e6

Browse files
authored
Merge pull request #5 from emoon/master
Added support for NFD_PickFolder
2 parents 58a3710 + 09a6c66 commit 87531e6

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nfd"
3-
version = "0.0.3"
3+
version = "0.0.4"
44
authors = [
55
"Saurav Sachidanand <[email protected]>",
66
"Daniel Collin <[email protected]>",

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fn main() {
4242
cfg.compile("libnfd.a");
4343
println!("cargo:rustc-link-lib=framework=AppKit");
4444
} else if env.contains("windows") {
45+
cfg.cpp(true);
4546
cfg.file(nfd!("nfd_win.cpp"));
4647
cfg.compile("libnfd.a");
4748
println!("cargo:rustc-link-lib=ole32");

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: 10 additions & 0 deletions
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,6 +151,10 @@ 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 {

0 commit comments

Comments
 (0)