Skip to content

Commit

Permalink
add 2 time format to switch by tapping Touch Bar icon
Browse files Browse the repository at this point in the history
  • Loading branch information
L1cardo committed Feb 12, 2020
1 parent 1cdb6f7 commit b3421b1
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 92 deletions.
16 changes: 8 additions & 8 deletions ClockBar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
/* Begin PBXFileReference section */
091F12975425693F99521C97 /* Pods_ClockBar.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ClockBar.framework; sourceTree = BUILT_PRODUCTS_DIR; };
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>"; };
964408C723F3CD3200CDF07C /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/MainMenu.strings"; sourceTree = "<group>"; };
964408CB23F3CEF400CDF07C /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/MainMenu.strings; sourceTree = "<group>"; };
9646ECC5237583FE0019A927 /* ClockBar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ClockBar.app; sourceTree = BUILT_PRODUCTS_DIR; };
9646ECC8237583FE0019A927 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
9646ECCA237583FF0019A927 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand All @@ -42,8 +44,6 @@
9646ECD62375843B0019A927 /* ClockBar-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ClockBar-Bridging-Header.h"; sourceTree = "<group>"; };
9646ECD72375843B0019A927 /* TouchBar.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBar.h; sourceTree = "<group>"; };
9646ECDA237584CE0019A927 /* DFRFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = DFRFoundation.framework; path = ../../../../System/Library/PrivateFrameworks/DFRFoundation.framework; sourceTree = "<group>"; };
96BB033923C2304F004D5AFF /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/MainMenu.strings; sourceTree = "<group>"; };
96D2C44D2375A94400B6B506 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/MainMenu.strings"; sourceTree = "<group>"; };
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>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -255,8 +255,8 @@
isa = PBXVariantGroup;
children = (
9646ECCD237583FF0019A927 /* Base */,
96D2C44D2375A94400B6B506 /* zh-Hans */,
96BB033923C2304F004D5AFF /* en */,
964408C723F3CD3200CDF07C /* zh-Hans */,
964408CB23F3CEF400CDF07C /* en */,
);
name = MainMenu.xib;
sourceTree = "<group>";
Expand Down Expand Up @@ -392,15 +392,15 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 20200105;
CURRENT_PROJECT_VERSION = 20200212;
DEVELOPMENT_TEAM = 2U23P5CPX2;
ENABLE_HARDENED_RUNTIME = NO;
INFOPLIST_FILE = "ClockBar/Supporting Files/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.1.1;
MARKETING_VERSION = 1.2.0;
PRODUCT_BUNDLE_IDENTIFIER = cn.licardo.ClockBar;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "ClockBar/TouchBar/ClockBar-Bridging-Header.h";
Expand All @@ -424,15 +424,15 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 20200105;
CURRENT_PROJECT_VERSION = 20200212;
DEVELOPMENT_TEAM = 2U23P5CPX2;
ENABLE_HARDENED_RUNTIME = NO;
INFOPLIST_FILE = "ClockBar/Supporting Files/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.1.1;
MARKETING_VERSION = 1.2.0;
PRODUCT_BUNDLE_IDENTIFIER = cn.licardo.ClockBar;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "ClockBar/TouchBar/ClockBar-Bridging-Header.h";
Expand Down
Binary file not shown.
57 changes: 41 additions & 16 deletions ClockBar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

import Cocoa
import LoginServiceKit
import Defaults

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate {

@IBOutlet weak var statusBarMenu: NSMenu!
@IBOutlet weak var preferencesWindowVersionNum: NSTextField!
@IBOutlet weak var aboutWindowVersionNum: NSTextField!
@IBOutlet weak var launchAtLoginCheckbox: NSButton!
@IBOutlet weak var timeFormat12h: NSButton!
@IBOutlet weak var timeFormat24h: NSButton!
@IBOutlet weak var time1Text: NSTextField!
@IBOutlet weak var time2Text: NSTextField!
@IBOutlet weak var preferencesWindow: NSWindow!
@IBOutlet weak var aboutWindow: NSWindow!
@IBOutlet weak var alertWindow: NSWindow!
Expand All @@ -38,14 +39,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {

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

switch UserDefaults.standard.string(forKey: "timeFormat") {
case "HH:mm":
timeFormat12h.state = .off
timeFormat24h.state = .on
default:
timeFormat12h.state = .on
timeFormat24h.state = .off
}
time1Text.delegate = self
time2Text.delegate = self
time1Text.stringValue = Defaults[.time1]
time2Text.stringValue = Defaults[.time2]

clockBar()

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

func showTime() {
switch Defaults[.shouldShowTime1] {
case true:
Defaults[.showTime] = Defaults[.time1]
default:
Defaults[.showTime] = Defaults[.time2]
}
}

func controlTextDidChange(_ obj: Notification) {
Defaults[.time1] = time1Text.stringValue
Defaults[.time2] = time2Text.stringValue
showTime()
}

// add clockbar to touch bar
func clockBar() {
DFRSystemModalShowsCloseBoxWhenFrontMost(true)

timeFormatter = DateFormatter()
timeFormatter?.dateFormat = UserDefaults.standard.string(forKey: "timeFormat") ?? "h:mm"
timeFormatter?.dateFormat = Defaults[.showTime]
let nowTime = timeFormatter?.string(from: Date())

let clockBarIdentifier = NSTouchBarItem.Identifier(rawValue: "ClockBar")
let clockBar = NSCustomTouchBarItem.init(identifier: clockBarIdentifier)
touchBarButton = NSButton(title: nowTime!, target: self, action: nil)
touchBarButton = NSButton(title: nowTime!, target: self, action: #selector(changeTime))
clockBar.view = touchBarButton!
NSTouchBarItem.addSystemTrayItem(clockBar)
DFRElementSetControlStripPresenceForIdentifier(clockBarIdentifier, true)
}

@objc func changeTime() {
showTime()
Defaults[.shouldShowTime1] = !Defaults[.shouldShowTime1]
}

// update time
@objc func updateTime() {
timeFormatter?.dateFormat = UserDefaults.standard.string(forKey: "timeFormat") ?? "h:mm"
timeFormatter?.dateFormat = Defaults[.showTime]
touchBarButton?.title = (timeFormatter?.string(from: Date()))!
}

Expand Down Expand Up @@ -130,10 +146,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
switch sender.tag {
case 0:
UserDefaults.standard.set("h:mm", forKey: "timeFormat")
timeFormat24h.state = .off

case 1:
UserDefaults.standard.set("HH:mm", forKey: "timeFormat")
timeFormat12h.state = .off

default:
return
}
Expand Down Expand Up @@ -164,6 +180,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBAction func didClickURL(_ sender: NSButton) {
let url: String
switch sender.tag {
case 0:
url = "https://blog.licardo.cn/posts/33030"
case 1:
url = "https://github.com/L1cardo"
case 2:
Expand All @@ -185,3 +203,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

}

extension Defaults.Keys {
static let time1 = Key<String>("time1", default: "h:mm")
static let time2 = Key<String>("time2", default: "HH:mm")
static let showTime = Key<String>("showTime", default: "h:mm")
static let shouldShowTime1 = Key<Bool>("showTime1", default: true)
}
Loading

0 comments on commit b3421b1

Please sign in to comment.