main.rs 3.0 KB

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