React Native macOS 菜单栏应用程序是否可行?

Are React Native macOS menu bar apps possible?

目前有什么方法可以创建在 macOS 菜单栏中后台运行的 React Native 应用程序吗?

到目前为止,最好的方法是使用 Electron 平台。

这个问题不是最近出现的,但我在搜索这个主题时发现了它,并且有一个最近的相关更新:随着微软新桌面的努力,这现在是可行的,尽管仍然很棘手。

感谢 ospfranco 的出色工作。


唯一的方法是编辑您的 AppDelegate class 并使用来自 React Native 应用程序的根视图内容初始化状态按钮标签及其弹出窗口内容。也可以自定义其大小、外观和栏中的按钮,但仅限于 Swift 代码。

func applicationDidFinishLaunching(_ aNotification: Notification) {
    let jsCodeLocation: URL

    jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource:nil)
    let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "tempomat", initialProperties: nil, launchOptions: nil)
    let rootViewController = NSViewController()
    rootViewController.view = rootView

    popover = NSPopover()

    popover.contentSize = NSSize(width: 700, height: 800)
    popover.animates = true
    popover.behavior = .transient
    popover.contentViewController = rootViewController

    statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(60))

    if let button = self.statusBarItem.button {
        button.action = #selector(togglePopover(_:))
      button.title = "Tempomat"
    }

  }

参考文献: