Skip to content

Commit

Permalink
Add StatusBar Item
Browse files Browse the repository at this point in the history
  • Loading branch information
LEOYoon-Tsaw committed Oct 12, 2022
1 parent a136bd1 commit 5ca06a4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 49 deletions.
8 changes: 4 additions & 4 deletions Chinese Time.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
26 changes: 23 additions & 3 deletions ChineseTime/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]) {
Expand Down Expand Up @@ -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()
}

Expand Down
14 changes: 7 additions & 7 deletions ChineseTime/Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions ChineseTime/WatchFace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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!)
}
Expand Down
Loading

0 comments on commit 5ca06a4

Please sign in to comment.