# Windows and browsers

> What a window and a browser are, who opens them and how your app reaches them.

A window is what the operating system shows. A browser is Chromium's page inside it. Kurogane tracks the two apart. Each has its own id and its own lifetime.

## Kinds of browsers

[`BrowserType`](https://kurogane-rs.org/docs/api/windows/browsertype.md) says what a browser is:

| Kind | What it is | Its parent |
|---|---|---|
| `Main` | the start window's browser, a `create_window` browser or an embedded browser | none |
| `Popup` | a window a page opened | the browser that opened it |
| `DevTools` | Chromium's developer tools | the browser it inspects |
| `ChromeUi` | a window Chromium opened on its own, such as one restored from an earlier session | none |

The app keeps running while any browser is open, a hidden window's browser included. A `ChromeUi` browser is the exception. It closes with the app instead of keeping it running. Closing DevTools or a popup never ends the app.

## Application windows

The start window and every window from [`AppInstance::create_window`](https://kurogane-rs.org/docs/api/app/appinstance.md#method.create_window) are application windows. [`WindowOptions`](https://kurogane-rs.org/docs/api/windows/windowoptions.md) describes each one before it exists:

```rust
use kurogane::{App, WindowOptions, WindowState};

App::new("content")
    .window(
        WindowOptions::new()
            .name("main")
            .title("Notes")
            .size(1100, 720)
            .min_size(640, 480)
            .state(WindowState::Maximized),
    )
    .run_or_exit();
```

* **Title.** A window without one takes its page's `<title>` and follows it. Popups do the same.
* **Size.** Sizes and positions are in density-independent pixels and measure the content without the frame. A window without a size opens at 800 by 600. A size larger than the display's work area shrinks to fit.
* **Place.** A window without a [`WindowPlacement`](https://kurogane-rs.org/docs/api/windows/windowplacement.md) opens centered on the primary display. A placement no display shows anymore moves onto the nearest one. Wayland lets no app place its windows. There only the size applies.
* **State.** [`WindowState`](https://kurogane-rs.org/docs/api/windows/windowstate.md) is `Normal`, `Minimized`, `Maximized`, `Fullscreen` or `Hidden`.
* **Name.** A name is your app's handle on a window and is never shown. One open window holds a name at a time. `create_window` with a taken name fails with [`RuntimeError::WindowNameTaken`](https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.WindowNameTaken). The name is free again once that window's close is reported.

[`App::on_window_closing`](https://kurogane-rs.org/docs/api/app/app.md#method.on_window_closing) reports each application window's name and placement as it closes. Save the placement and give it back with [`WindowOptions::placement`](https://kurogane-rs.org/docs/api/windows/windowoptions.md#method.placement) to reopen the window where it was.

[`App::window_class`](https://kurogane-rs.org/docs/api/app/app.md#method.window_class) sets the class Linux desktops group your windows by. [`App::window_icon`](https://kurogane-rs.org/docs/api/app/app.md#method.window_icon) gives every window your app's icon.

## Windows a page opens

A page can't give itself a window. Kurogane decides before the window exists:

* A page of your app's own origin opens in an application window.
* An `http` or `https` link the user clicked opens in the system's default browser.
* Anything else is refused.

[`App::on_new_window`](https://kurogane-rs.org/docs/api/app/app.md#method.on_new_window) changes that per request. Chromium's own tabbed browser window never opens. Its new window and new tab shortcuts do nothing.

A page can't take its window anywhere either. A window shows your app's own origin, the origins your app loaded there and the origins a hook let in. A navigation anywhere else is answered as a new window is. [`App::on_navigation`](https://kurogane-rs.org/docs/api/app/app.md#method.on_navigation) changes that per navigation.

## Embedded browsers

An app started with [`App::start_embedded`](https://kurogane-rs.org/docs/api/app/app.md#method.start_embedded) has no windows of Kurogane's. [`AppInstance::create_child_browser`](https://kurogane-rs.org/docs/api/app/appinstance.md#method.create_child_browser) places a `Main` browser inside a window of your own. See [Runtime and event loop](https://kurogane-rs.org/docs/concepts/runtime-and-event-loop.md#embedded-mode).

## Reaching them from Rust

Every hook and command gets an [`AppHandle`](https://kurogane-rs.org/docs/api/app/apphandle.md). It works from any thread:

| Group | Methods |
|---|---|
| Browsers | `browsers`, `browser_count`, `get_browser_handle`, `browser_for_window`, `browser_opener`, `browser_parent`, `children_of` |
| Windows | `windows`, `window_ids`, `window_count`, `find_window_by_name`, `find_window_by_browser` |
| Ending | `close_all_browsers`, `close_all_windows`, `shutdown`, `should_shutdown` |

[`BrowserId`](https://kurogane-rs.org/docs/api/windows/browserid.md) and [`WindowId`](https://kurogane-rs.org/docs/api/windows/windowid.md) name a browser and a window. [`BrowserMetadata`](https://kurogane-rs.org/docs/api/windows/browsermetadata.md) holds a browser's kind, parent, opener and creation time.

A [`BrowserHandle`](https://kurogane-rs.org/docs/api/app/browserhandle.md) drives one browser:

| Group | Methods |
|---|---|
| Navigation | `navigate`, `reload`, `reload_ignore_cache`, `go_back`, `go_forward` |
| State | `url`, `is_loading`, `can_go_back`, `can_go_forward` |
| Page | `execute_javascript` |
| DevTools | `show_devtools`, `close_devtools`, `has_devtools` |
| Closing | `close` |
| Embedding | `set_bounds`, `notify_move_or_resize_started` |

`set_bounds` and `has_devtools` run only on the UI thread.

Lookups reflect what is open now. A closed browser leaves its window's link at once even while Chromium is still destroying the window.
