I created a Status Bar app using the SwiftUI demo project Dad Jokes. I am targeting macOS 10.15 so I had to modify the entry point of my application. I created a SwiftUI view that has a button. The issue that I have is that sometimes the button hangs where it stop responding. Sometimes I have to keep pressing the button before it start responding to button taps. This happens when the button is connected to a function or a simple print to the console. Its seem to only happen when I use .buttonStyle(PlainButtonStyle())
on my button. Below is a sample of my application
AppDelegate.swift
import Cocoa
import SwiftUI
import RevenueCat
class AppDelegate: NSObject, NSApplicationDelegate {
static private(set) var instance: AppDelegate!
let statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
private var window: NSWindow!
private let menu = ApplicationMenu()
func applicationDidFinishLaunching(_ aNotification: Notification) {
AppDelegate.instance = self
DispatchQueue.global().async {
// Initialize RevenueCat (Purchases)
Purchases.logLevel = .debug
Purchases.configure(withAPIKey: Constants.apiKey)
}
// Setup app to run in the status bar
statusBarItem.button?.image = NSImage(named: NSImage.Name("ELMC"))
statusBarItem.button?.imagePosition = .imageLeading
statusBarItem.button?.image?.isTemplate = true
statusBarItem.menu = menu.createMenu()
}
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}
}
ApplicationMenu.swift
import Foundation
import SwiftUI
class ApplicationMenu: NSObject {
static let shared = ApplicationMenu()
let menu = NSMenu()
var settingsWindow: NSWindow!
func createMenu() -> NSMenu {
let mainView = MainView()
let topView = NSHostingController(rootView: mainView)
topView.view.frame.size = CGSize(width: 250, height: 180)
let customMenuItem = NSMenuItem()
customMenuItem.view = topView.view
menu.addItem(customMenuItem)
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem.separator())
let aboutMenuItem = NSMenuItem(title: "About My Remote Bridge",
action: #selector(about),
keyEquivalent: "")
aboutMenuItem.target = self
menu.addItem(aboutMenuItem)
let webLinkMenuItem = NSMenuItem(title: "Contact Support",
action: #selector(openLink),
keyEquivalent: "")
webLinkMenuItem.target = self
webLinkMenuItem.representedObject = "https://codewithchris.com"
menu.addItem(webLinkMenuItem)
let quitMenuItem = NSMenuItem(title: "Quit",
action: #selector(quit),
keyEquivalent: "q")
quitMenuItem.target = self
menu.addItem(quitMenuItem)
return menu
}
@objc func about(sender: NSMenuItem) {
NSApp.orderFrontStandardAboutPanel()
}
@objc func openLink(sender: NSMenuItem) {
guard let link = sender.representedObject as? String,
let url = URL(string: link) else {
return
}
NSWorkspace.shared.open(url)
}
@objc func quit(sender: NSMenuItem) {
NSApp.terminate(self)
}
}
MainView.swift
import SwiftUI
struct MainView: View {
var body: some View {
VStack() {
Spacer()
Button(action: {
print("Button tapped")
}) {
Text("Get New Connection Pin")
.frame(maxWidth: .infinity)
.frame(height: 40)
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(4)
}
.buttonStyle(PlainButtonStyle())
Spacer()
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
.frame(width: 225, height: 180)
.background(Color.white.opacity(0.1))
}
}
If I remove .buttonStyle(PlainButtonStyle())
Everything works as expected but when I add it then that’s when I have the issue with freezing or hanging or the button stop responding. The application menu items work its just the Swiftui button stops. The weird thing is that I can add other elements that have user interaction and they will keep working with out issue. Its just something with the button press
I attached a video to show issue: