2025-03-20 06:01:38 +08:00
|
|
|
|
use crate::{
|
|
|
|
|
|
config::Config,
|
2025-06-18 20:00:33 +08:00
|
|
|
|
core::{handle, timer::Timer, tray::Tray},
|
2025-06-11 00:51:34 +08:00
|
|
|
|
log_err, logging,
|
2025-08-26 01:49:51 +08:00
|
|
|
|
process::AsyncHandler,
|
2025-09-09 18:50:24 +08:00
|
|
|
|
utils::logging::Type,
|
2025-03-20 06:01:38 +08:00
|
|
|
|
};
|
2025-03-20 03:23:14 +08:00
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
2025-06-11 00:51:34 +08:00
|
|
|
|
use crate::logging_error;
|
2025-06-11 00:19:06 +08:00
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
use crate::utils::window_manager::WindowManager;
|
2025-04-16 02:19:25 +08:00
|
|
|
|
use anyhow::{Context, Result};
|
|
|
|
|
|
use delay_timer::prelude::TaskBuilder;
|
edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
|
use std::sync::atomic::{AtomicU8, AtomicU32, Ordering};
|
2025-09-09 18:50:24 +08:00
|
|
|
|
use tauri::Listener;
|
2025-04-16 02:19:25 +08:00
|
|
|
|
|
2025-03-20 06:01:38 +08:00
|
|
|
|
const LIGHT_WEIGHT_TASK_UID: &str = "light_weight_task";
|
2025-03-20 03:23:14 +08:00
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
#[repr(u8)]
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
|
enum LightweightState {
|
|
|
|
|
|
Normal = 0,
|
|
|
|
|
|
In = 1,
|
|
|
|
|
|
Exiting = 2,
|
|
|
|
|
|
}
|
2025-06-22 15:42:01 +08:00
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
impl From<u8> for LightweightState {
|
|
|
|
|
|
fn from(v: u8) -> Self {
|
|
|
|
|
|
match v {
|
|
|
|
|
|
1 => LightweightState::In,
|
|
|
|
|
|
2 => LightweightState::Exiting,
|
|
|
|
|
|
_ => LightweightState::Normal,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-02 08:00:53 +08:00
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
impl LightweightState {
|
|
|
|
|
|
fn as_u8(self) -> u8 {
|
|
|
|
|
|
self as u8
|
refactor: optimize singleton macro usage with Default trait implementations (#4279)
* refactor: implement DRY principle improvements across backend
Major DRY violations identified and addressed:
1. **IPC Stream Monitor Pattern**:
- Created `utils/ipc_monitor.rs` with generic `IpcStreamMonitor` trait
- Added `IpcMonitorManager` for common async task management patterns
- Eliminates duplication across traffic.rs, memory.rs, and logs.rs
2. **Singleton Pattern Duplication**:
- Created `utils/singleton.rs` with `singleton\!` and `singleton_with_logging\!` macros
- Replaces 16+ duplicate singleton implementations across codebase
- Provides consistent, tested patterns for global instances
3. **macOS Activation Policy Refactoring**:
- Consolidated 3 duplicate methods into single parameterized `set_activation_policy()`
- Eliminated code duplication while maintaining backward compatibility
- Reduced maintenance burden for macOS-specific functionality
These improvements enhance maintainability, reduce bug potential, and ensure consistent patterns across the backend codebase.
* fix: resolve test failures and clippy warnings
- Fix doctest in singleton.rs by using rust,ignore syntax and proper code examples
- Remove unused time::Instant import from ipc_monitor.rs
- Add #[allow(dead_code)] attributes to future-use utility modules
- All 11 unit tests now pass successfully
- All clippy checks pass with -D warnings strict mode
- Documentation tests properly ignore example code that requires full context
* refactor: migrate code to use new utility tools (partial)
Progress on systematic migration to use created utility tools:
1. **Reorganized IPC Monitor**:
- Moved ipc_monitor.rs to src-tauri/src/ipc/monitor.rs for better organization
- Updated module structure to emphasize IPC relationship
2. **IpcManager Singleton Migration**:
- Replaced manual OnceLock singleton pattern with singleton_with_logging\! macro
- Simplified initialization code and added consistent logging
- Removed unused imports (OnceLock, logging::Type)
3. **ProxyRequestCache Singleton Migration**:
- Migrated from once_cell::sync::OnceCell to singleton\! macro
- Cleaner, more maintainable singleton pattern
- Consistent with project-wide singleton approach
These migrations demonstrate the utility and effectiveness of the created tools:
- Less boilerplate code
- Consistent patterns across codebase
- Easier maintenance and debugging
* feat: complete migration to new utility tools - phase 1
Successfully migrated core components to use the created utility tools:
- Moved `ipc_monitor.rs` to `src-tauri/src/ipc/monitor.rs`
- Better organization emphasizing IPC relationship
- Updated module exports and imports
- **IpcManager**: Migrated to `singleton_with_logging\!` macro
- **ProxyRequestCache**: Migrated to `singleton\!` macro
- Eliminated ~30 lines of boilerplate singleton code
- Consistent logging and initialization patterns
- Removed unused imports (OnceLock, once_cell, logging::Type)
- Cleaner, more maintainable code structure
- All 11 unit tests pass successfully
- Zero compilation warnings
- **Lines of code reduced**: ~50+ lines of boilerplate
- **Consistency improved**: Unified singleton patterns
- **Maintainability enhanced**: Centralized utility functions
- **Test coverage maintained**: 100% test pass rate
Remaining complex monitors (traffic, memory, logs) will be migrated to use the shared IPC monitoring patterns in the next phase, which requires careful refactoring of their streaming logic.
* refactor: complete singleton pattern migration to utility macros
Migrate remaining singleton patterns across the backend to use standardized
utility macros, achieving significant code reduction and consistency improvements.
- **LogsMonitor** (ipc/logs.rs): `OnceLock` → `singleton_with_logging\!`
- **Sysopt** (core/sysopt.rs): `OnceCell` → `singleton_lazy\!`
- **Tray** (core/tray/mod.rs): Complex `OnceCell` → `singleton_lazy\!`
- **Handle** (core/handle.rs): `OnceCell` → `singleton\!`
- **CoreManager** (core/core.rs): `OnceCell` → `singleton_lazy\!`
- **TrafficMonitor** (ipc/traffic.rs): `OnceLock` → `singleton_lazy_with_logging\!`
- **MemoryMonitor** (ipc/memory.rs): `OnceLock` → `singleton_lazy_with_logging\!`
- `singleton_lazy\!` - For complex initialization patterns
- `singleton_lazy_with_logging\!` - For complex initialization with logging
- **Code Reduction**: -33 lines of boilerplate singleton code
- **DRY Compliance**: Eliminated duplicate initialization patterns
- **Consistency**: Unified singleton approach across codebase
- **Maintainability**: Centralized singleton logic in utility macros
- **Zero Breaking Changes**: All existing APIs remain compatible
All tests pass and clippy warnings resolved.
* refactor: optimize singleton macros using Default trait implementation
Simplify singleton macro usage by implementing Default trait for complex
initialization patterns, significantly improving code readability and maintainability.
- **MemoryMonitor**: Move IPC client initialization to Default impl
- **TrafficMonitor**: Move IPC client initialization to Default impl
- **Sysopt**: Move Arc<Mutex> initialization to Default impl
- **Tray**: Move struct field initialization to Default impl
- **CoreManager**: Move Arc<Mutex> initialization to Default impl
```rust
singleton_lazy_with_logging\!(MemoryMonitor, INSTANCE, "MemoryMonitor", || {
let ipc_path_buf = ipc_path().unwrap();
let ipc_path = ipc_path_buf.to_str().unwrap_or_default();
let client = IpcStreamClient::new(ipc_path).unwrap();
MemoryMonitor::new(client)
});
```
```rust
impl Default for MemoryMonitor { /* initialization logic */ }
singleton_lazy_with_logging\!(MemoryMonitor, INSTANCE, "MemoryMonitor", MemoryMonitor::default);
```
- **Code Reduction**: -17 lines of macro closure code (80%+ simplification)
- **Separation of Concerns**: Initialization logic moved to proper Default impl
- **Readability**: Single-line macro calls vs multi-line closures
- **Testability**: Default implementations can be tested independently
- **Rust Idioms**: Using standard Default trait pattern
- **Performance**: Function calls more efficient than closures
All tests pass and clippy warnings resolved.
* refactor: implement MonitorData and StreamingParser traits for IPC monitors
* refactor: add timeout and retry_interval fields to IpcStreamMonitor; update TrafficMonitorState to derive Default
* refactor: migrate AppHandleManager to unified singleton control
- Replace manual singleton implementation with singleton_with_logging\! macro
- Remove std::sync::Once dependency in favor of OnceLock-based pattern
- Improve error handling for macOS activation policy methods
- Maintain thread safety with parking_lot::Mutex for AppHandle storage
- Add proper initialization check to prevent duplicate handle assignment
- Enhance logging consistency across AppHandleManager operations
* refactor: improve hotkey management with enum-based operations
- Add HotkeyFunction enum for type-safe function selection
- Add SystemHotkey enum for predefined system shortcuts
- Implement Display and FromStr traits for type conversions
- Replace string-based hotkey registration with enum methods
- Add register_system_hotkey() and unregister_system_hotkey() methods
- Maintain backward compatibility with string-based register() method
- Migrate singleton pattern to use singleton_with_logging\! macro
- Extract hotkey function execution logic into centralized execute_function()
- Update lib.rs to use new enum-based SystemHotkey operations
- Improve type safety and reduce string manipulation errors
Benefits:
- Type safety prevents invalid hotkey function names
- Centralized function execution reduces code duplication
- Enum-based API provides better IDE autocomplete support
- Maintains full backward compatibility with existing configurations
* fix: resolve LightWeightState initialization order panic
- Modify with_lightweight_status() to safely handle unmanaged state using try_state()
- Return Option<R> instead of R to gracefully handle state unavailability
- Update is_in_lightweight_mode() to use unwrap_or(false) for safe defaults
- Add state availability check in auto_lightweight_mode_init() before access
- Maintain singleton check priority while preventing early state access panics
- Fix clippy warnings for redundant pattern matching
Resolves runtime panic: "state() called before manage() for LightWeightState"
* refactor: add unreachable patterns for non-macOS in hotkey handling
* refactor: simplify SystemHotkey enum by removing redundant cfg attributes
* refactor: add macOS conditional compilation for system hotkey registration methods
* refactor: streamline hotkey unregistration and error logging for macOS
2025-07-31 14:35:13 +08:00
|
|
|
|
}
|
2025-09-02 08:00:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
static LIGHTWEIGHT_STATE: AtomicU8 = AtomicU8::new(LightweightState::Normal as u8);
|
|
|
|
|
|
|
|
|
|
|
|
static WINDOW_CLOSE_HANDLER: AtomicU32 = AtomicU32::new(0);
|
|
|
|
|
|
static WEBVIEW_FOCUS_HANDLER: AtomicU32 = AtomicU32::new(0);
|
|
|
|
|
|
|
|
|
|
|
|
fn set_state(new: LightweightState) {
|
|
|
|
|
|
LIGHTWEIGHT_STATE.store(new.as_u8(), Ordering::Release);
|
|
|
|
|
|
match new {
|
|
|
|
|
|
LightweightState::Normal => {
|
|
|
|
|
|
logging!(info, Type::Lightweight, true, "轻量模式已关闭");
|
|
|
|
|
|
}
|
|
|
|
|
|
LightweightState::In => {
|
|
|
|
|
|
logging!(info, Type::Lightweight, true, "轻量模式已开启");
|
|
|
|
|
|
}
|
|
|
|
|
|
LightweightState::Exiting => {
|
|
|
|
|
|
logging!(info, Type::Lightweight, true, "正在退出轻量模式");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn get_state() -> LightweightState {
|
|
|
|
|
|
LIGHTWEIGHT_STATE.load(Ordering::Acquire).into()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否处于轻量模式
|
|
|
|
|
|
pub fn is_in_lightweight_mode() -> bool {
|
|
|
|
|
|
get_state() == LightweightState::In
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置轻量模式状态(仅 Normal <-> In)
|
|
|
|
|
|
async fn set_lightweight_mode(value: bool) {
|
|
|
|
|
|
let current = get_state();
|
|
|
|
|
|
if value && current != LightweightState::In {
|
|
|
|
|
|
set_state(LightweightState::In);
|
|
|
|
|
|
} else if !value && current != LightweightState::Normal {
|
|
|
|
|
|
set_state(LightweightState::Normal);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 只有在状态可用时才触发托盘更新
|
|
|
|
|
|
if let Err(e) = Tray::global().update_part().await {
|
|
|
|
|
|
log::warn!("Failed to update tray: {e}");
|
|
|
|
|
|
}
|
2025-06-11 04:20:50 +08:00
|
|
|
|
}
|
2025-04-21 12:20:55 +08:00
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
pub async fn run_once_auto_lightweight() {
|
|
|
|
|
|
let verge_config = Config::verge().await;
|
|
|
|
|
|
let enable_auto = verge_config
|
|
|
|
|
|
.data_mut()
|
|
|
|
|
|
.enable_auto_light_weight_mode
|
|
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
let is_silent_start = verge_config
|
|
|
|
|
|
.latest_ref()
|
|
|
|
|
|
.enable_silent_start
|
|
|
|
|
|
.unwrap_or(false);
|
2025-06-18 20:00:33 +08:00
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
if !(enable_auto && is_silent_start) {
|
|
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Lightweight,
|
|
|
|
|
|
true,
|
|
|
|
|
|
"不满足静默启动且自动进入轻量模式的条件,跳过自动进入轻量模式"
|
|
|
|
|
|
);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-07 13:00:51 +08:00
|
|
|
|
set_lightweight_mode(true).await;
|
2025-09-02 08:00:53 +08:00
|
|
|
|
enable_auto_light_weight_mode().await;
|
2025-04-20 23:47:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-29 23:51:09 +08:00
|
|
|
|
pub async fn auto_lightweight_mode_init() -> Result<()> {
|
2025-09-02 08:00:53 +08:00
|
|
|
|
let is_silent_start =
|
|
|
|
|
|
{ Config::verge().await.latest_ref().enable_silent_start }.unwrap_or(false);
|
|
|
|
|
|
let enable_auto = {
|
|
|
|
|
|
Config::verge()
|
|
|
|
|
|
.await
|
|
|
|
|
|
.latest_ref()
|
|
|
|
|
|
.enable_auto_light_weight_mode
|
|
|
|
|
|
}
|
|
|
|
|
|
.unwrap_or(false);
|
refactor: optimize singleton macro usage with Default trait implementations (#4279)
* refactor: implement DRY principle improvements across backend
Major DRY violations identified and addressed:
1. **IPC Stream Monitor Pattern**:
- Created `utils/ipc_monitor.rs` with generic `IpcStreamMonitor` trait
- Added `IpcMonitorManager` for common async task management patterns
- Eliminates duplication across traffic.rs, memory.rs, and logs.rs
2. **Singleton Pattern Duplication**:
- Created `utils/singleton.rs` with `singleton\!` and `singleton_with_logging\!` macros
- Replaces 16+ duplicate singleton implementations across codebase
- Provides consistent, tested patterns for global instances
3. **macOS Activation Policy Refactoring**:
- Consolidated 3 duplicate methods into single parameterized `set_activation_policy()`
- Eliminated code duplication while maintaining backward compatibility
- Reduced maintenance burden for macOS-specific functionality
These improvements enhance maintainability, reduce bug potential, and ensure consistent patterns across the backend codebase.
* fix: resolve test failures and clippy warnings
- Fix doctest in singleton.rs by using rust,ignore syntax and proper code examples
- Remove unused time::Instant import from ipc_monitor.rs
- Add #[allow(dead_code)] attributes to future-use utility modules
- All 11 unit tests now pass successfully
- All clippy checks pass with -D warnings strict mode
- Documentation tests properly ignore example code that requires full context
* refactor: migrate code to use new utility tools (partial)
Progress on systematic migration to use created utility tools:
1. **Reorganized IPC Monitor**:
- Moved ipc_monitor.rs to src-tauri/src/ipc/monitor.rs for better organization
- Updated module structure to emphasize IPC relationship
2. **IpcManager Singleton Migration**:
- Replaced manual OnceLock singleton pattern with singleton_with_logging\! macro
- Simplified initialization code and added consistent logging
- Removed unused imports (OnceLock, logging::Type)
3. **ProxyRequestCache Singleton Migration**:
- Migrated from once_cell::sync::OnceCell to singleton\! macro
- Cleaner, more maintainable singleton pattern
- Consistent with project-wide singleton approach
These migrations demonstrate the utility and effectiveness of the created tools:
- Less boilerplate code
- Consistent patterns across codebase
- Easier maintenance and debugging
* feat: complete migration to new utility tools - phase 1
Successfully migrated core components to use the created utility tools:
- Moved `ipc_monitor.rs` to `src-tauri/src/ipc/monitor.rs`
- Better organization emphasizing IPC relationship
- Updated module exports and imports
- **IpcManager**: Migrated to `singleton_with_logging\!` macro
- **ProxyRequestCache**: Migrated to `singleton\!` macro
- Eliminated ~30 lines of boilerplate singleton code
- Consistent logging and initialization patterns
- Removed unused imports (OnceLock, once_cell, logging::Type)
- Cleaner, more maintainable code structure
- All 11 unit tests pass successfully
- Zero compilation warnings
- **Lines of code reduced**: ~50+ lines of boilerplate
- **Consistency improved**: Unified singleton patterns
- **Maintainability enhanced**: Centralized utility functions
- **Test coverage maintained**: 100% test pass rate
Remaining complex monitors (traffic, memory, logs) will be migrated to use the shared IPC monitoring patterns in the next phase, which requires careful refactoring of their streaming logic.
* refactor: complete singleton pattern migration to utility macros
Migrate remaining singleton patterns across the backend to use standardized
utility macros, achieving significant code reduction and consistency improvements.
- **LogsMonitor** (ipc/logs.rs): `OnceLock` → `singleton_with_logging\!`
- **Sysopt** (core/sysopt.rs): `OnceCell` → `singleton_lazy\!`
- **Tray** (core/tray/mod.rs): Complex `OnceCell` → `singleton_lazy\!`
- **Handle** (core/handle.rs): `OnceCell` → `singleton\!`
- **CoreManager** (core/core.rs): `OnceCell` → `singleton_lazy\!`
- **TrafficMonitor** (ipc/traffic.rs): `OnceLock` → `singleton_lazy_with_logging\!`
- **MemoryMonitor** (ipc/memory.rs): `OnceLock` → `singleton_lazy_with_logging\!`
- `singleton_lazy\!` - For complex initialization patterns
- `singleton_lazy_with_logging\!` - For complex initialization with logging
- **Code Reduction**: -33 lines of boilerplate singleton code
- **DRY Compliance**: Eliminated duplicate initialization patterns
- **Consistency**: Unified singleton approach across codebase
- **Maintainability**: Centralized singleton logic in utility macros
- **Zero Breaking Changes**: All existing APIs remain compatible
All tests pass and clippy warnings resolved.
* refactor: optimize singleton macros using Default trait implementation
Simplify singleton macro usage by implementing Default trait for complex
initialization patterns, significantly improving code readability and maintainability.
- **MemoryMonitor**: Move IPC client initialization to Default impl
- **TrafficMonitor**: Move IPC client initialization to Default impl
- **Sysopt**: Move Arc<Mutex> initialization to Default impl
- **Tray**: Move struct field initialization to Default impl
- **CoreManager**: Move Arc<Mutex> initialization to Default impl
```rust
singleton_lazy_with_logging\!(MemoryMonitor, INSTANCE, "MemoryMonitor", || {
let ipc_path_buf = ipc_path().unwrap();
let ipc_path = ipc_path_buf.to_str().unwrap_or_default();
let client = IpcStreamClient::new(ipc_path).unwrap();
MemoryMonitor::new(client)
});
```
```rust
impl Default for MemoryMonitor { /* initialization logic */ }
singleton_lazy_with_logging\!(MemoryMonitor, INSTANCE, "MemoryMonitor", MemoryMonitor::default);
```
- **Code Reduction**: -17 lines of macro closure code (80%+ simplification)
- **Separation of Concerns**: Initialization logic moved to proper Default impl
- **Readability**: Single-line macro calls vs multi-line closures
- **Testability**: Default implementations can be tested independently
- **Rust Idioms**: Using standard Default trait pattern
- **Performance**: Function calls more efficient than closures
All tests pass and clippy warnings resolved.
* refactor: implement MonitorData and StreamingParser traits for IPC monitors
* refactor: add timeout and retry_interval fields to IpcStreamMonitor; update TrafficMonitorState to derive Default
* refactor: migrate AppHandleManager to unified singleton control
- Replace manual singleton implementation with singleton_with_logging\! macro
- Remove std::sync::Once dependency in favor of OnceLock-based pattern
- Improve error handling for macOS activation policy methods
- Maintain thread safety with parking_lot::Mutex for AppHandle storage
- Add proper initialization check to prevent duplicate handle assignment
- Enhance logging consistency across AppHandleManager operations
* refactor: improve hotkey management with enum-based operations
- Add HotkeyFunction enum for type-safe function selection
- Add SystemHotkey enum for predefined system shortcuts
- Implement Display and FromStr traits for type conversions
- Replace string-based hotkey registration with enum methods
- Add register_system_hotkey() and unregister_system_hotkey() methods
- Maintain backward compatibility with string-based register() method
- Migrate singleton pattern to use singleton_with_logging\! macro
- Extract hotkey function execution logic into centralized execute_function()
- Update lib.rs to use new enum-based SystemHotkey operations
- Improve type safety and reduce string manipulation errors
Benefits:
- Type safety prevents invalid hotkey function names
- Centralized function execution reduces code duplication
- Enum-based API provides better IDE autocomplete support
- Maintains full backward compatibility with existing configurations
* fix: resolve LightWeightState initialization order panic
- Modify with_lightweight_status() to safely handle unmanaged state using try_state()
- Return Option<R> instead of R to gracefully handle state unavailability
- Update is_in_lightweight_mode() to use unwrap_or(false) for safe defaults
- Add state availability check in auto_lightweight_mode_init() before access
- Maintain singleton check priority while preventing early state access panics
- Fix clippy warnings for redundant pattern matching
Resolves runtime panic: "state() called before manage() for LightWeightState"
* refactor: add unreachable patterns for non-macOS in hotkey handling
* refactor: simplify SystemHotkey enum by removing redundant cfg attributes
* refactor: add macOS conditional compilation for system hotkey registration methods
* refactor: streamline hotkey unregistration and error logging for macOS
2025-07-31 14:35:13 +08:00
|
|
|
|
|
2025-09-02 08:00:53 +08:00
|
|
|
|
if enable_auto && !is_silent_start {
|
|
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Lightweight,
|
|
|
|
|
|
true,
|
|
|
|
|
|
"非静默启动直接挂载自动进入轻量模式监听器!"
|
|
|
|
|
|
);
|
2025-09-09 18:50:24 +08:00
|
|
|
|
set_state(LightweightState::Normal);
|
2025-09-02 08:00:53 +08:00
|
|
|
|
enable_auto_light_weight_mode().await;
|
2025-06-11 04:20:50 +08:00
|
|
|
|
}
|
2025-08-29 23:51:09 +08:00
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2025-04-21 12:20:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
pub async fn enable_auto_light_weight_mode() {
|
|
|
|
|
|
if let Err(e) = Timer::global().init().await {
|
2025-08-18 02:02:25 +08:00
|
|
|
|
logging!(error, Type::Lightweight, "Failed to initialize timer: {e}");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-03-29 07:52:46 +08:00
|
|
|
|
logging!(info, Type::Lightweight, true, "开启自动轻量模式");
|
2025-03-20 06:01:38 +08:00
|
|
|
|
setup_window_close_listener();
|
|
|
|
|
|
setup_webview_focus_listener();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn disable_auto_light_weight_mode() {
|
2025-03-29 07:52:46 +08:00
|
|
|
|
logging!(info, Type::Lightweight, true, "关闭自动轻量模式");
|
2025-03-20 06:01:38 +08:00
|
|
|
|
let _ = cancel_light_weight_timer();
|
|
|
|
|
|
cancel_window_close_listener();
|
2025-09-09 18:50:24 +08:00
|
|
|
|
cancel_webview_focus_listener();
|
2025-03-20 06:01:38 +08:00
|
|
|
|
}
|
2025-03-20 03:23:14 +08:00
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
pub async fn entry_lightweight_mode() -> bool {
|
|
|
|
|
|
// 尝试从 Normal -> In
|
|
|
|
|
|
if LIGHTWEIGHT_STATE
|
|
|
|
|
|
.compare_exchange(
|
|
|
|
|
|
LightweightState::Normal as u8,
|
|
|
|
|
|
LightweightState::In as u8,
|
|
|
|
|
|
Ordering::Acquire,
|
|
|
|
|
|
Ordering::Relaxed,
|
|
|
|
|
|
)
|
|
|
|
|
|
.is_err()
|
|
|
|
|
|
{
|
|
|
|
|
|
logging!(info, Type::Lightweight, true, "无需进入轻量模式,跳过调用");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-06-17 13:02:47 +08:00
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
WindowManager::destroy_main_window();
|
2025-06-17 13:02:47 +08:00
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
set_lightweight_mode(true).await;
|
2025-03-20 23:17:37 +08:00
|
|
|
|
let _ = cancel_light_weight_timer();
|
2025-09-09 18:50:24 +08:00
|
|
|
|
|
|
|
|
|
|
// 回到 In
|
|
|
|
|
|
set_state(LightweightState::In);
|
|
|
|
|
|
|
|
|
|
|
|
true
|
2025-03-20 03:23:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-21 12:20:55 +08:00
|
|
|
|
// 添加从轻量模式恢复的函数
|
2025-09-09 18:50:24 +08:00
|
|
|
|
pub async fn exit_lightweight_mode() -> bool {
|
|
|
|
|
|
// 尝试从 In -> Exiting
|
|
|
|
|
|
if LIGHTWEIGHT_STATE
|
|
|
|
|
|
.compare_exchange(
|
|
|
|
|
|
LightweightState::In as u8,
|
|
|
|
|
|
LightweightState::Exiting as u8,
|
|
|
|
|
|
Ordering::Acquire,
|
|
|
|
|
|
Ordering::Relaxed,
|
|
|
|
|
|
)
|
2025-06-22 15:42:01 +08:00
|
|
|
|
.is_err()
|
|
|
|
|
|
{
|
|
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Lightweight,
|
|
|
|
|
|
true,
|
2025-09-09 18:50:24 +08:00
|
|
|
|
"轻量模式不在退出条件(可能已退出或正在退出),跳过调用"
|
2025-06-22 15:42:01 +08:00
|
|
|
|
);
|
2025-09-09 18:50:24 +08:00
|
|
|
|
return false;
|
2025-06-22 15:42:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
WindowManager::show_main_window().await;
|
2025-04-21 12:20:55 +08:00
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
set_lightweight_mode(false).await;
|
2025-09-09 18:50:24 +08:00
|
|
|
|
let _ = cancel_light_weight_timer();
|
2025-04-20 23:47:09 +08:00
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
// 回到 Normal
|
|
|
|
|
|
set_state(LightweightState::Normal);
|
2025-06-15 17:40:59 +08:00
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
logging!(info, Type::Lightweight, true, "轻量模式退出完成");
|
|
|
|
|
|
true
|
2025-04-20 23:47:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-11 00:19:06 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
2025-08-26 01:49:51 +08:00
|
|
|
|
pub async fn add_light_weight_timer() {
|
|
|
|
|
|
logging_error!(Type::Lightweight, setup_light_weight_timer().await);
|
2025-04-12 03:27:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
fn setup_window_close_listener() {
|
2025-10-08 12:32:40 +08:00
|
|
|
|
if let Some(window) = handle::Handle::get_window() {
|
2025-03-20 06:01:38 +08:00
|
|
|
|
let handler = window.listen("tauri://close-requested", move |_event| {
|
2025-08-26 01:49:51 +08:00
|
|
|
|
std::mem::drop(AsyncHandler::spawn(|| async {
|
|
|
|
|
|
if let Err(e) = setup_light_weight_timer().await {
|
|
|
|
|
|
log::warn!("Failed to setup light weight timer: {e}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}));
|
2025-03-28 03:39:21 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Lightweight,
|
2025-03-29 07:52:46 +08:00
|
|
|
|
true,
|
2025-03-28 03:39:21 +08:00
|
|
|
|
"监听到关闭请求,开始轻量模式计时"
|
|
|
|
|
|
);
|
2025-03-20 06:01:38 +08:00
|
|
|
|
});
|
2025-09-09 18:50:24 +08:00
|
|
|
|
|
|
|
|
|
|
WINDOW_CLOSE_HANDLER.store(handler, Ordering::Release);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn cancel_window_close_listener() {
|
2025-10-08 12:32:40 +08:00
|
|
|
|
if let Some(window) = handle::Handle::get_window() {
|
2025-09-09 18:50:24 +08:00
|
|
|
|
let handler = WINDOW_CLOSE_HANDLER.swap(0, Ordering::AcqRel);
|
|
|
|
|
|
if handler != 0 {
|
|
|
|
|
|
window.unlisten(handler);
|
|
|
|
|
|
logging!(info, Type::Lightweight, true, "取消了窗口关闭监听");
|
|
|
|
|
|
}
|
2025-03-20 06:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
fn setup_webview_focus_listener() {
|
2025-10-08 12:32:40 +08:00
|
|
|
|
if let Some(window) = handle::Handle::get_window() {
|
2025-03-20 06:01:38 +08:00
|
|
|
|
let handler = window.listen("tauri://focus", move |_event| {
|
|
|
|
|
|
log_err!(cancel_light_weight_timer());
|
2025-03-28 03:39:21 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Lightweight,
|
|
|
|
|
|
"监听到窗口获得焦点,取消轻量模式计时"
|
|
|
|
|
|
);
|
2025-03-20 06:01:38 +08:00
|
|
|
|
});
|
2025-09-09 18:50:24 +08:00
|
|
|
|
|
|
|
|
|
|
WEBVIEW_FOCUS_HANDLER.store(handler, Ordering::Release);
|
2025-03-20 06:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 18:50:24 +08:00
|
|
|
|
fn cancel_webview_focus_listener() {
|
2025-10-08 12:32:40 +08:00
|
|
|
|
if let Some(window) = handle::Handle::get_window() {
|
2025-09-09 18:50:24 +08:00
|
|
|
|
let handler = WEBVIEW_FOCUS_HANDLER.swap(0, Ordering::AcqRel);
|
|
|
|
|
|
if handler != 0 {
|
|
|
|
|
|
window.unlisten(handler);
|
|
|
|
|
|
logging!(info, Type::Lightweight, true, "取消了窗口焦点监听");
|
|
|
|
|
|
}
|
2025-03-20 06:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
async fn setup_light_weight_timer() -> Result<()> {
|
|
|
|
|
|
Timer::global().init().await?;
|
2025-03-20 06:01:38 +08:00
|
|
|
|
let once_by_minutes = Config::verge()
|
2025-08-26 01:49:51 +08:00
|
|
|
|
.await
|
2025-07-04 22:43:23 +08:00
|
|
|
|
.latest_ref()
|
2025-03-20 06:01:38 +08:00
|
|
|
|
.auto_light_weight_minutes
|
|
|
|
|
|
.unwrap_or(10);
|
|
|
|
|
|
|
2025-06-15 19:35:12 +08:00
|
|
|
|
// 获取task_id
|
|
|
|
|
|
let task_id = {
|
2025-08-01 23:02:11 +08:00
|
|
|
|
Timer::global()
|
|
|
|
|
|
.timer_count
|
|
|
|
|
|
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
2025-06-15 19:35:12 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 创建任务
|
2025-03-20 06:01:38 +08:00
|
|
|
|
let task = TaskBuilder::default()
|
|
|
|
|
|
.set_task_id(task_id)
|
|
|
|
|
|
.set_maximum_parallel_runnable_num(1)
|
|
|
|
|
|
.set_frequency_once_by_minutes(once_by_minutes)
|
|
|
|
|
|
.spawn_async_routine(move || async move {
|
2025-03-29 07:52:46 +08:00
|
|
|
|
logging!(info, Type::Timer, true, "计时器到期,开始进入轻量模式");
|
2025-08-26 01:49:51 +08:00
|
|
|
|
entry_lightweight_mode().await;
|
2025-03-20 06:01:38 +08:00
|
|
|
|
})
|
2025-03-28 03:39:21 +08:00
|
|
|
|
.context("failed to create timer task")?;
|
2025-03-20 06:01:38 +08:00
|
|
|
|
|
2025-06-15 19:35:12 +08:00
|
|
|
|
// 添加任务到定时器
|
|
|
|
|
|
{
|
|
|
|
|
|
let delay_timer = Timer::global().delay_timer.write();
|
|
|
|
|
|
delay_timer
|
|
|
|
|
|
.add_task(task)
|
|
|
|
|
|
.context("failed to add timer task")?;
|
|
|
|
|
|
}
|
2025-03-20 06:01:38 +08:00
|
|
|
|
|
2025-06-15 19:35:12 +08:00
|
|
|
|
// 更新任务映射
|
|
|
|
|
|
{
|
|
|
|
|
|
let mut timer_map = Timer::global().timer_map.write();
|
|
|
|
|
|
let timer_task = crate::core::timer::TimerTask {
|
|
|
|
|
|
task_id,
|
|
|
|
|
|
interval_minutes: once_by_minutes,
|
|
|
|
|
|
last_run: chrono::Local::now().timestamp(),
|
|
|
|
|
|
};
|
|
|
|
|
|
timer_map.insert(LIGHT_WEIGHT_TASK_UID.to_string(), timer_task);
|
|
|
|
|
|
}
|
2025-03-20 06:01:38 +08:00
|
|
|
|
|
2025-03-28 03:39:21 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Timer,
|
2025-03-29 07:52:46 +08:00
|
|
|
|
true,
|
2025-03-28 03:39:21 +08:00
|
|
|
|
"计时器已设置,{} 分钟后将自动进入轻量模式",
|
2025-03-20 06:01:38 +08:00
|
|
|
|
once_by_minutes
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn cancel_light_weight_timer() -> Result<()> {
|
|
|
|
|
|
let mut timer_map = Timer::global().timer_map.write();
|
|
|
|
|
|
let delay_timer = Timer::global().delay_timer.write();
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(task) = timer_map.remove(LIGHT_WEIGHT_TASK_UID) {
|
|
|
|
|
|
delay_timer
|
|
|
|
|
|
.remove_task(task.task_id)
|
2025-03-28 03:39:21 +08:00
|
|
|
|
.context("failed to remove timer task")?;
|
2025-03-29 07:52:46 +08:00
|
|
|
|
logging!(info, Type::Timer, true, "计时器已取消");
|
2025-03-20 06:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2025-03-20 03:23:14 +08:00
|
|
|
|
}
|