-
Notifications
You must be signed in to change notification settings - Fork 11
/
extension.js
272 lines (238 loc) · 5.7 KB
/
extension.js
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
265
266
267
268
269
270
271
272
import Clutter from 'gi://Clutter';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GnomeDesktop from 'gi://GnomeDesktop';
import GObject from 'gi://GObject';
import St from 'gi://St';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
const ONES = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
];
const TEENS = [
'',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen',
];
const TENS = [
'',
'ten',
'twenty',
'thirty',
'fourty',
'fifty',
'sixty',
'seventy',
'eighty',
'ninenty',
];
const ORDINAL_ONES = [
'',
'first',
'second',
'third',
'fourth',
'fifth',
'sixth',
'seventh',
'eighth',
'ninth',
];
const ORDINAL_TEENS = [
'',
'eleventh',
'twelfth',
'thirteenth',
'fourteenth',
'fifteenth',
'sixteenth',
'seventeenth',
'eighteenth',
'nineteenth',
];
const ORDINAL_TENS = [
'',
'tenth',
'twentieth',
'thirtieth',
'fourtieth',
'fiftieth',
'sixtieth',
'seventieth',
'eightieth',
'ninetieth',
];
const DAYS = [
'',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday',
];
const MONTHS = [
'',
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december',
];
const WORDS = {
it_is: 'it is',
it_is_a: 'it is a',
o_clock: "o'clock",
noon: 'noon',
midnight: 'midnight',
quarter: 'quarter',
half: 'half',
to: 'to',
past: 'past',
of: 'of',
on: 'on',
};
const WordClockUpdater = GObject.registerClass({
GTypeName: 'WordClockUpdater',
}, class A extends GObject.Object {
constructor() {
super();
this._dateMenu = Main.panel.statusArea['dateMenu'];
this._settings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' });
}
setup() {
this._updateClockAndDate();
this._updateClockId = this._dateMenu._clock.connect('notify::clock',
this._updateClockAndDate.bind(this));
}
teardown() {
this._dateMenu._clockDisplay.text = this._dateMenu._clock.clock;
this._dateMenu._clock.disconnect(this._updateClockId);
this._updateClockId = 0;
}
_appendNumber(num) {
let tensVal = Math.floor(num / 10) % 10;
let onesVal = num % 10;
let res = '';
if (tensVal > 0) {
if (tensVal == 1 && num != 10) {
return TEENS[onesVal];
}
res = TENS[tensVal];
if (onesVal > 0) {
res += ' ';
}
}
if (onesVal > 0 || num == 0) {
res += ONES[onesVal];
}
return res;
}
_appendOrdinal(num) {
let tensVal = Math.floor(num / 10) % 10;
let onesVal = num % 10;
let res = '';
if (tensVal > 0) {
if (tensVal == 1 && num != 10) {
return ORDINAL_TEENS[onesVal];
}
if (onesVal == 0) {
return ORDINAL_TENS[tensVal];
}
res = TENS[tensVal];
if (onesVal > 0) {
res += ' ';
}
}
if (onesVal > 0 || num == 0) {
res += ORDINAL_ONES[onesVal];
}
return res;
}
_timeToWords(hours, minutes) {
let wordHours = hours;
let wordMinutes = minutes;
let res = '';
if (wordMinutes != 0) {
if (wordMinutes == 15) {
res = WORDS.it_is_a + ' ' + WORDS.quarter + ' ' + WORDS.past + ' ';
}
else if (wordMinutes == 45) {
res = WORDS.it_is_a + ' ' + WORDS.quarter + ' ' + WORDS.to + ' ';
wordHours = (wordHours + 1) % 24;
}
else if (wordMinutes == 30) {
res = WORDS.it_is + ' ' + WORDS.half + ' ' + WORDS.past + ' ';
}
else if (wordMinutes < 30) {
res = WORDS.it_is + ' ' + this._appendNumber(wordMinutes) + ' ' + WORDS.past + ' ';
}
else {
res = WORDS.it_is + ' ' + this._appendNumber(60 - wordMinutes) + ' ' + WORDS.to + ' ';
wordHours = (wordHours + 1) % 24;
}
} else {
res = WORDS.it_is + ' ';
}
if (wordHours == 0) {
res += WORDS.midnight;
}
else if (wordHours == 12) {
res += WORDS.noon;
}
else {
res += this._appendNumber(wordHours % 12);
}
if (wordMinutes == 0 && !(wordHours == 0 || wordHours == 12)) {
res = res + ' ' + WORDS.o_clock;
}
return res;
}
_dateToWords(weekDay, day, month) {
return DAYS[weekDay] + ', ' + this._appendOrdinal(day) + ' ' + WORDS.of + ' ' + MONTHS[month];
}
_updateClockAndDate() {
let tz = this._dateMenu._clock.get_timezone();
let date = GLib.DateTime.new_now(tz);
let str = this._timeToWords(date.get_hour(), date.get_minute());
if (this._settings.get_boolean('clock-show-date')) {
str += (' ' + WORDS.on + ' ' + this._dateToWords(date.get_day_of_week(), date.get_day_of_month(), date.get_month()));
}
this._dateMenu._clockDisplay.text = str;
}
}
);
export default class WordClockExtension extends Extension {
enable() {
this._wordClockUpdater = new WordClockUpdater();
this._wordClockUpdater.setup();
}
disable() {
this._wordClockUpdater.teardown();
this._wordClockUpdater = null;
}
}