是React本机MacOS菜单栏应用程序



当前有什么方法可以创建一个在MacOS菜单栏中在后台运行的React Native应用程序?

这个问题不是最新的,但是我发现它正在搜索此主题,并且有一个最新的相关更新:在Microsoft的新桌面努力下,这现在是可行的,尽管仍然很棘手。

学分归功于Ospfranco的出色工作。


这样做的唯一方法是编辑您的AppDelegate类,并用React Native应用程序中的root View内容初始化状态按钮标签及其弹出内容。也可以自定义其尺寸,外观和栏上的按钮,但仅在Swift Code中。

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"
    }
  }

参考:

  • REECT Native MacOS(Microsoft(:https://github.com/microsoft/reaect-native-macos

  • 示例代码:https://github.com/oppfranco/reaeact-native-macos-macos-menubar-template

  • 博客文章:https://oppfranco.github.io/post/2020/05/05/23/how-to-make-aec-make-a-react-native-native-native-menu-bar-app-for-mac-mac-mac-os-mac-os/

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

最新更新