Skip to content

Commit

Permalink
Add Options UI, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
LEOYoon-Tsaw committed Oct 15, 2022
1 parent ee0b6f2 commit 43ef784
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 35 deletions.
4 changes: 2 additions & 2 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 = 39;
CURRENT_PROJECT_VERSION = 40;
DEVELOPMENT_TEAM = 28HU5A7B46;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = ChineseTime/Info.plist;
Expand All @@ -331,7 +331,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 39;
CURRENT_PROJECT_VERSION = 40;
DEVELOPMENT_TEAM = 28HU5A7B46;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = ChineseTime/Info.plist;
Expand Down
8 changes: 8 additions & 0 deletions ChineseTime/Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ class ColorWell: NSColorWell {

class ConfigurationViewController: NSViewController, NSWindowDelegate {
static var currentInstance: ConfigurationViewController? = nil
@IBOutlet weak var readLayout: NSButton!
@IBOutlet weak var writeLayout: NSButton!
@IBOutlet weak var globalMonthPicker: NSPopUpButton!
@IBOutlet weak var apparentTimePicker: NSPopUpButton!
@IBOutlet weak var datetimePicker: NSDatePicker!
Expand Down Expand Up @@ -678,6 +680,12 @@ class ConfigurationViewController: NSViewController, NSWindowDelegate {
WatchFaceView.layoutTemplate = delegate.saveLayout()
self.view.window?.close()
}
@IBAction func readFile(_ sender: Any) {
(NSApp.delegate as! AppDelegate).openFile(sender)
}
@IBAction func writeFile(_ sender: Any) {
(NSApp.delegate as! AppDelegate).saveFile(sender)
}

func populateTimezonePicker(timezone: TimeZone?) {
timezonePicker.removeAllItems()
Expand Down
129 changes: 119 additions & 10 deletions ChineseTime/WatchFace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ class WatchFaceView: NSView {
gradientLayer.colors = gradient.colors.map { $0.cgColor }
gradientLayer.locations = gradient.locations.map { NSNumber(value: Double($0)) }
}
gradientLayer.frame = self.frame
gradientLayer.frame = self.bounds

let trackMask = shapeFrom(path: path)
let mask: CALayer
Expand Down Expand Up @@ -723,7 +723,7 @@ class WatchFaceView: NSView {
gradientLayer.type = .axial
gradientLayer.colors = watchLayout.centerFontColor.colors.map { $0.cgColor }
gradientLayer.locations = watchLayout.centerFontColor.locations.map { NSNumber(value: Double($0)) }
gradientLayer.frame = self.frame
gradientLayer.frame = self.bounds
gradientLayer.mask = centerText

return gradientLayer as CALayer
Expand Down Expand Up @@ -868,12 +868,86 @@ class WatchFaceView: NSView {
}
}

class OptionView: NSView {
let background: NSVisualEffectView
let button: NSButton
override var frame: NSRect {
didSet {
self.background.frame = self.bounds
self.button.frame = self.bounds
(background.layer?.mask as? CAShapeLayer)?.path = RoundedRect(rect: background.bounds, nodePos: background.bounds.height / 2, ankorPos: background.bounds.height / 2 * 0.2).path
}
}

override init(frame frameRect: NSRect) {
let background = NSVisualEffectView(frame: frameRect)
background.blendingMode = .behindWindow
background.material = .popover
background.state = .active
background.wantsLayer = true
let optionMask = CAShapeLayer()
optionMask.path = RoundedRect(rect: background.bounds, nodePos: background.bounds.height / 2, ankorPos: background.bounds.height / 2 * 0.2).path
background.layer?.mask = optionMask

let button = NSButton(frame: frameRect)
button.alignment = .center
button.isBordered = false

self.background = background
self.button = button
super.init(frame: frameRect)
self.addSubview(self.background)
self.addSubview(self.button)
}

required init?(coder: NSCoder) {
self.background = NSVisualEffectView()
self.button = NSButton()
super.init(coder: coder)
}

var title: String {
get {
button.title
} set {
button.title = newValue
}
}
var image: NSImage? {
get {
button.image
} set {
button.image = newValue
}
}
var action: Selector? {
get {
button.action
} set {
button.action = newValue
}
}
var target: AnyObject? {
get {
button.target
} set {
button.target = newValue
}
}
}

class WatchFace: NSWindow {
let _view: WatchFaceView
let _backView: NSVisualEffectView
let _settingButton: OptionView
let _closingButton: OptionView
private var _timer: Timer?
static var currentInstance: WatchFace? = nil
private static let updateInterval: CGFloat = 14.4
var buttonSize: NSSize {
let ratio = 80 * 2.3 / (self._view.watchLayout.watchSize.width / 2)
return NSMakeSize(80 / ratio, 30 / ratio)
}

init(position: NSRect) {
_view = WatchFaceView(frame: position)
Expand All @@ -883,7 +957,17 @@ class WatchFace: NSWindow {
blurView.state = .active
blurView.wantsLayer = true
_backView = blurView
_settingButton = OptionView(frame: NSZeroRect)
_closingButton = OptionView(frame: NSZeroRect)
super.init(contentRect: position, styleMask: .borderless, backing: .buffered, defer: true)

_settingButton.image = NSImage(systemSymbolName: "square.and.pencil", accessibilityDescription: "Setting")
_settingButton.target = self
_settingButton.action = #selector(self.openSetting(_:))
_closingButton.image = NSImage(systemSymbolName: "power", accessibilityDescription: "Quit")
_closingButton.target = self
_closingButton.action = #selector(self.closeApp(_:))

self.alphaValue = 1
self.level = NSWindow.Level.floating
self.hasShadow = true
Expand All @@ -893,9 +977,26 @@ class WatchFace: NSWindow {
self.contentView = contentView
contentView.addSubview(_backView)
contentView.addSubview(_view)
contentView.addSubview(_settingButton)
contentView.addSubview(_closingButton)
self.isMovableByWindowBackground = false
}

@objc func openSetting(_ sender: Any) {
if ConfigurationViewController.currentInstance == nil {
let storyboard = NSStoryboard(name: "Main", bundle: nil)
let windowController = storyboard.instantiateController(withIdentifier: "WindowController") as! NSWindowController
if let window = windowController.window {
let viewController = window.contentViewController as! ConfigurationViewController
ConfigurationViewController.currentInstance = viewController
windowController.showWindow(nil)
}
}
}
@objc func closeApp(_ sender: Any) {
NSApp.terminate(sender)
}

override var isVisible: Bool {
get {
contentView != nil && !contentView!.isHidden
Expand All @@ -908,16 +1009,16 @@ class WatchFace: NSWindow {
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.midY - _view.watchLayout.watchSize.height / 2 - buttonSize.height * 0.85,
_view.watchLayout.watchSize.width, _view.watchLayout.watchSize.height + buttonSize.height * 1.7), 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
to.y - _view.watchLayout.watchSize.height - buttonSize.height * 1.7,
_view.watchLayout.watchSize.width, _view.watchLayout.watchSize.height + buttonSize.height * 1.7
)
if NSMaxX(frame) >= NSMaxX(windowRect) {
frame.origin.x = NSMaxX(windowRect) - frame.width
Expand All @@ -942,16 +1043,24 @@ class WatchFace: NSWindow {

func updateSize(with frame: NSRect?) {
let watchDimension = _view.watchLayout.watchSize
let buttonSize = buttonSize
if frame != nil {
self.setFrame(NSMakeRect(
frame!.midX - watchDimension.width / 2,
frame!.midY - watchDimension.height / 2,
watchDimension.width, watchDimension.height), display: true)
frame!.midY - watchDimension.height / 2 - buttonSize.height * 0.85,
watchDimension.width, watchDimension.height + buttonSize.height * 1.7), display: true)
} else {
setCenter()
}
_view.frame = _view.superview!.bounds
_backView.frame = _view.superview!.bounds
var bounds = _view.superview!.bounds
bounds.origin.y += buttonSize.height * 1.7
bounds.size.height -= buttonSize.height * 1.7
_view.frame = bounds
_backView.frame = bounds
_settingButton.frame = NSMakeRect(bounds.width / 2 - buttonSize.width * 1.15, buttonSize.height / 2, buttonSize.width, buttonSize.height)
_settingButton.button.font = _settingButton.button.font?.withSize(buttonSize.height / 2)
_closingButton.frame = NSMakeRect(bounds.width / 2 + buttonSize.width * 0.15, buttonSize.height / 2, buttonSize.width, buttonSize.height)
_closingButton.button.font = _closingButton.button.font?.withSize(buttonSize.height / 2)
_view.cornerSize = _view.watchLayout.cornerRadiusRatio * min(watchDimension.width, watchDimension.height)
_view.graphicArtifects = WatchFaceView.GraphicArtifects()
}
Expand Down
Loading

0 comments on commit 43ef784

Please sign in to comment.