From 5ca06a494bd8162eae4b4a9fb943aa5c1686e975 Mon Sep 17 00:00:00 2001 From: LEO Yoon-Tsaw Date: Tue, 11 Oct 2022 21:04:00 -0400 Subject: [PATCH] Add StatusBar Item --- Chinese Time.xcodeproj/project.pbxproj | 8 +-- ChineseTime/AppDelegate.swift | 26 ++++++++- ChineseTime/Layout.swift | 14 ++--- ChineseTime/WatchFace.swift | 5 ++ ChineseTime/en.lproj/Main.storyboard | 77 ++++++++++++++------------ 5 files changed, 81 insertions(+), 49 deletions(-) diff --git a/Chinese Time.xcodeproj/project.pbxproj b/Chinese Time.xcodeproj/project.pbxproj index 2d52777..960f8f5 100644 --- a/Chinese Time.xcodeproj/project.pbxproj +++ b/Chinese Time.xcodeproj/project.pbxproj @@ -304,7 +304,7 @@ CODE_SIGN_IDENTITY = "-"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 37; + CURRENT_PROJECT_VERSION = 38; DEVELOPMENT_TEAM = 28HU5A7B46; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = ChineseTime/Info.plist; @@ -315,7 +315,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 11.0; - MARKETING_VERSION = 2.4; + MARKETING_VERSION = 2.5; PRODUCT_BUNDLE_IDENTIFIER = "Chinese-Time"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; @@ -331,7 +331,7 @@ CODE_SIGN_IDENTITY = "-"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 37; + CURRENT_PROJECT_VERSION = 38; DEVELOPMENT_TEAM = 28HU5A7B46; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = ChineseTime/Info.plist; @@ -342,7 +342,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 11.0; - MARKETING_VERSION = 2.4; + MARKETING_VERSION = 2.5; PRODUCT_BUNDLE_IDENTIFIER = "Chinese-Time"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; diff --git a/ChineseTime/AppDelegate.swift b/ChineseTime/AppDelegate.swift index ec1c4f9..70ff3c0 100644 --- a/ChineseTime/AppDelegate.swift +++ b/ChineseTime/AppDelegate.swift @@ -10,15 +10,20 @@ import CoreLocation import MapKit var locManager: CLLocationManager? +var statusItem: NSStatusItem? +func updateStatusTitle(title: String) { + if let button = statusItem?.button { + button.title = title + statusItem?.length = button.intrinsicContentSize.width + } +} @main class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate { @IBOutlet weak var lockedMenuItem: NSMenuItem! @IBOutlet weak var keepTopMenuItem: NSMenuItem! - class var locationManager: CLLocationManager? { - locManager - } + @IBOutlet weak var statusBarItem: NSMenuItem! @IBAction func toggleLocked(_ sender: Any) { if let watchFace = WatchFace.currentInstance { @@ -40,10 +45,22 @@ class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate { NSWorkspace.shared.open(URL(string: "https://github.com/LEOYoon-Tsaw/ChineseTime")!) } + @IBAction func toggleStatusBar(_ sender: Any) { + if statusItem == nil { + statusItem = NSStatusBar.system.statusItem(withLength: 0) + WatchFace.currentInstance?._view.updateStatusBar() + statusBarItem.state = .on + } else { + statusItem = nil + statusBarItem.state = .off + } + } + func applicationWillFinishLaunching(_ aNotification: Notification) { locManager = CLLocationManager() locManager?.delegate = self locManager?.desiredAccuracy = kCLLocationAccuracyKilometer + statusItem = NSStatusBar.system.statusItem(withLength: 0) } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { @@ -169,6 +186,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { loadSave() let preview = WatchFace(position: NSZeroRect) + if locManager?.authorizationStatus == .authorized || locManager?.authorizationStatus == .authorizedAlways { + locManager?.startUpdatingLocation() + } preview.show() } diff --git a/ChineseTime/Layout.swift b/ChineseTime/Layout.swift index 883125f..cd72f53 100644 --- a/ChineseTime/Layout.swift +++ b/ChineseTime/Layout.swift @@ -571,7 +571,7 @@ class ConfigurationViewController: NSViewController, NSWindowDelegate { } @IBAction func currentLocationToggled(_ sender: Any) { if readToggle(button: currentLocationToggle) { - if AppDelegate.locationManager?.authorizationStatus == .authorized || AppDelegate.locationManager?.authorizationStatus == .authorizedAlways { + if Chinese_Time.locManager?.authorizationStatus == .authorized || Chinese_Time.locManager?.authorizationStatus == .authorizedAlways { longitudeSpherePicker.isEnabled = false longitudeDegreePicker.isEnabled = false longitudeMinutePicker.isEnabled = false @@ -581,16 +581,16 @@ class ConfigurationViewController: NSViewController, NSWindowDelegate { latitudeMinutePicker.isEnabled = false latitudeSecondPicker.isEnabled = false if let sender = sender as? NSButton, sender == currentLocationToggle { - AppDelegate.locationManager!.startUpdatingLocation() + Chinese_Time.locManager!.startUpdatingLocation() } - } else if AppDelegate.locationManager?.authorizationStatus == .notDetermined || AppDelegate.locationManager?.authorizationStatus == .restricted { - AppDelegate.locationManager!.requestWhenInUseAuthorization() + } else if Chinese_Time.locManager?.authorizationStatus == .notDetermined || Chinese_Time.locManager?.authorizationStatus == .restricted { + Chinese_Time.locManager!.requestWhenInUseAuthorization() currentLocationToggle.state = .off } else { currentLocationToggle.state = .off let alert = NSAlert() - alert.messageText = "定位未開" - alert.informativeText = "若需獲取所在地經緯度,請打開定位服務" + alert.messageText = "Location service disabled" + alert.informativeText = "Please enable location service to obtain your longitude and latitude" alert.runModal() } } else { @@ -885,7 +885,7 @@ class ConfigurationViewController: NSViewController, NSWindowDelegate { override func viewWillAppear() { super.viewWillAppear() self.view.window?.delegate = self - if AppDelegate.locationManager?.authorizationStatus == .authorized || AppDelegate.locationManager?.authorizationStatus == .authorizedAlways { + if Chinese_Time.locManager?.authorizationStatus == .authorized || Chinese_Time.locManager?.authorizationStatus == .authorizedAlways { currentLocationToggle.state = .on } else { currentLocationToggle.state = .off diff --git a/ChineseTime/WatchFace.swift b/ChineseTime/WatchFace.swift index e1fdad0..775e1cb 100644 --- a/ChineseTime/WatchFace.swift +++ b/ChineseTime/WatchFace.swift @@ -389,6 +389,10 @@ class WatchFaceView: NSView { self.needsDisplay = true } + func updateStatusBar() { + Chinese_Time.updateStatusTitle(title: "\(chineseCalendar.dateString) \(chineseCalendar.timeString)") + } + override func draw(_ rawRect: NSRect) { let frameOffset = 0.05 * min(rawRect.width, rawRect.height) let dirtyRect = rawRect.insetBy(dx: frameOffset, dy: frameOffset) @@ -863,6 +867,7 @@ class WatchFaceView: NSView { if (graphicArtifects.centerText == nil) || (dateString+timeString != keyStates.datetimeString) { graphicArtifects.centerText = drawCenterTextGradient(innerBound: graphicArtifects.innerBound!, dateString: dateString, timeString: timeString) keyStates.datetimeString = dateString+timeString + updateStatusBar() } self.layer?.addSublayer(graphicArtifects.centerText!) } diff --git a/ChineseTime/en.lproj/Main.storyboard b/ChineseTime/en.lproj/Main.storyboard index 10d2e13..44b0957 100644 --- a/ChineseTime/en.lproj/Main.storyboard +++ b/ChineseTime/en.lproj/Main.storyboard @@ -641,6 +641,12 @@ + + + + + + @@ -662,10 +668,11 @@ - + + @@ -702,7 +709,7 @@ - + @@ -736,7 +743,7 @@ - + @@ -771,7 +778,7 @@ - + @@ -802,7 +809,7 @@ - + @@ -835,7 +842,7 @@ - + @@ -848,22 +855,22 @@ - + - + - + - + @@ -877,7 +884,7 @@ - + @@ -891,7 +898,7 @@ - + @@ -905,7 +912,7 @@ - + @@ -928,7 +935,7 @@ - + @@ -942,7 +949,7 @@ - + @@ -956,12 +963,12 @@ - + - + @@ -975,7 +982,7 @@ - + @@ -989,7 +996,7 @@ - + @@ -1309,7 +1316,7 @@ - + @@ -1366,12 +1373,12 @@ - + - + @@ -1385,7 +1392,7 @@ - + @@ -1399,7 +1406,7 @@ - + @@ -1413,7 +1420,7 @@ - + @@ -1427,7 +1434,7 @@ - + @@ -1441,7 +1448,7 @@ - + @@ -1455,7 +1462,7 @@ - + @@ -1504,7 +1511,7 @@ - + @@ -1518,7 +1525,7 @@ - + @@ -1532,7 +1539,7 @@ - + @@ -1546,7 +1553,7 @@ - + @@ -1660,7 +1667,7 @@ - + @@ -1674,7 +1681,7 @@ - + @@ -1688,7 +1695,7 @@ - +