1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 17:38:38 +00:00

Fixed widget not showing up, also made it possible to pause the timer and reset it

This commit is contained in:
MaandagDev 2019-12-10 10:45:04 +01:00
parent e9e5a6f739
commit 3a9073454c
3 changed files with 57 additions and 24 deletions

View File

@ -239,7 +239,7 @@ class LongPressGestureRecognizer: NSPressGestureRecognizer {
extension String { extension String {
var defaultTouchbarAttributedString: NSAttributedString { var defaultTouchbarAttributedString: NSAttributedString {
let attrTitle = NSMutableAttributedString(string: self, attributes: [.foregroundColor: NSColor.white, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1]) let attrTitle = NSMutableAttributedString(string: self, attributes: [.foregroundColor: NSColor.white, .font: NSFont.monospacedDigitSystemFont(ofSize: 15, weight: .regular), .baselineOffset: 1])
attrTitle.setAlignment(.center, range: NSRange(location: 0, length: count)) attrTitle.setAlignment(.center, range: NSRange(location: 0, length: count))
return attrTitle return attrTitle
} }

View File

@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>0.25</string> <string>0.25</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>297</string> <string>317</string>
<key>LSApplicationCategoryType</key> <key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string> <string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key> <key>LSMinimumSystemVersion</key>

View File

@ -34,13 +34,15 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
case rest case rest
case none case none
} }
//Vars are used for pausing the timer.
private var started = false
private var timerPaused: Bool = false;
private let defaultTitle = "🍅" private let defaultTitle = ""
private let workTime: TimeInterval private let workTime: TimeInterval
private let restTime: TimeInterval private let restTime: TimeInterval
private var typeTime: TimeTypes = .none private var typeTime: TimeTypes = .none
private var timer: DispatchSourceTimer? private var timer: DispatchSourceTimer?
private var timeLeft: Int = 0 private var timeLeft: Int = 0
private var timeLeftString: String { private var timeLeftString: String {
return String(format: "%.2i:%.2i ", timeLeft / 60, timeLeft % 60) return String(format: "%.2i:%.2i ", timeLeft / 60, timeLeft % 60)
@ -62,13 +64,27 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
timer?.cancel() timer?.cancel()
timer = nil timer = nil
} }
@objc func startStopWork() { @objc func startStopWork() {
if !started {
started = true;
typeTime = .work typeTime = .work
startStopTimer() startStopTimer()
} else {
if timerPaused {
timerPaused = false;
resumeTimer();
} else {
timerPaused = true;
pauseTimer();
}
}
print("short")
} }
@objc func startStopRest() { @objc func startStopRest() {
print("looong")
started = false;
typeTime = .rest typeTime = .rest
startStopTimer() startStopTimer()
} }
@ -77,6 +93,16 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
timer == nil ? start() : reset() timer == nil ? start() : reset()
} }
func resumeTimer() {
guard let timervalue = timer else { return }
timervalue.resume();
}
func pauseTimer() {
guard let timervalue = timer else { return }
timervalue.suspend();
}
private func start() { private func start() {
timeLeft = Int(typeTime == .work ? workTime : restTime) timeLeft = Int(typeTime == .work ? workTime : restTime)
let queue: DispatchQueue = DispatchQueue(label: "Timer") let queue: DispatchQueue = DispatchQueue(label: "Timer")
@ -99,8 +125,13 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
private func reset() { private func reset() {
typeTime = .none typeTime = .none
timer?.cancel() timer?.cancel()
if timerPaused {
resumeTimer()
}
timer = nil timer = nil
title = defaultTitle title = defaultTitle
timerPaused = false
started = false
} }
private func tick() { private func tick() {
@ -121,4 +152,6 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
notification.soundName = "Submarine" notification.soundName = "Submarine"
NSUserNotificationCenter.default.deliver(notification) NSUserNotificationCenter.default.deliver(notification)
} }
} }