目前有什么方法可以创建一个在 macOS 菜单栏中后台运行的 React Native 应用程序吗?
这个问题不是最近提出的,但我发现它在搜索这个主题,并且最近有一个相关更新:随着微软新的桌面工作,这现在是可行的,尽管仍然很棘手。
感谢ospfranco的出色工作。
唯一的方法是编辑
AppDelegate
类,并使用 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"
}
}
参考资料:
到目前为止,最好的方法是使用 Electron 平台。
是的!使用 React Native 构建的开源 macOS 菜单栏应用程序的一个很好的例子是 Expo Orbit:https://github.com/expo/orbit