Show NSMenu only on NSStatusBarButton right click?

前端 未结 4 1100
一向
一向 2020-12-18 01:23

I have the following code: (can be copy-pasted to New macOS project)

import Cocoa
import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSAppl         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-18 01:46

    Here is possible approach. There might be more accurate calculations for menu position, including taking into account possible differences of userInterfaceLayoutDirection, but the idea remains the same - take possible events under manual control and make own decision about what to do on each event.

    Important places commented in code. (Tested on Xcode 11.2, macOS 10.15)

    @NSApplicationMain
    class AppDelegate: NSObject, NSApplicationDelegate {
        var statusBarItem: NSStatusItem!
    
        func applicationDidFinishLaunching(_ aNotification: Notification) {
    
            let statusBar = NSStatusBar.system
            statusBarItem = statusBar.statusItem(
                withLength: NSStatusItem.squareLength)
            statusBarItem.button?.title = "

提交回复
热议问题