refactor: enhance async initialization and streamline setup process (#4560)

* feat: Implement DNS management for macOS

- Added `set_public_dns` and `restore_public_dns` functions in `dns.rs` to manage system DNS settings.
- Introduced `resolve` module to encapsulate DNS and scheme resolution functionalities.
- Implemented `resolve_scheme` function in `scheme.rs` to handle deep links and profile imports.
- Created UI readiness management in `ui.rs` to track and update UI loading states.
- Developed window management logic in `window.rs` to handle window creation and visibility.
- Added initial loading overlay script in `window_script.rs` for better user experience during startup.
- Updated server handling in `server.rs` to integrate new resolve functionalities.
- Refactored window creation calls in `window_manager.rs` to use the new window management logic.

* refactor: streamline asynchronous handling in config and resolve setup

* Revert "refactor: streamline asynchronous handling in config and resolve setup"

This reverts commit 23d7dc86d5.

* fix: optimize asynchronous memory handling

* fix: enhance task logging by adding size check for special cases

* refactor: enhance async initialization and streamline setup process

* refactor: optimize async setup by consolidating initialization tasks

* chore: update changelog for Mihomo(Meta) kernel upgrade to v1.19.13

* fix: improve startup phase initialization performance

* refactor: optimize file read/write performance to reduce application wait time

* refactor: simplify app instance exit logic and adjust system proxy guard initialization

* refactor: change resolve_setup_async to synchronous execution for improved performance

* refactor: update resolve_setup_async to accept AppHandle for improved initialization flow

* refactor: remove unnecessary initialization of portable flag in run function

* refactor: consolidate async initialization tasks into a single blocking call for improved execution flow

* refactor: optimize resolve_setup_async by restructuring async tasks for improved concurrency

* refactor: streamline resolve_setup_async and embed_server for improved async handling

* refactor: separate synchronous and asynchronous setup functions for improved clarity

* refactor: simplify async notification handling and remove redundant network manager initialization

* refactor: enhance async handling in proxy request cache and window creation logic

* refactor: improve code formatting and readability in ProxyRequestCache

* refactor: adjust singleton check timeout and optimize trace size conditions

* refactor: update TRACE_SPECIAL_SIZE to include additional size condition

* refactor: update kode-bridge dependency to version 0.2.1-rc2

* refactor: replace RwLock with AtomicBool for UI readiness and implement event-driven monitoring

* refactor: convert async functions to synchronous for window management

* Update src-tauri/src/utils/resolve/window.rs

* fix: handle missing app_handle in create_window function

* Update src-tauri/src/module/lightweight.rs
This commit is contained in:
Tunglies
2025-08-29 23:51:09 +08:00
committed by Tunglies
parent 2080dbdc0f
commit c6a6ea48dd
22 changed files with 956 additions and 879 deletions

View File

@@ -0,0 +1,213 @@
use tauri::AppHandle;
use crate::{
config::Config,
core::{handle, hotkey::Hotkey, sysopt, tray::Tray, CoreManager, Timer},
logging, logging_error,
module::lightweight::auto_lightweight_mode_init,
process::AsyncHandler,
utils::{init, logging::Type, network::NetworkManager, resolve::window::create_window, server},
};
pub mod dns;
pub mod scheme;
pub mod ui;
pub mod window;
pub mod window_script;
pub fn resolve_setup_sync(app_handle: AppHandle) {
init_handle(app_handle);
init_scheme();
init_embed_server();
NetworkManager::new().init();
}
pub fn resolve_setup_async() {
let start_time = std::time::Instant::now();
logging!(
info,
Type::Setup,
true,
"开始执行异步设置任务... 线程ID: {:?}",
std::thread::current().id()
);
AsyncHandler::spawn(|| async {
init_resources().await;
init_work_config().await;
init_startup_script().await;
init_timer().await;
init_hotkey().await;
init_auto_lightweight_mode().await;
init_verge_config().await;
init_core_manager().await;
init_system_proxy().await;
// 在单独的 spawn 中运行 sync 函数,避免 Send 问题
AsyncHandler::spawn_blocking(|| {
init_system_proxy_guard();
});
init_window().await;
init_tray().await;
refresh_tray_menu().await
});
let elapsed = start_time.elapsed();
logging!(
info,
Type::Setup,
true,
"异步设置任务完成,耗时: {:?}",
elapsed
);
if elapsed.as_secs() > 10 {
logging!(
warn,
Type::Setup,
true,
"异步设置任务耗时较长({:?})",
elapsed
);
}
}
// 其它辅助函数不变
pub async fn resolve_reset_async() {
logging!(info, Type::Tray, true, "Resetting system proxy");
logging_error!(
Type::System,
true,
sysopt::Sysopt::global().reset_sysproxy().await
);
logging!(info, Type::Core, true, "Stopping core service");
logging_error!(Type::Core, true, CoreManager::global().stop_core().await);
#[cfg(target_os = "macos")]
{
use dns::restore_public_dns;
logging!(info, Type::System, true, "Restoring system DNS settings");
restore_public_dns().await;
}
}
pub fn init_handle(app_handle: AppHandle) {
logging!(info, Type::Setup, true, "Initializing app handle...");
handle::Handle::global().init(app_handle);
}
pub(super) fn init_scheme() {
logging!(info, Type::Setup, true, "Initializing custom URL scheme");
logging_error!(Type::Setup, true, init::init_scheme());
}
pub(super) fn init_embed_server() {
logging!(info, Type::Setup, true, "Initializing embedded server...");
server::embed_server();
}
pub(super) async fn init_resources() {
logging!(info, Type::Setup, true, "Initializing resources...");
logging_error!(Type::Setup, true, init::init_resources().await);
}
pub(super) async fn init_startup_script() {
logging!(info, Type::Setup, true, "Initializing startup script");
logging_error!(Type::Setup, true, init::startup_script().await);
}
pub(super) async fn init_timer() {
logging!(info, Type::Setup, true, "Initializing timer...");
logging_error!(Type::Setup, true, Timer::global().init().await);
}
pub(super) async fn init_hotkey() {
logging!(info, Type::Setup, true, "Initializing hotkey...");
logging_error!(Type::Setup, true, Hotkey::global().init().await);
}
pub(super) async fn init_auto_lightweight_mode() {
logging!(
info,
Type::Setup,
true,
"Initializing auto lightweight mode..."
);
logging_error!(Type::Setup, true, auto_lightweight_mode_init().await);
}
pub async fn init_work_config() {
logging!(
info,
Type::Setup,
true,
"Initializing work configuration..."
);
logging_error!(Type::Setup, true, init::init_config().await);
}
pub(super) async fn init_tray() {
logging!(info, Type::Setup, true, "Initializing system tray...");
logging_error!(Type::Setup, true, Tray::global().init().await);
}
pub(super) async fn init_verge_config() {
logging!(
info,
Type::Setup,
true,
"Initializing verge configuration..."
);
logging_error!(Type::Setup, true, Config::init_config().await);
}
pub(super) async fn init_core_manager() {
logging!(info, Type::Setup, true, "Initializing core manager...");
logging_error!(Type::Setup, true, CoreManager::global().init().await);
}
pub(super) async fn init_system_proxy() {
logging!(info, Type::Setup, true, "Initializing system proxy...");
logging_error!(
Type::Setup,
true,
sysopt::Sysopt::global().update_sysproxy().await
);
}
pub(super) fn init_system_proxy_guard() {
logging!(
info,
Type::Setup,
true,
"Initializing system proxy guard..."
);
logging_error!(
Type::Setup,
true,
sysopt::Sysopt::global().init_guard_sysproxy()
);
}
pub(super) async fn refresh_tray_menu() {
logging!(info, Type::Setup, true, "Refreshing tray menu...");
logging_error!(Type::Setup, true, Tray::global().update_part().await);
}
pub(super) async fn init_window() {
let is_silent_start =
{ Config::verge().await.latest_ref().enable_silent_start }.unwrap_or(false);
#[cfg(target_os = "macos")]
{
if is_silent_start {
use crate::core::handle::Handle;
Handle::global().set_activation_policy_accessory();
}
}
create_window(!is_silent_start).await;
}