From ad335ba0059e0572730af19c0f72e0dd09a4673d Mon Sep 17 00:00:00 2001 From: huzibaca Date: Tue, 31 Dec 2024 06:27:29 +0800 Subject: [PATCH] fix: unused code --- src-tauri/src/core/clash_api.rs | 1 + src-tauri/src/core/tray/mod.rs | 50 +++++++++++++++++---------- src-tauri/src/core/tray/speed_rate.rs | 33 ++++++++++-------- src-tauri/src/utils/help.rs | 1 + 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/src-tauri/src/core/clash_api.rs b/src-tauri/src/core/clash_api.rs index 55e6a633..5cad9130 100644 --- a/src-tauri/src/core/clash_api.rs +++ b/src-tauri/src/core/clash_api.rs @@ -123,6 +123,7 @@ pub fn parse_check_output(log: String) -> String { log } +#[cfg(target_os = "macos")] pub fn get_traffic_ws_url() -> Result { let (url, _) = clash_client_info()?; let ws_url = url.replace("http://", "ws://") + "/traffic"; diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index 70168be5..763979dc 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -1,7 +1,6 @@ -pub mod speed_rate; -use speed_rate::Rate; -pub use speed_rate::{SpeedRate, Traffic}; +use once_cell::sync::OnceCell; +pub mod speed_rate; use crate::utils::dirs; use crate::{ cmds, @@ -10,42 +9,57 @@ use crate::{ utils::resolve::{self, VERSION}, }; use anyhow::Result; +#[cfg(target_os = "macos")] use futures::StreamExt; -use once_cell::sync::OnceCell; -use parking_lot::{Mutex, RwLock}; +#[cfg(target_os = "macos")] +use parking_lot::Mutex; +#[cfg(target_os = "macos")] +use parking_lot::RwLock; +use speed_rate::Rate; +#[cfg(target_os = "macos")] +pub use speed_rate::{SpeedRate, Traffic}; +#[cfg(target_os = "macos")] use std::sync::Arc; +use tauri::menu::CheckMenuItem; use tauri::AppHandle; -use tauri::{ - menu::CheckMenuItem, - tray::{MouseButton, MouseButtonState, TrayIconEvent, TrayIconId}, -}; use tauri::{ menu::{MenuEvent, MenuItem, PredefinedMenuItem, Submenu}, + tray::{MouseButton, MouseButtonState, TrayIconEvent, TrayIconId}, Wry, }; +#[cfg(target_os = "macos")] use tokio::sync::broadcast; use super::handle; +#[cfg(target_os = "macos")] pub struct Tray { pub speed_rate: Arc>>, - #[cfg(target_os = "macos")] shutdown_tx: Arc>>>, } +#[cfg(not(target_os = "macos"))] +pub struct Tray {} + impl Tray { pub fn global() -> &'static Tray { static TRAY: OnceCell = OnceCell::new(); - TRAY.get_or_init(|| Tray { + #[cfg(target_os = "macos")] + return TRAY.get_or_init(|| Tray { speed_rate: Arc::new(Mutex::new(None)), - #[cfg(target_os = "macos")] shutdown_tx: Arc::new(RwLock::new(None)), - }) + }); + + #[cfg(not(target_os = "macos"))] + return TRAY.get_or_init(|| Tray {}); } pub fn init(&self) -> Result<()> { - let mut speed_rate = self.speed_rate.lock(); - *speed_rate = Some(SpeedRate::new()); + #[cfg(target_os = "macos")] + { + let mut speed_rate = self.speed_rate.lock(); + *speed_rate = Some(SpeedRate::new()); + } Ok(()) } @@ -120,6 +134,7 @@ impl Tray { } /// 更新托盘图标 + #[allow(unused_variables)] pub fn update_icon(&self, rate: Option) -> Result<()> { let app_handle = handle::Handle::global().app_handle().unwrap(); let verge = Config::verge().latest().clone(); @@ -163,7 +178,7 @@ impl Tray { }; #[cfg(not(target_os = "macos"))] - let mut icon = include_bytes!("../../icons/tray-icon-tun.ico").to_vec(); + let mut icon = include_bytes!("../../../icons/tray-icon-tun.ico").to_vec(); if *tun_tray_icon { let icon_dir_path = dirs::app_home_dir()?.join("icons"); let png_path = icon_dir_path.join("tun.png"); @@ -183,7 +198,7 @@ impl Tray { }; #[cfg(not(target_os = "macos"))] - let mut icon = include_bytes!("../../icons/tray-icon.ico").to_vec(); + let mut icon = include_bytes!("../../../icons/tray-icon.ico").to_vec(); if *common_tray_icon { let icon_dir_path = dirs::app_home_dir()?.join("icons"); let png_path = icon_dir_path.join("common.png"); @@ -302,7 +317,6 @@ impl Tray { /// 取消订阅 traffic 数据 #[cfg(target_os = "macos")] - #[allow(unused)] pub fn unsubscribe_traffic(&self) { log::info!(target: "app", "unsubscribe traffic"); if let Some(tx) = self.shutdown_tx.write().take() { diff --git a/src-tauri/src/core/tray/speed_rate.rs b/src-tauri/src/core/tray/speed_rate.rs index 8f9af419..92896397 100644 --- a/src-tauri/src/core/tray/speed_rate.rs +++ b/src-tauri/src/core/tray/speed_rate.rs @@ -1,14 +1,17 @@ -use crate::core::clash_api::get_traffic_ws_url; -use crate::utils::help::format_bytes_speed; -use anyhow::Result; -use futures::Stream; -use image::{ImageBuffer, Rgba}; -use imageproc::drawing::draw_text_mut; -use parking_lot::Mutex; -use rusttype::{Font, Scale}; -use std::io::Cursor; -use std::sync::Arc; -use tokio_tungstenite::tungstenite::Message; +#[cfg(target_os = "macos")] +use { + crate::core::clash_api::get_traffic_ws_url, + crate::utils::help::format_bytes_speed, + anyhow::Result, + futures::Stream, + image::{ImageBuffer, Rgba}, + imageproc::drawing::draw_text_mut, + parking_lot::Mutex, + rusttype::{Font, Scale}, + std::io::Cursor, + std::sync::Arc, + tokio_tungstenite::tungstenite::Message, +}; #[derive(Debug, Clone, Default, PartialEq)] pub struct Rate { @@ -16,11 +19,13 @@ pub struct Rate { pub down: u64, } +#[cfg(target_os = "macos")] #[derive(Debug, Clone)] pub struct SpeedRate { rate: Arc>, } +#[cfg(target_os = "macos")] impl SpeedRate { pub fn new() -> Self { Self { @@ -28,7 +33,6 @@ impl SpeedRate { } } - /// 更新流量数据并返回变化后的速率(如果有变化) pub fn update_and_check_changed(&self, up: u64, down: u64) -> Option { let mut rates = self.rate.lock(); let (current, previous) = &mut *rates; @@ -44,14 +48,12 @@ impl SpeedRate { } } - // 获取当前的速率 pub fn get_curent_rate(&self) -> Option { let rates = self.rate.lock(); let (current, _) = &*rates; Some(current.clone()) } - /// 在图标上添加速率显示 pub fn add_speed_text(icon: Vec, rate: Option) -> Result> { let rate = rate.unwrap_or(Rate { up: 0, down: 0 }); let img = image::load_from_memory(&icon)?; @@ -140,11 +142,14 @@ impl SpeedRate { } } +#[cfg(target_os = "macos")] #[derive(Debug, Clone)] pub struct Traffic { pub up: u64, pub down: u64, } + +#[cfg(target_os = "macos")] impl Traffic { pub async fn get_traffic_stream() -> Result>> { diff --git a/src-tauri/src/utils/help.rs b/src-tauri/src/utils/help.rs index 04e4bf22..9b0bc6a7 100644 --- a/src-tauri/src/utils/help.rs +++ b/src-tauri/src/utils/help.rs @@ -210,6 +210,7 @@ macro_rules! t { /// assert_eq!(format_bytes_speed(1024), "1.0KB/s"); /// assert_eq!(format_bytes_speed(1024 * 1024), "1.0MB/s"); /// ``` +#[cfg(target_os = "macos")] pub fn format_bytes_speed(speed: u64) -> String { if speed < 1024 { format!("{}B/s", speed)