feat: 增加 egui 打卡列表窗口,便于和 Slint 比较
This commit is contained in:
@@ -0,0 +1,244 @@
|
||||
//! egui 版最近一周打卡列表。数据和 `punchlog-ui` 相同,用来并排比较。
|
||||
|
||||
use eframe::egui;
|
||||
|
||||
const BG: egui::Color32 = egui::Color32::from_rgb(0xf7, 0xf7, 0xf5);
|
||||
const INK: egui::Color32 = egui::Color32::from_rgb(0x1a, 0x1a, 0x1a);
|
||||
const MUTED: egui::Color32 = egui::Color32::from_rgb(0x6a, 0x6a, 0x6a);
|
||||
const FAINT: egui::Color32 = egui::Color32::from_rgb(0x8a, 0x8a, 0x8a);
|
||||
const LINE: egui::Color32 = egui::Color32::from_rgb(0xe6, 0xe6, 0xe3);
|
||||
|
||||
struct Row {
|
||||
date: String,
|
||||
mark: String,
|
||||
time: String,
|
||||
hours: String,
|
||||
status: String,
|
||||
site: String,
|
||||
memo: String,
|
||||
problem: bool,
|
||||
}
|
||||
|
||||
struct View {
|
||||
range: String,
|
||||
error: String,
|
||||
rows: Vec<Row>,
|
||||
}
|
||||
|
||||
struct PunchApp {
|
||||
view: View,
|
||||
}
|
||||
|
||||
fn main() -> eframe::Result {
|
||||
let view = load_view();
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default()
|
||||
.with_inner_size([880.0, 720.0])
|
||||
.with_title("打卡"),
|
||||
renderer: eframe::Renderer::Glow,
|
||||
system_font_fallback: true,
|
||||
persist_window: false,
|
||||
centered: true,
|
||||
..Default::default()
|
||||
};
|
||||
eframe::run_native(
|
||||
"punchlog",
|
||||
options,
|
||||
Box::new(move |cc| {
|
||||
install_look(&cc.egui_ctx);
|
||||
Ok(Box::new(PunchApp { view }))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
impl eframe::App for PunchApp {
|
||||
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
|
||||
ui.painter().rect_filled(ui.max_rect(), 0.0, BG);
|
||||
let refresh = header(ui, &self.view.range);
|
||||
if refresh {
|
||||
self.view = load_view();
|
||||
}
|
||||
hairline(ui);
|
||||
if !self.view.error.is_empty() {
|
||||
message(ui, &self.view.error, INK);
|
||||
return;
|
||||
}
|
||||
if self.view.rows.is_empty() {
|
||||
message(ui, "这一周没有打卡记录。", FAINT);
|
||||
return;
|
||||
}
|
||||
egui::ScrollArea::vertical()
|
||||
.auto_shrink([false, false])
|
||||
.show(ui, |ui| {
|
||||
for row in &self.view.rows {
|
||||
day(ui, row);
|
||||
hairline(ui);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn clear_color(&self, _visuals: &egui::Visuals) -> [f32; 4] {
|
||||
BG.to_normalized_gamma_f32()
|
||||
}
|
||||
}
|
||||
|
||||
fn header(ui: &mut egui::Ui, range: &str) -> bool {
|
||||
ui.allocate_ui_with_layout(
|
||||
egui::vec2(ui.available_width(), 52.0),
|
||||
egui::Layout::left_to_right(egui::Align::Center),
|
||||
|ui| {
|
||||
ui.set_width(ui.available_width());
|
||||
ui.spacing_mut().item_spacing.x = 0.0;
|
||||
ui.add_space(24.0);
|
||||
ui.label(egui::RichText::new("打卡").size(16.0).strong().color(INK));
|
||||
ui.add_space(16.0);
|
||||
ui.label(egui::RichText::new(range).size(13.0).color(FAINT));
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
ui.add_space(24.0);
|
||||
let (rect, response) =
|
||||
ui.allocate_exact_size(egui::vec2(48.0, 32.0), egui::Sense::click());
|
||||
ui.painter().text(
|
||||
rect.right_center(),
|
||||
egui::Align2::RIGHT_CENTER,
|
||||
"刷新",
|
||||
egui::FontId::proportional(13.0),
|
||||
INK,
|
||||
);
|
||||
if response.hovered() {
|
||||
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
|
||||
}
|
||||
response.clicked()
|
||||
})
|
||||
.inner
|
||||
},
|
||||
)
|
||||
.inner
|
||||
}
|
||||
|
||||
fn day(ui: &mut egui::Ui, row: &Row) {
|
||||
let width = ui.available_width();
|
||||
ui.add_space(14.0);
|
||||
ui.horizontal(|ui| {
|
||||
ui.set_min_width(width);
|
||||
ui.spacing_mut().item_spacing.x = 0.0;
|
||||
ui.add_space(24.0);
|
||||
ui.label(egui::RichText::new(&row.date).size(15.0).strong().color(INK));
|
||||
ui.add_space(16.0);
|
||||
ui.label(egui::RichText::new(&row.time).size(13.0).color(MUTED));
|
||||
ui.add_space(16.0);
|
||||
ui.label(egui::RichText::new(&row.hours).size(13.0).color(MUTED));
|
||||
if !row.mark.is_empty() {
|
||||
ui.add_space(12.0);
|
||||
ui.label(egui::RichText::new(&row.mark).size(13.0).color(MUTED));
|
||||
}
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
ui.add_space(24.0);
|
||||
let color = if row.problem { INK } else { MUTED };
|
||||
ui.label(egui::RichText::new(&row.status).size(13.0).color(color));
|
||||
});
|
||||
});
|
||||
ui.add_space(8.0);
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_space(24.0);
|
||||
ui.vertical(|ui| {
|
||||
ui.set_width((width - 48.0).max(0.0));
|
||||
ui.spacing_mut().item_spacing.y = 4.0;
|
||||
if !row.site.is_empty() {
|
||||
ui.add(
|
||||
egui::Label::new(egui::RichText::new(&row.site).size(12.0).color(FAINT))
|
||||
.wrap_mode(egui::TextWrapMode::Truncate),
|
||||
);
|
||||
}
|
||||
if !row.memo.is_empty() {
|
||||
ui.label(egui::RichText::new(&row.memo).size(14.0).color(INK));
|
||||
}
|
||||
});
|
||||
});
|
||||
ui.add_space(14.0);
|
||||
}
|
||||
|
||||
fn message(ui: &mut egui::Ui, text: &str, color: egui::Color32) {
|
||||
ui.add_space(16.0);
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_space(24.0);
|
||||
ui.add(egui::Label::new(egui::RichText::new(text).size(14.0).color(color)).wrap());
|
||||
});
|
||||
}
|
||||
|
||||
fn hairline(ui: &mut egui::Ui) {
|
||||
let rect = ui.available_rect_before_wrap();
|
||||
let y = ui.cursor().top();
|
||||
ui.painter()
|
||||
.hline(rect.x_range(), y, egui::Stroke::new(1.0, LINE));
|
||||
ui.add_space(1.0);
|
||||
}
|
||||
|
||||
fn install_look(ctx: &egui::Context) {
|
||||
install_font(ctx);
|
||||
let mut style = (*ctx.global_style()).clone();
|
||||
style.visuals.panel_fill = BG;
|
||||
style.visuals.window_fill = BG;
|
||||
style.visuals.extreme_bg_color = BG;
|
||||
style.visuals.faint_bg_color = BG;
|
||||
style.visuals.code_bg_color = BG;
|
||||
style.visuals.override_text_color = Some(INK);
|
||||
style.visuals.window_shadow = egui::Shadow::NONE;
|
||||
style.visuals.popup_shadow = egui::Shadow::NONE;
|
||||
style.visuals.selection.bg_fill = egui::Color32::from_rgb(0xe7, 0xe7, 0xe2);
|
||||
style.visuals.selection.stroke.color = egui::Color32::from_rgb(0xc8, 0xc8, 0xc2);
|
||||
style.spacing.item_spacing = egui::vec2(0.0, 4.0);
|
||||
style.spacing.scroll = egui::style::ScrollStyle::floating();
|
||||
ctx.set_global_style(style);
|
||||
}
|
||||
|
||||
fn install_font(ctx: &egui::Context) {
|
||||
let Ok(bytes) = std::fs::read(r"C:\Windows\Fonts\msyh.ttc") else {
|
||||
return;
|
||||
};
|
||||
let mut fonts = egui::FontDefinitions::default();
|
||||
let blob: std::sync::Arc<dyn AsRef<[u8]> + Send + Sync> = std::sync::Arc::new(bytes);
|
||||
fonts.font_data.insert(
|
||||
"yahei".to_owned(),
|
||||
std::sync::Arc::new(egui::FontData::from_blob(blob, 0)),
|
||||
);
|
||||
for family in [egui::FontFamily::Proportional, egui::FontFamily::Monospace] {
|
||||
if let Some(list) = fonts.families.get_mut(&family) {
|
||||
list.insert(0, "yahei".to_owned());
|
||||
}
|
||||
}
|
||||
ctx.set_fonts(fonts);
|
||||
}
|
||||
|
||||
fn load_view() -> View {
|
||||
match load_report() {
|
||||
Ok(report) => View {
|
||||
range: format!("{} — {}", report.start, report.end),
|
||||
error: String::new(),
|
||||
rows: report.days.iter().map(to_row).collect(),
|
||||
},
|
||||
Err(err) => View {
|
||||
range: String::new(),
|
||||
error: err.to_string(),
|
||||
rows: Vec::new(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn load_report() -> punchlog::Result<punchlog::WeekReport> {
|
||||
let mut config = punchlog::AppConfig::load()?;
|
||||
punchlog::load_last_week(&mut config)
|
||||
}
|
||||
|
||||
fn to_row(day: &punchlog::DaySummary) -> Row {
|
||||
let (site, memo) = day.content();
|
||||
Row {
|
||||
date: day.day.format("%m-%d").to_string(),
|
||||
mark: if day.has_supplement { "补" } else { "" }.to_owned(),
|
||||
time: format!("{} – {}", day.first.format("%H:%M"), day.last.format("%H:%M")),
|
||||
hours: format!("{:.1}h", day.hours),
|
||||
status: day.status.label().to_owned(),
|
||||
site,
|
||||
memo: memo.replace(" / ", "\n"),
|
||||
problem: day.status.is_problem(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user