fix: load system Chinese font
This commit is contained in:
43
src/gui.rs
43
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<RwLock<AppState>>;
|
||||
|
||||
@@ -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<String, FontData> = 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 {
|
||||
|
||||
Reference in New Issue
Block a user