Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nullchinchilla committed Apr 2, 2024
1 parent c1e8b54 commit dde75d1
Show file tree
Hide file tree
Showing 10 changed files with 1,814 additions and 16 deletions.
1,605 changes: 1,589 additions & 16 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions binaries/geph5-client-gui/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "geph5-client-gui"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.81"
csv = "1.3.0"
dirs = "5.0.1"
eframe = {version="0.27.2", default-features=false, features=["wgpu", "x11"]}
egui = "0.27.2"
moka = {version="0.12.5", features=["sync"]}

once_cell = "1.19.0"
smol_str = {version="0.2.1", features=["serde"]}
tap = "1.0.1"
Binary file not shown.
81 changes: 81 additions & 0 deletions binaries/geph5-client-gui/src/assets/chars.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

!
"
&
'
(
)
,
-
.
/
:
;
<
=
>
?
[
]
_
{
|
}
0
1
2
3
5
6
8
a
A
b
B
c
C
d
D
e
E
f
F
g
G
h
H
i
I
j
J
k
K
l
L
m
M
n
N
o
O
p
P
q
r
R
s
S
t
T
u
v
V
w
x
y
z
3 changes: 3 additions & 0 deletions binaries/geph5-client-gui/src/assets/subset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
find ../ -type f -name '*.rs' -o -name '*.csv' | xargs cat | grep -o . | sort -u > chars.txt
pyftsubset SarasaUiSC-Regular.ttf --text-file=chars.txt --output-file=subset.ttf
Binary file added binaries/geph5-client-gui/src/assets/subset.ttf
Binary file not shown.
2 changes: 2 additions & 0 deletions binaries/geph5-client-gui/src/l10n.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
label,en,zh
geph,Geph,迷雾通
22 changes: 22 additions & 0 deletions binaries/geph5-client-gui/src/l10n.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::collections::BTreeMap;

use once_cell::sync::Lazy;
use smol_str::SmolStr;

use crate::prefs::pref_read;

static L10N_TABLE: Lazy<BTreeMap<SmolStr, BTreeMap<SmolStr, SmolStr>>> = Lazy::new(|| {
let csv = include_bytes!("l10n.csv");
let mut rdr = csv::Reader::from_reader(&csv[..]);
let mut toret = BTreeMap::new();
for result in rdr.deserialize() {
let mut record: BTreeMap<SmolStr, SmolStr> = result.unwrap();
let label = record.remove("label").unwrap();
toret.insert(label, record);
}
toret
});

pub fn l10n(label: &str) -> &str {
&L10N_TABLE[label][&pref_read("lang").unwrap()]
}
65 changes: 65 additions & 0 deletions binaries/geph5-client-gui/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
mod l10n;
mod prefs;
use std::time::Duration;

use egui::{Color32, FontData, FontDefinitions, FontFamily, Visuals};
use l10n::l10n;
use prefs::pref_write;
use tap::Tap as _;

fn main() {
// default prefs
for (key, value) in [("lang", "en")] {
pref_write(key, value).unwrap();
}

let native_options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([800.0, 600.0])
.with_min_inner_size([300.0, 220.0]),
..Default::default()
};
eframe::run_native(
l10n("geph"),
native_options,
Box::new(|cc| Box::new(App::new(cc))),
)
.unwrap();
}

pub struct App {}

impl App {
/// Constructs the app.
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
// light mode
cc.egui_ctx.set_visuals(
Visuals::light()
.tap_mut(|vis| vis.widgets.noninteractive.fg_stroke.color = Color32::BLACK),
);

// set up fonts. currently this uses SC for CJK, but this can be autodetected instead.
let mut fonts = FontDefinitions::default();
fonts.font_data.insert(
"sarasa_sc".into(),
FontData::from_static(include_bytes!("assets/subset.ttf")),
);
fonts
.families
.get_mut(&FontFamily::Proportional)
.unwrap()
.insert(0, "sarasa_sc".into());
cc.egui_ctx.set_fonts(fonts);
Self {}
}
}

impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
ctx.request_repaint_after(Duration::from_secs(1));

egui::TopBottomPanel::top("top").show(ctx, |ui| {});
egui::TopBottomPanel::bottom("bottom").show(ctx, |ui| {});
egui::CentralPanel::default().show(ctx, |ui| ui.label("hello world 你好你好"));
}
}
34 changes: 34 additions & 0 deletions binaries/geph5-client-gui/src/prefs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::path::PathBuf;

use anyhow::Context as _;
use moka::sync::Cache;
use once_cell::sync::Lazy;
use smol_str::SmolStr;

static PREF_DIR: Lazy<PathBuf> = Lazy::new(|| {
let dir = dirs::config_dir()
.context("no config dir")
.unwrap()
.join("geph5-prefs");
std::fs::create_dir_all(&dir).unwrap();
dir
});

static PREF_CACHE: Lazy<Cache<SmolStr, SmolStr>> = Lazy::new(|| Cache::new(10000));

pub fn pref_write(key: &str, val: &str) -> anyhow::Result<()> {
PREF_CACHE.remove(key);
let key_path = PREF_DIR.join(key);
std::fs::write(key_path, val.as_bytes())?;
Ok(())
}

pub fn pref_read(key: &str) -> anyhow::Result<SmolStr> {
PREF_CACHE
.try_get_with(key.into(), || {
let key_path = PREF_DIR.join(key);
let contents = std::fs::read_to_string(key_path)?;
anyhow::Ok(SmolStr::from(contents))
})
.map_err(|e| anyhow::anyhow!("{e}"))
}

0 comments on commit dde75d1

Please sign in to comment.