# SwiftUI Kliknutie na tlačidlo na neaktívnom NSWindow štruktúre predbežne spracuje udalosti myši tlačidla namiesto okna
NSView predefinuje metódu acceptsFirstMouse (opens new window), ktorá vždy vráti true
.
V štruktúre SwiftUI je táto predefinovaná View pridaná cez overlay.
import SwiftUI
import Cocoa
// Nahlásič myši
class MyViewView : NSView {
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
return true
}
}
// Obal reprezentovaný (most k 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
}
// Použitie (niekde vo vašej štruktúre SwiftUI zásobníka)
Text("Klikni na mňa")
.padding(20)
.background(Color.yellow)
.overlay(AcceptingFirstMouse()) // musí byť navrchu (žiadne zavádzanie, je transparentné)
.onTapGesture {
print("Kliknutie na štítok")
}
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
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
Autor: Sintone Li
Link na článok:https://cleanclip.cc/sk/developer/swiftui-nswindow-inactive-firstmouse/