main.rs 3.0 KB

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