2024-12-31 06:27:29 +08:00
|
|
|
use once_cell::sync::OnceCell;
|
feat: update Cargo.toml for 2024 edition and optimize release profiles (#4681)
* 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
2025-09-08 13:57:32 +08:00
|
|
|
use tauri::tray::TrayIconBuilder;
|
2025-09-08 21:48:09 +08:00
|
|
|
use tauri::Emitter;
|
2024-12-31 11:11:29 +08:00
|
|
|
#[cfg(target_os = "macos")]
|
2024-12-31 06:27:29 +08:00
|
|
|
pub mod speed_rate;
|
feat: migrate mihomo to use kode-bridge IPC on Windows and Unix (#4051)
* Refactor Mihomo API integration and remove crate_mihomo_api
- Removed the `mihomo_api` crate and its dependencies from the project.
- Introduced `IpcManager` for handling IPC communication with Mihomo.
- Implemented IPC methods for managing proxies, connections, and configurations.
- Updated `MihomoManager` to utilize `IpcManager` instead of the removed crate.
- Added platform-specific IPC socket path handling for macOS, Linux, and Windows.
- Cleaned up related tests and configuration files.
* fix: remove duplicate permission entry in desktop capabilities
* refactor: replace MihomoManager with IpcManager and remove Mihomo module
* fix: restore tempfile dependency in dev-dependencies
* fix: update kode-bridge dependency to use git source from the dev branch
* feat: migrate mihomo to use kode-bridge IPC on Windows
This commit implements a comprehensive migration from legacy service IPC to the kode-bridge library for Windows IPC communication. Key changes include:
Replace service_ipc with kode-bridge IpcManager for all mihomo communications
Simplify proxy commands using new caching mechanism with ProxyRequestCache
Add Windows named pipe (\.\pipe\mihomo) and Unix socket IPC endpoint configuration
Update Tauri permissions and dependencies (dashmap, tauri-plugin-notification)
Add IPC logging support and improve error handling
Fix Windows IPC path handling in directory utilities
This migration enables better cross-platform IPC support and improved performance for mihomo proxy core communication.
* doc: add IPC communication with Mihomo kernel, removing Restful API dependency
* fix: standardize logging type naming from IPC to Ipc for consistency
* refactor: clean up and optimize code structure across multiple components and services
- Removed unnecessary comments and whitespace in various files.
- Improved code readability and maintainability by restructuring functions and components.
- Updated localization files for consistency and accuracy.
- Enhanced performance by optimizing hooks and utility functions.
- General code cleanup in settings, pages, and services to adhere to best practices.
* fix: simplify URL formatting in test_proxy_delay method
* fix: update kode-bridge dependency to version 0.1.3 and change source to crates.io
* fix: update macOS target versions in development workflow
* Revert "fix: update macOS target versions in development workflow"
This reverts commit b9831357e462e0f308d11a9a53cb718f98ae1295.
* feat: enhance IPC path handling for Unix systems and improve directory safety checks
* feat: add conditional compilation for Unix-specific IPC path handling
* chore: update cagro.lock
* feat: add external controller configuration and UI support
* Refactor proxy and connection management to use IPC-based commands
- Updated `get_proxies` function in `proxy.rs` to call the new IPC command.
- Renamed `get_refresh_proxies` to `get_proxies` in `ipc/general.rs` for consistency.
- Added new IPC commands for managing proxies, connections, and configurations in `cmds.ts`.
- Refactored API calls in various components to use the new IPC commands instead of HTTP requests.
- Improved error handling and response management in the new IPC functions.
- Cleaned up unused API functions in `api.ts` and redirected relevant calls to `cmds.ts`.
- Enhanced connection management features including health checks and updates for proxy providers.
* chore: update dependencies and improve error handling in IPC manager
* fix: downgrade zip dependency from 4.3.0 to 4.2.0
* feat: Implement traffic and memory data monitoring service
- Added `TrafficService` and `TrafficManager` to manage traffic and memory data collection.
- Introduced commands to get traffic and memory data, start and stop the traffic service.
- Integrated IPC calls for traffic and memory data retrieval in the frontend.
- Updated `AppDataProvider` and `EnhancedTrafficStats` components to utilize new data fetching methods.
- Removed WebSocket connections for traffic and memory data, replaced with IPC polling.
- Added logging for better traceability of data fetching and service status.
* refactor: unify external controller handling and improve IPC path resolution
* fix: replace direct IPC path retrieval with guard function for external controller
* fix: convert external controller IPC path to string for proper insertion in config map
* fix: update dependencies and improve IPC response handling
* fix: remove unnecessary unix conditional for ipc path import
* Refactor traffic and memory monitoring to use IPC stream; remove TrafficService and TrafficManager. Introduce new IPC-based data retrieval methods for traffic and memory, including formatted data and system overview. Update frontend components to utilize new APIs for enhanced data display and management.
* chore: bump crate rand version to 0.9.2
* feat: Implement enhanced traffic monitoring system with data compression and sampling
- Introduced `useTrafficMonitorEnhanced` hook for advanced traffic data management.
- Added `TrafficDataSampler` class for handling raw and compressed traffic data.
- Implemented reference counting to manage data collection based on component usage.
- Enhanced data validation with `SystemMonitorValidator` for API responses.
- Created diagnostic tools for monitoring performance and error tracking.
- Updated existing hooks to utilize the new enhanced monitoring features.
- Added utility functions for generating and formatting diagnostic reports.
* feat(ipc): improve URL encoding and error handling for IPC requests
- Add percent-encoding for URL paths to handle special characters properly
- Enhance error handling in update_proxy with proper logging
- Remove excessive debug logging to reduce noise
- Update kode-bridge dependency to v0.1.5
- Fix JSON parsing error handling in PUT requests
Changes include:
- Proper URL encoding for connection IDs, proxy names, and test URLs
- Enhanced error handling with fallback responses in updateProxy
- Comment out verbose debug logs in traffic monitoring and data validation
- Update dependency version for improved IPC functionality
* feat: major improvements in architecture, traffic monitoring, and data validation
* Refactor traffic graph components: Replace EnhancedTrafficGraph with EnhancedCanvasTrafficGraph, improve rendering performance, and enhance visual elements. Remove deprecated code and ensure compatibility with global data management.
* chore: update UPDATELOG.md for v2.4.0 release, refine traffic monitoring system details, and enhance IPC functionality
* chore: update UPDATELOG.md to reflect removal of deprecated MihomoManager and unify IPC control
* refactor: remove global traffic service testing method from cmds.ts
* Update src/components/home/enhanced-canvas-traffic-graph.tsx
* Update src/hooks/use-traffic-monitor-enhanced.ts
* Update src/components/layout/layout-traffic.tsx
* refactor: remove debug state management from LayoutTraffic component
---------
2025-07-24 00:47:42 +08:00
|
|
|
use crate::ipc::Rate;
|
2025-09-09 18:50:24 +08:00
|
|
|
use crate::module::lightweight;
|
2025-08-26 01:49:51 +08:00
|
|
|
use crate::process::AsyncHandler;
|
2025-09-09 18:50:24 +08:00
|
|
|
use crate::utils::window_manager::WindowManager;
|
2024-02-24 11:25:22 +08:00
|
|
|
use crate::{
|
2025-09-08 21:48:09 +08:00
|
|
|
cmd,
|
2024-02-24 11:25:22 +08:00
|
|
|
config::Config,
|
2025-08-31 14:20:57 +08:00
|
|
|
feat,
|
|
|
|
|
ipc::IpcManager,
|
|
|
|
|
logging,
|
feat: migrate mihomo to use kode-bridge IPC on Windows and Unix (#4051)
* Refactor Mihomo API integration and remove crate_mihomo_api
- Removed the `mihomo_api` crate and its dependencies from the project.
- Introduced `IpcManager` for handling IPC communication with Mihomo.
- Implemented IPC methods for managing proxies, connections, and configurations.
- Updated `MihomoManager` to utilize `IpcManager` instead of the removed crate.
- Added platform-specific IPC socket path handling for macOS, Linux, and Windows.
- Cleaned up related tests and configuration files.
* fix: remove duplicate permission entry in desktop capabilities
* refactor: replace MihomoManager with IpcManager and remove Mihomo module
* fix: restore tempfile dependency in dev-dependencies
* fix: update kode-bridge dependency to use git source from the dev branch
* feat: migrate mihomo to use kode-bridge IPC on Windows
This commit implements a comprehensive migration from legacy service IPC to the kode-bridge library for Windows IPC communication. Key changes include:
Replace service_ipc with kode-bridge IpcManager for all mihomo communications
Simplify proxy commands using new caching mechanism with ProxyRequestCache
Add Windows named pipe (\.\pipe\mihomo) and Unix socket IPC endpoint configuration
Update Tauri permissions and dependencies (dashmap, tauri-plugin-notification)
Add IPC logging support and improve error handling
Fix Windows IPC path handling in directory utilities
This migration enables better cross-platform IPC support and improved performance for mihomo proxy core communication.
* doc: add IPC communication with Mihomo kernel, removing Restful API dependency
* fix: standardize logging type naming from IPC to Ipc for consistency
* refactor: clean up and optimize code structure across multiple components and services
- Removed unnecessary comments and whitespace in various files.
- Improved code readability and maintainability by restructuring functions and components.
- Updated localization files for consistency and accuracy.
- Enhanced performance by optimizing hooks and utility functions.
- General code cleanup in settings, pages, and services to adhere to best practices.
* fix: simplify URL formatting in test_proxy_delay method
* fix: update kode-bridge dependency to version 0.1.3 and change source to crates.io
* fix: update macOS target versions in development workflow
* Revert "fix: update macOS target versions in development workflow"
This reverts commit b9831357e462e0f308d11a9a53cb718f98ae1295.
* feat: enhance IPC path handling for Unix systems and improve directory safety checks
* feat: add conditional compilation for Unix-specific IPC path handling
* chore: update cagro.lock
* feat: add external controller configuration and UI support
* Refactor proxy and connection management to use IPC-based commands
- Updated `get_proxies` function in `proxy.rs` to call the new IPC command.
- Renamed `get_refresh_proxies` to `get_proxies` in `ipc/general.rs` for consistency.
- Added new IPC commands for managing proxies, connections, and configurations in `cmds.ts`.
- Refactored API calls in various components to use the new IPC commands instead of HTTP requests.
- Improved error handling and response management in the new IPC functions.
- Cleaned up unused API functions in `api.ts` and redirected relevant calls to `cmds.ts`.
- Enhanced connection management features including health checks and updates for proxy providers.
* chore: update dependencies and improve error handling in IPC manager
* fix: downgrade zip dependency from 4.3.0 to 4.2.0
* feat: Implement traffic and memory data monitoring service
- Added `TrafficService` and `TrafficManager` to manage traffic and memory data collection.
- Introduced commands to get traffic and memory data, start and stop the traffic service.
- Integrated IPC calls for traffic and memory data retrieval in the frontend.
- Updated `AppDataProvider` and `EnhancedTrafficStats` components to utilize new data fetching methods.
- Removed WebSocket connections for traffic and memory data, replaced with IPC polling.
- Added logging for better traceability of data fetching and service status.
* refactor: unify external controller handling and improve IPC path resolution
* fix: replace direct IPC path retrieval with guard function for external controller
* fix: convert external controller IPC path to string for proper insertion in config map
* fix: update dependencies and improve IPC response handling
* fix: remove unnecessary unix conditional for ipc path import
* Refactor traffic and memory monitoring to use IPC stream; remove TrafficService and TrafficManager. Introduce new IPC-based data retrieval methods for traffic and memory, including formatted data and system overview. Update frontend components to utilize new APIs for enhanced data display and management.
* chore: bump crate rand version to 0.9.2
* feat: Implement enhanced traffic monitoring system with data compression and sampling
- Introduced `useTrafficMonitorEnhanced` hook for advanced traffic data management.
- Added `TrafficDataSampler` class for handling raw and compressed traffic data.
- Implemented reference counting to manage data collection based on component usage.
- Enhanced data validation with `SystemMonitorValidator` for API responses.
- Created diagnostic tools for monitoring performance and error tracking.
- Updated existing hooks to utilize the new enhanced monitoring features.
- Added utility functions for generating and formatting diagnostic reports.
* feat(ipc): improve URL encoding and error handling for IPC requests
- Add percent-encoding for URL paths to handle special characters properly
- Enhance error handling in update_proxy with proper logging
- Remove excessive debug logging to reduce noise
- Update kode-bridge dependency to v0.1.5
- Fix JSON parsing error handling in PUT requests
Changes include:
- Proper URL encoding for connection IDs, proxy names, and test URLs
- Enhanced error handling with fallback responses in updateProxy
- Comment out verbose debug logs in traffic monitoring and data validation
- Update dependency version for improved IPC functionality
* feat: major improvements in architecture, traffic monitoring, and data validation
* Refactor traffic graph components: Replace EnhancedTrafficGraph with EnhancedCanvasTrafficGraph, improve rendering performance, and enhance visual elements. Remove deprecated code and ensure compatibility with global data management.
* chore: update UPDATELOG.md for v2.4.0 release, refine traffic monitoring system details, and enhance IPC functionality
* chore: update UPDATELOG.md to reflect removal of deprecated MihomoManager and unify IPC control
* refactor: remove global traffic service testing method from cmds.ts
* Update src/components/home/enhanced-canvas-traffic-graph.tsx
* Update src/hooks/use-traffic-monitor-enhanced.ts
* Update src/components/layout/layout-traffic.tsx
* refactor: remove debug state management from LayoutTraffic component
---------
2025-07-24 00:47:42 +08:00
|
|
|
module::lightweight::is_in_lightweight_mode,
|
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
|
|
|
singleton_lazy,
|
2025-08-29 23:51:09 +08:00
|
|
|
utils::{dirs::find_target_icons, i18n::t},
|
2025-09-08 21:48:09 +08:00
|
|
|
Type,
|
2024-02-24 11:25:22 +08:00
|
|
|
};
|
2024-12-28 08:12:46 +03:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
use super::handle;
|
2022-11-14 01:26:33 +08:00
|
|
|
use anyhow::Result;
|
2025-08-26 01:49:51 +08:00
|
|
|
use futures::future::join_all;
|
2024-12-31 06:27:29 +08:00
|
|
|
use parking_lot::Mutex;
|
2025-05-26 16:08:16 +08:00
|
|
|
use std::{
|
|
|
|
|
fs,
|
|
|
|
|
sync::atomic::{AtomicBool, Ordering},
|
|
|
|
|
time::{Duration, Instant},
|
|
|
|
|
};
|
2024-09-04 08:54:15 +08:00
|
|
|
use tauri::{
|
2025-03-12 13:28:04 +08:00
|
|
|
menu::{CheckMenuItem, IsMenuItem, MenuEvent, MenuItem, PredefinedMenuItem, Submenu},
|
2025-03-12 13:04:15 +08:00
|
|
|
tray::{MouseButton, MouseButtonState, TrayIconEvent},
|
2025-09-08 21:48:09 +08:00
|
|
|
AppHandle, Wry,
|
2024-09-04 08:54:15 +08:00
|
|
|
};
|
2024-09-23 16:31:58 +08:00
|
|
|
|
2025-03-27 11:12:08 +08:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
struct TrayState {}
|
|
|
|
|
|
2025-06-22 15:42:01 +08:00
|
|
|
// 托盘点击防抖机制
|
|
|
|
|
static TRAY_CLICK_DEBOUNCE: OnceCell<Mutex<Instant>> = OnceCell::new();
|
|
|
|
|
const TRAY_CLICK_DEBOUNCE_MS: u64 = 300;
|
|
|
|
|
|
|
|
|
|
fn get_tray_click_debounce() -> &'static Mutex<Instant> {
|
|
|
|
|
TRAY_CLICK_DEBOUNCE.get_or_init(|| Mutex::new(Instant::now() - Duration::from_secs(1)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn should_handle_tray_click() -> bool {
|
|
|
|
|
let debounce_lock = get_tray_click_debounce();
|
|
|
|
|
let mut last_click = debounce_lock.lock();
|
|
|
|
|
let now = Instant::now();
|
|
|
|
|
|
|
|
|
|
if now.duration_since(*last_click) >= Duration::from_millis(TRAY_CLICK_DEBOUNCE_MS) {
|
|
|
|
|
*last_click = now;
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
log::debug!(target: "app", "托盘点击被防抖机制忽略,距离上次点击 {:?}ms",
|
|
|
|
|
now.duration_since(*last_click).as_millis());
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-31 06:27:29 +08:00
|
|
|
#[cfg(target_os = "macos")]
|
2024-12-25 02:11:07 +08:00
|
|
|
pub struct Tray {
|
2025-05-25 21:34:48 +08:00
|
|
|
last_menu_update: Mutex<Option<Instant>>,
|
|
|
|
|
menu_updating: AtomicBool,
|
2024-12-25 02:11:07 +08:00
|
|
|
}
|
2022-10-28 00:40:29 +08:00
|
|
|
|
2024-12-31 06:27:29 +08:00
|
|
|
#[cfg(not(target_os = "macos"))]
|
2025-05-25 21:34:48 +08:00
|
|
|
pub struct Tray {
|
|
|
|
|
last_menu_update: Mutex<Option<Instant>>,
|
|
|
|
|
menu_updating: AtomicBool,
|
|
|
|
|
}
|
2025-03-27 11:12:08 +08:00
|
|
|
|
|
|
|
|
impl TrayState {
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn get_common_tray_icon() -> (bool, Vec<u8>) {
|
|
|
|
|
let verge = Config::verge().await.latest_ref().clone();
|
2025-03-28 11:43:21 +08:00
|
|
|
let is_common_tray_icon = verge.common_tray_icon.unwrap_or(false);
|
2025-09-08 21:48:09 +08:00
|
|
|
if is_common_tray_icon {
|
|
|
|
|
if let Ok(Some(common_icon_path)) = find_target_icons("common") {
|
|
|
|
|
if let Ok(icon_data) = fs::read(common_icon_path) {
|
|
|
|
|
return (true, icon_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-27 11:12:08 +08:00
|
|
|
}
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
{
|
2025-03-28 11:43:21 +08:00
|
|
|
let tray_icon_colorful = verge.tray_icon.unwrap_or("monochrome".to_string());
|
2025-03-27 11:12:08 +08:00
|
|
|
if tray_icon_colorful == "monochrome" {
|
2025-03-27 12:04:52 +08:00
|
|
|
(
|
|
|
|
|
false,
|
|
|
|
|
include_bytes!("../../../icons/tray-icon-mono.ico").to_vec(),
|
|
|
|
|
)
|
2025-03-27 11:12:08 +08:00
|
|
|
} else {
|
2025-03-27 12:04:52 +08:00
|
|
|
(
|
|
|
|
|
false,
|
|
|
|
|
include_bytes!("../../../icons/tray-icon.ico").to_vec(),
|
|
|
|
|
)
|
2025-03-27 11:12:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
{
|
2025-03-27 12:04:52 +08:00
|
|
|
(
|
|
|
|
|
false,
|
|
|
|
|
include_bytes!("../../../icons/tray-icon.ico").to_vec(),
|
|
|
|
|
)
|
2025-03-27 11:12:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn get_sysproxy_tray_icon() -> (bool, Vec<u8>) {
|
|
|
|
|
let verge = Config::verge().await.latest_ref().clone();
|
2025-03-28 11:43:21 +08:00
|
|
|
let is_sysproxy_tray_icon = verge.sysproxy_tray_icon.unwrap_or(false);
|
2025-09-08 21:48:09 +08:00
|
|
|
if is_sysproxy_tray_icon {
|
|
|
|
|
if let Ok(Some(sysproxy_icon_path)) = find_target_icons("sysproxy") {
|
|
|
|
|
if let Ok(icon_data) = fs::read(sysproxy_icon_path) {
|
|
|
|
|
return (true, icon_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-27 11:12:08 +08:00
|
|
|
}
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
{
|
|
|
|
|
let tray_icon_colorful = verge.tray_icon.clone().unwrap_or("monochrome".to_string());
|
|
|
|
|
if tray_icon_colorful == "monochrome" {
|
2025-03-27 12:04:52 +08:00
|
|
|
(
|
|
|
|
|
false,
|
2025-06-22 15:31:59 +08:00
|
|
|
include_bytes!("../../../icons/tray-icon-sys-mono-new.ico").to_vec(),
|
2025-03-27 12:04:52 +08:00
|
|
|
)
|
2025-03-27 11:12:08 +08:00
|
|
|
} else {
|
2025-03-27 12:04:52 +08:00
|
|
|
(
|
|
|
|
|
false,
|
|
|
|
|
include_bytes!("../../../icons/tray-icon-sys.ico").to_vec(),
|
|
|
|
|
)
|
2025-03-27 11:12:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
{
|
2025-03-27 12:04:52 +08:00
|
|
|
(
|
|
|
|
|
false,
|
|
|
|
|
include_bytes!("../../../icons/tray-icon-sys.ico").to_vec(),
|
|
|
|
|
)
|
2025-03-27 11:12:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn get_tun_tray_icon() -> (bool, Vec<u8>) {
|
|
|
|
|
let verge = Config::verge().await.latest_ref().clone();
|
2025-03-28 11:43:21 +08:00
|
|
|
let is_tun_tray_icon = verge.tun_tray_icon.unwrap_or(false);
|
2025-09-08 21:48:09 +08:00
|
|
|
if is_tun_tray_icon {
|
|
|
|
|
if let Ok(Some(tun_icon_path)) = find_target_icons("tun") {
|
|
|
|
|
if let Ok(icon_data) = fs::read(tun_icon_path) {
|
|
|
|
|
return (true, icon_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-27 11:12:08 +08:00
|
|
|
}
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
{
|
|
|
|
|
let tray_icon_colorful = verge.tray_icon.clone().unwrap_or("monochrome".to_string());
|
|
|
|
|
if tray_icon_colorful == "monochrome" {
|
2025-03-27 12:04:52 +08:00
|
|
|
(
|
|
|
|
|
false,
|
2025-06-22 15:31:59 +08:00
|
|
|
include_bytes!("../../../icons/tray-icon-tun-mono-new.ico").to_vec(),
|
2025-03-27 12:04:52 +08:00
|
|
|
)
|
2025-03-27 11:12:08 +08:00
|
|
|
} else {
|
2025-03-27 12:04:52 +08:00
|
|
|
(
|
|
|
|
|
false,
|
|
|
|
|
include_bytes!("../../../icons/tray-icon-tun.ico").to_vec(),
|
|
|
|
|
)
|
2025-03-27 11:12:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
{
|
2025-03-27 12:04:52 +08:00
|
|
|
(
|
|
|
|
|
false,
|
|
|
|
|
include_bytes!("../../../icons/tray-icon-tun.ico").to_vec(),
|
|
|
|
|
)
|
2025-03-27 11:12:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-31 06:27:29 +08:00
|
|
|
|
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
|
|
|
impl Default for Tray {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Tray {
|
2025-05-25 21:34:48 +08:00
|
|
|
last_menu_update: Mutex::new(None),
|
|
|
|
|
menu_updating: AtomicBool::new(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
|
|
|
}
|
2024-12-25 02:11:07 +08:00
|
|
|
}
|
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
|
|
|
}
|
2024-12-25 02:11:07 +08:00
|
|
|
|
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
|
|
|
// Use simplified singleton_lazy macro
|
|
|
|
|
singleton_lazy!(Tray, TRAY, Tray::default);
|
|
|
|
|
|
|
|
|
|
impl Tray {
|
2025-08-29 23:51:09 +08:00
|
|
|
pub async fn init(&self) -> Result<()> {
|
|
|
|
|
let app_handle = handle::Handle::global()
|
|
|
|
|
.app_handle()
|
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Failed to get app handle for tray initialization"))?;
|
|
|
|
|
self.create_tray_from_handle(&app_handle).await?;
|
2024-12-25 02:11:07 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 09:48:49 +08:00
|
|
|
/// 更新托盘点击行为
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn update_click_behavior(&self) -> Result<()> {
|
2025-08-18 02:02:25 +08:00
|
|
|
let app_handle = handle::Handle::global()
|
|
|
|
|
.app_handle()
|
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Failed to get app handle for tray update"))?;
|
2025-08-26 01:49:51 +08:00
|
|
|
let tray_event = { Config::verge().await.latest_ref().tray_event.clone() };
|
2025-03-13 09:48:49 +08:00
|
|
|
let tray_event: String = tray_event.unwrap_or("main_window".into());
|
2025-08-18 02:02:25 +08:00
|
|
|
let tray = app_handle
|
|
|
|
|
.tray_by_id("main")
|
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Failed to get main tray"))?;
|
2025-03-13 09:48:49 +08:00
|
|
|
match tray_event.as_str() {
|
|
|
|
|
"tray_menu" => tray.set_show_menu_on_left_click(true)?,
|
|
|
|
|
_ => tray.set_show_menu_on_left_click(false)?,
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 20:00:33 +08:00
|
|
|
/// 更新托盘菜单
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn update_menu(&self) -> Result<()> {
|
2025-06-18 20:00:33 +08:00
|
|
|
// 调整最小更新间隔,确保状态及时刷新
|
|
|
|
|
const MIN_UPDATE_INTERVAL: Duration = Duration::from_millis(100);
|
2025-05-25 21:34:48 +08:00
|
|
|
|
2025-06-18 20:00:33 +08:00
|
|
|
// 检查是否正在更新
|
2025-05-25 21:34:48 +08:00
|
|
|
if self.menu_updating.load(Ordering::Acquire) {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 20:00:33 +08:00
|
|
|
// 检查更新频率,但允许重要事件跳过频率限制
|
|
|
|
|
let should_force_update = match std::thread::current().name() {
|
|
|
|
|
Some("main") => true,
|
|
|
|
|
_ => {
|
|
|
|
|
let last_update = self.last_menu_update.lock();
|
|
|
|
|
if let Some(last_time) = *last_update {
|
|
|
|
|
last_time.elapsed() >= MIN_UPDATE_INTERVAL
|
|
|
|
|
} else {
|
|
|
|
|
true
|
2025-05-25 21:34:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-06-18 20:00:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !should_force_update {
|
|
|
|
|
return Ok(());
|
2025-05-25 21:34:48 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-10 01:25:50 +08:00
|
|
|
let app_handle = match handle::Handle::global().app_handle() {
|
|
|
|
|
Some(handle) => handle,
|
|
|
|
|
None => {
|
|
|
|
|
log::warn!(target: "app", "更新托盘菜单失败: app_handle不存在");
|
2025-06-18 20:00:33 +08:00
|
|
|
return Ok(());
|
2025-05-10 01:25:50 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-25 21:34:48 +08:00
|
|
|
// 设置更新状态
|
|
|
|
|
self.menu_updating.store(true, Ordering::Release);
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
let result = self.update_menu_internal(&app_handle).await;
|
2025-05-25 21:34:48 +08:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut last_update = self.last_menu_update.lock();
|
|
|
|
|
*last_update = Some(Instant::now());
|
|
|
|
|
}
|
|
|
|
|
self.menu_updating.store(false, Ordering::Release);
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
}
|
2025-06-18 20:00:33 +08:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
async fn update_menu_internal(&self, app_handle: &AppHandle) -> Result<()> {
|
|
|
|
|
let verge = Config::verge().await.latest_ref().clone();
|
2024-12-24 04:52:14 +08:00
|
|
|
let system_proxy = verge.enable_system_proxy.as_ref().unwrap_or(&false);
|
|
|
|
|
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
|
2022-11-14 01:26:33 +08:00
|
|
|
let mode = {
|
2022-11-16 01:26:41 +08:00
|
|
|
Config::clash()
|
2025-08-26 01:49:51 +08:00
|
|
|
.await
|
2025-07-04 22:43:23 +08:00
|
|
|
.latest_ref()
|
2022-11-16 01:26:41 +08:00
|
|
|
.0
|
2022-11-14 01:26:33 +08:00
|
|
|
.get("mode")
|
|
|
|
|
.map(|val| val.as_str().unwrap_or("rule"))
|
|
|
|
|
.unwrap_or("rule")
|
|
|
|
|
.to_owned()
|
|
|
|
|
};
|
2025-02-08 09:36:04 +08:00
|
|
|
let profile_uid_and_name = Config::profiles()
|
2025-08-26 01:49:51 +08:00
|
|
|
.await
|
2025-07-04 22:43:23 +08:00
|
|
|
.data_mut()
|
2025-02-08 09:36:04 +08:00
|
|
|
.all_profile_uid_and_name()
|
2025-03-13 12:51:20 +08:00
|
|
|
.unwrap_or_default();
|
2025-04-21 04:33:56 +08:00
|
|
|
let is_lightweight_mode = is_in_lightweight_mode();
|
2022-10-28 00:40:29 +08:00
|
|
|
|
2025-08-31 14:20:57 +08:00
|
|
|
// 获取代理节点
|
2025-09-04 15:32:46 +08:00
|
|
|
let proxy_nodes_data = cmd::get_proxies().await.unwrap_or_else(|e| {
|
|
|
|
|
logging!(
|
|
|
|
|
error,
|
|
|
|
|
Type::Cmd,
|
|
|
|
|
"Failed to fetch proxies for tray menu: {e}"
|
|
|
|
|
);
|
|
|
|
|
serde_json::Value::Object(serde_json::Map::new())
|
|
|
|
|
});
|
2025-08-31 14:20:57 +08:00
|
|
|
|
2025-05-10 01:25:50 +08:00
|
|
|
match app_handle.tray_by_id("main") {
|
|
|
|
|
Some(tray) => {
|
2025-08-26 01:49:51 +08:00
|
|
|
let _ = tray.set_menu(Some(
|
|
|
|
|
create_tray_menu(
|
|
|
|
|
app_handle,
|
|
|
|
|
Some(mode.as_str()),
|
|
|
|
|
*system_proxy,
|
|
|
|
|
*tun_mode,
|
|
|
|
|
profile_uid_and_name,
|
|
|
|
|
is_lightweight_mode,
|
2025-08-31 14:20:57 +08:00
|
|
|
proxy_nodes_data,
|
2025-08-26 01:49:51 +08:00
|
|
|
)
|
|
|
|
|
.await?,
|
|
|
|
|
));
|
2025-05-25 21:34:48 +08:00
|
|
|
log::debug!(target: "app", "托盘菜单更新成功");
|
2025-05-10 01:25:50 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
log::warn!(target: "app", "更新托盘菜单失败: 托盘不存在");
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-24 04:52:14 +08:00
|
|
|
}
|
2024-05-15 12:00:38 +08:00
|
|
|
|
2024-12-24 04:52:14 +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 update_icon(&self, _rate: Option<Rate>) -> Result<()> {
|
2025-05-10 01:25:50 +08:00
|
|
|
let app_handle = match handle::Handle::global().app_handle() {
|
|
|
|
|
Some(handle) => handle,
|
|
|
|
|
None => {
|
|
|
|
|
log::warn!(target: "app", "更新托盘图标失败: app_handle不存在");
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let tray = match app_handle.tray_by_id("main") {
|
|
|
|
|
Some(tray) => tray,
|
|
|
|
|
None => {
|
|
|
|
|
log::warn!(target: "app", "更新托盘图标失败: 托盘不存在");
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
let verge = Config::verge().await.latest_ref().clone();
|
2025-03-27 11:12:08 +08:00
|
|
|
let system_mode = verge.enable_system_proxy.as_ref().unwrap_or(&false);
|
2024-12-24 04:52:14 +08:00
|
|
|
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
|
2024-12-24 06:03:23 +08:00
|
|
|
|
2025-06-22 16:28:06 +08:00
|
|
|
let (_is_custom_icon, icon_bytes) = match (*system_mode, *tun_mode) {
|
2025-08-26 01:49:51 +08:00
|
|
|
(true, true) => TrayState::get_tun_tray_icon().await,
|
|
|
|
|
(true, false) => TrayState::get_sysproxy_tray_icon().await,
|
|
|
|
|
(false, true) => TrayState::get_tun_tray_icon().await,
|
|
|
|
|
(false, false) => TrayState::get_common_tray_icon().await,
|
2024-09-04 08:54:15 +08:00
|
|
|
};
|
2025-03-22 23:00:45 +08:00
|
|
|
|
2025-06-11 00:19:06 +08:00
|
|
|
let colorful = verge.tray_icon.clone().unwrap_or("monochrome".to_string());
|
|
|
|
|
let is_colorful = colorful == "colorful";
|
|
|
|
|
|
2025-06-22 16:28:06 +08:00
|
|
|
let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&icon_bytes)?));
|
|
|
|
|
let _ = tray.set_icon_as_template(!is_colorful);
|
2025-06-11 00:19:06 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-10-25 13:58:22 +08:00
|
|
|
|
2025-06-11 00:19:06 +08:00
|
|
|
#[cfg(not(target_os = "macos"))]
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn update_icon(&self, _rate: Option<Rate>) -> Result<()> {
|
2025-06-11 00:19:06 +08:00
|
|
|
let app_handle = match handle::Handle::global().app_handle() {
|
|
|
|
|
Some(handle) => handle,
|
|
|
|
|
None => {
|
|
|
|
|
log::warn!(target: "app", "更新托盘图标失败: app_handle不存在");
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let tray = match app_handle.tray_by_id("main") {
|
|
|
|
|
Some(tray) => tray,
|
|
|
|
|
None => {
|
|
|
|
|
log::warn!(target: "app", "更新托盘图标失败: 托盘不存在");
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
let verge = Config::verge().await.latest_ref().clone();
|
2025-06-11 00:19:06 +08:00
|
|
|
let system_mode = verge.enable_system_proxy.as_ref().unwrap_or(&false);
|
|
|
|
|
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
|
|
|
|
|
|
2025-06-11 00:51:34 +08:00
|
|
|
let (_is_custom_icon, icon_bytes) = match (*system_mode, *tun_mode) {
|
2025-08-26 01:49:51 +08:00
|
|
|
(true, true) => TrayState::get_tun_tray_icon().await,
|
|
|
|
|
(true, false) => TrayState::get_sysproxy_tray_icon().await,
|
|
|
|
|
(false, true) => TrayState::get_tun_tray_icon().await,
|
|
|
|
|
(false, false) => TrayState::get_common_tray_icon().await,
|
2025-06-11 00:19:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&icon_bytes)?));
|
|
|
|
|
Ok(())
|
2024-12-24 04:52:14 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-18 20:00:33 +08:00
|
|
|
/// 更新托盘显示状态的函数
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn update_tray_display(&self) -> Result<()> {
|
2025-08-18 02:02:25 +08:00
|
|
|
let app_handle = handle::Handle::global()
|
|
|
|
|
.app_handle()
|
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Failed to get app handle for tray update"))?;
|
|
|
|
|
let _tray = app_handle
|
|
|
|
|
.tray_by_id("main")
|
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Failed to get main tray"))?;
|
2025-06-18 20:00:33 +08:00
|
|
|
|
|
|
|
|
// 更新菜单
|
2025-08-26 01:49:51 +08:00
|
|
|
self.update_menu().await?;
|
2025-06-18 20:00:33 +08:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 04:52:14 +08:00
|
|
|
/// 更新托盘提示
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn update_tooltip(&self) -> Result<()> {
|
2025-05-10 01:25:50 +08:00
|
|
|
let app_handle = match handle::Handle::global().app_handle() {
|
|
|
|
|
Some(handle) => handle,
|
|
|
|
|
None => {
|
|
|
|
|
log::warn!(target: "app", "更新托盘提示失败: app_handle不存在");
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
let verge = Config::verge().await.latest_ref().clone();
|
2024-12-24 04:52:14 +08:00
|
|
|
let system_proxy = verge.enable_system_proxy.as_ref().unwrap_or(&false);
|
|
|
|
|
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
|
2024-09-02 19:33:17 +08:00
|
|
|
|
2024-09-04 08:54:15 +08:00
|
|
|
let switch_map = {
|
|
|
|
|
let mut map = std::collections::HashMap::new();
|
|
|
|
|
map.insert(true, "on");
|
|
|
|
|
map.insert(false, "off");
|
|
|
|
|
map
|
|
|
|
|
};
|
2024-09-02 19:33:17 +08:00
|
|
|
|
2024-09-04 08:54:15 +08:00
|
|
|
let mut current_profile_name = "None".to_string();
|
2025-08-26 01:49:51 +08:00
|
|
|
{
|
|
|
|
|
let profiles = Config::profiles().await;
|
|
|
|
|
let profiles = profiles.latest_ref();
|
2025-09-08 21:48:09 +08:00
|
|
|
if let Some(current_profile_uid) = profiles.get_current() {
|
|
|
|
|
if let Ok(profile) = profiles.get_item(¤t_profile_uid) {
|
|
|
|
|
current_profile_name = match &profile.name {
|
|
|
|
|
Some(profile_name) => profile_name.to_string(),
|
|
|
|
|
None => current_profile_name,
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-05-10 01:25:50 +08:00
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get localized strings before using them
|
|
|
|
|
let sys_proxy_text = t("SysProxy").await;
|
|
|
|
|
let tun_text = t("TUN").await;
|
|
|
|
|
let profile_text = t("Profile").await;
|
2024-09-02 19:33:17 +08:00
|
|
|
|
2025-08-29 23:51:09 +08:00
|
|
|
let version = env!("CARGO_PKG_VERSION");
|
2025-05-10 01:25:50 +08:00
|
|
|
if let Some(tray) = app_handle.tray_by_id("main") {
|
|
|
|
|
let _ = tray.set_tooltip(Some(&format!(
|
|
|
|
|
"Clash Verge {version}\n{}: {}\n{}: {}\n{}: {}",
|
2025-08-26 01:49:51 +08:00
|
|
|
sys_proxy_text,
|
2025-05-10 01:25:50 +08:00
|
|
|
switch_map[system_proxy],
|
2025-08-26 01:49:51 +08:00
|
|
|
tun_text,
|
2025-05-10 01:25:50 +08:00
|
|
|
switch_map[tun_mode],
|
2025-08-26 01:49:51 +08:00
|
|
|
profile_text,
|
2025-05-10 01:25:50 +08:00
|
|
|
current_profile_name
|
|
|
|
|
)));
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!(target: "app", "更新托盘提示失败: 托盘不存在");
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-12 11:37:23 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-12-24 04:52:14 +08:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn update_part(&self) -> Result<()> {
|
|
|
|
|
// self.update_menu().await?;
|
2025-06-18 20:00:33 +08:00
|
|
|
// 更新轻量模式显示状态
|
2025-08-26 01:49:51 +08:00
|
|
|
self.update_tray_display().await?;
|
|
|
|
|
self.update_icon(None).await?;
|
|
|
|
|
self.update_tooltip().await?;
|
2024-12-24 04:52:14 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-12-25 02:11:07 +08:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn create_tray_from_handle(&self, app_handle: &AppHandle) -> Result<()> {
|
2025-05-10 01:25:50 +08:00
|
|
|
log::info!(target: "app", "正在从AppHandle创建系统托盘");
|
|
|
|
|
|
|
|
|
|
// 获取图标
|
2025-08-26 01:49:51 +08:00
|
|
|
let icon_bytes = TrayState::get_common_tray_icon().await.1;
|
2025-05-10 01:25:50 +08:00
|
|
|
let icon = tauri::image::Image::from_bytes(&icon_bytes)?;
|
|
|
|
|
|
2025-06-11 01:04:56 +08:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
let builder = TrayIconBuilder::with_id("main")
|
|
|
|
|
.icon(icon)
|
|
|
|
|
.icon_as_template(false);
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
|
|
|
|
let show_menu_on_left_click = {
|
|
|
|
|
let tray_event = { Config::verge().await.latest_ref().tray_event.clone() };
|
|
|
|
|
let tray_event: String = tray_event.unwrap_or("main_window".into());
|
|
|
|
|
tray_event.as_str() == "tray_menu"
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-11 01:04:56 +08:00
|
|
|
#[cfg(not(target_os = "linux"))]
|
2025-05-10 01:25:50 +08:00
|
|
|
let mut builder = TrayIconBuilder::with_id("main")
|
|
|
|
|
.icon(icon)
|
|
|
|
|
.icon_as_template(false);
|
|
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
|
|
|
|
{
|
2025-08-26 01:49:51 +08:00
|
|
|
if !show_menu_on_left_click {
|
2025-05-10 01:25:50 +08:00
|
|
|
builder = builder.show_menu_on_left_click(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
let tray = builder.build(app_handle)?;
|
|
|
|
|
|
|
|
|
|
tray.on_tray_icon_event(|_app_handle, event| {
|
|
|
|
|
AsyncHandler::spawn(|| async move {
|
|
|
|
|
let tray_event = { Config::verge().await.latest_ref().tray_event.clone() };
|
|
|
|
|
let tray_event: String = tray_event.unwrap_or("main_window".into());
|
|
|
|
|
log::debug!(target: "app", "tray event: {tray_event:?}");
|
|
|
|
|
|
|
|
|
|
if let TrayIconEvent::Click {
|
|
|
|
|
button: MouseButton::Left,
|
|
|
|
|
button_state: MouseButtonState::Down,
|
|
|
|
|
..
|
|
|
|
|
} = event
|
|
|
|
|
{
|
|
|
|
|
// 添加防抖检查,防止快速连击
|
|
|
|
|
if !should_handle_tray_click() {
|
|
|
|
|
return;
|
2025-05-10 01:25:50 +08:00
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
|
|
|
|
|
use std::future::Future;
|
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
|
|
let fut: Pin<Box<dyn Future<Output = ()> + Send>> = match tray_event.as_str() {
|
|
|
|
|
"system_proxy" => Box::pin(async move {
|
|
|
|
|
feat::toggle_system_proxy().await;
|
|
|
|
|
}),
|
|
|
|
|
"tun_mode" => Box::pin(async move {
|
|
|
|
|
feat::toggle_tun_mode(None).await;
|
|
|
|
|
}),
|
|
|
|
|
"main_window" => Box::pin(async move {
|
2025-09-09 18:50:24 +08:00
|
|
|
if !lightweight::exit_lightweight_mode().await {
|
|
|
|
|
WindowManager::toggle_main_window().await;
|
|
|
|
|
};
|
2025-08-26 01:49:51 +08:00
|
|
|
}),
|
|
|
|
|
_ => Box::pin(async move {}),
|
|
|
|
|
};
|
|
|
|
|
fut.await;
|
2025-05-10 01:25:50 +08:00
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
});
|
2025-05-10 01:25:50 +08:00
|
|
|
});
|
|
|
|
|
tray.on_menu_event(on_menu_event);
|
|
|
|
|
log::info!(target: "app", "系统托盘创建成功");
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-06-18 20:00:33 +08:00
|
|
|
|
|
|
|
|
// 托盘统一的状态更新函数
|
2025-08-26 01:49:51 +08:00
|
|
|
pub async fn update_all_states(&self) -> Result<()> {
|
2025-06-18 20:00:33 +08:00
|
|
|
// 确保所有状态更新完成
|
2025-08-26 01:49:51 +08:00
|
|
|
self.update_tray_display().await?;
|
|
|
|
|
// self.update_menu().await?;
|
|
|
|
|
self.update_icon(None).await?;
|
|
|
|
|
self.update_tooltip().await?;
|
2025-06-18 20:00:33 +08:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-09-04 08:54:15 +08:00
|
|
|
}
|
2022-10-28 00:40:29 +08:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
async fn create_tray_menu(
|
2024-09-04 08:54:15 +08:00
|
|
|
app_handle: &AppHandle,
|
|
|
|
|
mode: Option<&str>,
|
|
|
|
|
system_proxy_enabled: bool,
|
|
|
|
|
tun_mode_enabled: bool,
|
2025-02-08 09:36:04 +08:00
|
|
|
profile_uid_and_name: Vec<(String, String)>,
|
2025-04-21 04:33:56 +08:00
|
|
|
is_lightweight_mode: bool,
|
2025-08-31 14:20:57 +08:00
|
|
|
proxy_nodes_data: serde_json::Value,
|
2024-09-04 08:54:15 +08:00
|
|
|
) -> Result<tauri::menu::Menu<Wry>> {
|
|
|
|
|
let mode = mode.unwrap_or("");
|
2025-05-10 01:25:50 +08:00
|
|
|
|
2025-08-31 14:20:57 +08:00
|
|
|
// 获取当前配置文件的选中代理组信息
|
|
|
|
|
let current_profile_selected = {
|
2025-09-04 15:32:46 +08:00
|
|
|
let profiles_config = Config::profiles().await;
|
|
|
|
|
let profiles_ref = profiles_config.latest_ref();
|
|
|
|
|
profiles_ref
|
|
|
|
|
.get_current()
|
|
|
|
|
.and_then(|uid| profiles_ref.get_item(&uid).ok())
|
|
|
|
|
.and_then(|profile| profile.selected.clone())
|
|
|
|
|
.unwrap_or_default()
|
2025-08-31 14:20:57 +08:00
|
|
|
};
|
|
|
|
|
|
2025-08-29 23:51:09 +08:00
|
|
|
let version = env!("CARGO_PKG_VERSION");
|
2025-05-10 01:25:50 +08:00
|
|
|
|
2025-01-01 08:14:15 +08:00
|
|
|
let hotkeys = Config::verge()
|
2025-08-26 01:49:51 +08:00
|
|
|
.await
|
2025-07-04 22:43:23 +08:00
|
|
|
.latest_ref()
|
2025-01-01 08:14:15 +08:00
|
|
|
.hotkeys
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|h| {
|
|
|
|
|
h.iter()
|
|
|
|
|
.filter_map(|item| {
|
|
|
|
|
let mut parts = item.split(',');
|
|
|
|
|
match (parts.next(), parts.next()) {
|
|
|
|
|
(Some(func), Some(key)) => Some((func.to_string(), key.to_string())),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect::<std::collections::HashMap<String, String>>()
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_default();
|
2025-03-12 13:28:04 +08:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
let profile_menu_items: Vec<CheckMenuItem<Wry>> = {
|
|
|
|
|
let futures = profile_uid_and_name
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(profile_uid, profile_name)| {
|
|
|
|
|
let app_handle = app_handle.clone();
|
|
|
|
|
let profile_uid = profile_uid.clone();
|
|
|
|
|
let profile_name = profile_name.clone();
|
|
|
|
|
async move {
|
|
|
|
|
let is_current_profile = Config::profiles()
|
|
|
|
|
.await
|
|
|
|
|
.data_mut()
|
|
|
|
|
.is_current_profile_index(profile_uid.to_string());
|
|
|
|
|
CheckMenuItem::with_id(
|
|
|
|
|
&app_handle,
|
|
|
|
|
format!("profiles_{profile_uid}"),
|
|
|
|
|
t(&profile_name).await,
|
|
|
|
|
true,
|
|
|
|
|
is_current_profile,
|
|
|
|
|
None::<&str>,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
let results = join_all(futures).await;
|
|
|
|
|
results.into_iter().collect::<Result<Vec<_>, _>>()?
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-31 14:20:57 +08:00
|
|
|
// 代理组子菜单
|
|
|
|
|
let proxy_submenus: Vec<Submenu<Wry>> = {
|
|
|
|
|
let mut submenus = Vec::new();
|
|
|
|
|
|
|
|
|
|
if let Some(proxies) = proxy_nodes_data.get("proxies").and_then(|v| v.as_object()) {
|
|
|
|
|
for (group_name, group_data) in proxies.iter() {
|
2025-09-04 15:32:46 +08:00
|
|
|
// Filter groups based on mode
|
|
|
|
|
let should_show = match mode {
|
|
|
|
|
"global" => group_name == "GLOBAL",
|
|
|
|
|
_ => group_name != "GLOBAL",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !should_show {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-08-31 14:20:57 +08:00
|
|
|
|
2025-09-04 15:32:46 +08:00
|
|
|
let Some(all_proxies) = group_data.get("all").and_then(|v| v.as_array()) else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let now_proxy = group_data.get("now").and_then(|v| v.as_str()).unwrap_or("");
|
|
|
|
|
|
|
|
|
|
// Create proxy items
|
|
|
|
|
let group_items: Vec<CheckMenuItem<Wry>> = all_proxies
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|proxy_name| proxy_name.as_str())
|
|
|
|
|
.filter_map(|proxy_str| {
|
|
|
|
|
let is_selected = proxy_str == now_proxy;
|
|
|
|
|
let item_id = format!("proxy_{}_{}", group_name, proxy_str);
|
|
|
|
|
|
|
|
|
|
// Get delay for display
|
|
|
|
|
let delay_text = proxies
|
|
|
|
|
.get(proxy_str)
|
|
|
|
|
.and_then(|p| p.get("history"))
|
|
|
|
|
.and_then(|h| h.as_array())
|
|
|
|
|
.and_then(|h| h.last())
|
|
|
|
|
.and_then(|r| r.get("delay"))
|
|
|
|
|
.and_then(|d| d.as_i64())
|
|
|
|
|
.map(|delay| match delay {
|
|
|
|
|
-1 => "-ms".to_string(),
|
|
|
|
|
delay if delay >= 10000 => "-ms".to_string(),
|
|
|
|
|
_ => format!("{}ms", delay),
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_else(|| "-ms".to_string());
|
|
|
|
|
|
|
|
|
|
let display_text = format!("{} | {}", proxy_str, delay_text);
|
|
|
|
|
|
|
|
|
|
CheckMenuItem::with_id(
|
2025-08-31 14:20:57 +08:00
|
|
|
app_handle,
|
2025-09-04 15:32:46 +08:00
|
|
|
item_id,
|
|
|
|
|
display_text,
|
2025-08-31 14:20:57 +08:00
|
|
|
true,
|
2025-09-04 15:32:46 +08:00
|
|
|
is_selected,
|
|
|
|
|
None::<&str>,
|
|
|
|
|
)
|
|
|
|
|
.map_err(|e| log::warn!(target: "app", "创建代理菜单项失败: {}", e))
|
|
|
|
|
.ok()
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
if group_items.is_empty() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine if group is active
|
|
|
|
|
let is_group_active = match mode {
|
|
|
|
|
"global" => group_name == "GLOBAL" && !now_proxy.is_empty(),
|
|
|
|
|
"direct" => false,
|
|
|
|
|
_ => {
|
|
|
|
|
current_profile_selected
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|s| s.name.as_deref() == Some(group_name))
|
|
|
|
|
&& !now_proxy.is_empty()
|
2025-08-31 14:20:57 +08:00
|
|
|
}
|
2025-09-04 15:32:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let group_display_name = if is_group_active {
|
|
|
|
|
format!("✓ {}", group_name)
|
|
|
|
|
} else {
|
|
|
|
|
group_name.to_string()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let group_items_refs: Vec<&dyn IsMenuItem<Wry>> = group_items
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|item| item as &dyn IsMenuItem<Wry>)
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
if let Ok(submenu) = Submenu::with_id_and_items(
|
|
|
|
|
app_handle,
|
|
|
|
|
format!("proxy_group_{}", group_name),
|
|
|
|
|
group_display_name,
|
|
|
|
|
true,
|
|
|
|
|
&group_items_refs,
|
|
|
|
|
) {
|
|
|
|
|
submenus.push(submenu);
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!(target: "app", "创建代理组子菜单失败: {}", group_name);
|
2025-08-31 14:20:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
submenus
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
// Pre-fetch all localized strings
|
|
|
|
|
let dashboard_text = t("Dashboard").await;
|
|
|
|
|
let rule_mode_text = t("Rule Mode").await;
|
|
|
|
|
let global_mode_text = t("Global Mode").await;
|
|
|
|
|
let direct_mode_text = t("Direct Mode").await;
|
|
|
|
|
let profiles_text = t("Profiles").await;
|
2025-08-31 14:20:57 +08:00
|
|
|
let proxies_text = t("Proxies").await;
|
2025-08-26 01:49:51 +08:00
|
|
|
let system_proxy_text = t("System Proxy").await;
|
|
|
|
|
let tun_mode_text = t("TUN Mode").await;
|
|
|
|
|
let lightweight_mode_text = t("LightWeight Mode").await;
|
|
|
|
|
let copy_env_text = t("Copy Env").await;
|
|
|
|
|
let conf_dir_text = t("Conf Dir").await;
|
|
|
|
|
let core_dir_text = t("Core Dir").await;
|
|
|
|
|
let logs_dir_text = t("Logs Dir").await;
|
|
|
|
|
let open_dir_text = t("Open Dir").await;
|
|
|
|
|
let restart_clash_text = t("Restart Clash Core").await;
|
|
|
|
|
let restart_app_text = t("Restart App").await;
|
|
|
|
|
let verge_version_text = t("Verge Version").await;
|
|
|
|
|
let more_text = t("More").await;
|
|
|
|
|
let exit_text = t("Exit").await;
|
|
|
|
|
|
|
|
|
|
// Convert to references only when needed
|
|
|
|
|
let profile_menu_items_refs: Vec<&dyn IsMenuItem<Wry>> = profile_menu_items
|
2025-02-08 09:36:04 +08:00
|
|
|
.iter()
|
|
|
|
|
.map(|item| item as &dyn IsMenuItem<Wry>)
|
|
|
|
|
.collect();
|
2024-09-04 08:54:15 +08:00
|
|
|
|
2024-09-13 04:12:25 +08:00
|
|
|
let open_window = &MenuItem::with_id(
|
|
|
|
|
app_handle,
|
|
|
|
|
"open_window",
|
2025-08-26 01:49:51 +08:00
|
|
|
dashboard_text,
|
2024-09-13 04:12:25 +08:00
|
|
|
true,
|
2025-01-01 08:14:15 +08:00
|
|
|
hotkeys.get("open_or_close_dashboard").map(|s| s.as_str()),
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-13 04:12:25 +08:00
|
|
|
|
2024-09-20 17:59:24 +08:00
|
|
|
let rule_mode = &CheckMenuItem::with_id(
|
2024-09-13 04:12:25 +08:00
|
|
|
app_handle,
|
|
|
|
|
"rule_mode",
|
2025-08-26 01:49:51 +08:00
|
|
|
rule_mode_text,
|
2024-09-13 04:12:25 +08:00
|
|
|
true,
|
2024-09-20 17:59:24 +08:00
|
|
|
mode == "rule",
|
2025-01-01 08:14:15 +08:00
|
|
|
hotkeys.get("clash_mode_rule").map(|s| s.as_str()),
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-13 04:12:25 +08:00
|
|
|
|
2024-09-20 17:59:24 +08:00
|
|
|
let global_mode = &CheckMenuItem::with_id(
|
2024-09-13 04:12:25 +08:00
|
|
|
app_handle,
|
|
|
|
|
"global_mode",
|
2025-08-26 01:49:51 +08:00
|
|
|
global_mode_text,
|
2024-09-13 04:12:25 +08:00
|
|
|
true,
|
2024-09-20 17:59:24 +08:00
|
|
|
mode == "global",
|
2025-01-01 08:14:15 +08:00
|
|
|
hotkeys.get("clash_mode_global").map(|s| s.as_str()),
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-13 04:12:25 +08:00
|
|
|
|
2024-09-20 17:59:24 +08:00
|
|
|
let direct_mode = &CheckMenuItem::with_id(
|
2024-09-13 04:12:25 +08:00
|
|
|
app_handle,
|
|
|
|
|
"direct_mode",
|
2025-08-26 01:49:51 +08:00
|
|
|
direct_mode_text,
|
2024-09-13 04:12:25 +08:00
|
|
|
true,
|
2024-09-20 17:59:24 +08:00
|
|
|
mode == "direct",
|
2025-01-01 08:14:15 +08:00
|
|
|
hotkeys.get("clash_mode_direct").map(|s| s.as_str()),
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-13 04:12:25 +08:00
|
|
|
|
2025-02-05 08:52:47 +08:00
|
|
|
let profiles = &Submenu::with_id_and_items(
|
2025-03-12 13:28:04 +08:00
|
|
|
app_handle,
|
|
|
|
|
"profiles",
|
2025-08-26 01:49:51 +08:00
|
|
|
profiles_text,
|
2025-03-12 13:28:04 +08:00
|
|
|
true,
|
2025-08-26 01:49:51 +08:00
|
|
|
&profile_menu_items_refs,
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2025-02-05 08:52:47 +08:00
|
|
|
|
2025-08-31 14:20:57 +08:00
|
|
|
// 创建代理主菜单
|
|
|
|
|
let proxies_submenu = if !proxy_submenus.is_empty() {
|
|
|
|
|
let proxy_submenu_refs: Vec<&dyn IsMenuItem<Wry>> = proxy_submenus
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|submenu| submenu as &dyn IsMenuItem<Wry>)
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
Some(Submenu::with_id_and_items(
|
|
|
|
|
app_handle,
|
|
|
|
|
"proxies",
|
|
|
|
|
proxies_text,
|
|
|
|
|
true,
|
|
|
|
|
&proxy_submenu_refs,
|
|
|
|
|
)?)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-20 17:59:24 +08:00
|
|
|
let system_proxy = &CheckMenuItem::with_id(
|
2024-09-13 04:12:25 +08:00
|
|
|
app_handle,
|
|
|
|
|
"system_proxy",
|
2025-08-26 01:49:51 +08:00
|
|
|
system_proxy_text,
|
2024-09-13 04:12:25 +08:00
|
|
|
true,
|
2024-09-20 17:59:24 +08:00
|
|
|
system_proxy_enabled,
|
2025-01-01 08:14:15 +08:00
|
|
|
hotkeys.get("toggle_system_proxy").map(|s| s.as_str()),
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-13 04:12:25 +08:00
|
|
|
|
2024-09-20 17:59:24 +08:00
|
|
|
let tun_mode = &CheckMenuItem::with_id(
|
2024-09-13 04:12:25 +08:00
|
|
|
app_handle,
|
|
|
|
|
"tun_mode",
|
2025-08-26 01:49:51 +08:00
|
|
|
tun_mode_text,
|
2024-09-13 04:12:25 +08:00
|
|
|
true,
|
2024-09-20 17:59:24 +08:00
|
|
|
tun_mode_enabled,
|
2025-01-01 08:14:15 +08:00
|
|
|
hotkeys.get("toggle_tun_mode").map(|s| s.as_str()),
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-13 04:12:25 +08:00
|
|
|
|
2025-04-21 04:33:56 +08:00
|
|
|
let lighteweight_mode = &CheckMenuItem::with_id(
|
2025-03-23 03:10:48 +08:00
|
|
|
app_handle,
|
|
|
|
|
"entry_lightweight_mode",
|
2025-08-26 01:49:51 +08:00
|
|
|
lightweight_mode_text,
|
2025-03-23 03:10:48 +08:00
|
|
|
true,
|
2025-04-21 04:33:56 +08:00
|
|
|
is_lightweight_mode,
|
2025-03-23 03:10:48 +08:00
|
|
|
hotkeys.get("entry_lightweight_mode").map(|s| s.as_str()),
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2025-03-23 03:10:48 +08:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
let copy_env = &MenuItem::with_id(app_handle, "copy_env", copy_env_text, true, None::<&str>)?;
|
2024-09-13 04:12:25 +08:00
|
|
|
|
2024-09-12 05:45:15 +08:00
|
|
|
let open_app_dir = &MenuItem::with_id(
|
|
|
|
|
app_handle,
|
|
|
|
|
"open_app_dir",
|
2025-08-26 01:49:51 +08:00
|
|
|
conf_dir_text,
|
2024-09-12 05:45:15 +08:00
|
|
|
true,
|
|
|
|
|
None::<&str>,
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-12 05:45:15 +08:00
|
|
|
|
|
|
|
|
let open_core_dir = &MenuItem::with_id(
|
|
|
|
|
app_handle,
|
|
|
|
|
"open_core_dir",
|
2025-08-26 01:49:51 +08:00
|
|
|
core_dir_text,
|
2024-09-12 05:45:15 +08:00
|
|
|
true,
|
|
|
|
|
None::<&str>,
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-12 05:45:15 +08:00
|
|
|
|
|
|
|
|
let open_logs_dir = &MenuItem::with_id(
|
|
|
|
|
app_handle,
|
|
|
|
|
"open_logs_dir",
|
2025-08-26 01:49:51 +08:00
|
|
|
logs_dir_text,
|
2024-09-12 05:45:15 +08:00
|
|
|
true,
|
|
|
|
|
None::<&str>,
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2025-01-11 15:07:30 +08:00
|
|
|
|
2024-09-13 04:12:25 +08:00
|
|
|
let open_dir = &Submenu::with_id_and_items(
|
|
|
|
|
app_handle,
|
|
|
|
|
"open_dir",
|
2025-08-26 01:49:51 +08:00
|
|
|
open_dir_text,
|
2024-09-13 04:12:25 +08:00
|
|
|
true,
|
|
|
|
|
&[open_app_dir, open_core_dir, open_logs_dir],
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-12 05:45:15 +08:00
|
|
|
|
2024-09-04 08:54:15 +08:00
|
|
|
let restart_clash = &MenuItem::with_id(
|
|
|
|
|
app_handle,
|
|
|
|
|
"restart_clash",
|
2025-08-26 01:49:51 +08:00
|
|
|
restart_clash_text,
|
2024-09-04 08:54:15 +08:00
|
|
|
true,
|
|
|
|
|
None::<&str>,
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-04 08:54:15 +08:00
|
|
|
|
|
|
|
|
let restart_app = &MenuItem::with_id(
|
|
|
|
|
app_handle,
|
|
|
|
|
"restart_app",
|
2025-08-26 01:49:51 +08:00
|
|
|
restart_app_text,
|
2024-09-04 08:54:15 +08:00
|
|
|
true,
|
|
|
|
|
None::<&str>,
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-04 08:54:15 +08:00
|
|
|
|
|
|
|
|
let app_version = &MenuItem::with_id(
|
|
|
|
|
app_handle,
|
|
|
|
|
"app_version",
|
2025-08-26 01:49:51 +08:00
|
|
|
format!("{} {version}", verge_version_text),
|
2024-09-04 08:54:15 +08:00
|
|
|
true,
|
|
|
|
|
None::<&str>,
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-04 08:54:15 +08:00
|
|
|
|
2024-09-13 04:12:25 +08:00
|
|
|
let more = &Submenu::with_id_and_items(
|
|
|
|
|
app_handle,
|
|
|
|
|
"more",
|
2025-08-26 01:49:51 +08:00
|
|
|
more_text,
|
2024-09-13 04:12:25 +08:00
|
|
|
true,
|
|
|
|
|
&[restart_clash, restart_app, app_version],
|
2025-08-18 02:02:25 +08:00
|
|
|
)?;
|
2024-09-13 04:12:25 +08:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
let quit = &MenuItem::with_id(app_handle, "quit", exit_text, true, Some("CmdOrControl+Q"))?;
|
2024-09-13 04:12:25 +08:00
|
|
|
|
2025-08-18 02:02:25 +08:00
|
|
|
let separator = &PredefinedMenuItem::separator(app_handle)?;
|
2024-09-04 08:54:15 +08:00
|
|
|
|
2025-08-31 14:20:57 +08:00
|
|
|
// 动态构建菜单项
|
|
|
|
|
let mut menu_items: Vec<&dyn IsMenuItem<Wry>> = vec![
|
|
|
|
|
open_window,
|
|
|
|
|
separator,
|
|
|
|
|
rule_mode,
|
|
|
|
|
global_mode,
|
|
|
|
|
direct_mode,
|
|
|
|
|
separator,
|
|
|
|
|
profiles,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 如果有代理节点,添加代理节点菜单
|
|
|
|
|
if let Some(ref proxies_menu) = proxies_submenu {
|
|
|
|
|
menu_items.push(proxies_menu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
menu_items.extend_from_slice(&[
|
|
|
|
|
separator,
|
|
|
|
|
system_proxy as &dyn IsMenuItem<Wry>,
|
|
|
|
|
tun_mode as &dyn IsMenuItem<Wry>,
|
|
|
|
|
separator,
|
|
|
|
|
lighteweight_mode as &dyn IsMenuItem<Wry>,
|
|
|
|
|
copy_env as &dyn IsMenuItem<Wry>,
|
|
|
|
|
open_dir as &dyn IsMenuItem<Wry>,
|
|
|
|
|
more as &dyn IsMenuItem<Wry>,
|
|
|
|
|
separator,
|
|
|
|
|
quit as &dyn IsMenuItem<Wry>,
|
|
|
|
|
]);
|
|
|
|
|
|
2024-10-10 00:34:36 +08:00
|
|
|
let menu = tauri::menu::MenuBuilder::new(app_handle)
|
2025-08-31 14:20:57 +08:00
|
|
|
.items(&menu_items)
|
2025-08-18 02:02:25 +08:00
|
|
|
.build()?;
|
2024-09-04 08:54:15 +08:00
|
|
|
Ok(menu)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-09 23:11:02 +08:00
|
|
|
fn on_menu_event(_: &AppHandle, event: MenuEvent) {
|
2025-08-26 01:49:51 +08:00
|
|
|
AsyncHandler::spawn(|| async move {
|
|
|
|
|
match event.id.as_ref() {
|
|
|
|
|
mode @ ("rule_mode" | "global_mode" | "direct_mode") => {
|
|
|
|
|
let mode = &mode[0..mode.len() - 5]; // Removing the "_mode" suffix
|
|
|
|
|
logging!(
|
|
|
|
|
info,
|
|
|
|
|
Type::ProxyMode,
|
|
|
|
|
true,
|
|
|
|
|
"Switch Proxy Mode To: {}",
|
|
|
|
|
mode
|
|
|
|
|
);
|
|
|
|
|
feat::change_clash_mode(mode.into()).await; // Await async function
|
2025-06-22 15:42:01 +08:00
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
"open_window" => {
|
|
|
|
|
log::info!(target: "app", "托盘菜单点击: 打开窗口");
|
2025-06-22 15:42:01 +08:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
if !should_handle_tray_click() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-09-09 18:50:24 +08:00
|
|
|
if !lightweight::exit_lightweight_mode().await {
|
|
|
|
|
WindowManager::toggle_main_window().await;
|
|
|
|
|
};
|
2025-04-20 23:47:09 +08:00
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
"system_proxy" => {
|
|
|
|
|
feat::toggle_system_proxy().await; // Await async function
|
2025-06-22 15:42:01 +08:00
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
"tun_mode" => {
|
|
|
|
|
feat::toggle_tun_mode(None).await; // Await async function
|
|
|
|
|
}
|
|
|
|
|
"copy_env" => feat::copy_clash_env().await, // Await async function
|
|
|
|
|
"open_app_dir" => {
|
|
|
|
|
let _ = cmd::open_app_dir().await; // Await async function
|
|
|
|
|
}
|
|
|
|
|
"open_core_dir" => {
|
|
|
|
|
let _ = cmd::open_core_dir().await; // Await async function
|
2025-06-18 20:00:33 +08:00
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
"open_logs_dir" => {
|
|
|
|
|
let _ = cmd::open_logs_dir().await; // Await async function
|
|
|
|
|
}
|
|
|
|
|
"restart_clash" => feat::restart_clash_core().await, // Await async function
|
|
|
|
|
"restart_app" => feat::restart_app().await, // Await async function
|
|
|
|
|
"entry_lightweight_mode" => {
|
|
|
|
|
if !should_handle_tray_click() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-09-09 18:50:24 +08:00
|
|
|
lightweight::entry_lightweight_mode().await; // Await async function
|
2025-06-18 20:00:33 +08:00
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
"quit" => {
|
|
|
|
|
feat::quit().await; // Await async function
|
|
|
|
|
}
|
|
|
|
|
id if id.starts_with("profiles_") => {
|
|
|
|
|
let profile_index = &id["profiles_".len()..];
|
|
|
|
|
feat::toggle_proxy_profile(profile_index.into()).await; // Await async function
|
|
|
|
|
}
|
2025-08-31 14:20:57 +08:00
|
|
|
id if id.starts_with("proxy_") => {
|
|
|
|
|
// proxy_{group_name}_{proxy_name}
|
|
|
|
|
let parts: Vec<&str> = id.splitn(3, '_').collect();
|
|
|
|
|
|
|
|
|
|
if parts.len() == 3 && parts[0] == "proxy" {
|
|
|
|
|
let group_name = parts[1];
|
|
|
|
|
let proxy_name = parts[2];
|
|
|
|
|
|
|
|
|
|
match cmd::proxy::update_proxy_and_sync(
|
|
|
|
|
group_name.to_string(),
|
|
|
|
|
proxy_name.to_string(),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(_) => {
|
2025-09-04 15:32:46 +08:00
|
|
|
log::info!(target: "app", "切换代理成功: {} -> {}", group_name, proxy_name);
|
2025-08-31 14:20:57 +08:00
|
|
|
}
|
|
|
|
|
Err(e) => {
|
2025-09-04 15:32:46 +08:00
|
|
|
log::error!(target: "app", "切换代理失败: {} -> {}, 错误: {:?}", group_name, proxy_name, e);
|
2025-08-31 14:20:57 +08:00
|
|
|
|
2025-09-04 15:32:46 +08:00
|
|
|
// Fallback to IPC update
|
2025-09-06 14:10:09 +08:00
|
|
|
if (IpcManager::global()
|
2025-08-31 14:20:57 +08:00
|
|
|
.update_proxy(group_name, proxy_name)
|
2025-09-06 14:10:09 +08:00
|
|
|
.await)
|
|
|
|
|
.is_ok()
|
2025-08-31 14:20:57 +08:00
|
|
|
{
|
2025-09-04 15:32:46 +08:00
|
|
|
log::info!(target: "app", "代理切换回退成功: {} -> {}", group_name, proxy_name);
|
|
|
|
|
|
|
|
|
|
if let Some(app_handle) = handle::Handle::global().app_handle() {
|
|
|
|
|
let _ = app_handle.emit("verge://force-refresh-proxies", ());
|
2025-08-31 14:20:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
_ => {}
|
2025-06-18 20:00:33 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
// Ensure tray state update is awaited and properly handled
|
|
|
|
|
if let Err(e) = Tray::global().update_all_states().await {
|
|
|
|
|
log::warn!(target: "app", "更新托盘状态失败: {e}");
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-10-28 00:40:29 +08:00
|
|
|
}
|