Skip to content

Commit 39e5294

Browse files
Use generic runtime in command examples for testability (#3255)
Co-authored-by: Tony <[email protected]>
1 parent f2fa5cb commit 39e5294

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/content/docs/develop/calling-rust.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,29 @@ async fn my_custom_command(app_handle: tauri::AppHandle) {
457457
}
458458
```
459459

460+
:::tip
461+
462+
`AppHandle` and `WebviewWindow` both take a generic parameter `R: Runtime`,
463+
when the `wry` feature is enabled in `tauri` (which is enabled by default),
464+
we default the generic to the `Wry` runtime so you can use it directly,
465+
but if you want to use a different runtime, for example the [mock runtime],
466+
you need to write your functions like this
467+
468+
```rust title="src-tauri/src/lib.rs" ins="<R: Runtime>" ins="<R>"
469+
use tauri::{AppHandle, GlobalShortcutManager, Runtime, WebviewWindow};
470+
471+
#[tauri::command]
472+
async fn my_custom_command<R: Runtime>(app_handle: AppHandle<R>, webview_window: WebviewWindow<R>) {
473+
let app_dir = app_handle.path_resolver().app_dir();
474+
app_handle
475+
.global_shortcut_manager()
476+
.register("CTRL + U", move || {});
477+
println!("WebviewWindow: {}", webview_window.label());
478+
}
479+
```
480+
481+
:::
482+
460483
### Accessing Managed State
461484

462485
Tauri can manage state using the `manage` function on `tauri::Builder`.
@@ -700,3 +723,4 @@ To learn how to listen to events and emit events from your Rust code, see the [R
700723
[Rust Event System documentation]: /develop/calling-frontend/#event-system
701724
[channels documentation]: /develop/calling-frontend/#channels
702725
[Calling Rust from the Frontend]: /develop/calling-rust/
726+
[mock runtime]: https://docs.rs/tauri/2.0.0/tauri/test/struct.MockRuntime.html

0 commit comments

Comments
 (0)