498 lines
15 KiB
Plaintext
498 lines
15 KiB
Plaintext
// 标准控件:滚动列表、日期类型。
|
||
import { ScrollView, Date } from "std-widgets.slint";
|
||
|
||
// 一天的展示数据,由 Rust 填好后交给列表。
|
||
export struct DayRow {
|
||
date: string,
|
||
mark: string,
|
||
time: string,
|
||
hours: string,
|
||
status: string,
|
||
site: string,
|
||
memo: string,
|
||
problem: bool,
|
||
}
|
||
|
||
// 小日历。点某一天就选中并关闭,点外面则取消。
|
||
component MiniCalendar inherits PopupWindow {
|
||
in property <Date> date;
|
||
// 从公元 0 年 1 月起算的月份序号,由外面在打开前设好,翻月也由外面改。
|
||
in property <int> anchor;
|
||
callback accepted(Date);
|
||
callback month-step(int);
|
||
|
||
close-policy: PopupClosePolicy.close-on-click-outside;
|
||
|
||
property <int> view-year: root.anchor / 12;
|
||
property <int> view-month: Math.mod(root.anchor, 12) + 1;
|
||
// Sakamoto 公式里各月的偏移,用来算某天是星期几。0 是星期日。
|
||
property <[int]> month-shift: [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
|
||
property <[int]> cells: [
|
||
0, 1, 2, 3, 4, 5, 6,
|
||
7, 8, 9, 10, 11, 12,
|
||
13, 14, 15, 16, 17, 18, 19,
|
||
20, 21, 22, 23, 24, 25, 26,
|
||
27, 28, 29, 30, 31, 32, 33,
|
||
34, 35, 36, 37, 38, 39, 40, 41
|
||
];
|
||
|
||
function two(n: int) -> string {
|
||
if (n < 10) {
|
||
return "0" + n;
|
||
}
|
||
return "" + n;
|
||
}
|
||
|
||
function days-in-month(month: int, year: int) -> int {
|
||
if (month == 2) {
|
||
if (Math.mod(year, 400) == 0 || (Math.mod(year, 4) == 0 && Math.mod(year, 100) != 0)) {
|
||
return 29;
|
||
}
|
||
return 28;
|
||
}
|
||
if (month == 4 || month == 6 || month == 9 || month == 11) {
|
||
return 30;
|
||
}
|
||
return 31;
|
||
}
|
||
|
||
function weekday(year: int, month: int, day: int) -> int {
|
||
if (month < 3) {
|
||
return Math.mod((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + root.month-shift[month - 1] + day, 7);
|
||
}
|
||
return Math.mod(year + year / 4 - year / 100 + year / 400 + root.month-shift[month - 1] + day, 7);
|
||
}
|
||
|
||
function shift-month(step: int) {
|
||
root.month-step(step);
|
||
}
|
||
|
||
Rectangle {
|
||
background: #f7f7f5;
|
||
border-width: 1px;
|
||
border-color: #e6e6e3;
|
||
width: 248px;
|
||
|
||
VerticalLayout {
|
||
padding: 12px;
|
||
spacing: 8px;
|
||
|
||
HorizontalLayout {
|
||
spacing: 8px;
|
||
alignment: start;
|
||
|
||
Text {
|
||
text: root.view-year + "年" + root.two(root.view-month) + "月";
|
||
font-size: 14px;
|
||
font-weight: 600;
|
||
color: #1a1a1a;
|
||
vertical-alignment: center;
|
||
}
|
||
|
||
Rectangle {
|
||
horizontal-stretch: 1;
|
||
}
|
||
|
||
TouchArea {
|
||
width: 36px;
|
||
height: 24px;
|
||
Text {
|
||
width: parent.width;
|
||
height: parent.height;
|
||
text: "上月";
|
||
font-size: 12px;
|
||
color: #1a1a1a;
|
||
horizontal-alignment: center;
|
||
vertical-alignment: center;
|
||
}
|
||
clicked => { root.shift-month(-1); }
|
||
}
|
||
|
||
TouchArea {
|
||
width: 36px;
|
||
height: 24px;
|
||
Text {
|
||
width: parent.width;
|
||
height: parent.height;
|
||
text: "下月";
|
||
font-size: 12px;
|
||
color: #1a1a1a;
|
||
horizontal-alignment: center;
|
||
vertical-alignment: center;
|
||
}
|
||
clicked => { root.shift-month(1); }
|
||
}
|
||
}
|
||
|
||
// 星期标题,从周日开始。
|
||
HorizontalLayout {
|
||
spacing: 2px;
|
||
Text { width: 30px; text: "日"; font-size: 12px; color: #8a8a8a; horizontal-alignment: center; }
|
||
Text { width: 30px; text: "一"; font-size: 12px; color: #8a8a8a; horizontal-alignment: center; }
|
||
Text { width: 30px; text: "二"; font-size: 12px; color: #8a8a8a; horizontal-alignment: center; }
|
||
Text { width: 30px; text: "三"; font-size: 12px; color: #8a8a8a; horizontal-alignment: center; }
|
||
Text { width: 30px; text: "四"; font-size: 12px; color: #8a8a8a; horizontal-alignment: center; }
|
||
Text { width: 30px; text: "五"; font-size: 12px; color: #8a8a8a; horizontal-alignment: center; }
|
||
Text { width: 30px; text: "六"; font-size: 12px; color: #8a8a8a; horizontal-alignment: center; }
|
||
}
|
||
|
||
// 六行七列。点中当月的某一天即选中。
|
||
GridLayout {
|
||
spacing: 2px;
|
||
for n in root.cells: Rectangle {
|
||
property <int> day-num: n - root.weekday(root.view-year, root.view-month, 1) + 1;
|
||
property <bool> in-month: day-num >= 1 && day-num <= root.days-in-month(root.view-month, root.view-year);
|
||
property <bool> chosen: in-month && day-num == root.date.day && root.view-month == root.date.month && root.view-year == root.date.year;
|
||
|
||
row: n / 7;
|
||
col: Math.mod(n, 7);
|
||
width: 30px;
|
||
height: 30px;
|
||
background: chosen ? #1a1a1a : #f7f7f5;
|
||
|
||
Text {
|
||
width: parent.width;
|
||
height: parent.height;
|
||
text: in-month ? day-num : "";
|
||
font-size: 13px;
|
||
color: chosen ? #f7f7f5 : #1a1a1a;
|
||
horizontal-alignment: center;
|
||
vertical-alignment: center;
|
||
}
|
||
|
||
TouchArea {
|
||
enabled: in-month;
|
||
width: parent.width;
|
||
height: parent.height;
|
||
clicked => {
|
||
root.accepted({ year: root.view-year, month: root.view-month, day: day-num });
|
||
root.close();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 日期框。外框是矩形,点击后弹出上面的小日历。
|
||
component DateField inherits Rectangle {
|
||
in-out property <Date> date;
|
||
callback accepted();
|
||
// 从公元 0 年 1 月起算的月份序号。打开日历前写成当前日期。
|
||
property <int> anchor;
|
||
|
||
height: 28px;
|
||
border-width: 1px;
|
||
border-color: #d5d5d0;
|
||
background: #f7f7f5;
|
||
|
||
function two(n: int) -> string {
|
||
if (n < 10) {
|
||
return "0" + n;
|
||
}
|
||
return "" + n;
|
||
}
|
||
|
||
// 框里显示的日期文字,例如 2026-11-13。
|
||
Text {
|
||
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: center;
|
||
vertical-alignment: center;
|
||
}
|
||
|
||
// 小日历。选中某一天后把日期交回给日期框。
|
||
picker := MiniCalendar {
|
||
date: root.date;
|
||
anchor: root.anchor;
|
||
accepted(date) => {
|
||
root.date = date;
|
||
root.accepted();
|
||
}
|
||
month-step(step) => {
|
||
root.anchor += step;
|
||
}
|
||
}
|
||
|
||
// 盖住整个日期框的点击区域,用来打开上面的日历。
|
||
TouchArea {
|
||
width: parent.width;
|
||
height: parent.height;
|
||
clicked => {
|
||
root.anchor = root.date.year * 12 + root.date.month - 1;
|
||
picker.show();
|
||
}
|
||
}
|
||
}
|
||
|
||
// 列表中的一天。外框是矩形。
|
||
component DayBlock inherits Rectangle {
|
||
in property <DayRow> day;
|
||
height: 112px;
|
||
background: #f7f7f5;
|
||
|
||
// 顶行横向排列:日期、时间、时长靠左,站点靠右。
|
||
HorizontalLayout {
|
||
x: 24px;
|
||
y: 12px;
|
||
width: parent.width - 48px;
|
||
height: 22px;
|
||
spacing: 8px;
|
||
alignment: start;
|
||
|
||
// 日期,例如 11-13。
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 右下角状态:正常、缺卡或时长不足。
|
||
status-text := Text {
|
||
x: parent.width - 24px - self.width;
|
||
y: parent.height - 16px - self.height;
|
||
text: day.status;
|
||
font-size: 13px;
|
||
color: day.problem ? #1a1a1a : #6a6a6a;
|
||
horizontal-alignment: right;
|
||
}
|
||
|
||
// 打卡内容。宽度停在状态左边,避免盖住右下角。
|
||
Text {
|
||
x: 24px;
|
||
y: 42px;
|
||
width: max(0px, status-text.x - 24px - 16px);
|
||
height: parent.height - 42px - 14px;
|
||
text: day.memo;
|
||
font-size: 14px;
|
||
color: #1a1a1a;
|
||
wrap: word-wrap;
|
||
horizontal-alignment: left;
|
||
vertical-alignment: top;
|
||
}
|
||
|
||
// 这一天底部的分隔线。
|
||
Rectangle {
|
||
x: 0px;
|
||
y: parent.height - 1px;
|
||
width: parent.width;
|
||
height: 1px;
|
||
background: #e6e6e3;
|
||
}
|
||
}
|
||
|
||
// 主窗口。
|
||
export component MainWindow inherits Window {
|
||
in property <[DayRow]> days;
|
||
in-out property <Date> start-date;
|
||
in-out property <Date> end-date;
|
||
in property <bool> loading;
|
||
in-out property <string> toast;
|
||
in property <string> error;
|
||
callback refresh();
|
||
callback range-changed();
|
||
|
||
title: "打卡";
|
||
preferred-width: 880px;
|
||
preferred-height: 720px;
|
||
background: #f7f7f5;
|
||
default-font-family: "Segoe UI";
|
||
|
||
// 左上角标题。
|
||
Text {
|
||
x: 24px;
|
||
y: 16px;
|
||
text: "打卡";
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
color: #1a1a1a;
|
||
horizontal-alignment: left;
|
||
}
|
||
|
||
// 开始日期。选完后通知 Rust 按新范围重新拉取。
|
||
DateField {
|
||
x: 76px;
|
||
y: 12px;
|
||
width: 108px;
|
||
date <=> root.start-date;
|
||
accepted => { root.range-changed(); }
|
||
}
|
||
|
||
// 两个日期中间的间隔符。
|
||
Text {
|
||
x: 190px;
|
||
y: 16px;
|
||
text: "—";
|
||
font-size: 13px;
|
||
color: #8a8a8a;
|
||
horizontal-alignment: left;
|
||
}
|
||
|
||
// 结束日期。
|
||
DateField {
|
||
x: 210px;
|
||
y: 12px;
|
||
width: 108px;
|
||
date <=> root.end-date;
|
||
accepted => { root.range-changed(); }
|
||
}
|
||
|
||
// 右上角刷新按钮。加载时文字变成「正在刷新」,并且不能再点。
|
||
TouchArea {
|
||
x: parent.width - 28px - 80px;
|
||
y: 0px;
|
||
width: 80px;
|
||
height: 52px;
|
||
enabled: !root.loading;
|
||
Text {
|
||
y: 18px;
|
||
width: parent.width;
|
||
text: root.loading ? "正在刷新" : "刷新";
|
||
font-size: 13px;
|
||
color: root.loading ? #8a8a8a : #1a1a1a;
|
||
horizontal-alignment: right;
|
||
}
|
||
clicked => { root.refresh(); }
|
||
}
|
||
|
||
// 标题区域和列表之间的分隔线。
|
||
Rectangle {
|
||
x: 0px;
|
||
y: 51px;
|
||
width: parent.width;
|
||
height: 1px;
|
||
background: #e6e6e3;
|
||
}
|
||
|
||
// 拉取失败时显示的错误文字。
|
||
if root.error != "": Text {
|
||
x: 24px;
|
||
y: 68px;
|
||
width: parent.width - 48px;
|
||
text: root.error;
|
||
wrap: word-wrap;
|
||
font-size: 14px;
|
||
color: #1a1a1a;
|
||
horizontal-alignment: left;
|
||
}
|
||
|
||
// 这个日期范围内没有打卡记录。
|
||
if !root.loading && root.error == "" && root.days.length == 0: Text {
|
||
x: 24px;
|
||
y: 68px;
|
||
text: "这个范围没有打卡记录。";
|
||
font-size: 14px;
|
||
color: #8a8a8a;
|
||
horizontal-alignment: left;
|
||
}
|
||
|
||
// 打卡列表。ScrollView 负责滚动,里面的纵向布局把每一天从上往下排。
|
||
if root.error == "" && root.days.length > 0: ScrollView {
|
||
x: 0px;
|
||
y: 52px;
|
||
width: parent.width;
|
||
height: parent.height - 52px;
|
||
VerticalLayout {
|
||
alignment: start;
|
||
padding: 0px;
|
||
for day in root.days: DayBlock {
|
||
day: day;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 刷新完成后的顶部提示条。贴住标题栏,点击可关,3 秒后由 Rust 自动关掉。
|
||
if root.toast != "": island := Rectangle {
|
||
property <length> 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;
|
||
}
|
||
|
||
// 右侧红色关闭标记,距提示条右边缘 8 像素。
|
||
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 = ""; }
|
||
}
|
||
}
|
||
}
|