main.rs 3.2 KB

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