Simplify Iris app initialization and task updates
This commit is contained in:
1 parent
b5666cdef9
commit
166bac2a93
25 files changed
+330
-282
No files matched your search
@@ -25,31 +25,48 @@ application-id = "com.example.myapp"
|
||||
label = "My app"
|
||||
```
|
||||
|
||||
`#[iris::app_init]` marks the factory called when Android creates the Iris
|
||||
`#[iris::app_init]` marks the initializer called when Android creates the Iris
|
||||
view. The attribute supplies its own Android target gate and generates the JNI
|
||||
loader glue. The returned state chooses its resources through
|
||||
`AndroidAppState::Resources`; `StdRsc` is the standard bundle, not a
|
||||
requirement.
|
||||
loader glue. An application with no state beyond the UI state receives
|
||||
`AndroidUiState` directly by mutable reference:
|
||||
|
||||
```rust
|
||||
use iris::prelude::*;
|
||||
|
||||
#[derive(AndroidUiState)]
|
||||
struct State {
|
||||
ui_state: AndroidUiState,
|
||||
}
|
||||
|
||||
impl AndroidAppState for State {
|
||||
type Resources = StdRsc<Self>;
|
||||
}
|
||||
|
||||
#[iris::app_init]
|
||||
fn create(mut ui_state: AndroidUiState, rsc: &mut StdRsc<State>) -> State {
|
||||
rect(PaintId::RED).set_root(rsc, &mut ui_state);
|
||||
State { ui_state }
|
||||
fn create(
|
||||
ui_state: &mut AndroidUiState,
|
||||
rsc: &mut StdRsc<AndroidUiState>,
|
||||
) {
|
||||
rect(PaintId::RED).set_root(rsc, ui_state);
|
||||
}
|
||||
```
|
||||
|
||||
Desktop has the corresponding initializer form:
|
||||
|
||||
```rust
|
||||
DesktopApp::<DesktopUiState>::run_with(|ui_state, rsc| {
|
||||
rect(PaintId::RED).set_root(rsc, ui_state);
|
||||
});
|
||||
```
|
||||
|
||||
Background work updates either host through the same task context. An update
|
||||
wakes the UI thread; Iris schedules a frame automatically if the closure made
|
||||
the retained widget tree dirty:
|
||||
|
||||
```rust
|
||||
rsc.spawn_task(async move |mut ctx| {
|
||||
let text = load_text().await;
|
||||
ctx.update(move |_, rsc| label.edit(rsc).set(&text));
|
||||
});
|
||||
```
|
||||
|
||||
Applications do not need a winit event proxy or an explicit redraw request.
|
||||
|
||||
Applications with additional fields use their own state type. Its
|
||||
`AndroidAppState::Resources` associated type can also replace `StdRsc` with a
|
||||
custom resource bundle.
|
||||
|
||||
Install the Cargo subcommand from a checkout, then invoke it from the
|
||||
application's directory:
|
||||
|
||||
|
||||
Reference in new issue
Block a user