main.rs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
  5. // Application code for a splashscreen system that waits on a Rust initialization script
  6. mod rust {
  7. use std::{thread::sleep, time::Duration};
  8. use tauri::Manager;
  9. // this command is here just so the example doesn't throw an error
  10. #[tauri::command]
  11. fn close_splashscreen() {}
  12. pub fn main() {
  13. tauri::Builder::default()
  14. .setup(|app| {
  15. let splashscreen_window = app.get_window("splashscreen").unwrap();
  16. let main_window = app.get_window("main").unwrap();
  17. // we perform the initialization code on a new task so the app doesn't crash
  18. tauri::async_runtime::spawn(async move {
  19. println!("Initializing...");
  20. sleep(Duration::from_secs(2));
  21. println!("Done initializing.");
  22. // After it's done, close the splashscreen and display the main window
  23. splashscreen_window.close().unwrap();
  24. main_window.show().unwrap();
  25. });
  26. Ok(())
  27. })
  28. .invoke_handler(tauri::generate_handler![close_splashscreen])
  29. .run(super::context())
  30. .expect("failed to run app");
  31. }
  32. }
  33. // Application code for a splashscreen system that waits for the UI
  34. mod ui {
  35. use std::sync::{Arc, Mutex};
  36. use tauri::{Manager, State, Window};
  37. // wrappers around each Window
  38. // we use a dedicated type because Tauri can only manage a single instance of a given type
  39. struct SplashscreenWindow(Arc<Mutex<Window>>);
  40. struct MainWindow(Arc<Mutex<Window>>);
  41. #[tauri::command]
  42. fn close_splashscreen(
  43. _: Window, // force inference of P
  44. splashscreen: State<SplashscreenWindow>,
  45. main: State<MainWindow>,
  46. ) {
  47. // Close splashscreen
  48. splashscreen.0.lock().unwrap().close().unwrap();
  49. // Show main window
  50. main.0.lock().unwrap().show().unwrap();
  51. }
  52. pub fn main() {
  53. let context = super::context();
  54. tauri::Builder::default()
  55. .menu(if cfg!(target_os = "macos") {
  56. tauri::Menu::os_default(&context.package_info().name)
  57. } else {
  58. tauri::Menu::default()
  59. })
  60. .setup(|app| {
  61. // set the splashscreen and main windows to be globally available with the tauri state API
  62. app.manage(SplashscreenWindow(Arc::new(Mutex::new(
  63. app.get_window("splashscreen").unwrap(),
  64. ))));
  65. app.manage(MainWindow(Arc::new(Mutex::new(
  66. app.get_window("main").unwrap(),
  67. ))));
  68. Ok(())
  69. })
  70. .invoke_handler(tauri::generate_handler![close_splashscreen])
  71. .run(context)
  72. .expect("error while running tauri application");
  73. }
  74. }
  75. fn context() -> tauri::Context<tauri::utils::assets::EmbeddedAssets> {
  76. tauri::generate_context!("../../examples/splashscreen/tauri.conf.json")
  77. }
  78. fn main() {
  79. // toggle this flag to experiment with different splashscreen usages
  80. let ui = false;
  81. if ui {
  82. ui::main();
  83. } else {
  84. rust::main();
  85. }
  86. }