1 Commits
Author SHA1 Message Date
Ivan Sapozhnik 14cfd70594 Simple Event Monitor 2020-04-03 18:09:42 +02:00
3 changed files with 88 additions and 36 deletions
+34
View File
@@ -0,0 +1,34 @@
//
// File.swift
//
//
// Created by Ivan Sapozhnik on 03.04.20.
//
import Cocoa
class EventMonitor {
private var monitor: Any?
private let mask: NSEvent.EventTypeMask
private let handler: (NSEvent?) -> Void
init(mask: NSEvent.EventTypeMask, handler: @escaping (NSEvent?) -> Void) {
self.mask = mask
self.handler = handler
}
deinit {
stop()
}
func start() {
monitor = NSEvent.addGlobalMonitorForEvents(matching: mask, handler: handler)
}
func stop() {
guard let monitor = self.monitor else { return }
NSEvent.removeMonitor(monitor)
self.monitor = nil
}
}
+19 -36
View File
@@ -9,38 +9,12 @@
import Cocoa
public class Popover: NSObject {
private class StatusItemContainerView: NSView {
enum Constants {
static let maxContainerSize: CGFloat = 22.0
static let maxContentHeight: CGFloat = 18.0
}
func embed(_ view: NSView) {
addSubview(view)
let selfFrame = NSRect(
x: 0,
y: 0,
width: max(NSWidth(view.bounds), Constants.maxContainerSize),
height: Constants.maxContainerSize
)
let contentFrame = NSRect(
x: 0,
y: (Constants.maxContainerSize-Constants.maxContentHeight) / 2,
width: NSWidth(view.bounds),
height: Constants.maxContentHeight
)
frame = selfFrame
view.frame = contentFrame
}
}
public var item: NSStatusItem!
private var popoverWindowController: PopoverWindowController?
private let windowConfiguration: PopoverConfiguration
private var popoverWindowController: PopoverWindowController?
private var statusItemContainer: StatusItemContainerView?
private var eventMonitor: EventMonitor?
private var isPopoverWindowVisible: Bool {
return (popoverWindowController != nil) ? popoverWindowController!.windowIsOpen : false
@@ -48,6 +22,13 @@ public class Popover: NSObject {
public init(with configuration: PopoverConfiguration) {
windowConfiguration = configuration
super.init()
eventMonitor = EventMonitor(mask: [.rightMouseDown, .leftMouseDown], handler: { [weak self] event in
guard let self = self else { return }
self.dismissPopoverWindow()
})
}
public func presentPopover(with image: NSImage, contentViewController viewController: NSViewController) {
@@ -60,6 +41,16 @@ public class Popover: NSObject {
popoverWindowController = PopoverWindowController(with: self, contentViewController: viewController, windowConfiguration: windowConfiguration)
}
public func showPopoverWindow() {
popoverWindowController?.show()
eventMonitor?.start()
}
public func dismissPopoverWindow() {
popoverWindowController?.dismiss()
eventMonitor?.stop()
}
private func configureStatusBarButton(with image: NSImage) {
item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
guard let button = item.button else { return }
@@ -93,12 +84,4 @@ public class Popover: NSObject {
showPopoverWindow()
}
}
public func dismissPopoverWindow() {
popoverWindowController?.dismiss()
}
public func showPopoverWindow() {
popoverWindowController?.show()
}
}
@@ -0,0 +1,35 @@
//
// File.swift
//
//
// Created by Ivan Sapozhnik on 03.04.20.
//
import Cocoa
class StatusItemContainerView: NSView {
enum Constants {
static let maxContainerSize: CGFloat = 22.0
static let maxContentHeight: CGFloat = 18.0
}
func embed(_ view: NSView) {
addSubview(view)
let selfFrame = NSRect(
x: 0,
y: 0,
width: max(NSWidth(view.bounds), Constants.maxContainerSize),
height: Constants.maxContainerSize
)
let contentFrame = NSRect(
x: 0,
y: (Constants.maxContainerSize-Constants.maxContentHeight) / 2,
width: NSWidth(view.bounds),
height: Constants.maxContentHeight
)
frame = selfFrame
view.frame = contentFrame
}
}