| 1 |
## Folder Selection API |
| 2 |
|
| 3 |
The `pfd::select_folder` class handles folder opening dialogs. It can be provided a title, and an |
| 4 |
optional starting directory: |
| 5 |
|
| 6 |
```cpp |
| 7 |
pfd::select_folder::select_folder(std::string const &title, |
| 8 |
std::string const &default_path = "", |
| 9 |
pfd::opt option = pfd::opt::none); |
| 10 |
``` |
| 11 |
|
| 12 |
The `option` parameter can be `pfd::opt::force_path` to force the operating system to use the |
| 13 |
provided path. Some systems default to the most recently used path, if applicable. |
| 14 |
|
| 15 |
The selected folder is queried using `pfd::select_folder::result()`. If the user canceled the |
| 16 |
operation, the returned string is empty: |
| 17 |
|
| 18 |
```cpp |
| 19 |
std::string pfd::select_folder::result(); |
| 20 |
``` |
| 21 |
|
| 22 |
It is possible to ask the folder selection dialog whether the user took action using the |
| 23 |
`pfd::message::ready()` method, with an optional `timeout` argument. If the user did not validate |
| 24 |
the dialog within `timeout` milliseconds, the function will return `false`: |
| 25 |
|
| 26 |
```cpp |
| 27 |
bool pfd::select_folder::ready(int timeout = pfd::default_wait_timeout); |
| 28 |
``` |
| 29 |
|
| 30 |
## Example 1: simple folder selection |
| 31 |
|
| 32 |
Using `pfd::select_folder::result()` will wait for user action before returning. This operation |
| 33 |
will block and return the user choice: |
| 34 |
|
| 35 |
```cpp |
| 36 |
auto selection = pfd::select_folder("Select a folder").result(); |
| 37 |
if (!selection.empty()) |
| 38 |
std::cout << "User selected folder " << selection << "\n"; |
| 39 |
``` |
| 40 |
|
| 41 |
## Example 2: asynchronous folder open |
| 42 |
|
| 43 |
Using `pfd::select_folder::ready()` allows the application to perform other tasks while waiting for user input: |
| 44 |
|
| 45 |
```cpp |
| 46 |
// Folder selection dialog |
| 47 |
auto dialog = pfd::select_folder("Select folder to open"); |
| 48 |
|
| 49 |
// Do something while waiting for user input |
| 50 |
while (!dialog.ready(1000)) |
| 51 |
std::cout << "Waited 1 second for user input...\n"; |
| 52 |
|
| 53 |
// Act depending on the user choice |
| 54 |
std::cout << "Selected folder: " << dialog.result() << "\n"; |
| 55 |
``` |