main.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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(tauri::menu::Menu::default)
  56. .setup(|app| {
  57. // set the splashscreen and main windows to be globally available with the tauri state API
  58. app.manage(SplashscreenWindow(Arc::new(Mutex::new(
  59. app.get_window("splashscreen").unwrap(),
  60. ))));
  61. app.manage(MainWindow(Arc::new(Mutex::new(
  62. app.get_window("main").unwrap(),
  63. ))));
  64. Ok(())
  65. })
  66. .invoke_handler(tauri::generate_handler![close_splashscreen])
  67. .run(context)
  68. .expect("error while running tauri application");
  69. }
  70. }
  71. fn context() -> tauri::Context<tauri::utils::assets::EmbeddedAssets> {
  72. tauri::generate_context!("../../examples/splashscreen/tauri.conf.json")
  73. }
  74. fn main() {
  75. // toggle this flag to experiment with different splashscreen usages
  76. let ui = false;
  77. if ui {
  78. ui::main();
  79. } else {
  80. rust::main();
  81. }
  82. }