nativeWindowOf function

Window? nativeWindowOf(
  1. BaseWindowController controller
)

The native window behind a Flutter window controller, as a nativeapi Window.

Flutter hands out an NSWindow* on macOS, an HWND on Windows, and a GtkWindow* on Linux; these are exactly the handles nativeapi wraps, and wrapping reuses the ID already attached to the native window, so the result compares equal (by Window.id) to what WindowManager returns.

Returns null once the controller is destroyed, or on a platform whose controller does not expose a window handle.

Implementation

Window? nativeWindowOf(fw.BaseWindowController controller) {
  final ffi.Pointer<ffi.Void> handle;
  try {
    handle = switch (controller) {
      final fw_macos.WindowControllerMacOS c => c.windowHandle,
      final fw_win32.WindowControllerWin32 c => c.windowHandle,
      final fw_linux.WindowControllerLinux c => c.windowHandle,
      _ => ffi.nullptr,
    };
  } on StateError {
    // The stable channel has no `isDestroyed`; a destroyed controller throws
    // from `windowHandle` instead.
    return null;
  }
  if (handle == ffi.nullptr) return null;
  return Window.createWithNativeWindow(handle);
}