Skip to content

Commit

Permalink
Fixed issue #3 where cancel not called when user clicks outside the w…
Browse files Browse the repository at this point in the history
…indow

See: [#3](#3)
  • Loading branch information
dagronf committed Dec 11, 2022
1 parent b5ad7ae commit 5666bef
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
18 changes: 16 additions & 2 deletions Sources/DSFQuickActionBar/DSFQuickActionBar.swift
Expand Up @@ -147,8 +147,22 @@ public extension DSFQuickActionBar {
quickBarWindow.placeholderText = placeholderText ?? ""

quickBarWindow.didDetectClose = { [weak self] in
self?.quickBarController = nil
self?.onCloseCallback?()
guard
let self = self,
let window = self.quickActionBarWindow
else {
return
}

// If the user hasn't activated an item, call the didCancel() delegate if it is present
if window.userDidActivateItem == false {
self.contentSource?.quickActionBarDidCancel(self)
}

self.quickBarController = nil

// Call the close callback
self.onCloseCallback?()
}

// Make sure that the application is frontmost or else the quick action bar won't display (it cannot be
Expand Down
Expand Up @@ -33,6 +33,11 @@ extension DSFQuickActionBar {
private let tableView = DSFQuickActionBar.ResultsTableView()
private let horizontalView = NSBox()

// The parent window
var quickActionBarWindow: DSFQuickActionBar.Window? {
self.window as? DSFQuickActionBar.Window
}

// The parent
var quickActionBar: DSFQuickActionBar!

Expand Down Expand Up @@ -284,6 +289,10 @@ extension DSFQuickActionBar.ResultsView {
return false
}

// Mark that the user activated an item in the list
self.quickActionBarWindow?.userDidActivateItem = true

// Tell the delegate that the user selected an item
self.quickActionBar.contentSource?.quickActionBar(self.quickActionBar, didActivateItem: which.key)

// Close the bar
Expand All @@ -307,10 +316,13 @@ extension DSFQuickActionBar.ResultsView {
return
}

// Mark that the user has activated an item in the list
self.quickActionBarWindow?.userDidActivateItem = true

let itemIdentifier = self.identifiers[selectedRow]
self.quickActionBar.contentSource?.quickActionBar(self.quickActionBar, didActivateItem: itemIdentifier)

// If the row is double-clicked, close the bar
// Make the bar lose focus, which will trigger it to close.
self.window?.resignMain()
}

Expand Down
Expand Up @@ -154,6 +154,9 @@ extension DSFQuickActionBar {

return r
}()

// Is set to true when the user 'activates' an item in the result list
internal var userDidActivateItem: Bool = false
}
}

Expand Down Expand Up @@ -227,7 +230,7 @@ internal extension DSFQuickActionBar.Window {
extension DSFQuickActionBar.Window {
// Called when the user presses 'escape' when the window is present
override func cancelOperation(_: Any?) {
self.quickActionBar.contentSource?.quickActionBarDidCancel(self.quickActionBar)
// Tell the window to lose its initial responder status, which will close it.
self.resignMain()
}

Expand Down

0 comments on commit 5666bef

Please sign in to comment.