Skip to content

Commit b3421b1

Browse files
committed
add 2 time format to switch by tapping Touch Bar icon
1 parent 1cdb6f7 commit b3421b1

File tree

9 files changed

+155
-92
lines changed

9 files changed

+155
-92
lines changed

ClockBar.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
/* Begin PBXFileReference section */
3434
091F12975425693F99521C97 /* Pods_ClockBar.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ClockBar.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3535
376ECC70A35AC58E99F85ACB /* Pods-ClockBar.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ClockBar.release.xcconfig"; path = "Target Support Files/Pods-ClockBar/Pods-ClockBar.release.xcconfig"; sourceTree = "<group>"; };
36+
964408C723F3CD3200CDF07C /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/MainMenu.strings"; sourceTree = "<group>"; };
37+
964408CB23F3CEF400CDF07C /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/MainMenu.strings; sourceTree = "<group>"; };
3638
9646ECC5237583FE0019A927 /* ClockBar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ClockBar.app; sourceTree = BUILT_PRODUCTS_DIR; };
3739
9646ECC8237583FE0019A927 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3840
9646ECCA237583FF0019A927 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -42,8 +44,6 @@
4244
9646ECD62375843B0019A927 /* ClockBar-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ClockBar-Bridging-Header.h"; sourceTree = "<group>"; };
4345
9646ECD72375843B0019A927 /* TouchBar.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBar.h; sourceTree = "<group>"; };
4446
9646ECDA237584CE0019A927 /* DFRFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = DFRFoundation.framework; path = ../../../../System/Library/PrivateFrameworks/DFRFoundation.framework; sourceTree = "<group>"; };
45-
96BB033923C2304F004D5AFF /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/MainMenu.strings; sourceTree = "<group>"; };
46-
96D2C44D2375A94400B6B506 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/MainMenu.strings"; sourceTree = "<group>"; };
4747
9B956657E1A6217BD8CE988F /* Pods-ClockBar.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ClockBar.debug.xcconfig"; path = "Target Support Files/Pods-ClockBar/Pods-ClockBar.debug.xcconfig"; sourceTree = "<group>"; };
4848
/* End PBXFileReference section */
4949

@@ -255,8 +255,8 @@
255255
isa = PBXVariantGroup;
256256
children = (
257257
9646ECCD237583FF0019A927 /* Base */,
258-
96D2C44D2375A94400B6B506 /* zh-Hans */,
259-
96BB033923C2304F004D5AFF /* en */,
258+
964408C723F3CD3200CDF07C /* zh-Hans */,
259+
964408CB23F3CEF400CDF07C /* en */,
260260
);
261261
name = MainMenu.xib;
262262
sourceTree = "<group>";
@@ -392,15 +392,15 @@
392392
CODE_SIGN_IDENTITY = "-";
393393
CODE_SIGN_STYLE = Automatic;
394394
COMBINE_HIDPI_IMAGES = YES;
395-
CURRENT_PROJECT_VERSION = 20200105;
395+
CURRENT_PROJECT_VERSION = 20200212;
396396
DEVELOPMENT_TEAM = 2U23P5CPX2;
397397
ENABLE_HARDENED_RUNTIME = NO;
398398
INFOPLIST_FILE = "ClockBar/Supporting Files/Info.plist";
399399
LD_RUNPATH_SEARCH_PATHS = (
400400
"$(inherited)",
401401
"@executable_path/../Frameworks",
402402
);
403-
MARKETING_VERSION = 1.1.1;
403+
MARKETING_VERSION = 1.2.0;
404404
PRODUCT_BUNDLE_IDENTIFIER = cn.licardo.ClockBar;
405405
PRODUCT_NAME = "$(TARGET_NAME)";
406406
SWIFT_OBJC_BRIDGING_HEADER = "ClockBar/TouchBar/ClockBar-Bridging-Header.h";
@@ -424,15 +424,15 @@
424424
CODE_SIGN_IDENTITY = "-";
425425
CODE_SIGN_STYLE = Automatic;
426426
COMBINE_HIDPI_IMAGES = YES;
427-
CURRENT_PROJECT_VERSION = 20200105;
427+
CURRENT_PROJECT_VERSION = 20200212;
428428
DEVELOPMENT_TEAM = 2U23P5CPX2;
429429
ENABLE_HARDENED_RUNTIME = NO;
430430
INFOPLIST_FILE = "ClockBar/Supporting Files/Info.plist";
431431
LD_RUNPATH_SEARCH_PATHS = (
432432
"$(inherited)",
433433
"@executable_path/../Frameworks",
434434
);
435-
MARKETING_VERSION = 1.1.1;
435+
MARKETING_VERSION = 1.2.0;
436436
PRODUCT_BUNDLE_IDENTIFIER = cn.licardo.ClockBar;
437437
PRODUCT_NAME = "$(TARGET_NAME)";
438438
SWIFT_OBJC_BRIDGING_HEADER = "ClockBar/TouchBar/ClockBar-Bridging-Header.h";
Binary file not shown.

ClockBar/AppDelegate.swift

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88

99
import Cocoa
1010
import LoginServiceKit
11+
import Defaults
1112

1213
@NSApplicationMain
13-
class AppDelegate: NSObject, NSApplicationDelegate {
14+
class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate {
1415

1516
@IBOutlet weak var statusBarMenu: NSMenu!
1617
@IBOutlet weak var preferencesWindowVersionNum: NSTextField!
1718
@IBOutlet weak var aboutWindowVersionNum: NSTextField!
1819
@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!
2122
@IBOutlet weak var preferencesWindow: NSWindow!
2223
@IBOutlet weak var aboutWindow: NSWindow!
2324
@IBOutlet weak var alertWindow: NSWindow!
@@ -38,14 +39,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
3839

3940
launchAtLoginCheckbox.state = LoginServiceKit.isExistLoginItems() ? .on : .off
4041

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]
4946

5047
clockBar()
5148

@@ -57,26 +54,45 @@ class AppDelegate: NSObject, NSApplicationDelegate {
5754
// Insert code here to tear down your application
5855
}
5956

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+
}
6071

6172
// add clockbar to touch bar
6273
func clockBar() {
6374
DFRSystemModalShowsCloseBoxWhenFrontMost(true)
6475

6576
timeFormatter = DateFormatter()
66-
timeFormatter?.dateFormat = UserDefaults.standard.string(forKey: "timeFormat") ?? "h:mm"
77+
timeFormatter?.dateFormat = Defaults[.showTime]
6778
let nowTime = timeFormatter?.string(from: Date())
6879

6980
let clockBarIdentifier = NSTouchBarItem.Identifier(rawValue: "ClockBar")
7081
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))
7283
clockBar.view = touchBarButton!
7384
NSTouchBarItem.addSystemTrayItem(clockBar)
7485
DFRElementSetControlStripPresenceForIdentifier(clockBarIdentifier, true)
7586
}
7687

88+
@objc func changeTime() {
89+
showTime()
90+
Defaults[.shouldShowTime1] = !Defaults[.shouldShowTime1]
91+
}
92+
7793
// update time
7894
@objc func updateTime() {
79-
timeFormatter?.dateFormat = UserDefaults.standard.string(forKey: "timeFormat") ?? "h:mm"
95+
timeFormatter?.dateFormat = Defaults[.showTime]
8096
touchBarButton?.title = (timeFormatter?.string(from: Date()))!
8197
}
8298

@@ -130,10 +146,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
130146
switch sender.tag {
131147
case 0:
132148
UserDefaults.standard.set("h:mm", forKey: "timeFormat")
133-
timeFormat24h.state = .off
149+
134150
case 1:
135151
UserDefaults.standard.set("HH:mm", forKey: "timeFormat")
136-
timeFormat12h.state = .off
152+
137153
default:
138154
return
139155
}
@@ -164,6 +180,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
164180
@IBAction func didClickURL(_ sender: NSButton) {
165181
let url: String
166182
switch sender.tag {
183+
case 0:
184+
url = "https://blog.licardo.cn/posts/33030"
167185
case 1:
168186
url = "https://github.com/L1cardo"
169187
case 2:
@@ -185,3 +203,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
185203
}
186204

187205
}
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

Comments
 (0)