feat: 用 Slint 显示最近一周打卡列表
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
use std::process::ExitCode;
|
||||
use std::rc::Rc;
|
||||
|
||||
slint::include_modules!();
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let ui = match MainWindow::new() {
|
||||
Ok(ui) => ui,
|
||||
Err(err) => {
|
||||
eprintln!("{err}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
};
|
||||
let ui_weak = ui.as_weak();
|
||||
show(&ui, load());
|
||||
ui.on_refresh(move || {
|
||||
let Some(ui) = ui_weak.upgrade() else {
|
||||
return;
|
||||
};
|
||||
show(&ui, load());
|
||||
});
|
||||
if let Err(err) = ui.run() {
|
||||
eprintln!("{err}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
|
||||
fn load() -> punchlog::Result<punchlog::WeekReport> {
|
||||
let mut config = punchlog::AppConfig::load()?;
|
||||
punchlog::load_last_week(&mut config)
|
||||
}
|
||||
|
||||
fn show(ui: &MainWindow, loaded: punchlog::Result<punchlog::WeekReport>) {
|
||||
match loaded {
|
||||
Ok(report) => {
|
||||
ui.set_error(String::new().into());
|
||||
ui.set_range(format!("{} — {}", report.start, report.end).into());
|
||||
let rows: Vec<DayRow> = report
|
||||
.days
|
||||
.iter()
|
||||
.map(|day| {
|
||||
let (site, memo) = day.content();
|
||||
DayRow {
|
||||
date: day.day.format("%m-%d").to_string().into(),
|
||||
mark: if day.has_supplement { "补" } else { "" }.into(),
|
||||
time: format!("{} – {}", day.first.format("%H:%M"), day.last.format("%H:%M")).into(),
|
||||
hours: format!("{:.1}h", day.hours).into(),
|
||||
status: day.status.label().into(),
|
||||
site: site.into(),
|
||||
memo: memo.into(),
|
||||
problem: day.status.is_problem(),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
ui.set_days(Rc::new(slint::VecModel::from(rows)).into());
|
||||
}
|
||||
Err(err) => {
|
||||
ui.set_error(err.to_string().into());
|
||||
ui.set_days(Rc::new(slint::VecModel::<DayRow>::default()).into());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user