Splashscreen
In this lab we’ll be implementing a basic splashscreen functionality in a Tauri app. Doing so is quite straight forward, a splashscreen is effectively just a matter of creating a new window that displays some contents during the period your app is doing some heavy setup related tasks and then closing it when setting up is done.
Prerequisites
Steps
-
Before you start developing any project it’s important to build and run the initial template, just to validate your setup is working as intended.
-
The easiest way of adding new windows is by adding them directly to
tauri.conf.json
. You can also create them dynamically at startup, but for the sake of simplicity lets just register them instead. Make sure you have a window with the labelmain
that’s being created as a hidden window and a window with the labelsplashscreen
that’s created as being shown directly. You can leave all other options as their defaults, or tweak them based on preference. -
Before you begin you’ll need to have some content to show. How you develop new pages depend on your chosen framework, most have the concept of a “router” that handles page navigation which should work just like normal in Tauri, in which case you just create a new splashscreen page. Or as we’re going to be doing here, create a new
splashscreen.html
file to host the contents.What’s important here is that you can navigate to a
/splashscreen
URL and be shown the contents you want for your splashscreen. Try running the app again after this step! -
Since splashscreens are generally intended to be used for the sake of hiding heavy setup related tasks, lets fake giving the app something heavy to do, some in the frontend and some in the backend.
To fake heavy setup in the frontend we’re going to be using a simple
setTimeout
function.The easiest way to fake heavy operations in the backend is by using the Tokio crate, which is the Rust crate that Tauri uses in the backend to provide an asynchronous runtime. While Tauri provides the runtime there are various utilities that Tauri doesn’t re-export from it, so we’ll need to add the crate to our project in order to access them. This is a perfectly normal practice within the Rust ecosystem.
Don’t use
std::thread::sleep
in async functions, they run cooperatively in a concurrent environment not in parallel, meaning that if you sleep the thread instead of the Tokio task you’ll be locking all tasks scheduled to run on that thread from being executed, causing your app to freeze. -
Run the application
You should now see a splashscreen window pop up, both the frontend and backend will perform their respective heavy 3 second setup tasks, after which the splashscreen disappears and the main window is shown!
Discuss
Should you have a splashscreen?
In general having a splashscreen is an admittance of defeat that you couldn’t make your app load fast enough to not need one. In fact it tends to be better to just go straight to a main window that then shows some little spinner somewhere in a corner informing the user there’s still setup tasks happening in the background.
However, with that said, it can be a stylistic choice that you want to have a splashscreen, or you might have some very particular requirement that makes it impossible to start the app until some tasks are performed. It’s definitely not wrong to have a splashscreen, it just tends to not be necessary and can make users feel like the app isn’t very well optimized.
© 2024 Tauri Contributors. CC-BY / MIT