Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4773 from toggl-open-source/fix/apply-start-time-…
Browse files Browse the repository at this point in the history
…when-close

Update time when duration dropdown is closed (mac)
  • Loading branch information
skel35 authored Dec 22, 2020
2 parents 307a438 + 96a6b9c commit 0d4f5ae
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class TimeEditView: NSView {
calendarView.superview != nil && !calendarView.isHidden
}

private var notificationObserverToken: Any?
private var isEscTriggeredClose = false

private enum Constants {
static let dayNameAttribute: [NSAttributedString.Key: Any] = {
return [NSAttributedString.Key.font: NSFont.systemFont(ofSize: 14),
Expand Down Expand Up @@ -116,6 +119,19 @@ class TimeEditView: NSView {
self.hideWindow()
}
}

notificationObserverToken = NotificationCenter.default.addObserver(
forName: NSWindow.didResignKeyNotification,
object: window,
queue: .main
) { [unowned self] notification in
let isCurrentWindow = (notification.object as? NSWindow) == self.window
if isCurrentWindow && !self.isEscTriggeredClose {
self.onStartTextChange?(self.startTextField.stringValue)
}
self.isEscTriggeredClose = false
self.isStartFieldChanged = false
}
}

override func becomeFirstResponder() -> Bool {
Expand All @@ -124,6 +140,7 @@ class TimeEditView: NSView {

override func keyDown(with event: NSEvent) {
if let key = Key(rawValue: Int(event.keyCode)), key == .escape {
isEscTriggeredClose = true
hideWindow()
return
}
Expand Down Expand Up @@ -220,10 +237,13 @@ extension TimeEditView: NSTextFieldDelegate {
}

func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
if commandSelector == #selector(cancelOperation(_:))
|| commandSelector == #selector(insertNewline(_:))
if commandSelector == #selector(insertNewline(_:))
|| commandSelector == #selector(insertBacktab(_:)) {
hideWindow()
}

if commandSelector == #selector(cancelOperation(_:)) {
isEscTriggeredClose = true
hideWindow()
}

Expand Down

0 comments on commit 0d4f5ae

Please sign in to comment.