From b09313756e50a5e389ad4d7b7296eaa1b247ef7b Mon Sep 17 00:00:00 2001 From: Tunglies Date: Thu, 27 Mar 2025 20:45:37 +0800 Subject: [PATCH] refactor: optimize total width calculation and format bytes speed function --- src-tauri/src/core/tray/speed_rate.rs | 43 +++++++++++++++++++-------- src-tauri/src/utils/help.rs | 17 ++++++----- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src-tauri/src/core/tray/speed_rate.rs b/src-tauri/src/core/tray/speed_rate.rs index 8af0acca..66821241 100644 --- a/src-tauri/src/core/tray/speed_rate.rs +++ b/src-tauri/src/core/tray/speed_rate.rs @@ -101,14 +101,18 @@ impl SpeedRate { false }; - let total_width = if is_custom_icon { - 450 - } else if icon_bytes.is_some() { - icon_width + 580 - } else { - 580 + let total_width = match (is_custom_icon, icon_bytes.is_some()) { + (true, true) => 510, + (true, false) => 740, + (false, false) => 740, + (false, true) => icon_width + 740, }; + // println!( + // "icon_height: {}, icon_wight: {}, total_width: {}", + // icon_height, icon_width, total_width + // ); + // 创建新的透明画布 let mut combined_image = RgbaImage::new(total_width, icon_height); @@ -143,17 +147,30 @@ impl SpeedRate { let scale = ab_glyph::PxScale::from(font_size); // 使用更简洁的速率格式 - let up_text = format_bytes_speed(rate.up); - let down_text = format_bytes_speed(rate.down); + let up_text = format!("↑ {}", format_bytes_speed(rate.up)); + let down_text = format!("↓ {}", format_bytes_speed(rate.down)); + + // For test rate display + // let down_text = format!("↓ {}", format_bytes_speed(102 * 1020 * 1024)); // 计算文本位置,确保垂直间距合适 // 修改文本位置为居右显示 - let up_text_width = imageproc::drawing::text_size(scale, &font, &up_text).0 as u32; - let down_text_width = imageproc::drawing::text_size(scale, &font, &down_text).0 as u32; - // 计算右对齐的文本位置 - let up_text_x = total_width - up_text_width; - let down_text_x = total_width - down_text_width; + // let up_text_width = imageproc::drawing::text_size(scale, &font, &up_text).0 as u32; + // let down_text_width = imageproc::drawing::text_size(scale, &font, &down_text).0 as u32; + // let up_text_x = total_width - up_text_width; + // let down_text_x = total_width - down_text_width; + + // 计算左对齐的文本位置 + let (up_text_x, down_text_x) = { + if is_custom_icon || icon_bytes.is_some() { + let text_left_offset = 30; + let left_begin = icon_width + text_left_offset; + (left_begin, left_begin) + } else { + (icon_width, icon_width) + } + }; // 优化垂直位置,使速率显示的高度和上下间距正好等于图标大小 let text_height = font_size as i32; diff --git a/src-tauri/src/utils/help.rs b/src-tauri/src/utils/help.rs index 2d4cef8e..9d1b17c1 100644 --- a/src-tauri/src/utils/help.rs +++ b/src-tauri/src/utils/help.rs @@ -167,15 +167,16 @@ macro_rules! t { /// ``` #[cfg(target_os = "macos")] pub fn format_bytes_speed(speed: u64) -> String { - if speed < 1024 { - format!("{}B/s", speed) - } else if speed < 1024 * 1024 { - format!("{:.1}KB/s", speed as f64 / 1024.0) - } else if speed < 1024 * 1024 * 1024 { - format!("{:.1}MB/s", speed as f64 / 1024.0 / 1024.0) - } else { - format!("{:.1}GB/s", speed as f64 / 1024.0 / 1024.0 / 1024.0) + const UNITS: [&str; 4] = ["B", "KB", "MB", "GB"]; + let mut size = speed as f64; + let mut unit_index = 0; + + while size >= 1000.0 && unit_index < UNITS.len() - 1 { + size /= 1024.0; + unit_index += 1; } + + format!("{:.1}{}/s", size, UNITS[unit_index]) } #[cfg(target_os = "macos")]