Repository navigation
Expand file tree
/
Copy pathModel.js
More file actions
379 lines (356 loc) · 14.8 KB
/
Copy pathModel.js
File metadata and controls
379 lines (356 loc) · 14.8 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
.pragma library
// ---------- localization ----------
//
// Omarchy itself ships no catalogue: each plugin carries its own strings. The
// dictionary below is keyed by a stable id, and `t(key, lang)` falls back to
// English and then to the key itself, so a missing translation shows up as a
// readable label rather than an empty field.
//
// `resolveLang` maps anything the settings or the environment can produce
// (ru_RU.UTF-8, ru-RU, Russian, en_US, ...) onto a supported code.
var SUPPORTED = ["en", "ru"]
var STRINGS = {
en: {
"plugin.title": "Shutdown timer",
"state.none": "No timer set",
"state.noneHint": "Wheel over the dial, or type below · hold the icon to cancel",
"state.paused": "%1 — paused",
"state.countdown": "%1 — counting down",
"state.active": "%1 on a timer",
"state.ready": "Ready to schedule",
"action.poweroff": "Power off",
"action.reboot": "Reboot",
"action.suspend": "Suspend",
"action.hibernate": "Hibernate",
"section.action": "ACTION",
"section.presets": "PRESETS",
"section.exact": "EXACT TIME",
"mode.relative": "In N minutes",
"mode.absolute": "At HH:MM",
"launch.enterTime": "Enter a time as HH:MM",
"launch.at": "Start at %1",
"launch.in": "Start in %1 min",
"launch.update": "Update to %1 min",
"launch.reset": "Reset timer (start over)",
"btn.cancel": "Cancel",
"confirm.title": "Cancel the timer?",
"confirm.body": "%1 left, then %2. This cannot be undone.",
"confirm.keep": "Keep it",
"confirm.yes": "Cancel timer",
"btn.snooze": "+5 minutes",
"btn.pause": "Pause",
"btn.resume": "Resume",
"unit.min": "min",
"unit.minShort": "m",
"unit.hourShort": "h",
"unit.secShort": "s",
"time.in": "in",
"error.badTime": "Enter the time as HH:MM",
"error.daemon": "The timer daemon is not responding — nothing is armed",
"overlay.cancel": "Cancel",
"overlay.snooze": "+%1 min",
"overlay.hint": "Esc or the button cancels · Enter postpones",
"overlay.action": "Power off",
"notify.early": "%1 in %2",
"notify.now": "%1 in %2",
"notify.title": "Shutdown timer",
"notify.system": "System",
"tooltip.idle": "Shutdown timer · click for the menu, hold to cancel",
"tooltip.cancelHint": "hold to cancel",
"tooltip.countdown": "Final countdown: %1 · hold to cancel",
"state.scheduled": "%1 — scheduled",
"section.repeat": "REPEAT",
"section.advanced": "ADVANCED",
"section.history": "HISTORY",
"hero.scrub": "Wheel over the dial: ±1 min, Shift ±5 · or drag the ring",
"hero.delta": "%1 min to the deadline",
"hero.snoozeRefused": "Snooze limit reached — the deadline did not move",
"time.hint": "Drag the ring · Shift for fine · wheel ±1 min",
"wheel.pending": "%1 min — click to start",
"tray.schedule": "Schedule",
"tray.more": "More",
"tray.history": "History",
"repeat.once": "Once",
"repeat.daily": "Every day",
"repeat.weekdays": "Weekdays",
"repeat.weekends": "Weekends",
"repeat.add": "Add schedule",
"repeat.remove": "Remove",
"repeat.none": "No schedules yet",
"adv.dryRun": "Dry run (do not really power off)",
"adv.dryRunHint": "The action is only logged",
"adv.battery": "Do not power off on battery",
"adv.quiet": "Quiet mode (no notifications)",
"adv.wait": "Wait for sleep/shutdown inhibitors",
"adv.fade": "Dim screen and sound before the action",
"adv.dnd": "Notifications are silenced",
"adv.snoozeStep": "Snooze step",
"history.empty": "No runs yet",
"history.executed": "executed",
"history.cancelled": "cancelled",
"history.snoozed": "postponed",
"history.set": "set",
"history.scheduled": "by schedule",
"week.1": "Mon",
"week.2": "Tue",
"week.3": "Wed",
"week.4": "Thu",
"week.5": "Fri",
"week.6": "Sat",
"week.7": "Sun",
"reason.battery": "Battery guard: will suspend instead",
"reason.inhibited": "Waiting for: %1",
},
ru: {
"plugin.title": "Таймер выключения",
"state.none": "Таймер не задан",
"state.noneHint": "Колесо над диском или введите ниже · удерживайте иконку, чтобы отменить",
"state.paused": "%1 — пауза",
"state.countdown": "%1 — отсчёт",
"state.active": "%1 по таймеру",
"state.ready": "Готово к планированию",
"action.poweroff": "Выключить",
"action.reboot": "Перезагрузить",
"action.suspend": "Сон",
"action.hibernate": "Гибернация",
"section.action": "ДЕЙСТВИЕ",
"section.presets": "ПРЕСЕТЫ",
"section.exact": "ТОЧНОЕ ВРЕМЯ",
"mode.relative": "Через N минут",
"mode.absolute": "В ЧЧ:ММ",
"launch.enterTime": "Введите время ЧЧ:ММ",
"launch.at": "Запустить в %1",
"launch.in": "Запустить на %1 мин",
"launch.update": "Обновить на %1 мин",
"launch.reset": "Сбросить таймер (начать заново)",
"btn.cancel": "Отмена",
"confirm.title": "Отменить таймер?",
"confirm.body": "Осталось %1, затем %2. Вернуть будет нельзя.",
"confirm.keep": "Оставить",
"confirm.yes": "Отменить таймер",
"btn.snooze": "+5 минут",
"btn.pause": "Пауза",
"btn.resume": "Продолжить",
"unit.min": "мин",
"unit.minShort": "м",
"unit.hourShort": "ч",
"unit.secShort": "с",
"time.in": "через",
"error.badTime": "Введите время в формате ЧЧ:ММ",
"error.daemon": "Демон таймера не отвечает — таймер не взведён",
"overlay.cancel": "Отмена",
"overlay.snooze": "+%1 мин",
"overlay.hint": "Esc или кнопка — отменить · Enter — отложить",
"overlay.action": "Выключение",
"notify.early": "%1 через %2",
"notify.now": "%1 через %2",
"notify.title": "Таймер выключения",
"notify.system": "Система",
"tooltip.idle": "Таймер выключения · ЛКМ — меню, удержание — отмена",
"tooltip.cancelHint": "удержание — отмена",
"tooltip.countdown": "Финальный отсчёт: %1 · удержание — отмена",
"state.scheduled": "%1 — по расписанию",
"section.repeat": "ПОВТОР",
"section.advanced": "ДОПОЛНИТЕЛЬНО",
"section.history": "ИСТОРИЯ",
"hero.scrub": "Колесо над диском: ±1 мин, Shift ±5 · или тяните кольцо",
"hero.delta": "%1 мин к выключению",
"hero.snoozeRefused": "Лимит отложить исчерпан — время не сдвинулось",
"time.hint": "Тяните кольцо · Shift — точно · колесо ±1 мин",
"wheel.pending": "%1 мин — ЛКМ запустить",
"tray.schedule": "Расписание",
"tray.more": "Ещё",
"tray.history": "История",
"repeat.once": "Однократно",
"repeat.daily": "Каждый день",
"repeat.weekdays": "Будни",
"repeat.weekends": "Выходные",
"repeat.add": "Добавить расписание",
"repeat.remove": "Удалить",
"repeat.none": "Расписаний пока нет",
"adv.dryRun": "Сухой прогон (не выключать ПК)",
"adv.dryRunHint": "Действие только пишется в журнал",
"adv.battery": "Не выключать на батарее",
"adv.quiet": "Тихий режим (без уведомлений)",
"adv.wait": "Ждать завершения загрузок",
"adv.fade": "Приглушать экран и звук перед действием",
"adv.dnd": "Уведомления заглушены",
"adv.snoozeStep": "Шаг отложить",
"history.empty": "Запусков пока нет",
"history.executed": "выполнено",
"history.cancelled": "отменено",
"history.snoozed": "отложено",
"history.set": "задан",
"history.scheduled": "по расписанию",
"week.1": "Пн",
"week.2": "Вт",
"week.3": "Ср",
"week.4": "Чт",
"week.5": "Пт",
"week.6": "Сб",
"week.7": "Вс",
"reason.battery": "Режим батареи: будет сон вместо выключения",
"reason.inhibited": "Ждём: %1"
}
}
// "ru_RU.UTF-8" | "ru-RU" | "Russian" | "en_US" → "ru" | "en"
function resolveLang(value, fallback) {
var fb = fallback === undefined ? "en" : String(fallback)
var raw = String(value === undefined || value === null ? "" : value).trim().toLowerCase()
if (raw === "") return fb
if (raw === "auto" || raw === "system") return fb
var head = raw.split(/[._@-]/)[0]
if (head === "russian" || head === "русский") head = "ru"
if (head === "english" || head === "английский") head = "en"
if (SUPPORTED.indexOf(head) !== -1) return head
// a bare "en"/"ru" that arrived as a longer tag was already handled; here the
// tag is something we do not carry — fall back rather than showing keys
for (var i = 0; i < SUPPORTED.length; i++) {
if (raw.indexOf(SUPPORTED[i]) === 0) return SUPPORTED[i]
}
return fb
}
// key → localized text; %1..%9 are substituted from `args` (array or scalar)
function t(key, lang, args) {
var code = resolveLang(lang, "en")
var table = STRINGS[code] || STRINGS.en
var text = table[key]
if (text === undefined) text = STRINGS.en[key]
if (text === undefined) return String(key)
if (args === undefined || args === null) return text
var list = (args instanceof Array) ? args : [args]
for (var i = 0; i < list.length; i++) {
text = text.replace("%" + (i + 1), String(list[i]))
}
return text
}
// ---------- pure formatting & null-safe access ----------
function field(obj, path, fallback) {
if (obj === null || obj === undefined) return fallback
var parts = String(path).split(".")
var cur = obj
for (var i = 0; i < parts.length; i++) {
if (cur === null || cur === undefined) return fallback
cur = cur[parts[i]]
}
return cur === null || cur === undefined ? fallback : cur
}
function known(value) {
return typeof value === "number" && isFinite(value)
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value))
}
// seconds → "12:34" or "1:23:45"
function formatTime(seconds) {
if (!known(seconds) || seconds < 0) return "--:--"
var s = Math.ceil(seconds)
var h = Math.floor(s / 3600)
var m = Math.floor((s % 3600) / 60)
var sec = s % 60
if (h > 0) {
return h + ":" + String(m).padStart(2, "0") + ":" + String(sec).padStart(2, "0")
}
return String(m).padStart(2, "0") + ":" + String(sec).padStart(2, "0")
}
// seconds → human readable, short units per language ("1h 5m", "1ч 5м")
function formatTimeHuman(seconds, lang) {
if (!known(seconds) || seconds < 0) return ""
var s = Math.ceil(seconds)
var h = Math.floor(s / 3600)
var m = Math.floor((s % 3600) / 60)
var sec = s % 60
var parts = []
if (h > 0) parts.push(h + t("unit.hourShort", lang))
if (m > 0) parts.push(m + t("unit.minShort", lang))
parts.push(sec + t("unit.secShort", lang))
return parts.join(" ")
}
// minutes → "5m" or "1h 30m"
function formatMinutes(minutes, lang) {
if (!known(minutes) || minutes < 1) return ""
if (minutes < 60) return String(minutes) + t("unit.minShort", lang)
var h = Math.floor(minutes / 60)
var m = minutes % 60
return h + t("unit.hourShort", lang) + (m > 0 ? " " + m + t("unit.minShort", lang) : "")
}
// action key → localized name
function actionLabel(action, lang) {
if (action === "poweroff") return t("action.poweroff", lang)
if (action === "reboot") return t("action.reboot", lang)
if (action === "suspend") return t("action.suspend", lang)
if (action === "hibernate") return t("action.hibernate", lang)
return action || "—"
}
// action key → Nerd Font glyph
function actionGlyph(action) {
if (action === "reboot") return "\uf021" // nf-fa-refresh
if (action === "suspend") return "\uf186" // nf-fa-moon_o
if (action === "hibernate") return "\uf186"
return "\uf011" // nf-fa-power_off
}
// state → color intent: "normal" | "warning" | "urgent"
function stateColor(status, timeLeft, overlaySeconds) {
if (status === "COUNTDOWN") return "urgent"
if (status === "ACTIVE") {
if (timeLeft <= 60) return "urgent"
if (timeLeft <= 300) return "warning"
return "normal"
}
return "normal"
}
// ISO timestamp → "HH:MM"
function formatTimeOnly(timestamp) {
if (!known(timestamp) || timestamp <= 0) return ""
var d = new Date(timestamp * 1000)
return String(d.getHours()).padStart(2, "0") + ":" + String(d.getMinutes()).padStart(2, "0")
}
// ---------- contrast (TZ 12: text ≥ 4.5:1 on the theme surface) ----------
// Some themes pair a warm red with a warm light surface and land just under
// 4.5:1 (Flexoki Light: #D14D41 on #FFFCF0 is 4.21). Rather than hope, the
// text color is nudged toward black or white — whichever direction lifts the
// ratio — until it clears the bar.
function _srgb(c) {
c = c / 255
return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4)
}
function _hexToRgb(hex) {
var h = String(hex).replace("#", "")
if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2]
var n = parseInt(h, 16)
if (isNaN(n)) return null
return [ (n >> 16) & 255, (n >> 8) & 255, n & 255 ]
}
function _luminance(hex) {
var rgb = _hexToRgb(hex)
if (!rgb) return 0
return 0.2126 * _srgb(rgb[0]) + 0.7152 * _srgb(rgb[1]) + 0.0722 * _srgb(rgb[2])
}
function contrastRatio(a, b) {
var la = _luminance(a), lb = _luminance(b)
var hi = Math.max(la, lb), lo = Math.min(la, lb)
return (hi + 0.05) / (lo + 0.05)
}
function _mix(hexA, hexB, t) {
var a = _hexToRgb(hexA), b = _hexToRgb(hexB)
if (!a || !b) return hexA
var out = "#"
for (var i = 0; i < 3; i++) {
var v = Math.round(a[i] * (1 - t) + b[i] * t)
out += (v < 16 ? "0" : "") + v.toString(16)
}
return out
}
// Returns `fg` itself when it already clears `minRatio` on `bg`; otherwise the
// nearest mix toward black/white that clears it, so urgency keeps its hue.
function readableOn(fg, bg, minRatio) {
minRatio = minRatio || 4.5
var target = String(fg)
if (contrastRatio(target, String(bg)) >= minRatio) return target
var toward = _luminance(String(bg)) > 0.5 ? "#000000" : "#ffffff"
for (var t = 0.05; t <= 0.85; t += 0.05) {
var candidate = _mix(target, toward, t)
if (contrastRatio(candidate, String(bg)) >= minRatio) return candidate
}
return toward
}