-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.swift
More file actions
2334 lines (2131 loc) ยท 117 KB
/
Copy pathmain.swift
File metadata and controls
2334 lines (2131 loc) ยท 117 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import AppKit
import CoreGraphics
import IOKit.ps
import ServiceManagement
// SleepBar โ a menu-bar tool for temporarily scheduling "screen off time".
//
// Semantics:
// ยท "Screen Off Timer" picks one duration โ the screen stays awake for that long
// (caffeinate -dis), then the "When Time's Up" action runs. "Never" = stay awake
// indefinitely. Clicking the selected item again = cancel (back to system defaults).
// ยท "When Time's Up" is a remembered preference: lock & turn off display /
// lock, turn off & sleep.
enum EndAction: String {
case lockOnly = "lockOnly" // lock the screen
case lockOff = "lockOff" // lock & turn off the display
case lockSleep = "lockSleep" // lock, turn off & sleep
case lockOffNoSleep = "lockOffNoSleep" // lock & turn off the display but keep the system awake
case screenOff = "screenOff" // dim the built-in display to 0 (no lock), keep awake, auto-restore on return
}
enum Lang: String, CaseIterable {
case zh, en, es, ar, pt, ja, de // pt = Brazilian Portuguese
// Shown in the Language submenu, always in the language itself
var nativeName: String {
switch self {
case .zh: return "ไธญๆ"
case .en: return "English"
case .es: return "Espaรฑol"
case .ar: return "ุงูุนุฑุจูุฉ"
case .pt: return "Portuguรชs (Brasil)"
case .ja: return "ๆฅๆฌ่ช"
case .de: return "Deutsch"
}
}
// Best match for the system's preferred language; English if none matches
static var systemDefault: Lang {
let pref = Locale.preferredLanguages.first ?? "en"
for l in Lang.allCases where pref.hasPrefix(l.rawValue) { return l }
return .en
}
}
// MARK: - Localization tables
// UI strings for every supported language, keyed by a stable identifier.
// %d / %@ placeholders are filled via String(format:).
private let l10n: [Lang: [String: String]] = [
.zh: [
"menu.feedback": "ๅ้ฆไธๅปบ่ฎฎ",
"feedback.type": "็ฑปๅ",
"feedback.bug": "Bug",
"feedback.feature": "ๅ่ฝๅปบ่ฎฎ",
"feedback.title": "ๅ้ฆไธๅปบ่ฎฎ",
"feedback.description": "้ฎ้ข๏ผ",
"feedback.notice": "ๅฐๅจๆต่งๅจไธญๆๅผ GitHub๏ผ้็ปๅฝๅนถ็กฎ่ฎคๆไบคใๅ้ฆๅฐๅ
ฌๅผ๏ผ่ฏทๅฟๅกซๅ้็งไฟกๆฏใ",
"feedback.open": "ๆไบคๅฐ GitHub",
"feedback.required": "่ฏทๅกซๅ้ฎ้ขๆๅ่ฝๅปบ่ฎฎใ",
"feedback.tooLong": "ๅ
ๅฎน่ฟ้ฟ๏ผๆ ๆณๆพๅ
ฅ้พๆฅใ่ฏท็ผฉ็ญๅ้่ฏ๏ผ่ฏฆ็ปๅ
ๅฎนๅฏๅจ GitHub ้กต้ข่กฅๅ
ใ",
"feedback.failed": "ๆ ๆณๆๅผๆต่งๅจ๏ผ่ฏทๆฃๆฅ้ป่ฎคๆต่งๅจ่ฎพ็ฝฎๅ้่ฏใ",
"section.screenOff": "ๅฑๅนๅ
ณ้ญๆถ้ด",
"menu.now": "็ซๅณ",
"menu.custom": "่ชๅฎไนๆถ้ฟโฆ",
"menu.customFmt": "่ชๅฎไน (%@)",
"menu.never": "ๆฐธไธ",
"section.endAction": "ๅฐๆถ้ดๅ",
"menu.lockOnly": "้ๅฎๅฑๅน",
"menu.lockOff": "้ๅฎๅนถๆฏๅฑ",
"menu.lockSleep": "้ๅฎใๆฏๅฑๅนถไผ็ ",
"menu.lockOffNoSleep": "้ๅฎใๆฏๅฑไธไธไผ็ ",
"menu.screenOff": "ๆฏๅฑ",
"section.timedLock": "ๅฎๆถ้ๅฑ",
"menu.language": "่ฏญ่จ",
"menu.keepAwake": "ไธไผ็ ",
"menu.launchAtLogin": "ๅผๆบ่ชๅฏ",
"menu.quit": "้ๅบ",
"unit.min": "%d ๅ้",
"unit.hour": "%d ๅฐๆถ",
"unit.hours": "%d ๅฐๆถ",
"custom.title": "่ชๅฎไนๅฑๅนๅ
ณ้ญๆถ้ด",
"custom.prompt": "่พๅ
ฅๅ้ๆฐ,ๅ่ฝฆ็กฎ่ฎค:",
"custom.placeholder": "ๅ้,ไพๅฆ 45",
"btn.start": "ๅผๅง",
"btn.cancel": "ๅๆถ",
"btn.ok": "ๅฅฝ",
"tl.ellipsis": "ๅฎๆถ้ๅฑโฆ",
"tl.activeFmt": "ๅฎๆถ้ๅฑ:ๆฏ %d ๅ ยท ๅฉ %@",
"tl.savedFmt": "ๅฎๆถ้ๅฑ (%d ๅ / %@)โฆ",
"tl.note": "้ๅฑใ่งฃ้้ฝไธไผๅฝฑๅใๆ็ปญใๅ่ฎกๆถใ",
"tl.lockAfter": "ๆ ๆไฝ",
"tl.minIdle": "ๅ้ๅณ้ๅฑ",
"tl.runFor": "ๆ็ปญ",
"tl.thenStop": "ๅ้ๅ่ชๅจๅๆญข",
"tl.until": "ๅฐ",
"tl.untilStop": "่ชๅจๅๆญข",
"tl.savedUntilFmt": "ๅฎๆถ้ๅฑ (%d ๅ / ๅฐ %@)โฆ",
"tl.pickOne": "ใๆ็ปญใๅใๅฐ็นใไบ้ไธ,ๅกซไธไธชๅฆไธไธชไผ่ชๅจๆธ
็ฉบใ",
"tl.badTime": "่ฏทๆๅฐๆถๅๅ้้ฝๅกซๆไธคไฝๆฐ,ไพๅฆ 23:30ใ",
"login.errTitle": "ๆ ๆณ่ฎพ็ฝฎๅผๆบ่ชๅฏ",
"login.errMsg": "่ฏทๅฐ SleepBar.app ็งปๅจๅฐใๅบ็จ็จๅบใๆไปถๅคนๅๅ่ฏใ",
"section.sysOff": "็ณป็ปๅ
ณๅฑ",
"sys.offAfterFmt": "ๅ
ณๅฑๆถ้ด: %@",
"sys.autoOff": "ๆๅ 1 ๅ้่ชๅจๆฏๅฑ",
"sys.writeFailed": "ๅฑๅนๅ
ณ้ญๆถ้ดๆช่ฝไฟฎๆน",
"update.availableFmt": "ๆๆฐ็ๆฌ %@ ยท ็นๅปไธ่ฝฝ",
"dupe.title": "ๅ็ฐไธคไปฝ SleepBar",
"dupe.msg": "ๅฆไธไปฝ่ฃ
ๅจ:\n%@\n\nไธคไปฝไผๅ่ชๅผๆบ่ชๅฏ,่ๅๆ ไผๅบ็ฐไธคไธชๆไบฎใ่ฆๆ้ฃไธไปฝ็งปๅฐๅบ็บธ็ฏๅ?ๅฏไปฅ้ๆถไปๅบ็บธ็ฏๆขๅคใ",
"dupe.trash": "็งปๅฐๅบ็บธ็ฏ",
"dupe.keep": "้ฝไฟ็",
"feedback.system": "้ๅธฆ็ณป็ป็ๆฌ",
"feedback.app": "้ๅธฆ App ็ๆฌ",
],
.en: [
"menu.feedback": "Report a Problem / Suggest a Featureโฆ",
"feedback.type": "Type",
"feedback.bug": "Bug",
"feedback.feature": "Feature request",
"feedback.title": "Feedback & Suggestions",
"feedback.description": "Problem:",
"feedback.notice": "Opens GitHub in your browser. Sign in and confirm submission there. Feedback will be public; do not include private information.",
"feedback.open": "Submit to GitHub",
"feedback.required": "Please describe the problem or feature request.",
"feedback.tooLong": "The content is too long for a link. Shorten it and add further details on GitHub.",
"feedback.failed": "Could not open your browser. Check your default browser settings and try again.",
"section.screenOff": "Screen Off Timer",
"menu.now": "Now",
"menu.custom": "Customโฆ",
"menu.customFmt": "Custom (%@)",
"menu.never": "Never",
"section.endAction": "When Time's Up",
"menu.lockOnly": "Lock Screen",
"menu.lockOff": "Lock & Turn Off Display",
"menu.lockSleep": "Lock, Off & Sleep",
"menu.lockOffNoSleep": "Lock, Off & Stay Awake",
"menu.screenOff": "Screen Off",
"section.timedLock": "Timed Lock",
"menu.language": "Language",
"menu.keepAwake": "Keep Awake",
"menu.launchAtLogin": "Launch at Login",
"menu.quit": "Quit",
"unit.min": "%d min",
"unit.hour": "%d hour",
"unit.hours": "%d hours",
"custom.title": "Custom Screen-Off Time",
"custom.prompt": "Enter minutes, press Return:",
"custom.placeholder": "minutes, e.g. 45",
"btn.start": "Start",
"btn.cancel": "Cancel",
"btn.ok": "OK",
"tl.ellipsis": "Timed Lockโฆ",
"tl.activeFmt": "Timed Lock: every %dm ยท %@ left",
"tl.savedFmt": "Timed Lock (%dm / %@)โฆ",
"tl.note": "Locking or unlocking won't affect the countdown.",
"tl.lockAfter": "Lock after",
"tl.minIdle": "min idle",
"tl.runFor": "Run for",
"tl.thenStop": "min, then stop",
"tl.until": "Until",
"tl.untilStop": "then stop",
"tl.savedUntilFmt": "Timed Lock (%dm / until %@)โฆ",
"tl.pickOne": "\u{201C}Run for\u{201D} and \u{201C}Until\u{201D} are either/or \u{2014} filling one clears the other.",
"tl.badTime": "Fill in both the hour and the minute as two digits, e.g. 23:30.",
"login.errTitle": "Couldn't set Launch at Login",
"login.errMsg": "Move SleepBar.app to your Applications folder and try again.",
"section.sysOff": "System Display Off",
"sys.offAfterFmt": "Turn Off After: %@",
"sys.autoOff": "Auto Screen Off 1 Min Early",
"sys.writeFailed": "Couldn't change the display-off time",
"update.availableFmt": "Version %@ available ยท click to download",
"dupe.title": "Two copies of SleepBar",
"dupe.msg": "Another copy is installed at:\n%@\n\nBoth will start at login, and you\u{2019}ll get two moons in the menu bar. Move that one to the Trash? You can put it back from there at any time.",
"dupe.trash": "Move to Trash",
"dupe.keep": "Keep Both",
"feedback.system": "Include system version",
"feedback.app": "Include app version",
],
.es: [
"menu.feedback": "Informar de un problema / Sugerir una funciรณnโฆ",
"feedback.type": "Tipo",
"feedback.bug": "Bug",
"feedback.feature": "Sugerir una funciรณn",
"feedback.title": "Comentarios y sugerencias",
"feedback.description": "Problema:",
"feedback.notice": "Se abrirรก GitHub en el navegador. Inicia sesiรณn y confirma el envรญo allรญ. El contenido serรก pรบblico; no incluyas informaciรณn privada.",
"feedback.open": "Enviar a GitHub",
"feedback.required": "Describe el problema o la funciรณn que sugieres.",
"feedback.tooLong": "El contenido es demasiado largo para un enlace. Acรณrtalo y aรฑade mรกs detalles en GitHub.",
"feedback.failed": "No se pudo abrir el navegador. Revisa el navegador predeterminado e intรฉntalo de nuevo.",
"section.screenOff": "Temporizador de pantalla",
"menu.now": "Ahora",
"menu.custom": "Personalizadoโฆ",
"menu.customFmt": "Personalizado (%@)",
"menu.never": "Nunca",
"section.endAction": "Al terminar",
"menu.lockOnly": "Bloquear pantalla",
"menu.lockOff": "Bloquear y apagar pantalla",
"menu.lockSleep": "Bloquear, apagar y suspender",
"menu.lockOffNoSleep": "Bloquear, apagar, sin suspender",
"menu.screenOff": "Apagar pantalla",
"section.timedLock": "Bloqueo programado",
"menu.language": "Idioma",
"menu.keepAwake": "Mantener activo",
"menu.launchAtLogin": "Abrir al iniciar sesiรณn",
"menu.quit": "Salir",
"unit.min": "%d min",
"unit.hour": "%d hora",
"unit.hours": "%d horas",
"custom.title": "Tiempo personalizado",
"custom.prompt": "Introduce los minutos y pulsa Intro:",
"custom.placeholder": "minutos, p. ej. 45",
"btn.start": "Iniciar",
"btn.cancel": "Cancelar",
"btn.ok": "Aceptar",
"tl.ellipsis": "Bloqueo programadoโฆ",
"tl.activeFmt": "Bloqueo: cada %d min ยท quedan %@",
"tl.savedFmt": "Bloqueo programado (%d min / %@)โฆ",
"tl.note": "Bloquear o desbloquear no afecta a la cuenta atrรกs.",
"tl.lockAfter": "Bloquear tras",
"tl.minIdle": "min inactivo",
"tl.runFor": "Durante",
"tl.thenStop": "min, luego parar",
"tl.until": "Hasta las",
"tl.untilStop": "detener",
"tl.savedUntilFmt": "Bloqueo programado (%d min / hasta %@)โฆ",
"tl.pickOne": "ยซDuranteยป y ยซHastaยป son excluyentes: al rellenar uno se borra el otro.",
"tl.badTime": "Rellena la hora y los minutos con dos cifras, p. ej. 23:30.",
"login.errTitle": "No se pudo configurar el inicio de sesiรณn",
"login.errMsg": "Mueve SleepBar.app a la carpeta Aplicaciones e intรฉntalo de nuevo.",
"section.sysOff": "Apagado del sistema",
"sys.offAfterFmt": "Apagar tras: %@",
"sys.autoOff": "Apagar pantalla 1 min antes",
"sys.writeFailed": "No se pudo cambiar el tiempo de apagado",
"update.availableFmt": "Versiรณn %@ disponible ยท haz clic para descargar",
"dupe.title": "Dos copias de SleepBar",
"dupe.msg": "Hay otra copia instalada en:\n%@\n\nLas dos se abrirรกn al iniciar sesiรณn y verรกs dos lunas en la barra de menรบs. ยฟMover esa a la Papelera? Puedes recuperarla cuando quieras.",
"dupe.trash": "Mover a la Papelera",
"dupe.keep": "Conservar ambas",
"feedback.system": "Incluir versiรณn del sistema",
"feedback.app": "Incluir versiรณn de la app",
],
.ar: [
"menu.feedback": "ุงูุฅุจูุงุบ ุนู ู
ุดููุฉ / ุงูุชุฑุงุญ ู
ูุฒุฉโฆ",
"feedback.type": "ุงูููุน",
"feedback.bug": "Bug",
"feedback.feature": "ุงูุชุฑุงุญ ู
ูุฒุฉ",
"feedback.title": "ุงูู
ูุงุญุธุงุช ูุงูุงูุชุฑุงุญุงุช",
"feedback.description": "ุงูู
ุดููุฉ:",
"feedback.notice": "ุณูููุชุญ GitHub ูู ุงูู
ุชุตูุญ. ุณุฌูู ุงูุฏุฎูู ูุฃููุฏ ุงูุฅุฑุณุงู ููุงู. ุณุชููู ุงูู
ูุงุญุธุงุช ุนูููุฉุ ูุง ุชูุฏุฑุฌ ู
ุนููู
ุงุช ุฎุงุตุฉ.",
"feedback.open": "ุฅุฑุณุงู ุฅูู GitHub",
"feedback.required": "ุตูู ุงูู
ุดููุฉ ุฃู ุงูู
ูุฒุฉ ุงูู
ูุชุฑุญุฉ.",
"feedback.tooLong": "ุงูู
ุญุชูู ุฃุทูู ู
ู ุฃู ูุชุณุน ูู ุงูุฑุงุจุท. ุงุฎุชุตุฑู ูุฃุถู ุงูุชูุงุตูู ุนูู GitHub.",
"feedback.failed": "ุชุนุฐูุฑ ูุชุญ ุงูู
ุชุตูุญ. ุชุญููู ู
ู ุฅุนุฏุงุฏุงุช ุงูู
ุชุตูุญ ุงูุงูุชุฑุงุถู ูุญุงูู ู
ุฌุฏุฏูุง.",
"section.screenOff": "ู
ุคููุช ุฅุทูุงุก ุงูุดุงุดุฉ",
"menu.now": "ุงูุขู",
"menu.custom": "ู
ุฎุตูุตโฆ",
"menu.customFmt": "ู
ุฎุตูุต (%@)",
"menu.never": "ุฃุจุฏูุง",
"section.endAction": "ุนูุฏ ุงูุชูุงุก ุงูููุช",
"menu.lockOnly": "ููู ุงูุดุงุดุฉ",
"menu.lockOff": "ููู ูุฅุทูุงุก ุงูุดุงุดุฉ",
"menu.lockSleep": "ููู ูุฅุทูุงุก ูุณุจุงุช",
"menu.lockOffNoSleep": "ููู ูุฅุทูุงุก ุฏูู ุณุจุงุช",
"menu.screenOff": "ุฅุทูุงุก ุงูุดุงุดุฉ",
"section.timedLock": "ููู ุฏูุฑู",
"menu.language": "ุงููุบุฉ",
"menu.keepAwake": "ู
ูุน ุงูุณููู",
"menu.launchAtLogin": "ุงููุชุญ ุนูุฏ ุชุณุฌูู ุงูุฏุฎูู",
"menu.quit": "ุฅููุงุก",
"unit.min": "%d ุฏูููุฉ",
"unit.hour": "%d ุณุงุนุฉ",
"unit.hours": "%d ุณุงุนุงุช",
"custom.title": "ููุช ู
ุฎุตูุต ูุฅุทูุงุก ุงูุดุงุดุฉ",
"custom.prompt": "ุฃุฏุฎู ุนุฏุฏ ุงูุฏูุงุฆู ุซู
ุงุถุบุท Return:",
"custom.placeholder": "ุฏูุงุฆูุ ู
ุซู 45",
"btn.start": "ุจุฏุก",
"btn.cancel": "ุฅูุบุงุก",
"btn.ok": "ุญุณููุง",
"tl.ellipsis": "ููู ุฏูุฑูโฆ",
"tl.activeFmt": "ููู ุฏูุฑู: ูู %dุฏ ยท ู
ุชุจูู %@",
"tl.savedFmt": "ููู ุฏูุฑู (%d ุฏูููุฉ / %@)โฆ",
"tl.note": "ุงูููู ุฃู ูุชุญ ุงูููู ูุง ูุคุซูุฑ ุนูู ุงูุนุฏูุงุฏ.",
"tl.lockAfter": "ุงูููู ุจุนุฏ",
"tl.minIdle": "ุฏูููุฉ ุฎู
ูู",
"tl.runFor": "ุงูุชุดุบูู ูู
ุฏุฉ",
"tl.thenStop": "ุฏูููุฉ ุซู
ุงูุชูููู",
"tl.until": "ุญุชู",
"tl.untilStop": "ุซู
ุงูุชูููู",
"tl.savedUntilFmt": "ููู ุฏูุฑู (%d ุฏูููุฉ / ุญุชู %@)โฆ",
"tl.pickOne": "ยซุงูุชุดุบูู ูู
ุฏุฉยป ูยซุญุชูยป ุจุฏููุงู: ุชุนุจุฆุฉ ุฃุญุฏูู
ุง ุชู
ุณุญ ุงูุขุฎุฑ.",
"tl.badTime": "ุฃุฏุฎู ุงูุณุงุนุฉ ูุงูุฏูููุฉ ุจุฑูู
ููุ ู
ุซู 23:30.",
"login.errTitle": "ุชุนุฐูุฑ ุชูุนูู ุงููุชุญ ุนูุฏ ุชุณุฌูู ุงูุฏุฎูู",
"login.errMsg": "ุงููู SleepBar.app ุฅูู ู
ุฌูุฏ ุงูุชุทุจููุงุช ูุญุงูู ู
ุฌุฏุฏูุง.",
"section.sysOff": "ุฅุทูุงุก ุดุงุดุฉ ุงููุธุงู
",
"sys.offAfterFmt": "ุงูุฅุทูุงุก ุจุนุฏ: %@",
"sys.autoOff": "ุฅุทูุงุก ุชููุงุฆู ูุจู ุฏูููุฉ",
"sys.writeFailed": "ุชุนุฐูุฑ ุชุบููุฑ ู
ุฏุฉ ุฅุทูุงุก ุงูุดุงุดุฉ",
"update.availableFmt": "ุงูุฅุตุฏุงุฑ %@ ู
ุชุงุญ ยท ุงููุฑ ููุชูุฒูู",
"dupe.title": "ูุณุฎุชุงู ู
ู SleepBar",
"dupe.msg": "ุชูุฌุฏ ูุณุฎุฉ ุฃุฎุฑู ูู:\n%@\n\nููุชุงูู
ุง ุณุชุจุฏุฃ ุนูุฏ ุชุณุฌูู ุงูุฏุฎูู ูุณูุธูุฑ ูู
ุฑุงู ูู ุดุฑูุท ุงูููุงุฆู
. ูู ุชููู ุชูู ุงููุณุฎุฉ ุฅูู ุงูู
ูู
ูุงุชุ ูู
ููู ุงุณุชุฑุฌุงุนูุง ูู ุฃู ููุช.",
"dupe.trash": "ุงูููู ุฅูู ุงูู
ูู
ูุงุช",
"dupe.keep": "ุงูุฅุจูุงุก ุนูู ุงูุงุซูุชูู",
"feedback.system": "ุฅุฑูุงู ุฅุตุฏุงุฑ ุงููุธุงู
",
"feedback.app": "ุฅุฑูุงู ุฅุตุฏุงุฑ ุงูุชุทุจูู",
],
.pt: [
"menu.feedback": "Relatar problema / Sugerir recursoโฆ",
"feedback.type": "Tipo",
"feedback.bug": "Bug",
"feedback.feature": "Sugerir recurso",
"feedback.title": "Feedback e sugestรตes",
"feedback.description": "Problema:",
"feedback.notice": "Abre o GitHub no navegador. Entre na sua conta e confirme o envio lรก. O conteรบdo serรก pรบblico; nรฃo inclua informaรงรตes privadas.",
"feedback.open": "Enviar ao GitHub",
"feedback.required": "Descreva o problema ou o recurso sugerido.",
"feedback.tooLong": "O conteรบdo รฉ longo demais para um link. Encurte-o e adicione mais detalhes no GitHub.",
"feedback.failed": "Nรฃo foi possรญvel abrir o navegador. Verifique o navegador padrรฃo e tente novamente.",
"section.screenOff": "Temporizador de tela",
"menu.now": "Agora",
"menu.custom": "Personalizadoโฆ",
"menu.customFmt": "Personalizado (%@)",
"menu.never": "Nunca",
"section.endAction": "Ao terminar",
"menu.lockOnly": "Bloquear tela",
"menu.lockOff": "Bloquear e desligar a tela",
"menu.lockSleep": "Bloquear, desligar e suspender",
"menu.lockOffNoSleep": "Bloquear, desligar, sem suspender",
"menu.screenOff": "Desligar a tela",
"section.timedLock": "Bloqueio programado",
"menu.language": "Idioma",
"menu.keepAwake": "Manter ativo",
"menu.launchAtLogin": "Abrir ao fazer login",
"menu.quit": "Encerrar",
"unit.min": "%d min",
"unit.hour": "%d hora",
"unit.hours": "%d horas",
"custom.title": "Tempo personalizado",
"custom.prompt": "Digite os minutos e pressione Return:",
"custom.placeholder": "minutos, ex.: 45",
"btn.start": "Iniciar",
"btn.cancel": "Cancelar",
"btn.ok": "OK",
"tl.ellipsis": "Bloqueio programadoโฆ",
"tl.activeFmt": "Bloqueio: a cada %d min ยท faltam %@",
"tl.savedFmt": "Bloqueio programado (%d min / %@)โฆ",
"tl.note": "Bloquear ou desbloquear nรฃo afeta a contagem.",
"tl.lockAfter": "Bloquear apรณs",
"tl.minIdle": "min inativo",
"tl.runFor": "Durante",
"tl.thenStop": "min, depois parar",
"tl.until": "Atรฉ",
"tl.untilStop": "parar",
"tl.savedUntilFmt": "Bloqueio programado (%d min / atรฉ %@)โฆ",
"tl.pickOne": "ยซDuranteยป e ยซAtรฉยป sรฃo alternativos: preencher um limpa o outro.",
"tl.badTime": "Preencha hora e minuto com dois dรญgitos, ex.: 23:30.",
"login.errTitle": "Nรฃo foi possรญvel ativar a abertura ao fazer login",
"login.errMsg": "Mova o SleepBar.app para a pasta Aplicativos e tente novamente.",
"section.sysOff": "Desligamento do sistema",
"sys.offAfterFmt": "Desligar apรณs: %@",
"sys.autoOff": "Desligar a tela 1 min antes",
"sys.writeFailed": "Nรฃo foi possรญvel alterar o tempo de desligamento",
"update.availableFmt": "Versรฃo %@ disponรญvel ยท clique para baixar",
"dupe.title": "Duas cรณpias do SleepBar",
"dupe.msg": "Hรก outra cรณpia instalada em:\n%@\n\nAs duas vรฃo iniciar no login e vocรช verรก duas luas na barra de menus. Mover essa para o Lixo? Dรก para restaurar quando quiser.",
"dupe.trash": "Mover para o Lixo",
"dupe.keep": "Manter as duas",
"feedback.system": "Incluir versรฃo do sistema",
"feedback.app": "Incluir versรฃo do app",
],
.ja: [
"menu.feedback": "ๅ้กใๅ ฑๅ / ๆฉ่ฝใๆๆกโฆ",
"feedback.type": "็จฎ้ก",
"feedback.bug": "Bug",
"feedback.feature": "ๆฉ่ฝใๆๆก",
"feedback.title": "ใใฃใผใใใใฏใจๆๆก",
"feedback.description": "ๅ้ก๏ผ",
"feedback.notice": "ใใฉใฆใถใง GitHub ใ้ใใพใใใญใฐใคใณใใฆ้ไฟกใ็ขบๅฎใใฆใใ ใใใๅ
ๅฎนใฏๅ
ฌ้ใใใใใใๅไบบๆ
ๅ ฑใฏๅ
ฅๅใใชใใงใใ ใใใ",
"feedback.open": "GitHub ใซ้ไฟก",
"feedback.required": "ๅ้กใๆฉ่ฝใฎๆๆกใๅ
ฅๅใใฆใใ ใใใ",
"feedback.tooLong": "ใชใณใฏใซๅซใใใซใฏๅ
ๅฎนใ้ทใใใพใใ็ญใใใฆใ่ฉณ็ดฐใฏ GitHub ใง่ฟฝ่จใใฆใใ ใใใ",
"feedback.failed": "ใใฉใฆใถใ้ใใพใใใงใใใๆขๅฎใฎใใฉใฆใถ่จญๅฎใ็ขบ่ชใใฆๅ่ฉฆ่กใใฆใใ ใใใ",
"section.screenOff": "็ป้ขใชใใฟใคใใผ",
"menu.now": "ไปใใ",
"menu.custom": "ใซในใฟใ โฆ",
"menu.customFmt": "ใซในใฟใ (%@)",
"menu.never": "ใชใใซใใชใ",
"section.endAction": "ๆ้ใซใชใฃใใ",
"menu.lockOnly": "็ป้ขใใญใใฏ",
"menu.lockOff": "ใญใใฏใใฆ็ป้ขใใชใ",
"menu.lockSleep": "ใญใใฏใปใชใใใฆในใชใผใ",
"menu.lockOffNoSleep": "ใญใใฏใปใชใ๏ผในใชใผใใใชใ๏ผ",
"menu.screenOff": "็ป้ขใชใ",
"section.timedLock": "ๅฎๆใญใใฏ",
"menu.language": "่จ่ช",
"menu.keepAwake": "ในใชใผใใใชใ",
"menu.launchAtLogin": "ใญใฐใคใณๆใซ่ตทๅ",
"menu.quit": "็ตไบ",
"unit.min": "%d ๅ",
"unit.hour": "%d ๆ้",
"unit.hours": "%d ๆ้",
"custom.title": "ใซในใฟใ ็ป้ขใชใๆ้",
"custom.prompt": "ๅๆฐใๅ
ฅๅใใReturn ใญใผใๆผใใฆใใ ใใ:",
"custom.placeholder": "ๅ(ไพ: 45)",
"btn.start": "้ๅง",
"btn.cancel": "ใญใฃใณใปใซ",
"btn.ok": "OK",
"tl.ellipsis": "ๅฎๆใญใใฏโฆ",
"tl.activeFmt": "ๅฎๆใญใใฏ:%d ๅใใจ ยท ๆฎใ %@",
"tl.savedFmt": "ๅฎๆใญใใฏ (%d ๅ / %@)โฆ",
"tl.note": "ใญใใฏ/ใญใใฏ่งฃ้คใใฆใใซใฆใณใใใฆใณใฏๆญขใพใใพใใใ",
"tl.lockAfter": "็กๆไฝ",
"tl.minIdle": "ๅใงใญใใฏ",
"tl.runFor": "็ถ็ถ",
"tl.thenStop": "ๅๅพใซ่ชๅๅๆญข",
"tl.until": "็ตไบๆๅป",
"tl.untilStop": "ใซ่ชๅๅๆญข",
"tl.savedUntilFmt": "ๅฎๆใญใใฏ (%d ๅ / %@ ใพใง)โฆ",
"tl.pickOne": "ใ็ถ็ถใใจใ็ตไบๆๅปใใฏใฉใกใใไธๆนใงใใ็ๆนใๅ
ฅๅใใใจไปๆนใฏๆถใใพใใ",
"tl.badTime": "ๆใจๅใ 2 ๆกใใคๅ
ฅๅใใฆใใ ใใ(ไพ: 23:30)ใ",
"login.errTitle": "ใญใฐใคใณๆใฎ่ตทๅใ่จญๅฎใงใใพใใ",
"login.errMsg": "SleepBar.app ใใใขใใชใฑใผใทใงใณใใใฉใซใใซ็งปๅใใฆใใใใไธๅบฆใ่ฉฆใใใ ใใใ",
"section.sysOff": "ใทในใใ ใฎ็ป้ขใชใ",
"sys.offAfterFmt": "ใชใใพใงใฎๆ้: %@",
"sys.autoOff": "1ๅๅใซ่ชๅใง็ป้ขใชใ",
"sys.writeFailed": "็ป้ขใชใๆ้ใๅคๆดใงใใพใใใงใใ",
"update.availableFmt": "ๆฐใใใใผใธใงใณ %@ ยท ใฏใชใใฏใใฆใใฆใณใญใผใ",
"dupe.title": "SleepBar ใ 2 ใคใใใพใ",
"dupe.msg": "ใใ 1 ใคใฏใใใซใใใพใ:\n%@\n\nไธกๆนใใญใฐใคใณๆใซ่ตทๅใใใกใใฅใผใใผใซๆใ 2 ใคๅบใพใใใใกใใใดใ็ฎฑใซๅ
ฅใใพใใ?ใใคใงใๅ
ใซๆปใใพใใ",
"dupe.trash": "ใดใ็ฎฑใซๅ
ฅใใ",
"dupe.keep": "ไธกๆนๆฎใ",
"feedback.system": "ใทในใใ ใฎใใผใธใงใณใๆทปไป",
"feedback.app": "ใขใใชใฎใใผใธใงใณใๆทปไป",
],
.de: [
"menu.feedback": "Problem melden / Funktion vorschlagenโฆ",
"feedback.type": "Typ",
"feedback.bug": "Bug",
"feedback.feature": "Funktion vorschlagen",
"feedback.title": "Feedback und Vorschlรคge",
"feedback.description": "Problem:",
"feedback.notice": "รffnet GitHub im Browser. Dort anmelden und das Senden bestรคtigen. Der Inhalt wird รถffentlich; keine privaten Informationen eingeben.",
"feedback.open": "An GitHub senden",
"feedback.required": "Bitte das Problem oder den Funktionswunsch beschreiben.",
"feedback.tooLong": "Der Inhalt ist zu lang fรผr einen Link. Bitte kรผrzen und weitere Details auf GitHub ergรคnzen.",
"feedback.failed": "Der Browser konnte nicht geรถffnet werden. Bitte den Standardbrowser prรผfen und erneut versuchen.",
"section.screenOff": "Bildschirm-Timer",
"menu.now": "Jetzt",
"menu.custom": "Eigene Dauerโฆ",
"menu.customFmt": "Eigene Dauer (%@)",
"menu.never": "Nie",
"section.endAction": "Nach Ablauf",
"menu.lockOnly": "Bildschirm sperren",
"menu.lockOff": "Sperren und Bildschirm ausschalten",
"menu.lockSleep": "Sperren, ausschalten & Ruhezustand",
"menu.lockOffNoSleep": "Sperren, ausschalten, wach bleiben",
"menu.screenOff": "Bildschirm aus",
"section.timedLock": "Zeitgesteuerte Sperre",
"menu.language": "Sprache",
"menu.keepAwake": "Wach bleiben",
"menu.launchAtLogin": "Bei der Anmeldung รถffnen",
"menu.quit": "Beenden",
"unit.min": "%d Min.",
"unit.hour": "%d Stunde",
"unit.hours": "%d Stunden",
"custom.title": "Eigene Ausschaltzeit",
"custom.prompt": "Minuten eingeben, Eingabetaste drรผcken:",
"custom.placeholder": "Minuten, z. B. 45",
"btn.start": "Starten",
"btn.cancel": "Abbrechen",
"btn.ok": "OK",
"tl.ellipsis": "Zeitgesteuerte Sperreโฆ",
"tl.activeFmt": "Sperre: alle %d Min. ยท noch %@",
"tl.savedFmt": "Zeitgesteuerte Sperre (%d Min. / %@)โฆ",
"tl.note": "Sperren oder Entsperren beeinflusst den Countdown nicht.",
"tl.lockAfter": "Sperren nach",
"tl.minIdle": "Min. Inaktivitรคt",
"tl.runFor": "Dauer",
"tl.thenStop": "Min., dann stoppen",
"tl.until": "Bis",
"tl.untilStop": "dann stoppen",
"tl.savedUntilFmt": "Zeitgesteuerte Sperre (%d Min. / bis %@)โฆ",
"tl.pickOne": "โDauerโ und โBisโ schlieรen sich aus โ eines auszufรผllen leert das andere.",
"tl.badTime": "Stunde und Minute bitte zweistellig eingeben, z. B. 23:30.",
"login.errTitle": "โBei der Anmeldung รถffnenโ konnte nicht aktiviert werden",
"login.errMsg": "Verschiebe SleepBar.app in den Ordner โProgrammeโ und versuche es erneut.",
"section.sysOff": "System-Bildschirm aus",
"sys.offAfterFmt": "Ausschalten nach: %@",
"sys.autoOff": "1 Min. vorher Bildschirm aus",
"sys.writeFailed": "Bildschirm-Auszeit konnte nicht geรคndert werden",
"update.availableFmt": "Version %@ verfรผgbar ยท zum Herunterladen klicken",
"dupe.title": "Zwei Kopien von SleepBar",
"dupe.msg": "Eine weitere Kopie liegt unter:\n%@\n\nBeide starten bei der Anmeldung, und in der Menรผleiste erscheinen zwei Monde. Diese Kopie in den Papierkorb legen? Du kannst sie jederzeit zurรผckholen.",
"dupe.trash": "In den Papierkorb",
"dupe.keep": "Beide behalten",
"feedback.system": "Systemversion anhรคngen",
"feedback.app": "App-Version anhรคngen",
],
]
// Keep the entire encoded URL within a conservative browser/request size budget.
// Never truncate a report silently; the dialog asks the user to shorten it instead.
private func feedbackURL(title: String, description: String, isFeature: Bool) -> URL? {
var components = URLComponents(string: "https://github.com/ddasy/SleepBar/issues/new")!
components.queryItems = [
URLQueryItem(name: "title", value: "[\(isFeature ? "Feature" : "Bug")] \(title)"),
URLQueryItem(name: "body", value: description),
]
// GitHub decodes query strings as form data, where a literal + means a space.
components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
guard let url = components.url, url.absoluteString.utf8.count <= 7500 else { return nil }
return url
}
// A compact, modeless form keeps the menu and countdown responsive while writing.
private final class FeedbackWindowController: NSWindowController {
private let t: (String) -> String
private let details = NSTextView()
private let bug = NSButton()
private let feature = NSButton()
private let systemVersion = NSButton()
private let appVersion = NSButton()
init(localize: @escaping (String) -> String) {
t = localize
let panel = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 460, height: 540),
styleMask: [.titled, .closable], backing: .buffered, defer: false)
super.init(window: panel)
panel.title = t("feedback.title")
panel.isReleasedWhenClosed = false
panel.center()
guard let content = panel.contentView else { return }
let width: CGFloat = 396
func label(_ text: String, y: CGFloat, size: CGFloat = 14, bold: Bool = false) {
let field = NSTextField(labelWithString: text)
field.font = bold ? .boldSystemFont(ofSize: size) : .systemFont(ofSize: size)
field.frame = NSRect(x: 32, y: y, width: width, height: 28)
content.addSubview(field)
}
label(t("feedback.title"), y: 482, size: 22, bold: true)
label(t("feedback.type"), y: 427, bold: true)
for (button, text, x, w) in [(bug, t("feedback.bug"), CGFloat(32), CGFloat(86)),
(feature, t("feedback.feature"), CGFloat(132), CGFloat(296))] {
button.setButtonType(.radio)
button.title = text
button.font = .systemFont(ofSize: 14)
button.frame = NSRect(x: x, y: 394, width: w, height: 28)
button.target = self
button.action = #selector(selectType(_:))
content.addSubview(button)
}
bug.state = .on
label(t("feedback.description"), y: 348, bold: true)
let scroll = NSScrollView(frame: NSRect(x: 32, y: 190, width: width, height: 152))
scroll.borderType = .bezelBorder
scroll.hasVerticalScroller = true
details.frame = scroll.contentView.bounds
details.isRichText = false
details.isAutomaticQuoteSubstitutionEnabled = false
details.isAutomaticDashSubstitutionEnabled = false
details.font = .systemFont(ofSize: 14)
details.textContainerInset = NSSize(width: 8, height: 8)
details.isVerticallyResizable = true
details.isHorizontallyResizable = false
details.autoresizingMask = [.width]
details.textContainer?.widthTracksTextView = true
details.setAccessibilityLabel(t("feedback.description"))
scroll.documentView = details
content.addSubview(scroll)
for (button, key, y) in [(systemVersion, "feedback.system", CGFloat(146)),
(appVersion, "feedback.app", CGFloat(116))] {
button.setButtonType(.switch)
button.title = t(key)
button.font = .systemFont(ofSize: 14)
button.state = .on
button.frame = NSRect(x: 32, y: y, width: width, height: 26)
content.addSubview(button)
}
let notice = NSTextField(wrappingLabelWithString: t("feedback.notice"))
notice.font = .systemFont(ofSize: 11)
notice.textColor = .secondaryLabelColor
notice.frame = NSRect(x: 32, y: 65, width: width, height: 44)
content.addSubview(notice)
let submit = NSButton(title: t("feedback.open"), target: self, action: #selector(submit))
submit.bezelStyle = .rounded
submit.frame = NSRect(x: 100, y: 18, width: 260, height: 32)
submit.keyEquivalent = "\r"
submit.keyEquivalentModifierMask = [.command]
content.addSubview(submit)
bug.nextKeyView = feature
feature.nextKeyView = details
details.nextKeyView = systemVersion
systemVersion.nextKeyView = appVersion
appVersion.nextKeyView = submit
panel.initialFirstResponder = details
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
@objc private func selectType(_ sender: NSButton) {
bug.state = sender === bug ? .on : .off
feature.state = sender === feature ? .on : .off
}
@objc private func submit() {
let text = details.string.trimmingCharacters(in: .whitespacesAndNewlines)
guard !text.isEmpty else { showError(t("feedback.required")); return }
let os = ProcessInfo.processInfo.operatingSystemVersion
let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Development"
let body = feedbackBody(text: text,
systemVersion: systemVersion.state == .on ? "macOS \(os.majorVersion).\(os.minorVersion).\(os.patchVersion)" : nil,
appVersion: appVersion.state == .on ? version : nil)
let title = String((text.split(whereSeparator: { $0.isNewline }).first.map(String.init) ?? text).prefix(80))
guard let url = feedbackURL(title: title, description: body, isFeature: feature.state == .on) else {
showError(t("feedback.tooLong")); return
}
guard NSWorkspace.shared.open(url) else { showError(t("feedback.failed")); return }
close()
}
private func showError(_ message: String) {
guard let window = window else { return }
let alert = NSAlert()
alert.messageText = message
alert.addButton(withTitle: t("btn.ok"))
alert.beginSheetModal(for: window)
}
}
private func feedbackBody(text: String, systemVersion: String?, appVersion: String?) -> String {
var metadata: [String] = []
if let systemVersion = systemVersion { metadata.append("System: \(systemVersion)") }
if let appVersion = appVersion { metadata.append("SleepBar: \(appVersion)") }
return text + (metadata.isEmpty ? "" : "\n\n---\n" + metadata.joined(separator: "\n"))
}
// Built-in keyboard backlight control via CoreBrightness's private KeyboardBrightnessClient.
@objc private protocol KeyboardBacklight {
func brightnessForKeyboard(_ keyboard: Int64) -> Float
func setBrightness(_ brightness: Float, forKeyboard keyboard: Int64) -> Bool
}
// A text field that comes up fully selected when clicked, so a value is replaced by typing
// rather than by backspacing from wherever the caret landed.
//
// Selecting when editing begins is not enough for a click: NSTextField hands the click to
// the field editor's tracking loop, which places its own caret *after* the begin-editing
// notification has already gone out, so anything selected there is immediately undone.
// super.mouseDown returns once that loop ends (on mouse-up), which is the first moment a
// selection sticks.
private final class SelectAllTextField: NSTextField {
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
selectText(nil)
}
}
// Drives every input in the Timed Lock dialog.
//
// Two jobs (clicking into a box selects it โ that is SelectAllTextField's, above). First,
// the duration and the clock time are either/or, so typing into one side blanks the other โ
// the dialog can never carry two conflicting answers to "until when". Second, the clock
// time is two 2-digit
// boxes with a fixed ":" label between them, rather than one free-text field: each box
// takes digits only, and the hour hands focus to the minute on its second digit, so the
// whole time is four keystrokes with nothing to aim at in between.
private final class TimedLockFields: NSObject, NSTextFieldDelegate {
private let interval: NSTextField, duration: NSTextField
private let hour: NSTextField, minute: NSTextField
init(interval: NSTextField, duration: NSTextField, hour: NSTextField, minute: NSTextField) {
self.interval = interval; self.duration = duration
self.hour = hour; self.minute = minute
super.init()
for f in [interval, duration, hour, minute] { f.delegate = self }
}
func controlTextDidChange(_ note: Notification) {
guard let edited = note.object as? NSTextField else { return }
// The idle interval is its own thing โ it pairs with either end time, never replaces one.
if edited === duration {
if !edited.stringValue.trimmingCharacters(in: .whitespaces).isEmpty {
clear(hour); clear(minute)
}
return
}
guard edited === hour || edited === minute else { return }
// Digits only, two at most: ":" lives in a label, so it can't be deleted or doubled.
let digits = String(edited.stringValue.filter { $0.isNumber }.prefix(2))
if digits != edited.stringValue { setText(digits, in: edited) }
guard !digits.isEmpty else { return }
clear(duration)
// Both halves are always two digits, so the hour is settled after exactly two
// keystrokes โ hand the caret to the minute then, with nothing to aim at in between.
if edited === hour, digits.count == 2 { edited.window?.makeFirstResponder(minute) }
}
// Leaving a box with a single digit in it means 9 โ 09: pad rather than reject, so what
// is on screen is always the two-digit value that will actually be used.
func controlTextDidEndEditing(_ note: Notification) {
guard let edited = note.object as? NSTextField, edited === hour || edited === minute,
edited.stringValue.count == 1 else { return }
edited.stringValue = "0" + edited.stringValue
}
private func setText(_ s: String, in f: NSTextField) {
f.stringValue = s
f.currentEditor()?.selectedRange = NSRange(location: s.count, length: 0)
}
private func clear(_ f: NSTextField) {
guard !f.stringValue.isEmpty else { return }
setText("", in: f)
}
}
final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
private var statusItem: NSStatusItem!
private var task: Process? // current caffeinate process
private var endDate: Date? // countdown end time (nil for never/idle)
private var ticker: Timer?
// activeMinutes: nil = idle (system defaults); -1 = never (always awake); >0 = countdown minutes
private var activeMinutes: Int?
private var endAction: EndAction = .lockOff
private var customMinutes: Int = 0 // last custom duration in minutes (0 = never set)
private var lang: Lang = .en
// โโ Timed Lock โโ
// Within the window, lock the screen every time idle input reaches "interval".
// The window is a fixed countdown; locking/unlocking never resets it.
private var tlActive = false
private var tlInterval: Int = 0 // lock interval (minutes), also remembers the last value
private var tlWindowMin: Int = 0 // window length (minutes), also remembers the last value
private var tlUntil: String = "" // "23:30" when the window was given as a clock time ("" = a duration was used)
private var tlWindowEnd: Date? // absolute end time of the window
private var tlTimer: Timer? // adaptive idle check (not a per-second poll)
private var tlWindowTimer: Timer? // one-shot stop when the window ends
private var tlItem: NSMenuItem!
private var launchItem: NSMenuItem! // "Launch at Login" toggle (.app builds only)
// โโ Keep Awake โโ
// An always-on toggle (no trigger logic): while checked, a standalone caffeinate
// process blocks idle/system sleep (but not display sleep). Independent of the
// Screen Off Timer; persists across launches.
private var keepAwake = false
private var keepAwakeTask: Process?
private var keepAwakeItem: NSMenuItem!
// โโ Update check โโ
// A fortnightly, fire-and-forget GET of the repo's latest release tag. Nothing is
// downloaded or installed and no data is sent: when a newer version exists, a single
// menu item appears that opens the release page, and the user updates by hand.
private let updateFeedURL = URL(string: "https://api.github.com/repos/ddasy/SleepBar/releases/latest")!
private let updateReleasesURL = URL(string: "https://github.com/ddasy/SleepBar/releases/latest")!
private let updateInterval: TimeInterval = 14 * 24 * 3600
private var latestVersion: String? // set only when the remote tag is newer than ours
private var latestURL: URL? // that release's page
private var updateTimer: Timer?
private var updateItem: NSMenuItem!
private var updateSeparator: NSMenuItem!
// โโ System display-off time (pmset displaysleep) โโ
// Reading the current power source's displaysleep needs no privileges; changing it
// goes through pmset behind a one-time admin-password prompt (root is required to
// write power settings). autoOff triggers the "ๆฏๅฑ" action (no lock, mouse wakes it)
// one minute before the system would sleep the display, so the system's
// "require password after display off" lock never engages.
private var sysOffMinutes: Int = -1 // current-source displaysleep; -1 = not read yet, 0 = never
private var sysOffOnAC = true // which power source the value (and a write) applies to
private var sysOffItem: NSMenuItem!
private var sysOffPresetItems: [(min: Int, item: NSMenuItem)] = []
private let sysOffPresets = [1, 2, 5, 10, 15, 20, 30, 45, 60, 180, 0] // System Settings pillars; 0 = never
private var autoOffEnabled = false
private var autoOffTimer: Timer? // adaptive idle check, same pattern as Timed Lock
private var autoOffItem: NSMenuItem!
private var powerSourceRunLoopSource: CFRunLoopSource? // held for the app's lifetime
private var sigtermSource: DispatchSourceSignal? // ditto
// โโ Screen off via brightness (the "ๆฏๅฑ" / Screen Off end action) โโ
// Instead of sleeping the display (which stalls GPU rendering), drop the built-in
// display brightness to 0 and keep things awake; the watcher restores it on return.
private var savedBrightness: [CGDirectDisplayID: Float] = [:]
private var screenOffMonitor: Any? // global mouse monitor: restore on user return
private var screenOffIdleTimer: Timer? // keyboard fallback: watch HID idle for a drop
private var lastScreenOffIdle: TimeInterval = 0
private var screenOffActive = false
// Keyboard backlight (built-in keyboard = id 1). The client is held for the app's
// lifetime so the level we set actually sticks. savedKeyboardBrightness = pre-dim level.
private let keyboardID: Int64 = 1
private lazy var keyboardClientObj: NSObject? = {
guard dlopen("/System/Library/PrivateFrameworks/CoreBrightness.framework/CoreBrightness", RTLD_NOW) != nil,
let cls = NSClassFromString("KeyboardBrightnessClient") as? NSObject.Type else { return nil }
return cls.init()
}()
private var keyboardBacklight: KeyboardBacklight? { keyboardClientObj.map { unsafeBitCast($0, to: KeyboardBacklight.self) } }
private var savedKeyboardBrightness: Float?
// Output volume (system slider, 0โฆ100) dimmed to 1% and muted alongside the screen;
// the pre-dim level and mute state are restored on wake, same as brightness.
// savedVolume stays nil on outputs with no software volume control (HDMI, most USB
// DACs, AirPlay) โ there, muting is the whole of the effect. savedMuted != nil is the
// "we dimmed something and owe a restore" marker.
private var savedVolume: Int?
private var savedMuted: Bool?
// Preset durations (minutes); titles are generated per language
private let presets: [Int] = [5, 10, 15, 30, 60]
// Menu item references (for updating checkmarks)
private var presetItems: [(min: Int, item: NSMenuItem)] = []
private var customItem: NSMenuItem!
private var neverItem: NSMenuItem!
private var lockOnlyItem: NSMenuItem!
private var lockOffItem: NSMenuItem!
private var lockSleepItem: NSMenuItem!
private var lockOffNoSleepItem: NSMenuItem!
private var screenOffItem: NSMenuItem!
func applicationDidFinishLaunching(_ note: Notification) {
if let saved = UserDefaults.standard.string(forKey: "endAction"),
let a = EndAction(rawValue: saved) { endAction = a }
customMinutes = UserDefaults.standard.integer(forKey: "customMinutes")
if let s = UserDefaults.standard.string(forKey: "lang"), let l = Lang(rawValue: s) {
lang = l
} else {
lang = Lang.systemDefault
}
tlInterval = UserDefaults.standard.integer(forKey: "tlInterval")
tlWindowMin = UserDefaults.standard.integer(forKey: "tlWindowMin")
tlUntil = UserDefaults.standard.string(forKey: "tlUntil") ?? ""
keepAwake = UserDefaults.standard.bool(forKey: "keepAwake")
autoOffEnabled = UserDefaults.standard.bool(forKey: "autoScreenOff")
// Observe lock/unlock (event-driven; zero polling while locked)
let dc = DistributedNotificationCenter.default()
dc.addObserver(self, selector: #selector(screenDidLock),
name: NSNotification.Name("com.apple.screenIsLocked"), object: nil)
dc.addObserver(self, selector: #selector(screenDidUnlock),
name: NSNotification.Name("com.apple.screenIsUnlocked"), object: nil)
// Plugging in or unplugging swaps macOS to the other displaysleep profile, so both
// the menu label and any pending auto-off deadline have to be recomputed.
if let src = IOPSNotificationCreateRunLoopSource({ ctx in
guard let ctx = ctx else { return }
Unmanaged<AppDelegate>.fromOpaque(ctx).takeUnretainedValue().powerSourceChanged()
}, Unmanaged.passUnretained(self).toOpaque())?.takeRetainedValue() {
CFRunLoopAddSource(CFRunLoopGetMain(), src, .defaultMode)
powerSourceRunLoopSource = src
}
// Timers don't run while the machine is asleep, so a pending auto-off deadline comes
// back stale (and idle is zero again anyway). Recompute it from the wake.
NSWorkspace.shared.notificationCenter.addObserver(
self, selector: #selector(systemDidWake),
name: NSWorkspace.didWakeNotification, object: nil)
// install.sh passes --register-login on the first launch after installing:
// register as a Login Item (SMAppService; uncheck anytime via "Launch at Login")
if CommandLine.arguments.contains("--register-login"), isAppBundle,
#available(macOS 13.0, *) {
try? SMAppService.mainApp.register()
}
installTerminationHandler()
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
buildMenu()
refreshUI()
if keepAwake { startKeepAwake() } // restore the always-on keep-awake assertion
readSysOff { [weak self] _ in self?.rearmAutoOff() } // populate the menu; arm auto screen-off
scheduleUpdateCheck()
// After the menu bar item is up, so the alert isn't the first thing on screen.
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in self?.offerToRemoveDuplicate() }
}
// MARK: - Localization
private func t(_ key: String) -> String {
l10n[lang]?[key] ?? l10n[.en]?[key] ?? key
}
// Minutes โ localized title: "5 min" / "1 hour" (or their localized equivalents)
private func durationTitle(_ minutes: Int) -> String {
if minutes >= 60 && minutes % 60 == 0 {
let h = minutes / 60
return String(format: t(h == 1 ? "unit.hour" : "unit.hours"), h)
}
return String(format: t("unit.min"), minutes)
}
private func customLabel() -> String {
guard customMinutes > 0 else { return t("menu.custom") }
return String(format: t("menu.customFmt"), durationTitle(customMinutes))
}
// MARK: - Menu building
private func icon(_ name: String) -> NSImage? {
NSImage(systemSymbolName: name, accessibilityDescription: nil)
}
private func buildMenu() {
let menu = NSMenu()
menu.delegate = self
presetItems = []
// โโ Update notice (stays hidden unless a newer release was found) โโ
updateItem = NSMenuItem(title: "", action: #selector(openLatestRelease), keyEquivalent: "")
updateItem.target = self
updateItem.image = icon("arrow.down.circle.fill")
menu.addItem(updateItem)
updateSeparator = .separator()
menu.addItem(updateSeparator)
// โโ Screen Off Timer โโ
menu.addItem(.sectionHeader(title: t("section.screenOff")))
let nowItem = NSMenuItem(title: t("menu.now"), action: #selector(pickNow), keyEquivalent: "")
nowItem.target = self
nowItem.image = icon("bolt")
menu.addItem(nowItem)
for minutes in presets {
let item = NSMenuItem(title: durationTitle(minutes), action: #selector(pickPreset(_:)), keyEquivalent: "")
item.target = self
item.tag = minutes
item.image = icon("clock")
menu.addItem(item)
presetItems.append((minutes, item))
}
customItem = NSMenuItem(title: customLabel(), action: #selector(pickCustom), keyEquivalent: "")
customItem.target = self
customItem.image = icon("slider.horizontal.3")
menu.addItem(customItem)
neverItem = NSMenuItem(title: t("menu.never"), action: #selector(pickNever), keyEquivalent: "")
neverItem.target = self
neverItem.image = icon("nosign")
menu.addItem(neverItem)
// โโ When Time's Up โโ
menu.addItem(.sectionHeader(title: t("section.endAction")))
// Items are ordered shortest-label-first, so "ๆฏๅฑ" (Screen Off) comes first.
screenOffItem = NSMenuItem(title: t("menu.screenOff"), action: #selector(pickScreenOff), keyEquivalent: "")
screenOffItem.target = self
screenOffItem.image = icon("sun.min")
menu.addItem(screenOffItem)
lockOnlyItem = NSMenuItem(title: t("menu.lockOnly"), action: #selector(pickLockOnly), keyEquivalent: "")
lockOnlyItem.target = self
lockOnlyItem.image = icon("lock")
menu.addItem(lockOnlyItem)
lockOffItem = NSMenuItem(title: t("menu.lockOff"), action: #selector(pickLockOff), keyEquivalent: "")
lockOffItem.target = self
lockOffItem.image = icon("lock.display")
menu.addItem(lockOffItem)
lockSleepItem = NSMenuItem(title: t("menu.lockSleep"), action: #selector(pickLockSleep), keyEquivalent: "")
lockSleepItem.target = self
lockSleepItem.image = icon("powersleep")
menu.addItem(lockSleepItem)
lockOffNoSleepItem = NSMenuItem(title: t("menu.lockOffNoSleep"), action: #selector(pickLockOffNoSleep), keyEquivalent: "")
lockOffNoSleepItem.target = self
lockOffNoSleepItem.image = icon("lock.open.display")
menu.addItem(lockOffNoSleepItem)
// โโ Timed Lock โโ
menu.addItem(.sectionHeader(title: t("section.timedLock")))
tlItem = NSMenuItem(title: tlLabel(), action: #selector(toggleTimedLock), keyEquivalent: "")
tlItem.target = self
tlItem.image = icon("lock.rotation")
menu.addItem(tlItem)
// โโ System Display Off (read/write pmset displaysleep + auto early screen-off) โโ
menu.addItem(.sectionHeader(title: t("section.sysOff")))
sysOffItem = NSMenuItem(title: sysOffLabel(), action: nil, keyEquivalent: "")
sysOffItem.image = icon("timer")
let sysMenu = NSMenu()
sysOffPresetItems = []
for minutes in sysOffPresets {
let title = (minutes == 0) ? t("menu.never") : durationTitle(minutes)
let item = NSMenuItem(title: title, action: #selector(pickSysOff(_:)), keyEquivalent: "")
item.target = self
item.tag = minutes