From 56474629ee2593b0c8498f5d900a35f77e5b6481 Mon Sep 17 00:00:00 2001 From: Ellet Date: Wed, 22 Jan 2025 00:57:51 +0300 Subject: [PATCH 1/4] fix: sheet is smaller than expected #2, or might be displayed on the wrong window (or not displayed) #4 --- .../ImagePickerImpl.swift | 19 +++++++++++++++++-- .../NativeImagePickerPlugin.swift | 2 +- pigeons/messages.dart | 1 + 3 files changed, 19 insertions(+), 3 deletions(-) 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..2026058 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,11 +113,20 @@ 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 + + let windowSize = window.frame.size + + let scaleFactor = 0.80 // 80% of the parent window's size + + var pickerWidth = windowSize.width * scaleFactor + var pickerHeight = windowSize.height * scaleFactor + + picker.view.frame = NSRect(x: 0, y: 0, width: pickerWidth, height: pickerHeight) + window.contentViewController?.presentAsSheet(picker) } 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 From 90775e58f0ca7f82fe67fb8c6643a2d7fa49ce97 Mon Sep 17 00:00:00 2001 From: Ellet Date: Wed, 22 Jan 2025 01:24:16 +0300 Subject: [PATCH 2/4] docs: remove known issue from README --- README.md | 3 --- 1 file changed, 3 deletions(-) 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. From 9eb9116799792b11c071f19684556f61b290a63b Mon Sep 17 00:00:00 2001 From: Ellet Date: Wed, 22 Jan 2025 11:02:32 +0300 Subject: [PATCH 3/4] chore: set sheet min content size --- .../Sources/native_image_picker_macos/ImagePickerImpl.swift | 3 +++ 1 file changed, 3 insertions(+) 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 2026058..50b1b09 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 @@ -128,6 +128,9 @@ class ImagePickerImpl: NSObject, ImagePickerApi { picker.view.frame = NSRect(x: 0, y: 0, width: pickerWidth, height: pickerHeight) 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 { From a43597dfb0af27929ba8219e384e05cbe9c5549d Mon Sep 17 00:00:00 2001 From: Ellet Date: Wed, 22 Jan 2025 11:24:35 +0300 Subject: [PATCH 4/4] chore: set fixed initial sheet size --- .../native_image_picker_macos/ImagePickerImpl.swift | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) 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 50b1b09..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 @@ -118,14 +118,8 @@ class ImagePickerImpl: NSObject, ImagePickerApi { return } - let windowSize = window.frame.size - - let scaleFactor = 0.80 // 80% of the parent window's size - - var pickerWidth = windowSize.width * scaleFactor - var pickerHeight = windowSize.height * scaleFactor - - picker.view.frame = NSRect(x: 0, y: 0, width: pickerWidth, height: pickerHeight) + // 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)