我正在尝试删除停靠图标,关闭窗口,并让应用程序使用 Tauri 在后台运行。
我可以关闭窗口,但我不知道如何在窗口关闭后删除应用程序图标。
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello ->, {}!", name)
}
fn main() {
tauri::Builder::default()
.setup(|app| {
// #[cfg(target_os = "macos")]
// app.set_activation_policy(tauri::ActivationPolicy::Accessory); // this removes the dock icon by default
Ok(())
})
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(|_app_handle, event| match event {
// dock icon should be removed here, but I don't know how, share the app state?
tauri::RunEvent::ExitRequested { api, .. } => {
api.prevent_exit();
}
_ => {}
})
}
Tauri 似乎不支持在启动后设置激活策略,但您可以通过使用 crate 调用 AppKit API在运行时自行设置(请参阅此答案
cocoa
的评论和编辑) :在调用后立即插入此命令会
api.prevent_exit()
导致窗口关闭并且停靠图标消失,但程序继续运行。值得注意的是,Tauri 并不希望您这样做,因此它可能会导致其他意外行为 - 例如,稍后可能很难恢复窗口状态。