I have a ruler in SwiftUI that looks like this:
struct RulerScrollView<Content: View>: NSViewRepresentable {
@ObservedObject var dm: DrawModel
let content: Content
init(_ dm: DrawModel, @ViewBuilder content: () -> Content ) {
self.dm = dm
self.content = content()
}
override func mouseDown(with event: NSEvent) {
if event.clickCount == 2 {
NotificationCenter.default.post(name: Notification.Name("ShowRulerSettings"), object: nil)
}
}
func makeNSView(context: Context) -> NSScrollView {
let scrollView = NSScrollView()
scrollView.hasVerticalRuler = true
scrollView.hasHorizontalRuler = true
scrollView.rulersVisible = dm.showRulers
let hostingView = NSHostingView(rootView: content)
hostingView.wantsLayer = true
hostingView.layer?.backgroundColor = NSColor.white.cgColor
scrollView.documentView = hostingView
return scrollView
}
func updateNSView(_ nsView: NSScrollView, context: Context) {
nsView.rulersVisible = dm.showRulers
}
}
Which is called like this:
Stack {
RulerScrollView( dm ) {
DrawingView( dm )
.onReceive(dm.$showRulerSettings) { isShowingRulerSettings = $0 }
}
.background(Color.white)
}
.alert(isPresented: $isShowingRulerSettings) {
Alert(title: Text("Ruler Settings"), message: Text("Settings here"), primaryButton: .default(Text("OK")), secondaryButton: .cancel()) }
Unfortunately I get this error:
'override' can only be specified on class members
I added this to my RulerView:
I added this to makeNSView:
Then I called it like this:
I find NS a bit painful and I'm hoping SwiftUI continues to grow. :) The popover is not showing up in the right place (on the ruler). It's on the content window. One step at a time :)