Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow typing seconds with keyboard. #14

Merged
merged 2 commits into from
Jun 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions Timer/MVClockView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MVClockView: NSControl {
private var minutesLabelSecondsSuffixWidth: CGFloat = 0.0
private var secondsLabel: NSTextView!
private var secondsSuffixWidth: CGFloat = 0.0
private var inputSeconds: Bool = false
private var timerTime: Date? {
didSet {
self.updateTimeLabel()
Expand Down Expand Up @@ -219,32 +220,47 @@ class MVClockView: NSControl {
}

override func keyUp(with theEvent: NSEvent) {
let char = theEvent.characters
if let number = Int(char ?? "") {
let newSeconds = floor(self.seconds / 60) * 600 + (self.seconds.truncatingRemainder(dividingBy: 60)) + CGFloat(number) * 60
let key = theEvent.keyCode
let currentSeconds = self.seconds.truncatingRemainder(dividingBy: 60)
let currentMinutes = floor(self.seconds / 60)
if let number = Int(theEvent.characters ?? "") {
var newSeconds:CGFloat
if self.inputSeconds {
if currentSeconds < 6 || currentMinutes == 0 {
newSeconds = currentMinutes * 60 + currentSeconds * 10 + CGFloat(number)
} else {
newSeconds = self.seconds
}
} else {
newSeconds = currentMinutes * 600 + currentSeconds + CGFloat(number) * 60
}
if (newSeconds < 999*60) {
self.paused = false
self.stop()
self.seconds = newSeconds
self.updateTimerTime()
}
} else if theEvent.keyCode == 53 {
} else if key == 47 || key == 65 {
// Period or Decimal
self.inputSeconds = !self.inputSeconds
} else if key == 53 {
// Escape
self.paused = false
self.stop()
self.seconds = 0
self.updateTimerTime()
} else if theEvent.keyCode == 51 {
self.inputSeconds = false
} else if key == 51 {
// Backspace
self.paused = false
self.stop()
if self.seconds <= 60 * 10 {
self.seconds = 0
if self.inputSeconds {
self.seconds = currentMinutes * 60 + floor(currentSeconds / 10)
} else {
self.seconds = floor(floor(self.seconds / 60) / 10) * 60 + (self.seconds.truncatingRemainder(dividingBy: 60))
self.seconds = floor(currentMinutes / 10) * 60 + currentSeconds
}
self.updateTimerTime()
} else if theEvent.keyCode == 36 || theEvent.keyCode == 49 {
} else if key == 36 || key == 49 {
// Enter or Space
self.handleClick();
}
Expand Down