Skip to content

Commit 98fe4eb

Browse files
Swift 5 and new features
1 parent 47305d8 commit 98fe4eb

7 files changed

+158
-54
lines changed

SimpleClock/Base.lproj/Main.storyboard

+19-3
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,21 @@
352352
</tabViewItem>
353353
<tabViewItem label="SimpleClock Font" identifier="" id="iDh-80-elb">
354354
<view key="view" id="taf-1g-tyt">
355-
<rect key="frame" x="10" y="33" width="499" height="201"/>
355+
<rect key="frame" x="10" y="33" width="499" height="215"/>
356356
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
357+
<subviews>
358+
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Yf1-HE-CmJ">
359+
<rect key="frame" x="17" y="189" width="465" height="23"/>
360+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
361+
<buttonCell key="cell" type="roundTextured" title="Clock Interface Font" bezelStyle="texturedRounded" alignment="center" lineBreakMode="truncatingTail" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="3AV-f7-VPd">
362+
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
363+
<font key="font" metaFont="system"/>
364+
</buttonCell>
365+
<connections>
366+
<action selector="clockFontChanger:" target="XkM-dt-wNE" id="84X-P2-9Y7"/>
367+
</connections>
368+
</button>
369+
</subviews>
357370
</view>
358371
</tabViewItem>
359372
</tabViewItems>
@@ -368,6 +381,8 @@
368381
</view>
369382
<connections>
370383
<outlet property="DarkTheme" destination="Tb7-55-hYv" id="wvS-70-Pkb"/>
384+
<outlet property="DarkThemeStopwatch" destination="iBH-Kj-7HV" id="Gwa-3e-Msb"/>
385+
<outlet property="DarkThemeTimer" destination="i8L-N7-GWu" id="4Py-Wd-NEb"/>
371386
<outlet property="ShowAMPM" destination="Pe2-Hs-OEZ" id="YLm-cr-jEN"/>
372387
<outlet property="ShowSecond" destination="GTv-H8-BCQ" id="rQ0-Jj-emf"/>
373388
</connections>
@@ -739,6 +754,7 @@ DQ
739754
</touchBarItems>
740755
</touchBar>
741756
<connections>
757+
<outlet property="ModeChanger" destination="mRo-lU-Btf" id="CIT-9k-ngm"/>
742758
<segue destination="ZG4-kJ-tYT" kind="relationship" relationship="window.shadowedContentViewController" id="E2X-uu-NjB"/>
743759
</connections>
744760
</windowController>
@@ -791,7 +807,7 @@ DQ
791807
<modifierMask key="keyEquivalentModifierMask" command="YES"/>
792808
</buttonCell>
793809
<connections>
794-
<action selector="Pause:" target="ZG4-kJ-tYT" id="11s-GA-nc5"/>
810+
<action selector="PauseTimer:" target="ZG4-kJ-tYT" id="11s-GA-nc5"/>
795811
</connections>
796812
</button>
797813
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Dzu-4U-QMO">
@@ -804,7 +820,7 @@ DQ
804820
<modifierMask key="keyEquivalentModifierMask" command="YES"/>
805821
</buttonCell>
806822
<connections>
807-
<action selector="End:" target="ZG4-kJ-tYT" id="OV2-df-WXh"/>
823+
<action selector="EndTimer:" target="ZG4-kJ-tYT" id="OV2-df-WXh"/>
808824
</connections>
809825
</button>
810826
</subviews>

SimpleClock/ViewController/PreferenceViewController.swift

+12
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@ class PreferenceViewController: NSViewController, NSWindowDelegate{
1818
@IBOutlet weak var ShowAMPM : NSButton!
1919
@IBOutlet weak var ShowSecond: NSButton!
2020
@IBOutlet weak var DarkTheme: NSButton!
21+
@IBOutlet weak var DarkThemeTimer: NSButton!
22+
@IBOutlet weak var DarkThemeStopwatch: NSButton!
2123

2224
var delegate: PreferencesViewControllerDelegate?
2325

2426
override func viewDidLoad() {
2527
super.viewDidLoad()
2628
let defau = UserDefaults.standard
2729
let dark = defau.bool(forKey: "DarkTheme?")
30+
let dark2 = defau.bool(forKey: "DarkThemeTimer?")
31+
let dark3 = defau.bool(forKey: "DarkThemeStopwatch?")
2832
let AM = defau.bool(forKey: "ShowAM?")
2933
let Sec = defau.bool(forKey: "ShowSecond?")
3034

3135
setStatus(ShowAMPM, status: AM)
3236
setStatus(DarkTheme, status: dark)
37+
setStatus(DarkThemeTimer, status: dark2)
38+
setStatus(DarkThemeStopwatch, status: dark3)
3339
setStatus(ShowSecond, status: Sec)
3440

3541
ShowAMPM.action = #selector(setAMPM)
@@ -60,7 +66,13 @@ class PreferenceViewController: NSViewController, NSWindowDelegate{
6066
defau.setValue(getStatus(button: ShowAMPM), forKey: "ShowAM?")
6167
defau.setValue(getStatus(button: ShowSecond), forKey: "ShowSecond?")
6268
defau.setValue(getStatus(button: DarkTheme), forKey: "DarkTheme?")
69+
defau.setValue(getStatus(button: DarkThemeStopwatch), forKey: "DarkThemeStopwatch?")
70+
defau.setValue(getStatus(button: DarkThemeTimer), forKey: "DarkThemeTimer?")
6371
self.delegate?.preferencesDidUpdate()
6472
super.viewWillDisappear()
6573
}
74+
75+
@IBAction func clockFontChanger(_ sender: Any) {
76+
}
77+
6678
}

SimpleClock/ViewController/StopwatchViewController.swift

+46-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,44 @@ class StopwatchViewController : NSViewController {
3434

3535
override func viewDidLoad() {
3636
super.viewDidLoad()
37+
let defau = UserDefaults.standard
38+
let dark = defau.bool(forKey: "DarkThemeStopwatch?")
39+
if !dark {
40+
changeLight()
41+
}else{
42+
changeDark()
43+
}
3744
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (timer) in
3845
self.runTimer()
3946
})
4047
timer.fireDate = Date.distantFuture
4148
myQueue.addOperation {
4249
ProcessInfo.processInfo.beginActivity(options: ProcessInfo.ActivityOptions.idleSystemSleepDisabled, reason: "Timer")
4350
}
51+
NotificationCenter.default.addObserver(self, selector: #selector(changeDark), name: NSNotification.Name.init("SCSTOPWATCHDARK"), object: nil)
52+
NotificationCenter.default.addObserver(self, selector: #selector(changeLight), name: NSNotification.Name.init("SCSTOPWATCHLIGHT"), object: nil)
53+
NotificationCenter.default.addObserver(self, selector: #selector(start), name: NSNotification.Name.init("SCSTOPWATCHSTART"), object: nil)
54+
NotificationCenter.default.addObserver(self, selector: #selector(pause), name: NSNotification.Name.init("SCSTOPWATCHPAUSE"), object: nil)
55+
NotificationCenter.default.addObserver(self, selector: #selector(end), name: NSNotification.Name.init("SCSTOPWATCHEND"), object: nil)
56+
57+
}
58+
59+
override func viewWillDisappear() {
60+
let defau = UserDefaults.standard
61+
defau.setValue(isDark, forKey: "DarkThemeStopwatch?")
62+
super.viewWillDisappear()
63+
}
64+
65+
@objc func changeDark(){
66+
self.Background.material = NSVisualEffectView.Material.dark
67+
self.TimerLabel.textColor = NSColor.white
68+
self.isDark = true
69+
}
70+
71+
@objc func changeLight(){
72+
self.Background.material = NSVisualEffectView.Material.mediumLight
73+
self.TimerLabel.textColor = NSColor.black
74+
self.isDark = false
4475
}
4576

4677
func runTimer() -> Void {
@@ -60,7 +91,11 @@ class StopwatchViewController : NSViewController {
6091
TimerLabel.stringValue = String(format: "%02d:%02d:%02d", hour,minute,second)
6192
}
6293

63-
@IBAction func StartTimer(_ sender: NSButton) {
94+
@objc func start(){
95+
StartTimer(Any.self)
96+
}
97+
98+
@IBAction func StartTimer(_ sender: Any) {
6499
if isStart == false {
65100
isStart = true
66101
second = 0
@@ -77,16 +112,24 @@ class StopwatchViewController : NSViewController {
77112
}
78113
}
79114

115+
@objc func pause(){
116+
PauseTimer(Any.self)
117+
}
118+
80119

81-
@IBAction func Pause(_ sender: NSButton) {
120+
@IBAction func PauseTimer(_ sender: Any) {
82121
if isStart {
83122
timer.fireDate = Date.distantFuture
84123
isPause = true
85124
NSSound(named: "Pop.aiff")?.play()
86125
}
87126
}
127+
128+
@objc func end(){
129+
EndTimer(Any.self)
130+
}
88131

89-
@IBAction func End(_ sender: NSButton) {
132+
@IBAction func EndTimer(_ sender: Any) {
90133
if isStart {
91134
timer.fireDate = Date.distantFuture
92135
second = 0

SimpleClock/ViewController/TimerViewController.swift

+57-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TimerViewController : NSViewController, NSUserNotificationCenterDelegate{
2525

2626
var isStart = Bool(false)
2727
var isPause = Bool(false)
28+
var isDark = Bool(true)
2829

2930
var myQueue = OperationQueue()
3031
let myActivity = ProcessInfo.processInfo.beginActivity(
@@ -44,6 +45,18 @@ class TimerViewController : NSViewController, NSUserNotificationCenterDelegate{
4445

4546
override func viewDidLoad() {
4647
super.viewDidLoad()
48+
let defau = UserDefaults.standard
49+
let dark = defau.bool(forKey: "DarkThemeTimer?")
50+
if !dark {
51+
changeLight()
52+
}else{
53+
changeDark()
54+
}
55+
NotificationCenter.default.addObserver(self, selector: #selector(changeDark), name: NSNotification.Name.init("SCTIMERDARK"), object: nil)
56+
NotificationCenter.default.addObserver(self, selector: #selector(changeLight), name: NSNotification.Name.init("SCTIMERLIGHT"), object: nil)
57+
NotificationCenter.default.addObserver(self, selector: #selector(start), name: NSNotification.Name.init("SCTIMERSTART"), object: nil)
58+
NotificationCenter.default.addObserver(self, selector: #selector(pause), name: NSNotification.Name.init("SCTIMERPAUSE"), object: nil)
59+
NotificationCenter.default.addObserver(self, selector: #selector(end), name: NSNotification.Name.init("SCTIMEREND"), object: nil)
4760
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (timer) in
4861
self.timerMain()
4962
})
@@ -53,6 +66,36 @@ class TimerViewController : NSViewController, NSUserNotificationCenterDelegate{
5366
}
5467
}
5568

69+
override func viewWillDisappear() {
70+
let defau = UserDefaults.standard
71+
defau.setValue(isDark, forKey: "DarkThemeTimer?")
72+
super.viewWillDisappear()
73+
}
74+
75+
@objc func changeDark(){
76+
background.material = NSVisualEffectView.Material.dark
77+
TimerText.textColor = NSColor.white
78+
hourLabel.textColor = NSColor.white
79+
minuteLabel.textColor = NSColor.white
80+
secondLabel.textColor = NSColor.white
81+
HourText.textColor = NSColor.white
82+
MinuteText.textColor = NSColor.white
83+
SecondText.textColor = NSColor.white
84+
isDark = true
85+
}
86+
87+
@objc func changeLight(){
88+
background.material = NSVisualEffectView.Material.mediumLight
89+
TimerText.textColor = NSColor.black
90+
hourLabel.textColor = NSColor.black
91+
minuteLabel.textColor = NSColor.black
92+
secondLabel.textColor = NSColor.black
93+
HourText.textColor = NSColor.black
94+
MinuteText.textColor = NSColor.black
95+
SecondText.textColor = NSColor.black
96+
isDark = false
97+
}
98+
5699
func timerMain() -> Void {
57100
if TimerText.stringValue == "00:00:00" {
58101
timer.fireDate = Date.distantFuture
@@ -86,9 +129,12 @@ class TimerViewController : NSViewController, NSUserNotificationCenterDelegate{
86129
}
87130

88131
}
89-
90132

91-
@IBAction func StartTimer(_ sender: NSButton) {
133+
@objc func start(){
134+
StartTimer(Any.self)
135+
}
136+
137+
@IBAction func StartTimer(_ sender: Any) {
92138
if isStart==false{
93139
isStart = true
94140
if(SecondText.stringValue.count>10 || MinuteText.stringValue.count>10 || HourText.stringValue.count>10){
@@ -128,16 +174,23 @@ class TimerViewController : NSViewController, NSUserNotificationCenterDelegate{
128174
}
129175
}
130176

131-
@IBAction func PauseTimer(_ sender: NSButton) {
177+
@objc func pause(){
178+
PauseTimer(Any.self)
179+
}
180+
181+
@IBAction func PauseTimer(_ sender: Any) {
132182
if isStart {
133183
isPause = true
134184
timer.fireDate = Date.distantFuture
135185
NSSound(named: "Pop.aiff")?.play()
136186
}
137187
}
138188

189+
@objc func end(){
190+
EndTimer(Any.self)
191+
}
139192

140-
@IBAction func EndTimer(_ sender: NSButton) {
193+
@IBAction func EndTimer(_ sender: Any) {
141194
if isStart {
142195
isStart = false
143196
timer.fireDate = Date.distantFuture

SimpleClock/WindowController/StopwatchWindowController.swift

+12-17
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,34 @@
99
import Cocoa
1010
class StopwatchWindowController: NSWindowController {
1111

12+
@IBOutlet weak var ModeChanger: NSSegmentedControl!
1213

13-
1414
@IBAction func Start(_ sender: Any) {
15-
let rootViewController = contentViewController as! StopwatchViewController
16-
rootViewController.TimerStartEnd.performClick(nil)
15+
NotificationCenter.default.post(name: NSNotification.Name.init("SCSTOPWATCHSTART"), object: nil)
1716
}
1817

1918
@IBAction func Pause(_ sender: Any) {
20-
let rootViewController = contentViewController as! StopwatchViewController
21-
rootViewController.Pause.performClick(nil)
19+
NotificationCenter.default.post(name: NSNotification.Name.init("SCSTOPWATCHPAUSE"), object: nil)
2220
}
2321

2422
@IBAction func End(_ sender: NSButtonCell) {
25-
let rootViewController = contentViewController as! StopwatchViewController
26-
rootViewController.End.performClick(nil
27-
)
23+
NotificationCenter.default.post(name: NSNotification.Name.init("SCSTOPWATCHEND"), object: nil)
2824
}
2925

3026
@IBAction func Changeback(_ sender: Any){
31-
let rootViewController = contentViewController as! StopwatchViewController
32-
if rootViewController.isDark {
33-
rootViewController.Background.material = NSVisualEffectView.Material.mediumLight
34-
rootViewController.TimerLabel.textColor = NSColor.black
35-
rootViewController.isDark = false
27+
if ModeChanger.isSelected(forSegment: 0) {
28+
NotificationCenter.default.post(name: NSNotification.Name.init("SCSTOPWATCHLIGHT"), object: nil)
3629
}else{
37-
rootViewController.Background.material = NSVisualEffectView.Material.dark
38-
rootViewController.TimerLabel.textColor = NSColor.white
39-
rootViewController.isDark = true
30+
NotificationCenter.default.post(name: NSNotification.Name.init("SCSTOPWATCHDARK"), object: nil)
4031
}
4132
}
4233

43-
4434
override func windowDidLoad() {
4535
super.windowDidLoad()
36+
let defau = UserDefaults.standard
37+
let dark = defau.bool(forKey: "DarkThemeStopwatch?")
38+
if !dark {
39+
ModeChanger.selectedSegment = 0
40+
}
4641
}
4742
}

SimpleClock/WindowController/TimerWindowController.swift

+12-26
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,34 @@ class TimerWindowController: NSWindowController {
1313
@IBOutlet weak var StartB: NSButton!
1414
@IBOutlet weak var PauseB: NSButton!
1515
@IBOutlet weak var EndB: NSButton!
16+
@IBOutlet weak var ModeChanger: NSSegmentedControl!
1617

1718
@IBAction func Start(_ sender: Any) {
18-
let rootViewController = contentViewController as! TimerViewController
19-
rootViewController.StartButton.performClick(Any.self)
20-
19+
NotificationCenter.default.post(name: NSNotification.Name.init("SCTIMERSTART"), object: nil)
2120
}
2221

2322
@IBAction func Pause(_ sender: Any) {
24-
let rootViewController = contentViewController as! TimerViewController
25-
rootViewController.PauseButton.performClick(Any.self)
23+
NotificationCenter.default.post(name: NSNotification.Name.init("SCTIMERPAUSE"), object: nil)
2624
}
2725

2826
@IBAction func End(_ sender: Any) {
29-
let rootViewController = contentViewController as! TimerViewController
30-
rootViewController.EndButton.performClick(Any.self)
27+
NotificationCenter.default.post(name: NSNotification.Name.init("SCTIMEREND"), object: nil)
3128
}
29+
3230
override func windowDidLoad() {
3331
super.windowDidLoad()
32+
let defau = UserDefaults.standard
33+
let dark = defau.bool(forKey: "DarkThemeTimer?")
34+
if !dark {
35+
ModeChanger.selectedSegment = 0
36+
}
3437
}
3538

36-
@IBOutlet weak var ModeChanger: NSSegmentedControl!
37-
3839
@IBAction func ChangeMode(_ sender: Any) {
39-
let rootViewController = contentViewController as! TimerViewController
4040
if ModeChanger.isSelected(forSegment: 0) {
41-
rootViewController.background.material = NSVisualEffectView.Material.mediumLight
42-
rootViewController.TimerText.textColor = NSColor.black
43-
rootViewController.hourLabel.textColor = NSColor.black
44-
rootViewController.minuteLabel.textColor = NSColor.black
45-
rootViewController.secondLabel.textColor = NSColor.black
46-
rootViewController.HourText.textColor = NSColor.black
47-
rootViewController.MinuteText.textColor = NSColor.black
48-
rootViewController.SecondText.textColor = NSColor.black
41+
NotificationCenter.default.post(name: NSNotification.Name.init("SCTIMERLIGHT"), object: nil)
4942
}else{
50-
rootViewController.background.material = NSVisualEffectView.Material.dark
51-
rootViewController.TimerText.textColor = NSColor.white
52-
rootViewController.hourLabel.textColor = NSColor.white
53-
rootViewController.minuteLabel.textColor = NSColor.white
54-
rootViewController.secondLabel.textColor = NSColor.white
55-
rootViewController.HourText.textColor = NSColor.white
56-
rootViewController.MinuteText.textColor = NSColor.white
57-
rootViewController.SecondText.textColor = NSColor.white
43+
NotificationCenter.default.post(name: NSNotification.Name.init("SCTIMERDARK"), object: nil)
5844
}
5945
}
6046
}

0 commit comments

Comments
 (0)