Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mac Catalyst with menu-bar app in NSPopover #11

Open
tommycarpi opened this issue Jan 29, 2021 · 0 comments
Open

Mac Catalyst with menu-bar app in NSPopover #11

tommycarpi opened this issue Jan 29, 2021 · 0 comments

Comments

@tommycarpi
Copy link

Hi guys, I'm sorry if I'm flooding the issues here but at least I hope these may serve as reference for the ones to come 😆

Again with Mac Catalyst and React Native, I was trying to make a Menu-Bar app with Dynamic.NSPopover and Dynamic.NSVireController but it crashes badly:

Screen Shot 2021-01-29 at 20 21 27

Here is my AppDelegate


import UIKit
#if targetEnvironment(macCatalyst)
import Dynamic
import Cocoa
#endif

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  var bridge: RCTBridge!
  
  #if targetEnvironment(macCatalyst)
  let statusItem = Dynamic.NSStatusBar.systemStatusBar.statusItemWithLength(-1.0)
  let popover = Dynamic.NSPopover()
  #endif
  
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let jsCodeLocation: URL
    
    jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource:nil)
    let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "myApp", initialProperties: nil, launchOptions: launchOptions)

    // Code to make it work outside NSPopover
    // ################
    // let rootViewController = UIViewController()
    // rootViewController.view = rootView

    // self.window = UIWindow(frame: UIScreen.main.bounds)
    // self.window?.rootViewController = rootViewController
    // self.window?.makeKeyAndVisible()
    // ################
        
    #if targetEnvironment(macCatalyst)

    statusItem.button.image = Dynamic.NSImage.imageNamed("iconbar")
    statusItem.button.action = #selector(openApp(_:))
    
    let rvc = Dynamic.NSViewController()
    rvc.view = rootView

    popover.contentViewController = rvc
    popover.showRelativeToRect(statusItem.button.bounds, ofView: statusItem.button, preferredEdge: 1) // NSRectEdge.minY = 1
    
    #endif
    
    return true
  }
  
  
  #if targetEnvironment(macCatalyst)
  @objc func openApp(_ sender: Any?) {
    // Now it does nothing
  }
  #endif
  
}

#if targetEnvironment(macCatalyst)
extension UIWindow {
    var nsWindow: NSObject? {
        var nsWindow = Dynamic.NSApplication.sharedApplication.delegate.hostWindowForUIWindow(self)
        if #available(macOS 11, *) {
            nsWindow = nsWindow.attachedWindow
        }
        return nsWindow.asObject
    }
}

#endif

Any help would be appreciated. 😅

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant