ios – SwiftUI Contentview click on are taking a number of makes an attempt to occur


Iam engaged on MacOS Utility, My whole code is written in Swift however for including MenuItem I most popular to make use of SwiftUI ContentView. To do this I’ve written following code.

class StatusBarController {
    
    var statusBar: NSStatusBar!
    var statusItem: NSStatusItem!
    var nsMenu = NSMenu()
    
    init(delegate: AppDelegate) {
        
        statusBar = NSStatusBar()
        statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)

        statusItem.button?.picture = NSImage(systemSymbolName: "star.fill", accessibilityDescription: "Standing bar icon")

        let menuItem = NSMenuItem()
        let controller = NSHostingController(rootView: ContentView())
        
        controller.view.body.dimension = CGSize(width: 250, peak: 320)
        menuItem.view = controller.view
        
        
        nsMenu.addItem(menuItem)
        statusItem.menu = nsMenu
    }
}

That is working superb Im capable of current ContentView efficiently however in ContentView there may be click on motion added on label. However it’s taking a number of makes an attempt(3-4) to simply do single click on. I did lot of analysis it appears difficulty of home windows hierarchy. Could also be Contentview window is just not in entrance and therefore click on is just not passing via correctly.What ought to I do right here in order that click on will occur instantly in single click on.

That is my content material view:


struct ContentView: View {

    var physique: some View {

        VStack(alignment: .main, spacing: 0) {
           
         
            HStack() {
                Textual content("Give up")
                        .font(.title3)
                        .fontWeight(.medium)
                        .foregroundColor(.black)
                        .padding(.main, 10)
                        .padding(.prime, 10)
                Spacer()
            }
            .contentShape(Rectangle()).onTapGesture {
              
                //That is the motion Im speaking about
                NSApp.terminate(self)
            }
        }
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles