main.rs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019-2021 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(tauri::generate_context!(
  33. "../../examples/splashscreen/tauri.conf.json"
  34. ))
  35. .expect("failed to run app");
  36. }
  37. }
  38. // Application code for a splashscreen system that waits for the UI
  39. mod ui {
  40. use std::sync::{Arc, Mutex};
  41. use tauri::{Manager, State, Window};
  42. // wrappers around each Window
  43. // we use a dedicated type because Tauri can only manage a single instance of a given type
  44. struct SplashscreenWindow(Arc<Mutex<Window>>);
  45. struct MainWindow(Arc<Mutex<Window>>);
  46. #[tauri::command]
  47. fn close_splashscreen(
  48. _: Window, // force inference of P
  49. splashscreen: State<SplashscreenWindow>,
  50. main: State<MainWindow>,
  51. ) {
  52. // Close splashscreen
  53. splashscreen.0.lock().unwrap().close().unwrap();
  54. // Show main window
  55. main.0.lock().unwrap().show().unwrap();
  56. }
  57. pub fn main() {
  58. tauri::Builder::default()
  59. .setup(|app| {
  60. // set the splashscreen and main windows to be globally available with the tauri state API
  61. app.manage(SplashscreenWindow(Arc::new(Mutex::new(
  62. app.get_window("splashscreen").unwrap(),
  63. ))));
  64. app.manage(MainWindow(Arc::new(Mutex::new(
  65. app.get_window("main").unwrap(),
  66. ))));
  67. Ok(())
  68. })
  69. .invoke_handler(tauri::generate_handler![close_splashscreen])
  70. .run(tauri::generate_context!(
  71. "../../examples/splashscreen/tauri.conf.json"
  72. ))
  73. .expect("error while running tauri application");
  74. }
  75. }
  76. fn main() {
  77. // toggle this flag to experiment with different splashscreen usages
  78. let ui = false;
  79. if ui {
  80. ui::main();
  81. } else {
  82. rust::main();
  83. }
  84. }