From 4899209621e73d002b434c4c9167a55c98f3ae08 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Thu, 19 Nov 2026 17:09:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=A5=E6=9C=9F=E9=80=89=E6=8B=A9?= =?UTF-8?q?=E6=94=B9=E6=88=90=E5=B0=8F=E5=8F=B7=E4=B8=AD=E6=96=87=E6=97=A5?= =?UTF-8?q?=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/app.slint | 183 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 174 insertions(+), 9 deletions(-) diff --git a/ui/app.slint b/ui/app.slint index 198cd5a..03b4981 100644 --- a/ui/app.slint +++ b/ui/app.slint @@ -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; + // 从公元 0 年 1 月起算的月份序号,由外面在打开前设好,翻月也由外面改。 + in property anchor; + callback accepted(Date); + callback month-step(int); + + close-policy: PopupClosePolicy.close-on-click-outside; + + property view-year: root.anchor / 12; + property 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 day-num: n - root.weekday(root.view-year, root.view-month, 1) + 1; + property in-month: day-num >= 1 && day-num <= root.days-in-month(root.view-month, root.view-year); + property 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; - in property title; callback accepted(); + // 从公元 0 年 1 月起算的月份序号。打开日历前写成当前日期。 + property 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(); } }