8
8
9
9
import Cocoa
10
10
import LoginServiceKit
11
+ import Defaults
11
12
12
13
@NSApplicationMain
13
- class AppDelegate : NSObject , NSApplicationDelegate {
14
+ class AppDelegate : NSObject , NSApplicationDelegate , NSTextFieldDelegate {
14
15
15
16
@IBOutlet weak var statusBarMenu : NSMenu !
16
17
@IBOutlet weak var preferencesWindowVersionNum : NSTextField !
17
18
@IBOutlet weak var aboutWindowVersionNum : NSTextField !
18
19
@IBOutlet weak var launchAtLoginCheckbox : NSButton !
19
- @IBOutlet weak var timeFormat12h : NSButton !
20
- @IBOutlet weak var timeFormat24h : NSButton !
20
+ @IBOutlet weak var time1Text : NSTextField !
21
+ @IBOutlet weak var time2Text : NSTextField !
21
22
@IBOutlet weak var preferencesWindow : NSWindow !
22
23
@IBOutlet weak var aboutWindow : NSWindow !
23
24
@IBOutlet weak var alertWindow : NSWindow !
@@ -38,14 +39,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
38
39
39
40
launchAtLoginCheckbox. state = LoginServiceKit . isExistLoginItems ( ) ? . on : . off
40
41
41
- switch UserDefaults . standard. string ( forKey: " timeFormat " ) {
42
- case " HH:mm " :
43
- timeFormat12h. state = . off
44
- timeFormat24h. state = . on
45
- default :
46
- timeFormat12h. state = . on
47
- timeFormat24h. state = . off
48
- }
42
+ time1Text. delegate = self
43
+ time2Text. delegate = self
44
+ time1Text. stringValue = Defaults [ . time1]
45
+ time2Text. stringValue = Defaults [ . time2]
49
46
50
47
clockBar ( )
51
48
@@ -57,26 +54,45 @@ class AppDelegate: NSObject, NSApplicationDelegate {
57
54
// Insert code here to tear down your application
58
55
}
59
56
57
+ func showTime( ) {
58
+ switch Defaults [ . shouldShowTime1] {
59
+ case true :
60
+ Defaults [ . showTime] = Defaults [ . time1]
61
+ default :
62
+ Defaults [ . showTime] = Defaults [ . time2]
63
+ }
64
+ }
65
+
66
+ func controlTextDidChange( _ obj: Notification ) {
67
+ Defaults [ . time1] = time1Text. stringValue
68
+ Defaults [ . time2] = time2Text. stringValue
69
+ showTime ( )
70
+ }
60
71
61
72
// add clockbar to touch bar
62
73
func clockBar( ) {
63
74
DFRSystemModalShowsCloseBoxWhenFrontMost ( true )
64
75
65
76
timeFormatter = DateFormatter ( )
66
- timeFormatter? . dateFormat = UserDefaults . standard . string ( forKey : " timeFormat " ) ?? " h:mm "
77
+ timeFormatter? . dateFormat = Defaults [ . showTime ]
67
78
let nowTime = timeFormatter? . string ( from: Date ( ) )
68
79
69
80
let clockBarIdentifier = NSTouchBarItem . Identifier ( rawValue: " ClockBar " )
70
81
let clockBar = NSCustomTouchBarItem . init ( identifier: clockBarIdentifier)
71
- touchBarButton = NSButton ( title: nowTime!, target: self , action: nil )
82
+ touchBarButton = NSButton ( title: nowTime!, target: self , action: #selector ( changeTime ) )
72
83
clockBar. view = touchBarButton!
73
84
NSTouchBarItem . addSystemTrayItem ( clockBar)
74
85
DFRElementSetControlStripPresenceForIdentifier ( clockBarIdentifier, true )
75
86
}
76
87
88
+ @objc func changeTime( ) {
89
+ showTime ( )
90
+ Defaults [ . shouldShowTime1] = !Defaults[ . shouldShowTime1]
91
+ }
92
+
77
93
// update time
78
94
@objc func updateTime( ) {
79
- timeFormatter? . dateFormat = UserDefaults . standard . string ( forKey : " timeFormat " ) ?? " h:mm "
95
+ timeFormatter? . dateFormat = Defaults [ . showTime ]
80
96
touchBarButton? . title = ( timeFormatter? . string ( from: Date ( ) ) ) !
81
97
}
82
98
@@ -130,10 +146,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
130
146
switch sender. tag {
131
147
case 0 :
132
148
UserDefaults . standard. set ( " h:mm " , forKey: " timeFormat " )
133
- timeFormat24h . state = . off
149
+
134
150
case 1 :
135
151
UserDefaults . standard. set ( " HH:mm " , forKey: " timeFormat " )
136
- timeFormat12h . state = . off
152
+
137
153
default :
138
154
return
139
155
}
@@ -164,6 +180,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
164
180
@IBAction func didClickURL( _ sender: NSButton ) {
165
181
let url : String
166
182
switch sender. tag {
183
+ case 0 :
184
+ url = " https://blog.licardo.cn/posts/33030 "
167
185
case 1 :
168
186
url = " https://github.com/L1cardo "
169
187
case 2 :
@@ -185,3 +203,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
185
203
}
186
204
187
205
}
206
+
207
+ extension Defaults . Keys {
208
+ static let time1 = Key < String > ( " time1 " , default: " h:mm " )
209
+ static let time2 = Key < String > ( " time2 " , default: " HH:mm " )
210
+ static let showTime = Key < String > ( " showTime " , default: " h:mm " )
211
+ static let shouldShowTime1 = Key < Bool > ( " showTime1 " , default: true )
212
+ }
0 commit comments