r/FlutterDev • • 1d ago

Article Multi-window on macOS with one Flutter engine

Ferret lives in the Mac menu bar, and it needs several windows: the popover, a full window, a phone-shaped mirroring window, and a floating panel while you drag files.

One engine per window means N copies of the app, N device connections and N states to sync. So every window is a FlutterView on ONE engine.

Native side, a window is one more view controller:

let vc = FlutterViewController(engine: engine, nibName: nil, bundle: nil)
window.contentViewController = vc

Dart side, one View per window, all under the same providers:

ViewCollection(views: [
  View(view: views.main, child: app(views.mainNavigatorKey)),
  if (window != null) View(view: window, child: app(views.windowNavigatorKey)),
])

Copy on your phone, and the clip shows up in the popover and the window at once. One model, no syncing.

What I learned:

  • Accessibility. enableMultiView must run before the first view, which takes the implicit view from the popover, and VoiceOver only reads the implicit view. So Ferret keeps the popover implicit and flips the engine's private _multiViewEnabled afterwards. If a Flutter release drops it, there's simply no extra window. Know a cleaner way? Tell me.
  • Navigation. Each window gets its own navigator. The app-wide key is a GlobalKey that forwards to the window in front, so a page opens where you're looking.
  • Drag and drop. Drags pass View.of(context).viewId, so the native side knows which window they start from.
  • ViewAnchor. In another app of mine, extra windows hang off the main tree with ViewAnchor(view: ViewCollection(...), child: app). They inherit every provider, with no need to lift anything above runWidget.
  • Popping out. A dialog dragged out of the window can become its own window and keep its state. Its Future completes when that window closes.
  • Closing. Before a window closes, pop its navigator to the first route. Otherwise anyone awaiting a dialog in it waits forever.

Windows and Linux use one window plus the tray for now. The code is GPL-3.0.

https://github.com/SoFluffyOS/ferret 🦊

3 Upvotes

1 comment sorted by