summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell/Clock.qml
blob: edce57e3e944b601c8d950828636e5f3b28f6bc1 (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import QtQml
import QtQuick
import Quickshell
import Custom as Custom
import QtQuick.Controls
import QtQuick.Layouts
import Quickshell.Widgets

Item {
  id: clockItem

  width: clock.contentWidth
  height: parent.height
  anchors.verticalCenter: parent.verticalCenter

  WrapperMouseArea {
    id: clockMouseArea

    anchors.fill: parent
    hoverEnabled: true
    enabled: true

    property real angleRem: 0
    property real sensitivity: 1 / 120

    function scrollYear(event) {
      angleRem += event.angleDelta.y;
      const d = Math.round(angleRem * sensitivity);
      yearCalendar.year += d;
      angleRem -= d / sensitivity;
    }

    onWheel: event => scrollYear(event)

    Item {
      anchors.fill: parent

      Text {
	id: clock
	color: "white"

	anchors.verticalCenter: parent.verticalCenter

	Custom.Chrono {
	  id: chrono

	  onDateChanged: clock.text = format("W{0:%V-%u} {0:%F} {0:%H:%M:%S%Ez}")
	}

	font.pointSize: 10
	font.family: "Fira Sans"
	font.features: { "tnum": 1 }
      }
    }
  }

  PopupWindow {
    id: tooltip

    property bool nextVisible: clockMouseArea.containsMouse || tooltipMouseArea.containsMouse

    anchor {
      item: clockMouseArea
      edges: Edges.Bottom | Edges.Left
    }
    visible: false

    onNextVisibleChanged: hangTimer.restart()

    Timer {
      id: hangTimer
      interval: 100
      onTriggered: tooltip.visible = tooltip.nextVisible
    }

    implicitWidth: clockTooltipContent.width
    implicitHeight: clockTooltipContent.height
    color: "black"

    onVisibleChanged: {
      yearCalendar.year = chrono.date.getFullYear();
      clockMouseArea.angleRem = 0;
    }

    WrapperMouseArea {
      id: tooltipMouseArea

      hoverEnabled: true
      enabled: true

      onWheel: event => clockMouseArea.scrollYear(event)

      anchors.fill: parent

      WrapperItem {
	id: clockTooltipContent

	margin: 8
	leftMargin: 0

	ColumnLayout {
	  Text {
	    id: yearLabel

	    horizontalAlignment: Text.AlignHCenter

	    font.pointSize: 14
	    font.family: "Fira Sans"
	    font.features: { "tnum": 1 }
	    color: "white"

	    text: yearCalendar.year

	    Layout.fillWidth: true
	    Layout.bottomMargin: 8
	  }

	  GridLayout {
	    property int year: chrono.date.getFullYear()

	    id: yearCalendar

	    columns: 3
	    columnSpacing: 16
	    rowSpacing: 16

	    Layout.alignment: Qt.AlignHCenter
	    Layout.fillWidth: false

	    Repeater {
	      model: 12

	      GridLayout {
		columns: 2

		required property int index
		property int month: index

		id: monthCalendar

		Layout.alignment: Qt.AlignTop | Qt.AlignRight
		Layout.fillWidth: false

		Text {
		  Layout.column: 1
		  Layout.fillWidth: true

		  horizontalAlignment: Text.AlignHCenter

		  font.pointSize: 10
		  font.family: "Fira Sans"

		  text: new Date(yearCalendar.year, monthCalendar.month, 1).toLocaleString(Qt.locale("en_DK"), "MMMM")

		  color: "#ffead3"
		}

		DayOfWeekRow {
		  locale: grid.locale

		  Layout.row: 1
		  Layout.column: 1
		  Layout.fillWidth: true

		  delegate: WrapperItem {
		    required property string shortName

		    width: dowLabel.contentWidth + 6

		    Text {
		      id: dowLabel

		      anchors.fill: parent

		      font.pointSize: 10
		      font.family: "Fira Sans"

		      text: parent.shortName
		      color: "#ffcc66"

		      horizontalAlignment: Text.AlignHCenter
		      verticalAlignment: Text.AlignVCenter
		    }
		  }
		}

		WeekNumberColumn {
		  month: grid.month
		  year: grid.year
		  locale: grid.locale

		  Layout.fillHeight: true

		  delegate: Text {
		    required property int weekNumber

		    opacity: {
		      const simple = new Date(weekNumber == 1 && monthCalendar.month == 12 ? yearCalendar.year + 1 : yearCalendar.year, 0, 1 + (weekNumber - 1) * 7);
		      const dayOfWeek = simple.getDay();
		      const isoWeekStart = simple;

		      isoWeekStart.setDate(simple.getDate() - dayOfWeek + 1);
		      if (dayOfWeek > 4) {
			  isoWeekStart.setDate(isoWeekStart.getDate() + 7);
		      }

		      for (let i = 0; i < 7; i++) {
			const dayInWeek = new Date(isoWeekStart);
			dayInWeek.setDate(dayInWeek.getDate() + i);
			if (dayInWeek.getMonth() == monthCalendar.month)
			  return 1;
		      }

		      return 0;
		    }

		    font.pointSize: 10
		    font.family: "Fira Sans"
		    font.features: { "tnum": 1 }

		    text: weekNumber
		    color: "#99ffdd"

		    horizontalAlignment: Text.AlignRight
		    verticalAlignment: Text.AlignVCenter
		  }
		}

		MonthGrid {
		  id: grid

		  year: yearCalendar.year
		  month: monthCalendar.month
		  locale: Qt.locale("en_DK")

		  Layout.fillWidth: true
		  Layout.fillHeight: true

		  delegate: Text {
		      required property var model

		      opacity: model.month === monthCalendar.month ? 1 : 0

		      font.pointSize: 10
		      font.family: "Fira Sans"
		      font.features: { "tnum": 1 }

		      property bool today: chrono.date.getFullYear() == model.year && chrono.date.getMonth() == model.month && chrono.date.getDate() == model.day

		      text: model.day
		      color: today ? "#ff6699" : "white"

		      horizontalAlignment: Text.AlignRight
		      verticalAlignment: Text.AlignVCenter
		  }
		}
	      }
	    }
	  }
	}
      }
    }
  }
}