-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalendar.qml
More file actions
194 lines (172 loc) · 5.27 KB
/
Calendar.qml
File metadata and controls
194 lines (172 loc) · 5.27 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import Quickshell
import QtQuick
import QtQuick.Layouts
PopupWindow {
id: calendarWindow
visible: false
color: "transparent"
property var parentBar: null
anchor.window: parentBar
// Internal state for which month/year we're viewing
property int viewYear: new Date().getFullYear()
property int viewMonth: new Date().getMonth() // 0-indexed
// Reset to current month when opened
onVisibleChanged: {
if (visible) {
viewYear = new Date().getFullYear()
viewMonth = new Date().getMonth()
}
}
implicitWidth: 280
implicitHeight: calendarLayout.implicitHeight + 24
Rectangle {
anchors.fill: parent
color: Colors.background
radius: 16
border.width: 2
border.color: Colors.foreground
HoverHandler {
property bool hasHovered: false
onHoveredChanged: {
if (hovered) {
hasHovered = true
} else if (hasHovered && !hovered) {
calendarWindow.visible = false
hasHovered = false
}
}
}
ColumnLayout {
id: calendarLayout
anchors {
top: parent.top
left: parent.left
right: parent.right
topMargin: 12
leftMargin: 12
rightMargin: 12
bottomMargin: 12
}
spacing: 8
// Month/year header with prev/next buttons
RowLayout {
Layout.fillWidth: true
Text {
text: ""
color: prevArea.containsMouse ? Colors.foreground : Colors.inactive
font.pixelSize: 14
MouseArea {
id: prevArea
anchors.fill: parent
hoverEnabled: true
onClicked: {
if (calendarWindow.viewMonth === 0) {
calendarWindow.viewMonth = 11
calendarWindow.viewYear -= 1
} else {
calendarWindow.viewMonth -= 1
}
}
}
}
Text {
Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter
text: Qt.formatDate(new Date(calendarWindow.viewYear, calendarWindow.viewMonth, 1), "MMMM yyyy")
color: Colors.foreground
font.pixelSize: 14
font.bold: true
}
Text {
text: ""
color: nextArea.containsMouse ? Colors.foreground : Colors.inactive
font.pixelSize: 14
MouseArea {
id: nextArea
anchors.fill: parent
hoverEnabled: true
onClicked: {
if (calendarWindow.viewMonth === 11) {
calendarWindow.viewMonth = 0
calendarWindow.viewYear += 1
} else {
calendarWindow.viewMonth += 1
}
}
}
}
}
// Separator
Rectangle {
Layout.fillWidth: true
height: 1
color: Colors.foreground
opacity: 0.2
}
// Day of week headers
Grid {
Layout.fillWidth: true
columns: 7
spacing: 2
Repeater {
model: ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]
Text {
width: (calendarLayout.width - 12) / 7
horizontalAlignment: Text.AlignHCenter
text: modelData
color: Colors.inactive
font.pixelSize: 11
}
}
}
// Day grid
Grid {
id: dayGrid
Layout.fillWidth: true
columns: 7
spacing: 2
property var today: new Date()
property int todayDay: today.getDate()
property int todayMonth: today.getMonth()
property int todayYear: today.getFullYear()
// Day of week the 1st falls on (0=Sun..6=Sat), shifted to Mon-first
property int firstDayOfWeek: {
var d = new Date(calendarWindow.viewYear, calendarWindow.viewMonth, 1).getDay()
return (d + 6) % 7 // Monday = 0
}
property int daysInMonth: new Date(calendarWindow.viewYear, calendarWindow.viewMonth + 1, 0).getDate()
property int totalCells: firstDayOfWeek + daysInMonth
Repeater {
model: Math.ceil(dayGrid.totalCells / 7) * 7
Rectangle {
width: (calendarLayout.width - 12) / 7
height: width
radius: width / 2
color: {
var day = index - dayGrid.firstDayOfWeek + 1
if (day < 1 || day > dayGrid.daysInMonth) return "transparent"
if (day === dayGrid.todayDay &&
calendarWindow.viewMonth === dayGrid.todayMonth &&
calendarWindow.viewYear === dayGrid.todayYear)
return Colors.selection
return "transparent"
}
Text {
anchors.centerIn: parent
property int day: index - dayGrid.firstDayOfWeek + 1
text: (day >= 1 && day <= dayGrid.daysInMonth) ? day : ""
color: {
if (day === dayGrid.todayDay &&
calendarWindow.viewMonth === dayGrid.todayMonth &&
calendarWindow.viewYear === dayGrid.todayYear)
return Colors.foreground
return day >= 1 && day <= dayGrid.daysInMonth ? Colors.foreground : "transparent"
}
font.pixelSize: 12
}
}
}
}
}
}
}