1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- //
- // NSWindow+PopOver.swift
- // PDF Master
- //
- // Created by kdanmobile on 2023/10/26.
- //
- import Foundation
- let KMWindowPopOverKey = "KMWindowPopOverKey"
- let KMWindowPopOverSourcesRectKey = "KMWindowPopOverSourcesRectKey"
- extension NSWindow {
- var popover: NSPopover? {
- get {
- return objc_getAssociatedObject(self,KMWindowPopOverKey) as? NSPopover
- }
- set {
- objc_setAssociatedObject(self, KMWindowPopOverKey, newValue, .OBJC_ASSOCIATION_RETAIN)
- }
- }
-
- var sourcesRect: CGRect? {
- get {
- guard let value = objc_getAssociatedObject(self, KMWindowPopOverSourcesRectKey) as? NSValue else {
- return nil
- }
- return value.rectValue
- }
- set {
- if let newValue = newValue {
- let value = NSValue(rect: newValue)
- objc_setAssociatedObject(self, KMWindowPopOverSourcesRectKey, value, .OBJC_ASSOCIATION_RETAIN)
- } else {
- objc_setAssociatedObject(self, KMWindowPopOverSourcesRectKey, nil, .OBJC_ASSOCIATION_RETAIN)
- }
- }
- }
-
- open override func mouseMoved(with event: NSEvent) {
- super.mouseMoved(with: event)
- let point = event.locationInWindow
- if NSStringFromClass(event.window!.classForCoder).elementsEqual("_NSPopoverWindow") == false {
- if !sourcesRect!.contains(point) {
- if (self.popover != nil) {
- NotificationCenter.default.post(name: NSNotification.Name("KMPopOverClosedByWindowNotification"), object: self.popover)
- popover!.close()
- self.popover = nil
- self.sourcesRect = CGRect.null
- }
- }
- }
- }
- }
|