Skip to content

Commit f6c9e54

Browse files
committed
clean monitor onDisappear
1 parent 2701e6b commit f6c9e54

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

CodeEdit/Features/Editor/TabBar/Tabs/Tab/EditorTabView.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ struct EditorTabView: View {
4545
///
4646
/// By default, this value is `false`. When the root view is appeared, it turns `true`.
4747
@State private var isAppeared: Bool = false
48+
49+
@State private var keyMonitor: Any?
4850

4951
/// The id associating with the tab that is currently being dragged.
5052
///
@@ -194,7 +196,7 @@ struct EditorTabView: View {
194196
}
195197
}
196198
.onAppear {
197-
NSEvent.addLocalMonitorForEvents(matching: .otherMouseDown) { event in
199+
keyMonitor = NSEvent.addLocalMonitorForEvents(matching: .otherMouseDown) { event in
198200
if self.isHovering && event.type == .otherMouseDown && event.buttonNumber == 2 {
199201
DispatchQueue.main.async {
200202
editor.closeTab(file: tabFile)
@@ -203,6 +205,12 @@ struct EditorTabView: View {
203205
return event
204206
}
205207
}
208+
.onDisappear {
209+
if let keyMonitor = keyMonitor {
210+
NSEvent.removeMonitor(keyMonitor)
211+
self.keyMonitor = nil
212+
}
213+
}
206214
}
207215

208216
var body: some View {

0 commit comments

Comments
 (0)