diff --git a/README.md b/README.md index 20693e9..8f7c7d4 100644 --- a/README.md +++ b/README.md @@ -154,9 +154,6 @@ Contributions are welcome. File issues to the [GitHub repo](https://github.com/C * [Similarly to `image_picker_macos`](https://pub.dev/packages/image_picker_macos#limitations), `ImageSource.camera` is not supported [unless a `cameraDelegate` is set](https://pub.dev/packages/image_picker#windows-macos-and-linux). * [Similarly to `image_picker_macos`](https://pub.dev/packages/image_picker_macos#pickvideo), the `maxDuration` argument in `pickVideo` is unsupported and will be silently ignored. -> [!WARNING] -> **Known issue**: The native picker window initially appears smaller than expected, requiring the user to manually resize it. Refer to [#2](https://github.com/CompileKernel/native-image-picker-macos/issues/2) for details. - ## 📚 Additional information This functionality was originally proposed as a [pull request to `image_picker_macos`](https://github.com/flutter/packages/pull/8079/), but it was later decided to split it into a community package which is unendorsed. diff --git a/macos/native_image_picker_macos/Sources/native_image_picker_macos/ImagePickerImpl.swift b/macos/native_image_picker_macos/Sources/native_image_picker_macos/ImagePickerImpl.swift index 777dba5..f099286 100644 --- a/macos/native_image_picker_macos/Sources/native_image_picker_macos/ImagePickerImpl.swift +++ b/macos/native_image_picker_macos/Sources/native_image_picker_macos/ImagePickerImpl.swift @@ -8,6 +8,12 @@ import PhotosUI /// to use [PHPickerViewController](https://developer.apple.com/documentation/photokit/phpickerviewcontroller) which is supported on macOS 13.0+ /// otherwise fallback to file selector if unsupported or the user prefers the file selector implementation. class ImagePickerImpl: NSObject, ImagePickerApi { + private let view: NSView? + + init(view: NSView?) { + self.view = view + } + /// Returns `true` if the current macOS version supports this feature. /// /// `PHPicker` is supported on macOS 13.0+. @@ -107,12 +113,18 @@ class ImagePickerImpl: NSObject, ImagePickerApi { @available(macOS 13, *) private func showPHPicker(_ picker: PHPickerViewController, noActiveWindow: @escaping () -> Void) { - guard let window = NSApplication.shared.keyWindow else { + guard let window = view?.window else { noActiveWindow() return } - // TODO(EchoEllet): IMPORTANT The window size of the picker is smaller than expected, see the video in https://discord.com/channels/608014603317936148/1295165633931120642/1295470850283147335 + + // A similar initial sheet size to PhotosPicker in a macOS SwiftUI app. + picker.view.frame = NSRect(x: 0, y: 0, width: 780, height: 615) + window.contentViewController?.presentAsSheet(picker) + + // A similar minimum sheet size to PhotosPicker in a macOS SwiftUI app. + picker.view.window?.contentMinSize = NSSize(width: 320, height: 200) } func openPhotosApp() -> Bool { diff --git a/macos/native_image_picker_macos/Sources/native_image_picker_macos/NativeImagePickerPlugin.swift b/macos/native_image_picker_macos/Sources/native_image_picker_macos/NativeImagePickerPlugin.swift index 387f8f2..8869649 100644 --- a/macos/native_image_picker_macos/Sources/native_image_picker_macos/NativeImagePickerPlugin.swift +++ b/macos/native_image_picker_macos/Sources/native_image_picker_macos/NativeImagePickerPlugin.swift @@ -4,7 +4,7 @@ import FlutterMacOS public class NativeImagePickerPlugin: NSObject, FlutterPlugin { public static func register(with registrar: FlutterPluginRegistrar) { let messenger = registrar.messenger - let api = ImagePickerImpl() + let api = ImagePickerImpl(view: registrar.view) ImagePickerApiSetup.setUp(binaryMessenger: messenger, api: api) } } diff --git a/pigeons/messages.dart b/pigeons/messages.dart index 438d03e..ded1174 100644 --- a/pigeons/messages.dart +++ b/pigeons/messages.dart @@ -66,6 +66,7 @@ enum ImagePickerError { phpickerUnsupported, /// Could not show the picker due to the missing window. + /// This May occur if the `NSView` is `nil`, for instance in a headless environment. windowNotFound, /// When a `PHPickerResult` can't load `NSImage`. This error should not be reached