2021-05-03 17:16:51 +03:00
|
|
|
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
#![cfg_attr(
|
|
|
|
all(not(debug_assertions), target_os = "windows"),
|
|
|
|
windows_subsystem = "windows"
|
|
|
|
)]
|
|
|
|
|
|
|
|
// Application code for a splashscreen system that waits on a Rust initialization script
|
|
|
|
mod rust {
|
|
|
|
use std::{thread::sleep, time::Duration};
|
|
|
|
use tauri::Manager;
|
|
|
|
|
|
|
|
// this command is here just so the example doesn't throw an error
|
|
|
|
#[tauri::command]
|
|
|
|
fn close_splashscreen() {}
|
|
|
|
|
|
|
|
pub fn main() {
|
2022-06-15 16:51:40 +03:00
|
|
|
let context = tauri::generate_context!("../../examples/splashscreen/tauri.conf.json");
|
2021-05-03 17:16:51 +03:00
|
|
|
tauri::Builder::default()
|
2022-06-15 16:51:40 +03:00
|
|
|
.menu(tauri::Menu::os_default(&context.package_info().name))
|
2021-05-03 17:16:51 +03:00
|
|
|
.setup(|app| {
|
|
|
|
let splashscreen_window = app.get_window("splashscreen").unwrap();
|
|
|
|
let main_window = app.get_window("main").unwrap();
|
2021-09-26 07:50:27 +03:00
|
|
|
// we perform the initialization code on a new task so the app doesn't crash
|
2021-05-03 17:16:51 +03:00
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
println!("Initializing...");
|
|
|
|
sleep(Duration::from_secs(2));
|
|
|
|
println!("Done initializing.");
|
|
|
|
|
|
|
|
// After it's done, close the splashscreen and display the main window
|
|
|
|
splashscreen_window.close().unwrap();
|
|
|
|
main_window.show().unwrap();
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.invoke_handler(tauri::generate_handler![close_splashscreen])
|
2022-06-15 16:51:40 +03:00
|
|
|
.run(context)
|
2021-05-03 17:16:51 +03:00
|
|
|
.expect("failed to run app");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Application code for a splashscreen system that waits for the UI
|
|
|
|
mod ui {
|
|
|
|
use std::sync::{Arc, Mutex};
|
2021-07-15 13:05:29 +03:00
|
|
|
use tauri::{Manager, State, Window};
|
2021-05-03 17:16:51 +03:00
|
|
|
|
|
|
|
// wrappers around each Window
|
|
|
|
// we use a dedicated type because Tauri can only manage a single instance of a given type
|
2021-07-15 13:05:29 +03:00
|
|
|
struct SplashscreenWindow(Arc<Mutex<Window>>);
|
|
|
|
struct MainWindow(Arc<Mutex<Window>>);
|
2021-05-03 17:16:51 +03:00
|
|
|
|
|
|
|
#[tauri::command]
|
2021-07-15 13:05:29 +03:00
|
|
|
fn close_splashscreen(
|
|
|
|
_: Window, // force inference of P
|
|
|
|
splashscreen: State<SplashscreenWindow>,
|
|
|
|
main: State<MainWindow>,
|
2021-05-03 17:16:51 +03:00
|
|
|
) {
|
|
|
|
// Close splashscreen
|
|
|
|
splashscreen.0.lock().unwrap().close().unwrap();
|
|
|
|
// Show main window
|
|
|
|
main.0.lock().unwrap().show().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2022-06-15 16:51:40 +03:00
|
|
|
let context = tauri::generate_context!("../../examples/splashscreen/tauri.conf.json");
|
2021-05-03 17:16:51 +03:00
|
|
|
tauri::Builder::default()
|
2022-06-15 16:51:40 +03:00
|
|
|
.menu(tauri::Menu::os_default(&context.package_info().name))
|
2021-05-03 17:16:51 +03:00
|
|
|
.setup(|app| {
|
|
|
|
// set the splashscreen and main windows to be globally available with the tauri state API
|
|
|
|
app.manage(SplashscreenWindow(Arc::new(Mutex::new(
|
|
|
|
app.get_window("splashscreen").unwrap(),
|
|
|
|
))));
|
|
|
|
app.manage(MainWindow(Arc::new(Mutex::new(
|
|
|
|
app.get_window("main").unwrap(),
|
|
|
|
))));
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.invoke_handler(tauri::generate_handler![close_splashscreen])
|
2022-06-15 16:51:40 +03:00
|
|
|
.run(context)
|
2021-05-03 17:16:51 +03:00
|
|
|
.expect("error while running tauri application");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2022-03-24 02:45:43 +03:00
|
|
|
// toggle this flag to experiment with different splashscreen usages
|
|
|
|
let ui = false;
|
|
|
|
if ui {
|
|
|
|
ui::main();
|
|
|
|
} else {
|
|
|
|
rust::main();
|
|
|
|
}
|
2021-05-03 17:16:51 +03:00
|
|
|
}
|