Skip to content
Tauri 中文网

启动画面

在本实验中,我们将在 Tauri 应用中实现基本的启动画面功能。这样做非常简单,启动画面实际上只是在你的应用执行一些繁重的设置相关任务期间创建一个显示一些内容的新窗口,然后在设置完成后关闭它。

¥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

  1. 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.

  2. 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 label main that’s being created as a hidden window and a window with the label splashscreen that’s created as being shown directly. You can leave all other options as their defaults, or tweak them based on preference.

  3. 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!

  4. 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.

  5. 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.


Tauri 中文网 - 粤ICP备13048890号