use eframe::wasm_bindgen::{self, prelude::*};
use eframe::{egui, epaint::FontFamily, FontData};
use std::collections::BTreeMap;
#[wasm_bindgen]
pub fn start(canvas_id: &str) -> Result<(), eframe::wasm_bindgen::JsValue> {
let web_options = eframe::WebOptions::default();
wasm_bindgen_futures::spawn_local(async move {
eframe::start_web(
canvas_id,
web_options,
Box::new(|cc| {
let mut fonts = BTreeMap::new();
fonts.insert(
"NotoSansJP".to_owned(),
FontData::from_static(include_bytes!("assets/NotoSansJP-Regular.otf")),
);
cc.egui_ctx.set_fonts(egui::FontDefinitions {
font_data: fonts,
families: BTreeMap::from([(
FontFamily::Proportional,
vec!["NotoSansJP".to_owned()],
)]),
});
Box::new(CharCounterApp::default())
}),
)
.await
.expect("failed to start eframe");
});
Ok(())
}
struct CharCounterApp {
current_char_index: usize,
chars: Vec<char>,
}
impl Default for CharCounterApp {
fn default() -> Self {
Self {
current_char_index: 0,
chars: vec![
'あ', 'い', 'う', 'え', 'お',
'か', 'き', 'く', 'け', 'こ',
'さ', 'し', 'す', 'せ', 'そ',
'た', 'ち', 'つ', 'て', 'と',
'な', 'に', 'ぬ', 'ね', 'の',
'は', 'ひ', 'ふ', 'へ', 'ほ',
'ま', 'み', 'む', 'め', 'も',
'や', 'ゆ', 'よ',
'ら', 'り', 'る', 'れ', 'ろ',
'わ', 'を', 'ん'
],
}
}
}
impl eframe::App for CharCounterApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.vertical_centered(|ui| {
ui.add_space(50.0);
ui.heading(self.chars[self.current_char_index].to_string());
ui.label(format!("{} / {}", self.current_char_index + 1, self.chars.len()));
ui.add_space(20.0);
if ui.button("次へ").clicked() {
self.current_char_index =
if self.current_char_index >= self.chars.len() - 1 {
0
} else {
self.current_char_index + 1
};
}
});
});
}
}