mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 04:53:44 +08:00
fix: unused code
This commit is contained in:
parent
04d766884a
commit
ad335ba005
@ -123,6 +123,7 @@ pub fn parse_check_output(log: String) -> String {
|
|||||||
log
|
log
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
pub fn get_traffic_ws_url() -> Result<String> {
|
pub fn get_traffic_ws_url() -> Result<String> {
|
||||||
let (url, _) = clash_client_info()?;
|
let (url, _) = clash_client_info()?;
|
||||||
let ws_url = url.replace("http://", "ws://") + "/traffic";
|
let ws_url = url.replace("http://", "ws://") + "/traffic";
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
pub mod speed_rate;
|
use once_cell::sync::OnceCell;
|
||||||
use speed_rate::Rate;
|
|
||||||
pub use speed_rate::{SpeedRate, Traffic};
|
|
||||||
|
|
||||||
|
pub mod speed_rate;
|
||||||
use crate::utils::dirs;
|
use crate::utils::dirs;
|
||||||
use crate::{
|
use crate::{
|
||||||
cmds,
|
cmds,
|
||||||
@ -10,42 +9,57 @@ use crate::{
|
|||||||
utils::resolve::{self, VERSION},
|
utils::resolve::{self, VERSION},
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use once_cell::sync::OnceCell;
|
#[cfg(target_os = "macos")]
|
||||||
use parking_lot::{Mutex, RwLock};
|
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 std::sync::Arc;
|
||||||
|
use tauri::menu::CheckMenuItem;
|
||||||
use tauri::AppHandle;
|
use tauri::AppHandle;
|
||||||
use tauri::{
|
|
||||||
menu::CheckMenuItem,
|
|
||||||
tray::{MouseButton, MouseButtonState, TrayIconEvent, TrayIconId},
|
|
||||||
};
|
|
||||||
use tauri::{
|
use tauri::{
|
||||||
menu::{MenuEvent, MenuItem, PredefinedMenuItem, Submenu},
|
menu::{MenuEvent, MenuItem, PredefinedMenuItem, Submenu},
|
||||||
|
tray::{MouseButton, MouseButtonState, TrayIconEvent, TrayIconId},
|
||||||
Wry,
|
Wry,
|
||||||
};
|
};
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
use tokio::sync::broadcast;
|
use tokio::sync::broadcast;
|
||||||
|
|
||||||
use super::handle;
|
use super::handle;
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
pub struct Tray {
|
pub struct Tray {
|
||||||
pub speed_rate: Arc<Mutex<Option<SpeedRate>>>,
|
pub speed_rate: Arc<Mutex<Option<SpeedRate>>>,
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
shutdown_tx: Arc<RwLock<Option<broadcast::Sender<()>>>>,
|
shutdown_tx: Arc<RwLock<Option<broadcast::Sender<()>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
pub struct Tray {}
|
||||||
|
|
||||||
impl Tray {
|
impl Tray {
|
||||||
pub fn global() -> &'static Tray {
|
pub fn global() -> &'static Tray {
|
||||||
static TRAY: OnceCell<Tray> = OnceCell::new();
|
static TRAY: OnceCell<Tray> = OnceCell::new();
|
||||||
|
|
||||||
TRAY.get_or_init(|| Tray {
|
#[cfg(target_os = "macos")]
|
||||||
|
return TRAY.get_or_init(|| Tray {
|
||||||
speed_rate: Arc::new(Mutex::new(None)),
|
speed_rate: Arc::new(Mutex::new(None)),
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
shutdown_tx: Arc::new(RwLock::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<()> {
|
pub fn init(&self) -> Result<()> {
|
||||||
let mut speed_rate = self.speed_rate.lock();
|
#[cfg(target_os = "macos")]
|
||||||
*speed_rate = Some(SpeedRate::new());
|
{
|
||||||
|
let mut speed_rate = self.speed_rate.lock();
|
||||||
|
*speed_rate = Some(SpeedRate::new());
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,6 +134,7 @@ impl Tray {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 更新托盘图标
|
/// 更新托盘图标
|
||||||
|
#[allow(unused_variables)]
|
||||||
pub fn update_icon(&self, rate: Option<Rate>) -> Result<()> {
|
pub fn update_icon(&self, rate: Option<Rate>) -> Result<()> {
|
||||||
let app_handle = handle::Handle::global().app_handle().unwrap();
|
let app_handle = handle::Handle::global().app_handle().unwrap();
|
||||||
let verge = Config::verge().latest().clone();
|
let verge = Config::verge().latest().clone();
|
||||||
@ -163,7 +178,7 @@ impl Tray {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(not(target_os = "macos"))]
|
#[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 {
|
if *tun_tray_icon {
|
||||||
let icon_dir_path = dirs::app_home_dir()?.join("icons");
|
let icon_dir_path = dirs::app_home_dir()?.join("icons");
|
||||||
let png_path = icon_dir_path.join("tun.png");
|
let png_path = icon_dir_path.join("tun.png");
|
||||||
@ -183,7 +198,7 @@ impl Tray {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(not(target_os = "macos"))]
|
#[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 {
|
if *common_tray_icon {
|
||||||
let icon_dir_path = dirs::app_home_dir()?.join("icons");
|
let icon_dir_path = dirs::app_home_dir()?.join("icons");
|
||||||
let png_path = icon_dir_path.join("common.png");
|
let png_path = icon_dir_path.join("common.png");
|
||||||
@ -302,7 +317,6 @@ impl Tray {
|
|||||||
|
|
||||||
/// 取消订阅 traffic 数据
|
/// 取消订阅 traffic 数据
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
#[allow(unused)]
|
|
||||||
pub fn unsubscribe_traffic(&self) {
|
pub fn unsubscribe_traffic(&self) {
|
||||||
log::info!(target: "app", "unsubscribe traffic");
|
log::info!(target: "app", "unsubscribe traffic");
|
||||||
if let Some(tx) = self.shutdown_tx.write().take() {
|
if let Some(tx) = self.shutdown_tx.write().take() {
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
use crate::core::clash_api::get_traffic_ws_url;
|
#[cfg(target_os = "macos")]
|
||||||
use crate::utils::help::format_bytes_speed;
|
use {
|
||||||
use anyhow::Result;
|
crate::core::clash_api::get_traffic_ws_url,
|
||||||
use futures::Stream;
|
crate::utils::help::format_bytes_speed,
|
||||||
use image::{ImageBuffer, Rgba};
|
anyhow::Result,
|
||||||
use imageproc::drawing::draw_text_mut;
|
futures::Stream,
|
||||||
use parking_lot::Mutex;
|
image::{ImageBuffer, Rgba},
|
||||||
use rusttype::{Font, Scale};
|
imageproc::drawing::draw_text_mut,
|
||||||
use std::io::Cursor;
|
parking_lot::Mutex,
|
||||||
use std::sync::Arc;
|
rusttype::{Font, Scale},
|
||||||
use tokio_tungstenite::tungstenite::Message;
|
std::io::Cursor,
|
||||||
|
std::sync::Arc,
|
||||||
|
tokio_tungstenite::tungstenite::Message,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, PartialEq)]
|
#[derive(Debug, Clone, Default, PartialEq)]
|
||||||
pub struct Rate {
|
pub struct Rate {
|
||||||
@ -16,11 +19,13 @@ pub struct Rate {
|
|||||||
pub down: u64,
|
pub down: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SpeedRate {
|
pub struct SpeedRate {
|
||||||
rate: Arc<Mutex<(Rate, Rate)>>,
|
rate: Arc<Mutex<(Rate, Rate)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
impl SpeedRate {
|
impl SpeedRate {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -28,7 +33,6 @@ impl SpeedRate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 更新流量数据并返回变化后的速率(如果有变化)
|
|
||||||
pub fn update_and_check_changed(&self, up: u64, down: u64) -> Option<Rate> {
|
pub fn update_and_check_changed(&self, up: u64, down: u64) -> Option<Rate> {
|
||||||
let mut rates = self.rate.lock();
|
let mut rates = self.rate.lock();
|
||||||
let (current, previous) = &mut *rates;
|
let (current, previous) = &mut *rates;
|
||||||
@ -44,14 +48,12 @@ impl SpeedRate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取当前的速率
|
|
||||||
pub fn get_curent_rate(&self) -> Option<Rate> {
|
pub fn get_curent_rate(&self) -> Option<Rate> {
|
||||||
let rates = self.rate.lock();
|
let rates = self.rate.lock();
|
||||||
let (current, _) = &*rates;
|
let (current, _) = &*rates;
|
||||||
Some(current.clone())
|
Some(current.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 在图标上添加速率显示
|
|
||||||
pub fn add_speed_text(icon: Vec<u8>, rate: Option<Rate>) -> Result<Vec<u8>> {
|
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 rate = rate.unwrap_or(Rate { up: 0, down: 0 });
|
||||||
let img = image::load_from_memory(&icon)?;
|
let img = image::load_from_memory(&icon)?;
|
||||||
@ -140,11 +142,14 @@ impl SpeedRate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Traffic {
|
pub struct Traffic {
|
||||||
pub up: u64,
|
pub up: u64,
|
||||||
pub down: u64,
|
pub down: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
impl Traffic {
|
impl Traffic {
|
||||||
pub async fn get_traffic_stream() -> Result<impl Stream<Item = Result<Traffic, anyhow::Error>>>
|
pub async fn get_traffic_stream() -> Result<impl Stream<Item = Result<Traffic, anyhow::Error>>>
|
||||||
{
|
{
|
||||||
|
@ -210,6 +210,7 @@ macro_rules! t {
|
|||||||
/// assert_eq!(format_bytes_speed(1024), "1.0KB/s");
|
/// assert_eq!(format_bytes_speed(1024), "1.0KB/s");
|
||||||
/// assert_eq!(format_bytes_speed(1024 * 1024), "1.0MB/s");
|
/// assert_eq!(format_bytes_speed(1024 * 1024), "1.0MB/s");
|
||||||
/// ```
|
/// ```
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
pub fn format_bytes_speed(speed: u64) -> String {
|
pub fn format_bytes_speed(speed: u64) -> String {
|
||||||
if speed < 1024 {
|
if speed < 1024 {
|
||||||
format!("{}B/s", speed)
|
format!("{}B/s", speed)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user