lib.rs 1.8 KB

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