macosui / macos_window_utils.dart

macos_window_utils is a Flutter package that provides a set of methods for modifying the NSWindow of a Flutter application on macOS.
https://pub.dev/packages/macos_window_utils
MIT License
49 stars 9 forks source link

Flutter view not showing on older MacOS versions #44

Open cbenhagen opened 8 months ago

cbenhagen commented 8 months ago

Running the example in this repo renders an empty window on macOS 12.7.1 and 10.15.7 (both running on Intel) but works as expected on an Apple Silicon machine with macOS 14.2.1:

Screenshot 2023-12-24 at 14 25 59

I first tested older Flutter versions and then older versions of this repo. Using git bisect I was able to track it down to this one commit: 9f09f88fd8551482fcc1fd7297d9a42fd00cdf09

So I am guessing the changes in 24fe66e47d5029e0bd11709acf931e099b8c2dde somehow only work for newer macOS versions? @Adrian-Samoticha do you have any ideas why this might be the case and how to fix it?

Adrian-Samoticha commented 8 months ago

Related to https://github.com/macosui/macos_ui/issues/450. I am aware of the issue, but haven’t had much time to work on it lately.

EDIT: Thanks for finding out which commit is causing the issue, though. This might help with investigating the issue.

cbenhagen commented 8 months ago

I am using the old instructions as a workaround and can confirm that the example now works from macOS 10.15.7 to 14.2.1:

Open the macos/Runner.xcworkspace folder of your project using Xcode, press ⇧ + ⌘ + O and search for MainFlutterWindow.swift.

Insert import macos_window_utils at the top of the file. Then, replace the code above the super.awakeFromNib()-line with the following code:

let windowFrame = self.frame
let macOSWindowUtilsViewController = MacOSWindowUtilsViewController()
self.contentViewController = macOSWindowUtilsViewController
self.setFrame(windowFrame, display: true)

/* Initialize the macos_window_utils plugin */
MainFlutterWindowManipulator.start(mainFlutterWindow: self)

RegisterGeneratedPlugins(registry: macOSWindowUtilsViewController.flutterViewController)

Assuming you're starting with the default configuration, the finished code should look something like this:

import Cocoa
import FlutterMacOS
+import macos_window_utils

class MainFlutterWindow: NSWindow {
  override func awakeFromNib() {
-   let flutterViewController = FlutterViewController.init()
-   let windowFrame = self.frame
-   self.contentViewController = flutterViewController
-   self.setFrame(windowFrame, display: true)

-   RegisterGeneratedPlugins(registry: flutterViewController)

+   let windowFrame = self.frame
+   let macOSWindowUtilsViewController = MacOSWindowUtilsViewController()
+   self.contentViewController = macOSWindowUtilsViewController
+   self.setFrame(windowFrame, display: true)

+   /* Initialize the macos_window_utils plugin */
+   MainFlutterWindowManipulator.start(mainFlutterWindow: self)

+   RegisterGeneratedPlugins(registry: macOSWindowUtilsViewController.flutterViewController)

    super.awakeFromNib()
  }
}
whiplashoo commented 8 months ago

Thank you! The workaround seems to work.