process.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019-2022 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #![allow(unused_imports)]
  5. use super::InvokeContext;
  6. #[cfg(process_relaunch)]
  7. use crate::Manager;
  8. use crate::Runtime;
  9. use serde::Deserialize;
  10. use tauri_macros::{command_enum, module_command_handler, CommandModule};
  11. /// The API descriptor.
  12. #[command_enum]
  13. #[derive(Deserialize, CommandModule)]
  14. #[serde(tag = "cmd", rename_all = "camelCase")]
  15. pub enum Cmd {
  16. /// Relaunch application
  17. Relaunch,
  18. /// Close application with provided exit_code
  19. #[cmd(process_exit, "process > exit")]
  20. #[serde(rename_all = "camelCase")]
  21. Exit { exit_code: i32 },
  22. }
  23. impl Cmd {
  24. #[module_command_handler(process_relaunch)]
  25. fn relaunch<R: Runtime>(context: InvokeContext<R>) -> super::Result<()> {
  26. context.window.app_handle().restart();
  27. Ok(())
  28. }
  29. #[cfg(not(process_relaunch))]
  30. fn relaunch<R: Runtime>(_: InvokeContext<R>) -> super::Result<()> {
  31. Err(crate::Error::ApiNotAllowlisted("process > relaunch".into()).into_anyhow())
  32. }
  33. #[module_command_handler(process_exit)]
  34. fn exit<R: Runtime>(context: InvokeContext<R>, exit_code: i32) -> super::Result<()> {
  35. // would be great if we can have a handler inside tauri
  36. // who close all window and emit an event that user can catch
  37. // if they want to process something before closing the app
  38. context.window.app_handle.exit(exit_code);
  39. Ok(())
  40. }
  41. }
  42. #[cfg(test)]
  43. mod tests {
  44. #[tauri_macros::module_command_test(process_relaunch, "process > relaunch", runtime)]
  45. #[quickcheck_macros::quickcheck]
  46. fn relaunch() {}
  47. #[tauri_macros::module_command_test(process_exit, "process > exit")]
  48. #[quickcheck_macros::quickcheck]
  49. fn exit(_exit_code: i32) {}
  50. }