Skip to content

Commit

Permalink
Show up as menu
Browse files Browse the repository at this point in the history
  • Loading branch information
LEOYoon-Tsaw committed Oct 12, 2022
1 parent 5ca06a4 commit ee0b6f2
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 126 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 = 38;
CURRENT_PROJECT_VERSION = 39;
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.5;
MARKETING_VERSION = 3.0;
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 = 38;
CURRENT_PROJECT_VERSION = 39;
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.5;
MARKETING_VERSION = 3.0;
PRODUCT_BUNDLE_IDENTIFIER = "Chinese-Time";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down
58 changes: 23 additions & 35 deletions ChineseTime/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,38 @@ func updateStatusTitle(title: String) {
statusItem?.length = button.intrinsicContentSize.width
}
}
func updatePosition() {
if let frame = statusItem?.button?.window?.frame {
WatchFace.currentInstance?.moveTopCenter(to: NSMakePoint(NSMidX(frame), NSMinY(frame)))
}
}

@main
class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate {

@IBOutlet weak var lockedMenuItem: NSMenuItem!
@IBOutlet weak var keepTopMenuItem: NSMenuItem!
@IBOutlet weak var statusBarItem: NSMenuItem!

@IBAction func toggleLocked(_ sender: Any) {
if let watchFace = WatchFace.currentInstance {
watchFace.locked(!watchFace.isLocked)
lockedMenuItem.state = watchFace.isLocked ? .on : .off
}
}
@IBAction func togglekeepTop(_ sender: Any) {
if let watchFace = WatchFace.currentInstance {
watchFace.setTop(!watchFace.isTop)
keepTopMenuItem.state = watchFace.isTop ? .on : .off
}
}
@IBAction func bringCenter(_ sender: Any) {
WatchFace.currentInstance?.setCenter()
}

@IBAction func showHelp(_ sender: Any) {
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)
statusItem?.button?.action = #selector(self.toggleDisplay(sender:))
statusItem?.button?.sendAction(on: [.leftMouseDown])
}

@objc func toggleDisplay(sender: NSStatusItem) {
if let watchFace = WatchFace.currentInstance {
if watchFace.isVisible {
watchFace.hide()
} else {
watchFace.show()
updatePosition()
NSApp.activate(ignoringOtherApps: true)
}
}
}

func applicationDidResignActive(_ notification: Notification) {
WatchFace.currentInstance?.hide()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
Expand Down
4 changes: 2 additions & 2 deletions ChineseTime/GradientSlider.xib
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="GradientSlider" customModule="ChineseTime" customModuleProvider="target"/>
<customObject id="-2" userLabel="File's Owner" customClass="GradientSlider" customModule="Chinese_Time" customModuleProvider="target"/>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="c22-O7-iKe" customClass="GradientSlider" customModule="ChineseTime" customModuleProvider="target">
<customView id="c22-O7-iKe" customClass="GradientSlider" customModule="Chinese_Time" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="480" height="46"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<point key="canvasLocation" x="24" y="134"/>
Expand Down
2 changes: 2 additions & 0 deletions ChineseTime/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
<string>Main</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>LSUIElement</key>
<true/>
</dict>
</plist>
106 changes: 51 additions & 55 deletions ChineseTime/WatchFace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class RoundedRect {
thirdArc.append(((lambda * totalLength-(innerWidth*1.5+2*arcLength+innerHeight)) / arcLength, i))
case (innerWidth*1.5+3*arcLength+innerHeight)..<(innerWidth*1.5+3*arcLength+2*innerHeight):
fourthLine.append((lambda * totalLength - (innerWidth*1.5+3*arcLength+innerHeight), i))
case (innerWidth*1.5+3*arcLength+2*innerHeight)..<(totalLength-innerWidth/2):
case (innerWidth*1.5+3*arcLength+2*innerHeight)..<(innerWidth*1.5+4*arcLength+2*innerHeight):
fourthArc.append(((lambda * totalLength-(innerWidth*1.5+3*arcLength+2*innerHeight)) / arcLength, i))
case (totalLength-innerWidth/2)...totalLength:
fifthLine.append((lambda * totalLength - (totalLength-innerWidth/2), i))
Expand Down Expand Up @@ -273,6 +273,7 @@ class RoundedRect {
class WatchFaceView: NSView {
private static let majorUpdateInterval: CGFloat = 3600
private static let minorUpdateInterval: CGFloat = majorUpdateInterval / 12
static let frameOffset: CGFloat = 5

class GraphicArtifects {
var outerBound: RoundedRect?
Expand Down Expand Up @@ -394,8 +395,7 @@ class WatchFaceView: NSView {
}

override func draw(_ rawRect: NSRect) {
let frameOffset = 0.05 * min(rawRect.width, rawRect.height)
let dirtyRect = rawRect.insetBy(dx: frameOffset, dy: frameOffset)
let dirtyRect = rawRect.insetBy(dx: Self.frameOffset, dy: Self.frameOffset)
let isDark = self.isDark

func angleMask(angle: CGFloat, startingAngle: CGFloat, in circle: RoundedRect) -> CAShapeLayer {
Expand Down Expand Up @@ -731,20 +731,20 @@ class WatchFaceView: NSView {

func getVagueShapes(shortEdge: CGFloat, longEdge: CGFloat) {
// Basic paths
graphicArtifects.outerBound = RoundedRect(rect: dirtyRect, nodePos: cornerSize, ankorPos: cornerSize*0.2)
graphicArtifects.firstRingOuter = graphicArtifects.outerBound!.shrink(by: 0.05 * shortEdge)
graphicArtifects.firstRingInner = graphicArtifects.firstRingOuter!.shrink(by: 0.07 * shortEdge)
graphicArtifects.outerBound = RoundedRect(rect: dirtyRect, nodePos: cornerSize, ankorPos: cornerSize*0.2).shrink(by: 0.02 * shortEdge)
graphicArtifects.firstRingOuter = graphicArtifects.outerBound!.shrink(by: 0.047 * shortEdge)
graphicArtifects.firstRingInner = graphicArtifects.firstRingOuter!.shrink(by: 0.066 * shortEdge)

graphicArtifects.secondRingOuter = graphicArtifects.firstRingInner!.shrink(by: 0.01 * shortEdge)
graphicArtifects.secondRingInner = graphicArtifects.secondRingOuter!.shrink(by: 0.07 * shortEdge)
graphicArtifects.secondRingOuter = graphicArtifects.firstRingInner!.shrink(by: 0.00946 * shortEdge)
graphicArtifects.secondRingInner = graphicArtifects.secondRingOuter!.shrink(by: 0.066 * shortEdge)

graphicArtifects.thirdRingOuter = graphicArtifects.secondRingInner!.shrink(by: 0.01 * shortEdge)
graphicArtifects.thirdRingInner = graphicArtifects.thirdRingOuter!.shrink(by: 0.07 * shortEdge)
graphicArtifects.thirdRingOuter = graphicArtifects.secondRingInner!.shrink(by: 0.00946 * shortEdge)
graphicArtifects.thirdRingInner = graphicArtifects.thirdRingOuter!.shrink(by: 0.066 * shortEdge)

graphicArtifects.fourthRingOuter = graphicArtifects.thirdRingInner!.shrink(by: 0.01 * shortEdge)
graphicArtifects.fourthRingInner = graphicArtifects.fourthRingOuter!.shrink(by: 0.07 * shortEdge)
graphicArtifects.fourthRingOuter = graphicArtifects.thirdRingInner!.shrink(by: 0.00946 * shortEdge)
graphicArtifects.fourthRingInner = graphicArtifects.fourthRingOuter!.shrink(by: 0.066 * shortEdge)

graphicArtifects.innerBound = graphicArtifects.fourthRingInner!.shrink(by: 0.01 * shortEdge)
graphicArtifects.innerBound = graphicArtifects.fourthRingInner!.shrink(by: 0.00946 * shortEdge)

graphicArtifects.outerBoundPath = graphicArtifects.outerBound!.path
graphicArtifects.firstRingOuterPath = graphicArtifects.firstRingOuter!.path
Expand All @@ -766,13 +766,8 @@ class WatchFaceView: NSView {
graphicArtifects.innerBoundPath = graphicArtifects.innerBound!.path

// Will be used from outside this View
shape.path = graphicArtifects.firstRingOuterPath!
shape.fillColor = NSColor(deviceWhite: 1.0, alpha: watchLayout.backAlpha).cgColor
shape.shadowPath = graphicArtifects.outerBoundPath!
shape.shadowColor = shape.fillColor
shape.shadowOpacity = 1
shape.shadowOffset = NSMakeSize(0, 0)
shape.shadowRadius = frameOffset / 2
let shortEdge = min(dirtyRect.width, dirtyRect.height)
shape.path = RoundedRect(rect: dirtyRect, nodePos: shortEdge * 0.08, ankorPos: shortEdge*0.08*0.2).path
}

let shortEdge = min(dirtyRect.width, dirtyRect.height)
Expand Down Expand Up @@ -876,7 +871,6 @@ class WatchFaceView: NSView {
class WatchFace: NSWindow {
let _view: WatchFaceView
let _backView: NSVisualEffectView
private var _visible = false
private var _timer: Timer?
static var currentInstance: WatchFace? = nil
private static let updateInterval: CGFloat = 14.4
Expand All @@ -885,62 +879,60 @@ class WatchFace: NSWindow {
_view = WatchFaceView(frame: position)
let blurView = NSVisualEffectView()
blurView.blendingMode = .behindWindow
blurView.material = .fullScreenUI
blurView.material = .popover
blurView.state = .active
blurView.wantsLayer = true
_backView = blurView
super.init(contentRect: position, styleMask: .borderless, backing: .buffered, defer: true)
self.alphaValue = 1
self.level = NSWindow.Level.floating
self.hasShadow = false
self.hasShadow = true
self.isOpaque = false
self.backgroundColor = .clear
let contentView = NSView()
self.contentView = contentView
contentView.addSubview(_backView)
contentView.addSubview(_view)
self.isMovableByWindowBackground = true
self.isMovableByWindowBackground = false
}

override var isVisible: Bool {
_visible
}

var isLocked: Bool {
!self.isMovableByWindowBackground
}
var isTop: Bool {
self.level == NSWindow.Level.floating
}

func locked(_ on: Bool) {
if on {
self.isMovableByWindowBackground = false
} else {
self.isMovableByWindowBackground = true
}
}
func setTop(_ on: Bool) {
if on {
self.level = NSWindow.Level.floating
} else {
self.level = NSWindow.Level(rawValue: NSWindow.Level.normal.rawValue - 1)
get {
contentView != nil && !contentView!.isHidden
} set {
contentView?.isHidden = !newValue
}
}

func setCenter() {
let windowRect = self.getCurrentScreen()
self.setFrame(NSMakeRect(
windowRect.midX - _view.watchLayout.watchSize.width / 2,
windowRect.midY - _view.watchLayout.watchSize.height / 2,
_view.watchLayout.watchSize.width, _view.watchLayout.watchSize.height), display: true)
windowRect.midX - _view.watchLayout.watchSize.width / 2,
windowRect.midY - _view.watchLayout.watchSize.height / 2,
_view.watchLayout.watchSize.width, _view.watchLayout.watchSize.height), display: true)
}

func moveTopCenter(to: CGPoint) {
let windowRect = self.getCurrentScreen()
var frame = NSMakeRect(
to.x - _view.watchLayout.watchSize.width / 2,
to.y - _view.watchLayout.watchSize.height,
_view.watchLayout.watchSize.width, _view.watchLayout.watchSize.height
)
if NSMaxX(frame) >= NSMaxX(windowRect) {
frame.origin.x = NSMaxX(windowRect) - frame.width
} else if NSMinX(frame) <= NSMinX(windowRect) {
frame.origin.x = NSMinX(windowRect)
}
self.setFrame(frame, display: true)
}

func getCurrentScreen() -> NSRect {
var screenRect = NSScreen.main!.frame
let screens = NSScreen.screens
for i in 0..<screens.count {
let rect = screens[i].frame
if NSPointInRect(NSMakePoint(self.frame.midX, self.frame.midY), rect) {
if let statusBarFrame = Chinese_Time.statusItem?.button?.window?.frame, NSPointInRect(NSMakePoint(statusBarFrame.midX, statusBarFrame.midY), rect) {
screenRect = rect
break
}
Expand All @@ -950,13 +942,13 @@ class WatchFace: NSWindow {

func updateSize(with frame: NSRect?) {
let watchDimension = _view.watchLayout.watchSize
if frame == nil {
setCenter()
} else {
if frame != nil {
self.setFrame(NSMakeRect(
frame!.midX - watchDimension.width / 2,
frame!.midY - watchDimension.height / 2,
watchDimension.width, watchDimension.height), display: true)
} else {
setCenter()
}
_view.frame = _view.superview!.bounds
_backView.frame = _view.superview!.bounds
Expand All @@ -970,7 +962,7 @@ class WatchFace: NSWindow {
_view.drawView(forceRefresh: false)
self._backView.layer?.mask = _view.shape
self.orderFront(nil)
self._visible = true
self.isVisible = true
self._timer = Timer.scheduledTimer(withTimeInterval: Self.updateInterval, repeats: true) {_ in
self.invalidateShadow()
self._view.drawView(forceRefresh: false)
Expand All @@ -979,9 +971,13 @@ class WatchFace: NSWindow {
}

func hide() {
self.isVisible = false
}

override func close() {
self._timer?.invalidate()
self._timer = nil
self._visible = false
Self.currentInstance = nil
super.close()
}
}
30 changes: 1 addition & 29 deletions ChineseTime/en.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -624,29 +624,7 @@
<action selector="performZoom:" target="Ady-hI-5gd" id="DIl-cC-cCs"/>
</connections>
</menuItem>
<menuItem title="Center" keyEquivalent="C" id="vN3-6a-37W">
<modifierMask key="keyEquivalentModifierMask" control="YES" command="YES"/>
<connections>
<action selector="bringCenter:" target="Voe-Tx-rLC" id="8XF-tN-iOc"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="eu3-7i-yIM"/>
<menuItem title="Lock" keyEquivalent="l" id="PEQ-kQ-g5U">
<connections>
<action selector="toggleLocked:" target="Voe-Tx-rLC" id="6wE-3s-Heu"/>
</connections>
</menuItem>
<menuItem title="Keep top" state="on" keyEquivalent="T" id="4Vr-av-PsQ">
<connections>
<action selector="togglekeepTop:" target="Voe-Tx-rLC" id="a87-bM-KBc"/>
</connections>
</menuItem>
<menuItem title="Status Bar" state="on" id="VTQ-32-KyF">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="toggleStatusBar:" target="Voe-Tx-rLC" id="ERj-1N-J1B"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
Expand All @@ -668,13 +646,7 @@
<outlet property="delegate" destination="Voe-Tx-rLC" id="PrD-fu-P6m"/>
</connections>
</application>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Chinese_Time" customModuleProvider="target">
<connections>
<outlet property="keepTopMenuItem" destination="4Vr-av-PsQ" id="3BB-qy-pdp"/>
<outlet property="lockedMenuItem" destination="PEQ-kQ-g5U" id="l1s-l4-35J"/>
<outlet property="statusBarItem" destination="VTQ-32-KyF" id="Ogq-1F-6h4"/>
</connections>
</customObject>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Chinese_Time" customModuleProvider="target"/>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
Expand Down
2 changes: 1 addition & 1 deletion layout.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
globalMonth: true
apparentTime: true
backAlpha: 1.0
firstRing: locations: 0.0, 0.25, 0.5, 0.75; colors: 0xFFDB8174, 0xFF6173BC, 0xFF7033E7, 0xFFAE74B9; loop: true
firstRing: locations: 0.0, 0.25, 0.5, 0.75; colors: 0xFFDB8174, 0xFFAE74B9, 0xFF7033E7, 0xFF6173BC; loop: true
secondRing: locations: 0.0, 0.25, 0.5, 0.75; colors: 0xFFB765B7, 0xFF4B5FC1, 0xFF4598D9, 0xFF4B5FC1; loop: true
thirdRing: locations: 0.0, 0.5; colors: 0xFFCB6693, 0xFF5E44D3; loop: true
innerColor: 0x66FFFFFF
Expand Down

0 comments on commit ee0b6f2

Please sign in to comment.