feat: support for reopen app via desktop shortcuts (#5037)

* fix: singleton check

* docs: update UPDATELOG.md

---------

Co-authored-by: Slinetrac <realakayuki@gmail.com>
This commit is contained in:
oomeow
2025-10-12 22:55:40 +08:00
committed by GitHub
Unverified
parent 7789d0bd5c
commit b91087e175
3 changed files with 36 additions and 29 deletions

View File

@@ -26,6 +26,7 @@
- 更新了 Wayland 合成器检测逻辑,从而在 Hyprland 会话中保留原生 Wayland 后端
- 改进 Windows 和 Unix 的 服务连接方式以及权限,避免无法连接服务或内核
- 修改内核默认日志级别为 Info
- 支持通过桌面快捷方式重新打开应用
### 🐞 修复问题

View File

@@ -22,37 +22,23 @@ use tauri::{AppHandle, Manager};
#[cfg(target_os = "macos")]
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_deep_link::DeepLinkExt;
use tokio::time::{Duration, timeout};
use utils::logging::Type;
pub static APP_HANDLE: OnceCell<AppHandle> = OnceCell::new();
/// Application initialization helper functions
mod app_init {
use anyhow::Result;
use super::*;
/// Initialize singleton monitoring for other instances
pub fn init_singleton_check() {
AsyncHandler::spawn_blocking(move || async move {
pub fn init_singleton_check() -> Result<()> {
tauri::async_runtime::block_on(async move {
logging!(info, Type::Setup, "开始检查单例实例...");
match timeout(Duration::from_millis(500), server::check_singleton()).await {
Ok(result) => {
if result.is_err() {
logging!(info, Type::Setup, "检测到已有应用实例运行");
if let Some(app_handle) = APP_HANDLE.get() {
app_handle.exit(0);
} else {
std::process::exit(0);
}
} else {
logging!(info, Type::Setup, "未检测到其他应用实例");
}
}
Err(_) => {
logging!(warn, Type::Setup, "单例检查超时,假定没有其他实例运行");
}
}
});
server::check_singleton().await?;
Ok(())
})
}
/// Setup plugins for the Tauri builder
@@ -235,7 +221,9 @@ mod app_init {
pub fn run() {
// Setup singleton check
app_init::init_singleton_check();
if app_init::init_singleton_check().is_err() {
return;
}
let _ = utils::dirs::init_portable_flag();

View File

@@ -1,14 +1,18 @@
use std::time::Duration;
use super::resolve;
use crate::{
config::{Config, DEFAULT_PAC, IVerge},
logging_error,
logging, logging_error,
module::lightweight,
process::AsyncHandler,
utils::logging::Type,
utils::{logging::Type, window_manager::WindowManager},
};
use anyhow::{Result, bail};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use port_scanner::local_port_available;
use reqwest::ClientBuilder;
use tokio::sync::oneshot;
use warp::Filter;
@@ -24,20 +28,28 @@ static SHUTDOWN_SENDER: OnceCell<Mutex<Option<oneshot::Sender<()>>>> = OnceCell:
pub async fn check_singleton() -> Result<()> {
let port = IVerge::get_singleton_port();
if !local_port_available(port) {
let client = ClientBuilder::new()
.timeout(Duration::from_millis(1000))
.build()?;
let argvs: Vec<String> = std::env::args().collect();
if argvs.len() > 1 {
#[cfg(not(target_os = "macos"))]
{
let param = argvs[1].as_str();
if param.starts_with("clash:") {
let _ = reqwest::get(format!(
"http://127.0.0.1:{port}/commands/scheme?param={param}"
))
.await;
client
.get(format!(
"http://127.0.0.1:{port}/commands/scheme?param={param}"
))
.send()
.await?;
}
}
} else {
let _ = reqwest::get(format!("http://127.0.0.1:{port}/commands/visible")).await;
client
.get(format!("http://127.0.0.1:{port}/commands/visible"))
.send()
.await?;
}
log::error!("failed to setup singleton listen server");
bail!("app exists");
@@ -57,6 +69,12 @@ pub fn embed_server() {
AsyncHandler::spawn(move || async move {
let visible = warp::path!("commands" / "visible").and_then(|| async {
logging!(info, Type::Window, "检测到从单例模式恢复应用窗口");
if !lightweight::exit_lightweight_mode().await {
WindowManager::show_main_window().await;
} else {
logging!(error, Type::Window, "轻量模式退出失败,无法恢复应用窗口");
};
Ok::<_, warp::Rejection>(warp::reply::with_status(
"ok".to_string(),
warp::http::StatusCode::OK,