mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 03:23:57 +08:00
refactor: optimize total width calculation and format bytes speed function
This commit is contained in:
parent
f800e2e3b6
commit
b09313756e
@ -101,14 +101,18 @@ impl SpeedRate {
|
|||||||
false
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
let total_width = if is_custom_icon {
|
let total_width = match (is_custom_icon, icon_bytes.is_some()) {
|
||||||
450
|
(true, true) => 510,
|
||||||
} else if icon_bytes.is_some() {
|
(true, false) => 740,
|
||||||
icon_width + 580
|
(false, false) => 740,
|
||||||
} else {
|
(false, true) => icon_width + 740,
|
||||||
580
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// println!(
|
||||||
|
// "icon_height: {}, icon_wight: {}, total_width: {}",
|
||||||
|
// icon_height, icon_width, total_width
|
||||||
|
// );
|
||||||
|
|
||||||
// 创建新的透明画布
|
// 创建新的透明画布
|
||||||
let mut combined_image = RgbaImage::new(total_width, icon_height);
|
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 scale = ab_glyph::PxScale::from(font_size);
|
||||||
|
|
||||||
// 使用更简洁的速率格式
|
// 使用更简洁的速率格式
|
||||||
let up_text = format_bytes_speed(rate.up);
|
let up_text = format!("↑ {}", format_bytes_speed(rate.up));
|
||||||
let down_text = format_bytes_speed(rate.down);
|
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 up_text_width = imageproc::drawing::text_size(scale, &font, &up_text).0 as u32;
|
||||||
let down_text_x = total_width - down_text_width;
|
// 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;
|
let text_height = font_size as i32;
|
||||||
|
@ -167,15 +167,16 @@ macro_rules! t {
|
|||||||
/// ```
|
/// ```
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
pub fn format_bytes_speed(speed: u64) -> String {
|
pub fn format_bytes_speed(speed: u64) -> String {
|
||||||
if speed < 1024 {
|
const UNITS: [&str; 4] = ["B", "KB", "MB", "GB"];
|
||||||
format!("{}B/s", speed)
|
let mut size = speed as f64;
|
||||||
} else if speed < 1024 * 1024 {
|
let mut unit_index = 0;
|
||||||
format!("{:.1}KB/s", speed as f64 / 1024.0)
|
|
||||||
} else if speed < 1024 * 1024 * 1024 {
|
while size >= 1000.0 && unit_index < UNITS.len() - 1 {
|
||||||
format!("{:.1}MB/s", speed as f64 / 1024.0 / 1024.0)
|
size /= 1024.0;
|
||||||
} else {
|
unit_index += 1;
|
||||||
format!("{:.1}GB/s", speed as f64 / 1024.0 / 1024.0 / 1024.0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
format!("{:.1}{}/s", size, UNITS[unit_index])
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user