From a6c31feeeeca7aceb3aaadaf04cd490a5c600fec Mon Sep 17 00:00:00 2001 From: Ivan Sapozhnik Date: Tue, 31 Dec 2024 22:57:39 +0100 Subject: [PATCH 1/2] Fixing the leak --- Loop/Utilities/EventMonitor.swift | 34 ++++++++++++++++--------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Loop/Utilities/EventMonitor.swift b/Loop/Utilities/EventMonitor.swift index 9010b5a9..ad1e5043 100644 --- a/Loop/Utilities/EventMonitor.swift +++ b/Loop/Utilities/EventMonitor.swift @@ -14,12 +14,13 @@ protocol EventMonitor { } class NSEventMonitor: EventMonitor, Identifiable, Equatable { - private weak var localEventMonitor: AnyObject? - private weak var globalEventMonitor: AnyObject? + private var localEventMonitor: Any? + private var globalEventMonitor: Any? - private var scope: NSEventMonitor.Scope - private var eventTypeMask: NSEvent.EventTypeMask - private var eventHandler: (NSEvent) -> (NSEvent?) + private let scope: NSEventMonitor.Scope + private let eventTypeMask: NSEvent.EventTypeMask + private let eventHandler: (NSEvent) -> (NSEvent?) + var isEnabled: Bool = false enum Scope { @@ -42,35 +43,36 @@ class NSEventMonitor: EventMonitor, Identifiable, Equatable { if scope == .local || scope == .all { localEventMonitor = NSEvent.addLocalMonitorForEvents( matching: eventTypeMask, - handler: eventHandler - ) as AnyObject - - isEnabled = true + handler: { [weak self] event in + self?.eventHandler(event) + } + ) } if scope == .global || scope == .all { globalEventMonitor = NSEvent.addGlobalMonitorForEvents( matching: eventTypeMask, - handler: { _ = self.eventHandler($0) } - ) as AnyObject - - isEnabled = true + handler: { [weak self] event in + _ = self?.eventHandler(event) + } + ) } + + isEnabled = true } func stop() { if let localEventMonitor { NSEvent.removeMonitor(localEventMonitor) - isEnabled = false } if let globalEventMonitor { NSEvent.removeMonitor(globalEventMonitor) - isEnabled = false } + isEnabled = false } - var id = UUID() + let id = UUID() static func == (lhs: NSEventMonitor, rhs: NSEventMonitor) -> Bool { lhs.id == rhs.id } From 11e24b1765f85474d7558540c821eccee991c174 Mon Sep 17 00:00:00 2001 From: Kai Date: Tue, 31 Dec 2024 16:17:17 -0700 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20Format=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Loop/Utilities/EventMonitor.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Loop/Utilities/EventMonitor.swift b/Loop/Utilities/EventMonitor.swift index ad1e5043..69e4ed16 100644 --- a/Loop/Utilities/EventMonitor.swift +++ b/Loop/Utilities/EventMonitor.swift @@ -20,7 +20,7 @@ class NSEventMonitor: EventMonitor, Identifiable, Equatable { private let scope: NSEventMonitor.Scope private let eventTypeMask: NSEvent.EventTypeMask private let eventHandler: (NSEvent) -> (NSEvent?) - + var isEnabled: Bool = false enum Scope { @@ -57,7 +57,7 @@ class NSEventMonitor: EventMonitor, Identifiable, Equatable { } ) } - + isEnabled = true }