Просмотр исходного кода

Dismiss AI Companion notes sheet on outside clicks.

Start and clean up a local click monitor around the notes sheet lifecycle so clicks elsewhere in the app close the popup consistently.

Co-authored-by: Cursor <cursoragent@cursor.com>
huzaifahayat12 1 месяц назад
Родитель
Сommit
35a10cd51a
1 измененных файлов с 27 добавлено и 0 удалено
  1. 27 0
      meetings_app/ViewController.swift

+ 27 - 0
meetings_app/ViewController.swift

@@ -341,6 +341,7 @@ final class ViewController: NSViewController {
341
     private weak var aiCompanionNotesTextView: NSTextView?
341
     private weak var aiCompanionNotesTextView: NSTextView?
342
     private weak var aiCompanionNotesCopyButton: NSButton?
342
     private weak var aiCompanionNotesCopyButton: NSButton?
343
     private weak var aiCompanionNotesCloseButton: NSButton?
343
     private weak var aiCompanionNotesCloseButton: NSButton?
344
+    private var aiCompanionNotesOutsideClickMonitor: Any?
344
     private var aiCompanionNotesTaskByMeetingId = [String: Task<Void, Never>]()
345
     private var aiCompanionNotesTaskByMeetingId = [String: Task<Void, Never>]()
345
     private var aiCompanionAudioPlayer: AVPlayer?
346
     private var aiCompanionAudioPlayer: AVPlayer?
346
     private var aiCompanionLocalAudioPlayer: AVAudioPlayer?
347
     private var aiCompanionLocalAudioPlayer: AVAudioPlayer?
@@ -3544,6 +3545,7 @@ private extension ViewController {
3544
             if hostWindow.attachedSheet !== panel {
3545
             if hostWindow.attachedSheet !== panel {
3545
                 hostWindow.beginSheet(panel, completionHandler: nil)
3546
                 hostWindow.beginSheet(panel, completionHandler: nil)
3546
             }
3547
             }
3548
+            aiCompanionStartNotesOutsideClickMonitor()
3547
             panel.makeKeyAndOrderFront(nil)
3549
             panel.makeKeyAndOrderFront(nil)
3548
             NSApp.activate(ignoringOtherApps: true)
3550
             NSApp.activate(ignoringOtherApps: true)
3549
             return
3551
             return
@@ -3647,6 +3649,7 @@ private extension ViewController {
3647
 
3649
 
3648
         panel.contentView = root
3650
         panel.contentView = root
3649
         hostWindow.beginSheet(panel, completionHandler: nil)
3651
         hostWindow.beginSheet(panel, completionHandler: nil)
3652
+        aiCompanionStartNotesOutsideClickMonitor()
3650
         panel.makeKeyAndOrderFront(nil)
3653
         panel.makeKeyAndOrderFront(nil)
3651
         NSApp.activate(ignoringOtherApps: true)
3654
         NSApp.activate(ignoringOtherApps: true)
3652
 
3655
 
@@ -3671,7 +3674,30 @@ private extension ViewController {
3671
     }
3674
     }
3672
 
3675
 
3673
     @objc private func aiCompanionCloseNotesTapped(_ sender: NSButton) {
3676
     @objc private func aiCompanionCloseNotesTapped(_ sender: NSButton) {
3677
+        aiCompanionCloseNotesPanel()
3678
+    }
3679
+
3680
+    private func aiCompanionStartNotesOutsideClickMonitor() {
3681
+        guard aiCompanionNotesOutsideClickMonitor == nil else { return }
3682
+        aiCompanionNotesOutsideClickMonitor = NSEvent.addLocalMonitorForEvents(
3683
+            matching: [.leftMouseDown, .rightMouseDown, .otherMouseDown]
3684
+        ) { [weak self] event in
3685
+            guard let self, let panel = self.aiCompanionNotesPanel else { return event }
3686
+            if event.window === panel { return event }
3687
+            self.aiCompanionCloseNotesPanel()
3688
+            return event
3689
+        }
3690
+    }
3691
+
3692
+    private func aiCompanionStopNotesOutsideClickMonitor() {
3693
+        guard let monitor = aiCompanionNotesOutsideClickMonitor else { return }
3694
+        NSEvent.removeMonitor(monitor)
3695
+        aiCompanionNotesOutsideClickMonitor = nil
3696
+    }
3697
+
3698
+    private func aiCompanionCloseNotesPanel() {
3674
         guard let panel = aiCompanionNotesPanel else { return }
3699
         guard let panel = aiCompanionNotesPanel else { return }
3700
+        aiCompanionStopNotesOutsideClickMonitor()
3675
         if let hostWindow = view.window, hostWindow.attachedSheet === panel {
3701
         if let hostWindow = view.window, hostWindow.attachedSheet === panel {
3676
             hostWindow.endSheet(panel)
3702
             hostWindow.endSheet(panel)
3677
         }
3703
         }
@@ -6616,6 +6642,7 @@ extension ViewController: NSWindowDelegate {
6616
     func windowWillClose(_ notification: Notification) {
6642
     func windowWillClose(_ notification: Notification) {
6617
         guard let closingWindow = notification.object as? NSWindow else { return }
6643
         guard let closingWindow = notification.object as? NSWindow else { return }
6618
         if closingWindow === aiCompanionNotesPanel {
6644
         if closingWindow === aiCompanionNotesPanel {
6645
+            aiCompanionStopNotesOutsideClickMonitor()
6619
             aiCompanionNotesPanel = nil
6646
             aiCompanionNotesPanel = nil
6620
             aiCompanionNotesRootView = nil
6647
             aiCompanionNotesRootView = nil
6621
             aiCompanionNotesHeaderView = nil
6648
             aiCompanionNotesHeaderView = nil