From 96a6b9ce9f3574611d323164b3b15f5ead2fd5d9 Mon Sep 17 00:00:00 2001 From: Andrii Nester Date: Fri, 18 Dec 2020 16:53:20 +0200 Subject: [PATCH] Update time when duration dropdown is closed (mac) --- .../Views/TimeEditView.swift | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/Views/TimeEditView.swift b/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/Views/TimeEditView.swift index ef8f62ecd4..9aae47b75f 100644 --- a/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/Views/TimeEditView.swift +++ b/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/Views/TimeEditView.swift @@ -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), @@ -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 { @@ -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 } @@ -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() }