fix: 日期选择改成小号中文日历

This commit is contained in:
2026-11-19 17:09:51 +08:00
parent f565f7a3c3
commit 4899209621
+174 -9
View File
@@ -1,5 +1,5 @@
// 标准控件:滚动列表、日历弹层、日期类型。
import { ScrollView, DatePickerPopup, Date } from "std-widgets.slint";
// 标准控件:滚动列表、日期类型。
import { ScrollView, Date } from "std-widgets.slint";
// 一天的展示数据,由 Rust 填好后交给列表。
export struct DayRow {
@@ -13,11 +13,174 @@ export struct DayRow {
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;
in property <string> title;
callback accepted();
// 从公元 0 年 1 月起算的月份序号。打开日历前写成当前日期。
property <int> anchor;
height: 28px;
border-width: 1px;
@@ -43,14 +206,17 @@ component DateField inherits Rectangle {
vertical-alignment: center;
}
// 日历弹层。确定后把选中的日期交回给日期框。
picker := DatePickerPopup {
title: root.title;
// 小日历。选中某一天后把日期交回给日期框。
picker := MiniCalendar {
date: root.date;
anchor: root.anchor;
accepted(date) => {
root.date = date;
root.accepted();
}
month-step(step) => {
root.anchor += step;
}
}
// 盖住整个日期框的点击区域,用来打开上面的日历。
@@ -58,6 +224,7 @@ component DateField inherits Rectangle {
width: parent.width;
height: parent.height;
clicked => {
root.anchor = root.date.year * 12 + root.date.month - 1;
picker.show();
}
}
@@ -198,7 +365,6 @@ export component MainWindow inherits Window {
y: 12px;
width: 108px;
date <=> root.start-date;
title: "开始日期";
accepted => { root.range-changed(); }
}
@@ -218,7 +384,6 @@ export component MainWindow inherits Window {
y: 12px;
width: 108px;
date <=> root.end-date;
title: "结束日期";
accepted => { root.range-changed(); }
}