tauri/examples/splashscreen/main.rs
Lucas Fernandes Nogueira c77b40324e
refactor(core): add support to multiple webviews on a Tauri window (#8280)
* feat: update to latest wry

* wry dev branch [skip ci]

* fix linux [skip ci]

* refactor(runtime): split webview and window types

* split dispatch

* implement create_webview

* move webview message

* wip webview mod

* create webview manager, finish webview struct and builder

* fix tests and docs

* rename WindowUrl to WebviewUrl

* update examples

* event refactor

* update JS API

* fix events

* update example

* add WebviewWindow class on JS

* fix macos build

* allow creating window+webview on the same runtime call

* rename tauri://window-created to tauri://webview-created

* Window::add_child

* use inner_size from webview on macOS

* add multiwebview example

* automatically resize webviews on window resize

* fix tests

* set_position, set_size

* position, size getters

* set_focus

* add close fn

* update mock runtime

* lint [skip ci]

* fix inner_size getter [skip ci]

* import hwnd [skip ci]

* update webview bound ratios on set_size/set_position

* add auto_resize option

* fix android

* fix build on windows

* typo

* with_webview isnt desktop only

* add WebviewWindow rust struct (and builder)

* fix build on android

* license header

* fix macos/windows

* fix macos build

* resolve todo

* handle window not found

* hide unstable features

* document unstable feature [skip ci]

* webview plugin permissions

* hide more stuff

* fix doctests

* typos

* add change files

* fix examples

* rename hook
2024-01-24 11:05:18 -03:00

94 lines
2.9 KiB
Rust

// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
#![cfg_attr(not(debug_assertions), 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() {
tauri::Builder::default()
.setup(|app| {
let splashscreen_window = app.get_webview_window("splashscreen").unwrap();
let main_window = app.get_webview_window("main").unwrap();
// we perform the initialization code on a new task so the app doesn't crash
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])
.run(super::context())
.expect("failed to run app");
}
}
// Application code for a splashscreen system that waits for the UI
mod ui {
use std::sync::{Arc, Mutex};
use tauri::{Manager, State, WebviewWindow};
// wrappers around each Window
// we use a dedicated type because Tauri can only manage a single instance of a given type
struct SplashscreenWindow(Arc<Mutex<WebviewWindow>>);
struct MainWindow(Arc<Mutex<WebviewWindow>>);
#[tauri::command]
fn close_splashscreen(
_: WebviewWindow, // force inference of P
splashscreen: State<SplashscreenWindow>,
main: State<MainWindow>,
) {
// Close splashscreen
splashscreen.0.lock().unwrap().close().unwrap();
// Show main window
main.0.lock().unwrap().show().unwrap();
}
pub fn main() {
let context = super::context();
tauri::Builder::default()
.menu(tauri::menu::Menu::default)
.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_webview_window("splashscreen").unwrap(),
))));
app.manage(MainWindow(Arc::new(Mutex::new(
app.get_webview_window("main").unwrap(),
))));
Ok(())
})
.invoke_handler(tauri::generate_handler![close_splashscreen])
.run(context)
.expect("error while running tauri application");
}
}
fn context() -> tauri::Context<tauri::utils::assets::EmbeddedAssets> {
tauri::generate_context!("../../examples/splashscreen/tauri.conf.json")
}
fn main() {
// toggle this flag to experiment with different splashscreen usages
let ui = false;
if ui {
ui::main();
} else {
rust::main();
}
}