baoshiwei
6 天以前 5bf14aed888cd0e258e325c65f14022dad02985b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Import functionalities we'll be using
use std::sync::Mutex;
use tauri::{AppHandle, Manager, State};
use tokio::time::{sleep, Duration};
 
 
 
// Create a struct we'll use to track the completion of
// setup related tasks
pub struct SetupState {
    pub(crate) frontend_task: bool,
    pub(crate) backend_task: bool,
}
 
// // A custom task for setting the state of a setup task
// #[tauri::command]
// pub async fn set_complete(
//     app: AppHandle,
//     state: State<'_, Mutex<SetupState>>,
//     task: String,
// ) -> Result<(), ()> {
//     // 输出task
//     println!("task: {}", task);
//     // Lock the state without write access
//     let mut state_lock = state.lock().unwrap();
//     match task.as_str() {
//         "frontend" => state_lock.frontend_task = true,
//         "backend" => state_lock.backend_task = true,
//         _ => panic!("invalid task completed!"),
//     }
//     // Check if both tasks are completed
//     if state_lock.backend_task && state_lock.frontend_task {
//         // Setup is complete, we can close the splashscreen
//         // and unhide the main window!
//         let splash_window = app.get_webview_window("splashscreen").unwrap();
//         let main_window = app.get_webview_window("main").unwrap();
//         splash_window.close().unwrap();
//         main_window.show().unwrap();
//     }
//     Ok(())
// }
 
// A custom task for setting the state of a setup task
#[tauri::command]
pub async fn set_complete(
    app: AppHandle,
    state: State<'_, Mutex<SetupState>>,
    task: String,
) -> Result<(), ()> {
    // Lock the state without write access
    let mut state_lock = state.lock().unwrap();
    match task.as_str() {
        "frontend" => state_lock.frontend_task = true,
        "backend" => state_lock.backend_task = true,
        _ => panic!("invalid task completed!"),
    }
    // Check if both tasks are completed
    if state_lock.backend_task && state_lock.frontend_task {
        // Setup is complete, we can close the splashscreen
        // and unhide the main window!
        let splash_window = app.get_webview_window("splashscreen").unwrap();
        let main_window = app.get_webview_window("main").unwrap();
        splash_window.close().unwrap();
        main_window.show().unwrap();
    }
    Ok(())
}
 
 
// An async function that does some heavy setup task
pub async fn setup(app: AppHandle) -> Result<(), ()> {
    // Fake performing some heavy action for 5 seconds
    // 增加延迟时间,确保欢迎页面有足够时间显示
    println!("Performing really heavy backend setup task...");
    sleep(Duration::from_secs(3)).await;
    println!("Backend setup task completed!");
    // Set the backend task as being completed
    // Commands can be ran as regular functions as long as you take
    // care of the input arguments yourself
    set_complete(
        app.clone(),
        app.state::<Mutex<SetupState>>(),
        "backend".to_string(),
    )
    .await?;
    Ok(())
}