# عند النقر على زر في NSWindow غير النشط في SwiftUI، يتم تمكين الزر من معالجة حدث الماوس بدلاً من النافذة افتراضيًا

تعيد NSView الطريقة acceptsFirstMouse (opens new window) وتقوم بإرجاع true دائمًا. يضاف هذا الإعادة تحميل View إلى overlay في مشاهد SwiftUI.

import SwiftUI
import Cocoa

// Just mouse accepter
class MyViewView : NSView {
    override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
        return true
    }
}

// Representable wrapper (bridge to SwiftUI)
struct AcceptingFirstMouse : NSViewRepresentable {
    func makeNSView(context: NSViewRepresentableContext<AcceptingFirstMouse>) -> MyViewView {
        return MyViewView()
    }

    func updateNSView(_ nsView: MyViewView, context: NSViewRepresentableContext<AcceptingFirstMouse>) {
        nsView.setNeedsDisplay(nsView.bounds)
    }

    typealias NSViewType = MyViewView
}

// الاستخدام (في أي مكان في تراكيب SwiftUI الخاصة بك)
Text("انقر هنا")
  .padding(20)
  .background(Color.yellow)
  .overlay(AcceptingFirstMouse()) // يجب أن يكون في الأعلى (ليس هناك ارتباك، إنه شفاف)
  .onTapGesture {
      print("تم النقر على العلامة")
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

المؤلف: Sintone Li

عنوان المقال: https://cleanclip.cc/ar/developer/swiftui-nswindow-inactive-firstmouse/

تم التحديث لآخر مرة: ٣٠‏/٦‏/٢٠٢٤, ١٠:٥٠:٤٥ ص