From 0976a3e970ca16d9cd1b68d2dcf4cd80776ca52c Mon Sep 17 00:00:00 2001 From: Jordan Baird Date: Sun, 26 May 2024 02:17:02 -0600 Subject: [PATCH] Rework popover storage --- .../Views/Cocoa/CWColorWell.swift | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Sources/ColorWellKit/Views/Cocoa/CWColorWell.swift b/Sources/ColorWellKit/Views/Cocoa/CWColorWell.swift index d2dee00..fe80154 100644 --- a/Sources/ColorWellKit/Views/Cocoa/CWColorWell.swift +++ b/Sources/ColorWellKit/Views/Cocoa/CWColorWell.swift @@ -10,8 +10,6 @@ public class CWColorWell: _CWColorWellBaseControl { // MARK: Static Properties - private static let popoverStorage = ObjectAssociation() - /// Hexadecimal strings used to construct the default colors shown /// in a color well's popover. private static let defaultHexStrings = [ @@ -33,6 +31,8 @@ public class CWColorWell: _CWColorWellBaseControl { private var isExclusive = true + private var popover: NSPopover? + /// The color well's delegate object. public weak var delegate: CWColorWellDelegate? @@ -255,7 +255,7 @@ public class CWColorWell: _CWColorWellBaseControl { /// - Returns: `true` on success, `false` otherwise. @discardableResult func makeAndShowPopover(relativeTo segment: CWColorWellSegment) -> Bool { - if Self.popoverStorage[self] != nil { + if popover != nil { // a popover is already being shown return false } @@ -264,19 +264,18 @@ public class CWColorWell: _CWColorWellBaseControl { } let popover = CWColorWellPopover(colorWell: self) - // the popover is removed from storage when it is closed; we use the - // presence of the popover to determine whether the next call to this - // method should succeed or fail - Self.popoverStorage[self] = popover + // the popover is set to `nil` when it is closed; we use its presence to + // determine whether the next call to this method should succeed or fail + self.popover = popover popover.show(relativeTo: segment.frame, of: segment, preferredEdge: .minY) return true } - /// Closes and removes the color well's popover from storage. + /// Closes and sets the color well's popover to `nil`. func freePopover() { - Self.popoverStorage[self]?.close() - Self.popoverStorage[self] = nil + popover?.close() + popover = nil } // MARK: Overridden Instance Methods