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 eframe::{NativeOptions, run_native};
|
||||||
use egui::*;
|
use egui::{FontDefinitions, FontFamily, FontData};
|
||||||
use crate::api::ProxmoxClient;
|
use crate::api::ProxmoxClient;
|
||||||
use std::sync::{Arc, RwLock, Mutex};
|
use std::sync::{Arc, RwLock, Mutex};
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
pub type SharedState = Arc<RwLock<AppState>>;
|
pub type SharedState = Arc<RwLock<AppState>>;
|
||||||
|
|
||||||
@@ -68,21 +69,39 @@ pub fn gui_run() {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
run_native("Proxmox VM 控制器", options, Box::new(|cc| {
|
run_native("Proxmox VM Controller", options, Box::new(|cc| {
|
||||||
set_font(&cc.egui_ctx);
|
cc.egui_ctx.set_fonts(load_font_definitions());
|
||||||
Ok(Box::new(App::new()))
|
Ok(Box::new(App::new()))
|
||||||
})).unwrap();
|
})).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_font(ctx: &egui::Context) {
|
fn load_font_definitions() -> FontDefinitions {
|
||||||
let mut style = (*ctx.style()).clone();
|
let font_paths = [
|
||||||
let font_family = FontFamily::Name("Microsoft YaHei".into());
|
r"C:\Windows\Fonts\msyh.ttc",
|
||||||
style.text_styles = [
|
r"C:\Windows\Fonts\msyh.ttf",
|
||||||
(TextStyle::Heading, FontId::new(18.0, font_family.clone())),
|
r"C:\Windows\Fonts\simhei.ttf",
|
||||||
(TextStyle::Body, FontId::new(14.0, font_family.clone())),
|
];
|
||||||
(TextStyle::Button, FontId::new(14.0, font_family.clone())),
|
|
||||||
].into();
|
let mut font_data: BTreeMap<String, FontData> = BTreeMap::new();
|
||||||
ctx.set_style(style);
|
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 {
|
struct App {
|
||||||
|
|||||||
Reference in New Issue
Block a user