feat: 限制程序只能打开一个窗口
This commit is contained in:
Generated
+17
@@ -723,6 +723,7 @@ dependencies = [
|
||||
"tauri-build",
|
||||
"tauri-plugin-dialog",
|
||||
"tauri-plugin-opener",
|
||||
"tauri-plugin-single-instance",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
@@ -3576,6 +3577,22 @@ dependencies = [
|
||||
"zbus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-plugin-single-instance"
|
||||
version = "2.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "561cd99484561bc39c954768e7b41a66cebbe1ac3d9d081c34361f740c489e2c"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tauri",
|
||||
"thiserror 2.0.21",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"windows-sys 0.61.2",
|
||||
"zbus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-runtime"
|
||||
version = "2.12.2"
|
||||
|
||||
@@ -28,15 +28,16 @@ tauri-plugin-opener = "2"
|
||||
tauri-plugin-dialog = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
tauri-plugin-single-instance = "2"
|
||||
windows-sys = { version = "0.61", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_Security",
|
||||
"Win32_System_Diagnostics_ToolHelp",
|
||||
"Win32_System_JobObjects",
|
||||
"Win32_System_Threading",
|
||||
"Win32_UI_WindowsAndMessaging",
|
||||
] }
|
||||
|
||||
|
||||
# Read the optimization guideline for more details: https://tauri.app/concept/size/#cargo-configuration
|
||||
[profile.release]
|
||||
codegen-units = 1
|
||||
|
||||
@@ -189,9 +189,36 @@ fn stop_backend() {
|
||||
}
|
||||
}
|
||||
|
||||
fn focus_existing(app: &tauri::AppHandle) {
|
||||
let Some(window) = app.get_webview_window("main") else {
|
||||
return;
|
||||
};
|
||||
let _ = window.show();
|
||||
let _ = window.unminimize();
|
||||
#[cfg(windows)]
|
||||
if let Ok(hwnd) = window.hwnd() {
|
||||
unsafe {
|
||||
use windows_sys::Win32::UI::WindowsAndMessaging::{
|
||||
SetForegroundWindow, ShowWindow, SW_RESTORE,
|
||||
};
|
||||
let raw = hwnd.0 as windows_sys::Win32::Foundation::HWND;
|
||||
ShowWindow(raw, SW_RESTORE);
|
||||
SetForegroundWindow(raw);
|
||||
}
|
||||
}
|
||||
let _ = window.set_focus();
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
let mut builder = tauri::Builder::default();
|
||||
#[cfg(desktop)]
|
||||
{
|
||||
builder = builder.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
|
||||
focus_existing(app);
|
||||
}));
|
||||
}
|
||||
builder
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.invoke_handler(tauri::generate_handler![backend_port, write_sample_csv])
|
||||
|
||||
Reference in New Issue
Block a user