# Struct `kurogane::AppInstance`

```rust
pub struct AppInstance { /* private fields */ }
```

The running application, owned by the thread that started it.

Not `Send`; CEF shuts down on the thread that initialized it and dropping
an `AppInstance` shuts CEF down. Use [`AppInstance::handle`] from other
threads.

[`AppInstance::handle`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.handle

## Methods

### `handle`

```rust
pub fn handle(&self) -> &AppHandle
```

Returns the shared handle, usable from any thread.

### `pump`

```rust
pub fn pump(&self)
```

Advances Chromium by one iteration of its internal message loop.

When using external event-loop ownership via App::start,
this must be called repeatedly on the thread that initialized CEF.

Kurogane assumes pump calls are non-reentrant and originate from a
single UI thread.

### `should_shutdown`

```rust
pub fn should_shutdown(&self) -> bool
```

Returns true once the application has ended; see
[`AppHandle::should_shutdown`].

[`AppHandle::should_shutdown`]: https://kurogane-rs.org/docs/api/app/apphandle.md#method.should_shutdown

### `create_window`

```rust
pub fn create_window(
    &self,
    url: &str,
    options: WindowOptions,
) -> Result<WindowId, RuntimeError>
```

Opens `url` in a new window of the application's, as `options` say
(see [`WindowOptions`](https://kurogane-rs.org/docs/api/windows/windowoptions.md)).

#### Errors

[`RuntimeError::InvalidWindowOptions`] for options no window can
have; [`RuntimeError::WindowNameTaken`] when an open window has the
name the options give; [`RuntimeError::ShuttingDown`] once
[`AppHandle::shutdown`] has begun;
[`RuntimeError::BrowserCreationFailed`] or
[`RuntimeError::WindowCreationFailed`] when CEF creates neither.

[`AppHandle::shutdown`]: https://kurogane-rs.org/docs/api/app/apphandle.md#method.shutdown
[`RuntimeError::BrowserCreationFailed`]: https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.BrowserCreationFailed
[`RuntimeError::WindowCreationFailed`]: https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.WindowCreationFailed
[crate::WindowOptions]: https://kurogane-rs.org/docs/api/windows/windowoptions.md
[`RuntimeError::InvalidWindowOptions`]: https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.InvalidWindowOptions
[`RuntimeError::WindowNameTaken`]: https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.WindowNameTaken
[`RuntimeError::ShuttingDown`]: https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.ShuttingDown

### `run`

```rust
pub fn run(self) -> Result<(), RuntimeError>
```

Takes ownership and blocks on the CEF message loop.

The loop runs until the application's last browser has closed, after
[`AppHandle::shutdown`] (from any thread), its last window closing or
Ctrl+C. tanso::shutdown() then runs on the current (UI) thread.

Not for an application given an [`App::scheduler`](https://kurogane-rs.org/docs/api/app/app.md#method.scheduler).
The scheduler turns on CEF's external message pump, under which this
loop returns at once and tanso::shutdown() would run under a window
still opening. Such an application calls [`AppInstance::pump`] from its
own loop until [`AppInstance::should_shutdown`], then
[`AppInstance::shutdown`].

[`AppInstance::shutdown`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.shutdown
[`AppInstance::should_shutdown`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.should_shutdown
[`AppInstance::pump`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.pump
[`AppHandle::shutdown`]: https://kurogane-rs.org/docs/api/app/apphandle.md#method.shutdown
[crate::App::scheduler]: https://kurogane-rs.org/docs/api/app/app.md#method.scheduler

### `shutdown`

```rust
pub fn shutdown(&self)
```

Perform orderly CEF shutdown.

Calls tanso::shutdown() on the UI thread; [`AppHandle::should_shutdown`]
is true from here on. Safe to call multiple times. Subsequent calls are
no-ops.

Unlike [`AppHandle::shutdown`], this shuts down the CEF runtime itself.
All browsers must already be closed; [`AppHandle::should_shutdown`] is
true at that point.

From here on, [`AppHandle`] and [`BrowserHandle`] calls that would
reach CEF do nothing. Finish using them on other threads first. A call
made there at this moment can still reach CEF.

[`AppHandle`]: https://kurogane-rs.org/docs/api/app/apphandle.md
[`AppHandle::should_shutdown`]: https://kurogane-rs.org/docs/api/app/apphandle.md#method.should_shutdown
[`BrowserHandle`]: https://kurogane-rs.org/docs/api/app/browserhandle.md
[`AppHandle::shutdown`]: https://kurogane-rs.org/docs/api/app/apphandle.md#method.shutdown

### `create_child_browser`

```rust
pub fn create_child_browser(
    &self,
    parent: &impl HasWindowHandle,
    bounds: BrowserBounds,
    url: &str,
) -> Result<BrowserHandle, RuntimeError>
```

Creates a Chromium browser hosted inside an existing native window.

The browser is a native child window of `parent`, placed at `bounds`
in the parent's coordinates (see [`BrowserBounds`]).
[`BrowserHandle::set_bounds`] moves it later.

`parent` is the host's window as it is, anything that hands out a
[`raw_window_handle`] window handle (a winit `Window` for one). It must
outlive the browser, which the host closes first (docs/winit.md).

The runtime must have been started with App::start_embedded,
and AppInstance::pump must continue to be called regularly for
Chromium to process events.

#### Errors

[`RuntimeError::UnsupportedParentWindow`] when `parent` is not a
Win32 window, an AppKit view or an X11 window (a Wayland surface, for
one); [`RuntimeError::ShuttingDown`] once [`AppHandle::shutdown`] has
begun; [`RuntimeError::BrowserCreationFailed`] when CEF creates no
browser.

[`raw_window_handle`]: https://docs.rs/raw_window_handle/latest/raw_window_handle/index.html
[`BrowserHandle::set_bounds`]: https://kurogane-rs.org/docs/api/app/browserhandle.md#method.set_bounds
[`BrowserBounds`]: https://kurogane-rs.org/docs/api/app/browserbounds.md
[`RuntimeError::UnsupportedParentWindow`]: https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.UnsupportedParentWindow
[`RuntimeError::ShuttingDown`]: https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.ShuttingDown
[`AppHandle::shutdown`]: https://kurogane-rs.org/docs/api/app/apphandle.md#method.shutdown
[`RuntimeError::BrowserCreationFailed`]: https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.BrowserCreationFailed

### `create_child_browser_with_request_context`

```rust
pub fn create_child_browser_with_request_context(
    &self,
    parent: &impl HasWindowHandle,
    bounds: BrowserBounds,
    url: &str,
    rc_settings: &RequestContextSettings,
) -> Result<BrowserHandle, RuntimeError>
```

Creates a child browser with a custom request context (separate cookie/cache partition).

Same as create_child_browser but accepts RequestContextSettings to control
the cache partition, cookie persistence and accept language for this browser.

The runtime must have been started with App::start_embedded.

#### Errors

As [`create_child_browser`](https://kurogane-rs.org/docs/api/app/appinstance.md#method.create_child_browser); also
[`RuntimeError::BrowserCreationFailed`] when CEF creates no request
context from `rc_settings`.

[Self::create_child_browser]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.create_child_browser
[`RuntimeError::BrowserCreationFailed`]: https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.BrowserCreationFailed

## Trait implementations

- `impl Drop for AppInstance`

Source: https://github.com/0x48piraj/kurogane/blob/584c586118ee53cc04604007ee038557bd859092/kurogane/src/runtime.rs#L1127
