main.rs 3.0 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. let context = tauri::generate_context!("../../examples/splashscreen/tauri.conf.json");
  17. tauri::Builder::default()
  18. .menu(tauri::Menu::os_default(&context.package_info().name))
  19. .setup(|app| {
  20. let splashscreen_window = app.get_window("splashscreen").unwrap();
  21. let main_window = app.get_window("main").unwrap();
  22. // we perform the initialization code on a new task so the app doesn't crash
  23. tauri::async_runtime::spawn(async move {
  24. println!("Initializing...");
  25. sleep(Duration::from_secs(2));
  26. println!("Done initializing.");
  27. // After it's done, close the splashscreen and display the main window
  28. splashscreen_window.close().unwrap();
  29. main_window.show().unwrap();
  30. });
  31. Ok(())
  32. })
  33. .invoke_handler(tauri::generate_handler![close_splashscreen])
  34. .run(context)
  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. let context = tauri::generate_context!("../../examples/splashscreen/tauri.conf.json");
  59. tauri::Builder::default()
  60. .menu(tauri::Menu::os_default(&context.package_info().name))
  61. .setup(|app| {
  62. // set the splashscreen and main windows to be globally available with the tauri state API
  63. app.manage(SplashscreenWindow(Arc::new(Mutex::new(
  64. app.get_window("splashscreen").unwrap(),
  65. ))));
  66. app.manage(MainWindow(Arc::new(Mutex::new(
  67. app.get_window("main").unwrap(),
  68. ))));
  69. Ok(())
  70. })
  71. .invoke_handler(tauri::generate_handler![close_splashscreen])
  72. .run(context)
  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. }