From 3b737a881f5a176e344b0cc96dffc9b83a81184e Mon Sep 17 00:00:00 2001 From: Nixevol Date: Thu, 19 Nov 2026 16:59:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=97=A5=E6=9C=9F=E5=8F=AF=E6=89=8B?= =?UTF-8?q?=E9=80=89=EF=BC=8C=E5=88=B7=E6=96=B0=E6=98=BE=E7=A4=BA=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=B9=B6=E8=87=AA=E5=8A=A8=E5=85=B3=E9=97=AD=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bin/punchlog-ui.rs | 153 +++++++++++++++++++------- src/lib.rs | 2 +- src/report.rs | 6 ++ ui/app.slint | 237 ++++++++++++++++++++++++++++++----------- 4 files changed, 298 insertions(+), 100 deletions(-) diff --git a/src/bin/punchlog-ui.rs b/src/bin/punchlog-ui.rs index 3fdda96..2cf4985 100644 --- a/src/bin/punchlog-ui.rs +++ b/src/bin/punchlog-ui.rs @@ -1,5 +1,10 @@ use std::process::ExitCode; use std::rc::Rc; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::Arc; +use std::time::Duration; + +use chrono::{Datelike, NaiveDate}; slint::include_modules!(); @@ -11,14 +16,25 @@ fn main() -> ExitCode { return ExitCode::FAILURE; } }; + let (start, end) = punchlog::last_week(chrono::Local::now().date_naive()); + ui.set_start_date(to_slint(start)); + ui.set_end_date(to_slint(end)); + let ui_weak = ui.as_weak(); - show(&ui, load()); - ui.on_refresh(move || { - let Some(ui) = ui_weak.upgrade() else { - return; - }; - show(&ui, load()); - }); + let loads = Arc::new(AtomicU64::new(0)); + let toasts = Arc::new(AtomicU64::new(0)); + reload(ui_weak.clone(), Arc::clone(&loads), Arc::clone(&toasts), false); + + let weak = ui_weak.clone(); + let load_gen = Arc::clone(&loads); + let toast_gen = Arc::clone(&toasts); + ui.on_refresh(move || reload(weak.clone(), Arc::clone(&load_gen), Arc::clone(&toast_gen), true)); + + let weak = ui_weak.clone(); + let load_gen = Arc::clone(&loads); + let toast_gen = Arc::clone(&toasts); + ui.on_range_changed(move || reload(weak.clone(), Arc::clone(&load_gen), Arc::clone(&toast_gen), false)); + if let Err(err) = ui.run() { eprintln!("{err}"); return ExitCode::FAILURE; @@ -26,38 +42,103 @@ fn main() -> ExitCode { ExitCode::SUCCESS } -fn load() -> punchlog::Result { - let mut config = punchlog::AppConfig::load()?; - punchlog::load_last_week(&mut config) +fn reload(ui_weak: slint::Weak, generation: Arc, toasts: Arc, announce: bool) { + let Some(ui) = ui_weak.upgrade() else { + return; + }; + let Ok(start) = from_slint(ui.get_start_date()) else { + ui.set_loading(false); + show_toast(&ui, ui_weak, &toasts, "开始日期无效"); + return; + }; + let Ok(end) = from_slint(ui.get_end_date()) else { + ui.set_loading(false); + show_toast(&ui, ui_weak, &toasts, "结束日期无效"); + return; + }; + let (start, end) = if start <= end { (start, end) } else { (end, start) }; + ui.set_start_date(to_slint(start)); + ui.set_end_date(to_slint(end)); + ui.set_loading(true); + ui.set_toast(String::new().into()); + + let ticket = generation.fetch_add(1, Ordering::Relaxed) + 1; + std::thread::spawn(move || { + let loaded = load(start, end); + let _ = slint::invoke_from_event_loop(move || { + if generation.load(Ordering::Relaxed) != ticket { + return; + } + let Some(ui) = ui_weak.upgrade() else { + return; + }; + ui.set_loading(false); + match loaded { + Ok(report) => { + show(&ui, &report); + if announce { + show_toast(&ui, ui_weak.clone(), &toasts, "已刷新"); + } + } + Err(err) => { + ui.set_error(err.to_string().into()); + ui.set_days(Rc::new(slint::VecModel::::default()).into()); + show_toast(&ui, ui_weak.clone(), &toasts, "刷新失败"); + } + } + }); + }); } -fn show(ui: &MainWindow, loaded: punchlog::Result) { - match loaded { - Ok(report) => { - ui.set_error(String::new().into()); - ui.set_range(format!("{} — {}", report.start, report.end).into()); - let rows: Vec = 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.replace(" / ", "\n").into(), - problem: day.status.is_problem(), - } - }) - .collect(); - ui.set_days(Rc::new(slint::VecModel::from(rows)).into()); +fn show_toast(ui: &MainWindow, ui_weak: slint::Weak, toasts: &Arc, text: &str) { + ui.set_toast(text.into()); + let ticket = toasts.fetch_add(1, Ordering::Relaxed) + 1; + let toasts = Arc::clone(toasts); + slint::Timer::single_shot(Duration::from_secs(3), move || { + if toasts.load(Ordering::Relaxed) != ticket { + return; } - Err(err) => { - ui.set_error(err.to_string().into()); - ui.set_days(Rc::new(slint::VecModel::::default()).into()); + if let Some(ui) = ui_weak.upgrade() { + ui.set_toast(String::new().into()); } + }); +} + +fn load(start: NaiveDate, end: NaiveDate) -> punchlog::Result { + let mut config = punchlog::AppConfig::load()?; + punchlog::load_range(&mut config, start, end) +} + +fn show(ui: &MainWindow, report: &punchlog::WeekReport) { + ui.set_error(String::new().into()); + let rows: Vec = 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.replace(" / ", "\n").into(), + problem: day.status.is_problem(), + } + }) + .collect(); + ui.set_days(Rc::new(slint::VecModel::from(rows)).into()); +} + +fn to_slint(day: NaiveDate) -> Date { + Date { + year: day.year(), + month: day.month() as i32, + day: day.day() as i32, } } + +fn from_slint(date: Date) -> Result { + NaiveDate::from_ymd_opt(date.year, date.month as u32, date.day as u32).ok_or(()) +} diff --git a/src/lib.rs b/src/lib.rs index 5dccd49..24c872c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,4 +17,4 @@ pub use error::{Error, Result}; pub use iepms::IepmsClient; pub use phone::{read_phone, save_phone, PhoneIdentity}; pub use record::PunchRecord; -pub use report::{load_last_week, WeekReport}; +pub use report::{load_last_week, load_range, WeekReport}; diff --git a/src/report.rs b/src/report.rs index 7d666a7..2dada4c 100644 --- a/src/report.rs +++ b/src/report.rs @@ -17,6 +17,12 @@ pub struct WeekReport { /// 读取最近一周。token 失效时用账号密码重新登录,并尽量把新 token 写回 `.env`。 pub fn load_last_week(config: &mut AppConfig) -> Result { let (start, end) = last_week(Local::now().date_naive()); + load_range(config, start, end) +} + +/// 读取指定日期范围。结束日之后 5 天内审批通过的补卡仍会计入范围内的那一天。 +pub fn load_range(config: &mut AppConfig, start: NaiveDate, end: NaiveDate) -> Result { + let (start, end) = if start <= end { (start, end) } else { (end, start) }; let rows = match fetch(config, start, end) { Err(Error::AuthExpired) => { let session = auth::login(config)?; diff --git a/ui/app.slint b/ui/app.slint index 9bc4208..fe3d8ab 100644 --- a/ui/app.slint +++ b/ui/app.slint @@ -1,4 +1,4 @@ -import { ScrollView } from "std-widgets.slint"; +import { ScrollView, DatePickerPopup, Date } from "std-widgets.slint"; export struct DayRow { date: string, @@ -11,54 +11,115 @@ export struct DayRow { problem: bool, } -component DayBlock inherits Rectangle { - in property day; - height: 132px; +component DateField inherits Rectangle { + in-out property date; + in property title; + callback accepted(); + + height: 28px; + border-width: 1px; + border-color: #d5d5d0; background: #f7f7f5; + function two(n: int) -> string { + if (n < 10) { + return "0" + n; + } + return "" + n; + } + Text { - x: 24px; - y: 14px; - width: 56px; - text: day.date; - font-size: 15px; - font-weight: 600; + x: 8px; + width: parent.width - 16px; + height: parent.height; + text: root.date.year + "-" + root.two(root.date.month) + "-" + root.two(root.date.day); + font-size: 13px; color: #1a1a1a; - horizontal-alignment: left; + horizontal-alignment: center; + vertical-alignment: center; } - Text { - x: 88px; - y: 16px; - width: 120px; - text: day.time; - font-size: 13px; - color: #6a6a6a; - horizontal-alignment: left; + picker := DatePickerPopup { + title: root.title; + date: root.date; + accepted(date) => { + root.date = date; + root.accepted(); + } } - Text { - x: 216px; - y: 16px; - width: 52px; - text: day.hours; - font-size: 13px; - color: #6a6a6a; - horizontal-alignment: left; + TouchArea { + width: parent.width; + height: parent.height; + clicked => { + picker.show(); + } + } +} + +component DayBlock inherits Rectangle { + in property day; + height: 112px; + background: #f7f7f5; + + HorizontalLayout { + x: 24px; + y: 12px; + width: parent.width - 48px; + height: 22px; + spacing: 8px; + alignment: start; + + Text { + text: day.date; + font-size: 15px; + font-weight: 600; + color: #1a1a1a; + vertical-alignment: center; + } + + Text { + text: day.time; + font-size: 13px; + color: #6a6a6a; + vertical-alignment: center; + } + + Text { + text: day.hours; + font-size: 13px; + color: #6a6a6a; + vertical-alignment: center; + } + + if day.mark != "": Text { + text: day.mark; + font-size: 13px; + color: #6a6a6a; + vertical-alignment: center; + } + + Rectangle { + horizontal-stretch: 1; + min-width: 24px; + clip: true; + + Text { + width: parent.width; + height: parent.height; + text: day.site; + font-size: 12px; + color: #8a8a8a; + horizontal-alignment: right; + vertical-alignment: center; + overflow: elide; + } + } } - if day.mark != "": Text { - x: 276px; - y: 16px; - text: day.mark; - font-size: 13px; - color: #6a6a6a; - horizontal-alignment: left; - } - - Text { + status-text := Text { x: parent.width - 24px - self.width; - y: 16px; + y: parent.height - 16px - self.height; text: day.status; font-size: 13px; color: day.problem ? #1a1a1a : #6a6a6a; @@ -68,20 +129,8 @@ component DayBlock inherits Rectangle { Text { x: 24px; y: 42px; - width: parent.width - 48px; - height: 18px; - text: day.site; - font-size: 12px; - color: #8a8a8a; - horizontal-alignment: left; - overflow: elide; - } - - Text { - x: 24px; - y: 64px; - width: parent.width - 48px; - height: 52px; + width: max(0px, status-text.x - 24px - 16px); + height: parent.height - 42px - 14px; text: day.memo; font-size: 14px; color: #1a1a1a; @@ -101,9 +150,13 @@ component DayBlock inherits Rectangle { export component MainWindow inherits Window { in property <[DayRow]> days; - in property range; + in-out property start-date; + in-out property end-date; + in property loading; + in-out property toast; in property error; callback refresh(); + callback range-changed(); title: "打卡"; preferred-width: 880px; @@ -121,27 +174,45 @@ export component MainWindow inherits Window { horizontal-alignment: left; } + DateField { + x: 76px; + y: 12px; + width: 108px; + date <=> root.start-date; + title: "开始日期"; + accepted => { root.range-changed(); } + } + Text { - x: 72px; - y: 18px; - text: root.range; + x: 190px; + y: 16px; + text: "—"; font-size: 13px; color: #8a8a8a; horizontal-alignment: left; } + DateField { + x: 210px; + y: 12px; + width: 108px; + date <=> root.end-date; + title: "结束日期"; + accepted => { root.range-changed(); } + } + TouchArea { - x: parent.width - 72px; + x: parent.width - 28px - 80px; y: 0px; - width: 72px; + width: 80px; height: 52px; + enabled: !root.loading; Text { - x: 0px; y: 18px; width: parent.width; - text: "刷新"; + text: root.loading ? "正在刷新" : "刷新"; font-size: 13px; - color: #1a1a1a; + color: root.loading ? #8a8a8a : #1a1a1a; horizontal-alignment: right; } clicked => { root.refresh(); } @@ -166,10 +237,10 @@ export component MainWindow inherits Window { horizontal-alignment: left; } - if root.error == "" && root.days.length == 0: Text { + if !root.loading && root.error == "" && root.days.length == 0: Text { x: 24px; y: 68px; - text: "这一周没有打卡记录。"; + text: "这个范围没有打卡记录。"; font-size: 14px; color: #8a8a8a; horizontal-alignment: left; @@ -188,4 +259,44 @@ export component MainWindow inherits Window { } } } + + if root.toast != "": island := Rectangle { + property capsule-width: min(parent.width - 80px, notice.preferred-width + close-mark.preferred-width + 32px); + + x: (parent.width - self.width) / 2; + y: 0px; + width: capsule-width; + height: 30px; + border-top-left-radius: 0px; + border-top-right-radius: 0px; + border-bottom-left-radius: 15px; + border-bottom-right-radius: 15px; + background: #1a1a1a; + + notice := Text { + x: 16px; + height: parent.height; + text: root.toast; + font-size: 13px; + color: #f7f7f5; + vertical-alignment: center; + overflow: elide; + } + + close-mark := Text { + x: parent.width - 8px - self.width; + height: parent.height; + text: "×"; + font-size: 20px; + font-weight: 700; + color: #ff2430; + vertical-alignment: center; + } + + TouchArea { + width: parent.width; + height: parent.height; + clicked => { root.toast = ""; } + } + } }