From 4c3e236571e38d166aa5587582d48ef40100d1fb Mon Sep 17 00:00:00 2001 From: xiaji Date: Fri, 10 Apr 2026 23:21:15 +0800 Subject: [PATCH] fix: load system Chinese font --- src/gui.rs | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index 1e19454..fce55b2 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,10 +1,11 @@ use eframe::{NativeOptions, run_native}; -use egui::*; +use egui::{FontDefinitions, FontFamily, FontData}; use crate::api::ProxmoxClient; use std::sync::{Arc, RwLock, Mutex}; use dotenv::dotenv; use std::env; use std::thread; +use std::collections::BTreeMap; pub type SharedState = Arc>; @@ -68,21 +69,39 @@ pub fn gui_run() { ..Default::default() }; - run_native("Proxmox VM 控制器", options, Box::new(|cc| { - set_font(&cc.egui_ctx); + run_native("Proxmox VM Controller", options, Box::new(|cc| { + cc.egui_ctx.set_fonts(load_font_definitions()); Ok(Box::new(App::new())) })).unwrap(); } -fn set_font(ctx: &egui::Context) { - let mut style = (*ctx.style()).clone(); - let font_family = FontFamily::Name("Microsoft YaHei".into()); - style.text_styles = [ - (TextStyle::Heading, FontId::new(18.0, font_family.clone())), - (TextStyle::Body, FontId::new(14.0, font_family.clone())), - (TextStyle::Button, FontId::new(14.0, font_family.clone())), - ].into(); - ctx.set_style(style); +fn load_font_definitions() -> FontDefinitions { + let font_paths = [ + r"C:\Windows\Fonts\msyh.ttc", + r"C:\Windows\Fonts\msyh.ttf", + r"C:\Windows\Fonts\simhei.ttf", + ]; + + let mut font_data: BTreeMap = BTreeMap::new(); + let mut loaded_name = String::new(); + + for path in font_paths { + if let Ok(data) = std::fs::read(path) { + let name = if path.contains("msyh") { "msyh" } else { "simhei" }; + loaded_name = name.to_owned(); + font_data.insert(name.to_owned(), FontData::from_owned(data)); + break; + } + } + + let mut fonts: FontDefinitions = Default::default(); + if !loaded_name.is_empty() { + fonts.font_data = font_data; + fonts.families.entry(FontFamily::Proportional).or_default().insert(0, loaded_name.clone()); + fonts.families.entry(FontFamily::Monospace).or_default().insert(0, loaded_name); + } + + fonts } struct App {