KeSp_controller/src/stats.rs
Mae PUGIN 3e54361d0d refactor: Modularize main.rs (2478 → 58 lines), flat architecture
Split monolithic main.rs into domain modules:
- context.rs: AppContext shared state, BgMsg enum, serial_spawn helper
- models.rs: UI model builders, keycap labels, key entries, layout preview
- config.rs: keyboard config export/import via binary protocol
- dispatch.rs: BgMsg handler + WPM/layout timers
- keymap.rs: key selection, layer switch/rename, heatmap toggle
- key_selector.rs: dispatch_keycode router, apply_keycode, hex/MT/LT
- macros.rs: macro CRUD, shortcut presets, step builder
- advanced.rs: combos, KO, leaders, tap dance, BT, tama, autoshift
- settings.rs: OTA flash, config backup, keyboard layout
- flasher.rs: ESP32 bootloader flash
- layout.rs: layout JSON preview, load/export
- connection.rs: serial connect/disconnect, tab auto-refresh
- stats.rs: stats refresh

Rename logic/ → protocol/ with cleaner file names.
Remove unused original-src/ directory.
Fix DarkLineEdit double styling, add rename popup, macro shortcuts.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 12:02:22 +02:00

28 lines
1.1 KiB
Rust

use crate::context::AppContext;
use crate::{MainWindow, StatsBridge};
use crate::protocol;
use slint::ComponentHandle;
/// Wire up the stats refresh callback.
pub fn setup(window: &MainWindow, ctx: &AppContext) {
let serial = ctx.serial.clone();
let tx = ctx.bg_tx.clone();
window.global::<StatsBridge>().on_refresh_stats(move || {
let serial = serial.clone();
let tx = tx.clone();
std::thread::spawn(move || {
use protocol::binary::cmd;
use crate::context::BgMsg;
let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner());
if let Ok(r) = ser.send_binary(cmd::KEYSTATS_BIN, &[]) {
let (data, max) = protocol::parsers::parse_keystats_binary(&r.payload);
let _ = tx.send(BgMsg::HeatmapData(data, max));
}
let bigram_lines = if let Ok(r) = ser.send_binary(protocol::binary::cmd::BIGRAMS_TEXT, &[]) {
String::from_utf8_lossy(&r.payload).lines().map(|l| l.to_string()).collect()
} else { Vec::new() };
let _ = tx.send(BgMsg::BigramLines(bigram_lines));
});
});
}