File size: 1,881 Bytes
2d8be8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use serde::Deserialize;
use std::path::PathBuf;
use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
pub use models::*;
#[cfg(desktop)]
mod desktop;
#[cfg(mobile)]
mod mobile;
mod error;
mod models;
#[cfg(desktop)]
use desktop::Sample;
#[cfg(mobile)]
use mobile::Sample;
pub use error::*;
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the sample APIs.
pub trait SampleExt<R: Runtime> {
fn sample(&self) -> &Sample<R>;
}
impl<R: Runtime, T: Manager<R>> crate::SampleExt<R> for T {
fn sample(&self) -> &Sample<R> {
self.state::<Sample<R>>().inner()
}
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct PingScope {
path: PathBuf,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct SampleScope {
path: PathBuf,
}
#[tauri::command]
fn ping<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
value: Option<String>,
scope: tauri::ipc::CommandScope<PingScope>,
global_scope: tauri::ipc::GlobalScope<SampleScope>,
) -> std::result::Result<PingResponse, String> {
println!("local scope {scope:?}");
println!("global scope {global_scope:?}");
app
.sample()
.ping(PingRequest {
value,
on_event: tauri::ipc::Channel::new(|_| Ok(())),
})
.map_err(|e| e.to_string())
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("sample")
.setup(|app, api| {
#[cfg(mobile)]
let sample = mobile::init(app, api)?;
#[cfg(desktop)]
let sample = desktop::init(app, api)?;
app.manage(sample);
Ok(())
})
.invoke_handler(tauri::generate_handler![ping])
.on_navigation(|window, url| {
println!("navigation {} {url}", window.label());
true
})
.build()
}
|