lib.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //! Tauri is a framework for building tiny, blazing fast binaries for all major desktop platforms.
  2. //! Developers can integrate any front-end framework that compiles to HTML, JS and CSS for building their user interface.
  3. //! The backend of the application is a rust-sourced binary with an API that the front-end can interact with.
  4. //!
  5. //! The user interface in Tauri apps currently leverages Cocoa/WebKit on macOS, gtk-webkit2 on Linux and MSHTML (IE10/11) or Webkit via Edge on Windows.
  6. //! Tauri uses (and contributes to) the MIT licensed project that you can find at [webview](https://github.com/webview/webview).
  7. #![warn(missing_docs, rust_2018_idioms)]
  8. /// The embedded server helpers.
  9. #[cfg(embedded_server)]
  10. pub mod server;
  11. /// The Tauri-specific settings for your app e.g. notification permission status.
  12. pub mod settings;
  13. /// The webview application entry.
  14. mod app;
  15. /// The Tauri API endpoints.
  16. mod endpoints;
  17. mod error;
  18. /// The plugin manager module contains helpers to manage runtime plugins.
  19. pub mod plugin;
  20. /// The salt helpers.
  21. mod salt;
  22. /// Webview interface.
  23. mod webview;
  24. /// The Tauri error enum.
  25. pub use error::Error;
  26. /// Tauri result type.
  27. pub type Result<T> = std::result::Result<T, Error>;
  28. pub(crate) mod async_runtime;
  29. /// A task to run on the main thread.
  30. pub type SyncTask = Box<dyn FnOnce() + Send>;
  31. pub use app::*;
  32. pub use tauri_api as api;
  33. pub use tauri_macros::FromTauriContext;
  34. pub use webview::{
  35. ApplicationDispatcherExt, ApplicationExt, Callback, WebviewBuilderExt, WindowBuilderExt,
  36. };
  37. /// The Tauri webview implementations.
  38. pub mod flavors {
  39. pub use super::webview::wry::WryApplication as Wry;
  40. }
  41. use std::process::Stdio;
  42. use api::rpc::{format_callback, format_callback_result};
  43. use serde::Serialize;
  44. /// Synchronously executes the given task
  45. /// and evaluates its Result to the JS promise described by the `callback` and `error` function names.
  46. pub fn execute_promise_sync<
  47. D: ApplicationDispatcherExt + 'static,
  48. R: Serialize,
  49. F: FnOnce() -> Result<R> + Send + 'static,
  50. >(
  51. webview_manager: &crate::WebviewManager<D>,
  52. task: F,
  53. callback: String,
  54. error: String,
  55. ) {
  56. let webview_manager_ = webview_manager.clone();
  57. if let Ok(dispatcher) = webview_manager.current_webview() {
  58. dispatcher.send_event(webview::Event::Run(Box::new(move || {
  59. let callback_string =
  60. match format_callback_result(task().map_err(|err| err.to_string()), &callback, &error) {
  61. Ok(js) => js,
  62. Err(e) => format_callback_result(
  63. std::result::Result::<(), String>::Err(e.to_string()),
  64. &callback,
  65. &error,
  66. )
  67. .unwrap(),
  68. };
  69. if let Ok(dispatcher) = webview_manager_.current_webview() {
  70. dispatcher.eval(callback_string.as_str());
  71. }
  72. })));
  73. }
  74. }
  75. /// Asynchronously executes the given task
  76. /// and evaluates its Result to the JS promise described by the `success_callback` and `error_callback` function names.
  77. ///
  78. /// If the Result `is_ok()`, the callback will be the `success_callback` function name and the argument will be the Ok value.
  79. /// If the Result `is_err()`, the callback will be the `error_callback` function name and the argument will be the Err value.
  80. pub async fn execute_promise<
  81. D: ApplicationDispatcherExt,
  82. R: Serialize,
  83. F: futures::Future<Output = Result<R>> + Send + 'static,
  84. >(
  85. webview_manager: &crate::WebviewManager<D>,
  86. task: F,
  87. success_callback: String,
  88. error_callback: String,
  89. ) {
  90. let callback_string = match format_callback_result(
  91. task.await.map_err(|err| err.to_string()),
  92. success_callback,
  93. error_callback.clone(),
  94. ) {
  95. Ok(callback_string) => callback_string,
  96. Err(e) => format_callback(error_callback, e.to_string()),
  97. };
  98. if let Ok(dispatcher) = webview_manager.current_webview() {
  99. dispatcher.eval(callback_string.as_str());
  100. }
  101. }
  102. /// Calls the given command and evaluates its output to the JS promise described by the `callback` and `error` function names.
  103. pub async fn call<D: ApplicationDispatcherExt>(
  104. webview_manager: &crate::WebviewManager<D>,
  105. command: String,
  106. args: Vec<String>,
  107. callback: String,
  108. error: String,
  109. ) {
  110. execute_promise(
  111. webview_manager,
  112. async move { api::command::get_output(command, args, Stdio::piped()).map_err(|e| e.into()) },
  113. callback,
  114. error,
  115. )
  116. .await;
  117. }
  118. #[cfg(test)]
  119. mod test {
  120. use proptest::prelude::*;
  121. proptest! {
  122. #![proptest_config(ProptestConfig::with_cases(10000))]
  123. #[test]
  124. // check to see if spawn executes a function.
  125. fn check_spawn_task(task in "[a-z]+") {
  126. // create dummy task function
  127. let dummy_task = async move {
  128. format!("{}-run-dummy-task", task);
  129. };
  130. // call spawn
  131. crate::async_runtime::spawn(dummy_task);
  132. }
  133. }
  134. }