# File choosers

> Answer the file choosers a page opens with the system's dialog, files of your choosing or a picker of your own.

A page opens a file chooser with `<input type="file">` or the File System Access pickers (`showOpenFilePicker`, `showSaveFilePicker` and `showDirectoryPicker`). Without a hook the system's chooser shows. [`App::on_file_dialog`](https://kurogane-rs.org/docs/api/app/app.md#method.on_file_dialog) can answer instead.

## Give the page files of your choosing

```rust
use kurogane::{App, FileDialogDecision, FileDialogKind};

fn main() {
    let inbox = std::env::temp_dir().join("inbox.txt");

    App::new("content")
        .on_file_dialog(move |request, _app| match request.kind() {
            // The page's import always reads the inbox
            FileDialogKind::Open => FileDialogDecision::Files(vec![inbox.clone()]),
            _ => FileDialogDecision::Default,
        })
        .run_or_exit();
}
```

The page reads the files as if the user chose them. An `Open` or `Save` dialog takes the first file. An `OpenFolder` dialog takes a folder. An empty list cancels.

| [`FileDialogDecision`](https://kurogane-rs.org/docs/api/requests/filedialogdecision.md) | What happens |
|---|---|
| `Default` | the system's file chooser shows |
| `Files(paths)` | the page gets these files without a dialog |
| `Cancel` | no dialog; the page is told the user cancelled |
| `Later` | the request's responder answers later |

## Allow choosers on your own pages only

A page of another origin in your app may open a chooser too. Cancel those:

```rust
use kurogane::{App, FileDialogDecision, Origin};

fn main() {
    let own = Origin::parse("app://app").unwrap();

    App::new("content")
        .on_file_dialog(move |request, _app| {
            if request.origin() == &own {
                FileDialogDecision::Default
            } else {
                FileDialogDecision::Cancel
            }
        })
        .run_or_exit();
}
```

`request.origin()` is the origin of the frame that has the focus, the one whose control the user activated.

## Show a picker of your own

Return `FileDialogDecision::Later` and keep the request's [`FileDialogResponder`](https://kurogane-rs.org/docs/api/requests/filedialogresponder.md). The page waits until the responder selects or cancels from any thread:

```rust
use std::path::PathBuf;

use kurogane::{App, FileDialogDecision};

fn pick_files() -> Option<Vec<PathBuf>> {
    // Show your own picker here
    Some(vec![std::env::temp_dir().join("notes.txt")])
}

fn main() {
    App::new("content")
        .on_file_dialog(|request, _app| {
            let responder = request.responder();
            std::thread::spawn(move || match pick_files() {
                Some(files) => responder.select(files),
                None => responder.cancel(),
            });
            FileDialogDecision::Later
        })
        .run_or_exit();
}
```

* A request is answered once. The first answer counts.
* Dropping the responder without an answer cancels the request.
* An answer that comes after the page went away gives it nothing.
* `Later` without a responder kept cancels at once.

## Know what the page asked for

[`FileDialogRequest`](https://kurogane-rs.org/docs/api/requests/filedialogrequest.md) describes the chooser:

| Method | Gives |
|---|---|
| `kind()` | `Open`, `OpenMultiple`, `OpenFolder` or `Save` ([`FileDialogKind`](https://kurogane-rs.org/docs/api/requests/filedialogkind.md)) |
| `title()` | the dialog's title as the page gives it; empty for the system's own |
| `default_path()` | the file name or path the dialog would start at; the suggested name for a save picker |
| `accept()` | what the page accepts as it wrote it, such as `image/*` or `.png`; empty for any file |
| `origin()` | the origin of the page that opens the dialog |
| `browser()` | the browser of the page |

## Read files without a chooser

A page that needs files your app already knows doesn't need a chooser at all. The [filesystem capability](https://kurogane-rs.org/docs/concepts/capabilities-and-permissions.md#the-filesystem-capability) grants it a scope to read and write through `window.kurogane.fs`.

## Rules for the hook

* It runs on the UI thread and must not block.
* A hook that panics cancels the dialog.
* Kurogane's own Save As for a download isn't asked about. [`App::on_download`](https://kurogane-rs.org/docs/api/app/app.md#method.on_download) decides where downloads go.
* DevTools' dialogs aren't asked about.
* A later call replaces an earlier hook.
