Skip to content

Commit

Permalink
Enable showing the popover without taking application focus and keepi…
Browse files Browse the repository at this point in the history
…ng the popover visible without disabling right click
  • Loading branch information
ibash committed May 13, 2021
1 parent b305669 commit dee3296
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
20 changes: 11 additions & 9 deletions Sources/Popover/Popover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ public class Popover: NSObject {
return popoverWindowController?.window
}

public var keepPopoverVisible: Bool = false {
didSet {
keepPopoverVisible ? removeMonitors() : setupMonitors()
}
}
public var keepPopoverVisible: Bool = false

@objc dynamic var item: NSStatusItem! {
didSet {
Expand Down Expand Up @@ -90,10 +86,14 @@ public class Popover: NSObject {
localEventMonitor?.start()
}

/// Shows the Popover with no animation
public func show() {
self.show(withFocus: true)
}

/// Shows the Popover with no animation
public func show(withFocus: Bool) {
guard !isPopoverWindowVisible else { return }
popoverWindowController?.show()
popoverWindowController?.show(withFocus: withFocus)
globalEventMonitor?.start()

guard let button = item.button else { return }
Expand All @@ -117,7 +117,9 @@ public class Popover: NSObject {
private func setupMonitors() {
globalEventMonitor = EventMonitor(monitorType: .global, mask: [.leftMouseDown, .rightMouseDown], globalHandler: { [weak self] _ in
guard let self = self else { return }
self.dismiss()
if (!self.keepPopoverVisible) {
self.dismiss()
}
}, localHandler: nil)

if menuItems != nil, menuItems?.isNotEmpty ?? false {
Expand Down Expand Up @@ -167,7 +169,7 @@ public class Popover: NSObject {
}

@objc private func handleStatusItemButtonAction(_ sender: Any?) {
isPopoverWindowVisible ? dismiss() : show()
isPopoverWindowVisible ? dismiss() : show(withFocus: true)
}

private func setTargetAction(for button: NSButton) {
Expand Down
10 changes: 7 additions & 3 deletions Sources/Popover/PopoverWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ class PopoverWindowController: NSWindowController, NSWindowDelegate {
fatalError("init(coder:) has not been implemented")
}

func show() {
func show(withFocus: Bool) {
guard !isAnimating else { return }

updateWindowFrame()

showWindow(nil)
if (withFocus) {
showWindow(nil)
window?.makeKey()
} else {
window?.orderFrontRegardless()
}
windowIsOpen = true
window?.makeKey()
// TODO: animation
}

Expand Down

0 comments on commit dee3296

Please sign in to comment.