fix: unused code

This commit is contained in:
huzibaca 2024-12-31 06:27:29 +08:00
parent 04d766884a
commit ad335ba005
4 changed files with 53 additions and 32 deletions

View File

@ -123,6 +123,7 @@ pub fn parse_check_output(log: String) -> String {
log
}
#[cfg(target_os = "macos")]
pub fn get_traffic_ws_url() -> Result<String> {
let (url, _) = clash_client_info()?;
let ws_url = url.replace("http://", "ws://") + "/traffic";

View File

@ -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<Mutex<Option<SpeedRate>>>,
#[cfg(target_os = "macos")]
shutdown_tx: Arc<RwLock<Option<broadcast::Sender<()>>>>,
}
#[cfg(not(target_os = "macos"))]
pub struct Tray {}
impl Tray {
pub fn global() -> &'static Tray {
static TRAY: OnceCell<Tray> = OnceCell::new();
TRAY.get_or_init(|| Tray {
speed_rate: Arc::new(Mutex::new(None)),
#[cfg(target_os = "macos")]
return TRAY.get_or_init(|| Tray {
speed_rate: Arc::new(Mutex::new(None)),
shutdown_tx: Arc::new(RwLock::new(None)),
})
});
#[cfg(not(target_os = "macos"))]
return TRAY.get_or_init(|| Tray {});
}
pub fn init(&self) -> Result<()> {
#[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<Rate>) -> 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() {

View File

@ -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<Mutex<(Rate, Rate)>>,
}
#[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<Rate> {
let mut rates = self.rate.lock();
let (current, previous) = &mut *rates;
@ -44,14 +48,12 @@ impl SpeedRate {
}
}
// 获取当前的速率
pub fn get_curent_rate(&self) -> Option<Rate> {
let rates = self.rate.lock();
let (current, _) = &*rates;
Some(current.clone())
}
/// 在图标上添加速率显示
pub fn add_speed_text(icon: Vec<u8>, rate: Option<Rate>) -> Result<Vec<u8>> {
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<impl Stream<Item = Result<Traffic, anyhow::Error>>>
{

View File

@ -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)