Skip to content

Commit f4f4d21

Browse files
authored
fix: sheet is too small, or might be displayed on the wrong window (#7)
* fix: the sheet is smaller than expected #2, or might be displayed on the wrong window #4 * docs: remove known issue from README * chore: set sheet min-content size * chore: set fixed initial sheet size
1 parent 4b91e98 commit f4f4d21

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

README.md

-3
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ Contributions are welcome. File issues to the [GitHub repo](https://github.com/C
154154
* [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).
155155
* [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.
156156

157-
> [!WARNING]
158-
> **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.
159-
160157
## 📚 Additional information
161158

162159
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.

macos/native_image_picker_macos/Sources/native_image_picker_macos/ImagePickerImpl.swift

+14-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import PhotosUI
88
/// to use [PHPickerViewController](https://developer.apple.com/documentation/photokit/phpickerviewcontroller) which is supported on macOS 13.0+
99
/// otherwise fallback to file selector if unsupported or the user prefers the file selector implementation.
1010
class ImagePickerImpl: NSObject, ImagePickerApi {
11+
private let view: NSView?
12+
13+
init(view: NSView?) {
14+
self.view = view
15+
}
16+
1117
/// Returns `true` if the current macOS version supports this feature.
1218
///
1319
/// `PHPicker` is supported on macOS 13.0+.
@@ -107,12 +113,18 @@ class ImagePickerImpl: NSObject, ImagePickerApi {
107113
@available(macOS 13, *)
108114
private func showPHPicker(_ picker: PHPickerViewController, noActiveWindow: @escaping () -> Void)
109115
{
110-
guard let window = NSApplication.shared.keyWindow else {
116+
guard let window = view?.window else {
111117
noActiveWindow()
112118
return
113119
}
114-
// TODO(EchoEllet): IMPORTANT The window size of the picker is smaller than expected, see the video in https://discord.com/channels/608014603317936148/1295165633931120642/1295470850283147335
120+
121+
// A similar initial sheet size to PhotosPicker in a macOS SwiftUI app.
122+
picker.view.frame = NSRect(x: 0, y: 0, width: 780, height: 615)
123+
115124
window.contentViewController?.presentAsSheet(picker)
125+
126+
// A similar minimum sheet size to PhotosPicker in a macOS SwiftUI app.
127+
picker.view.window?.contentMinSize = NSSize(width: 320, height: 200)
116128
}
117129

118130
func openPhotosApp() -> Bool {

macos/native_image_picker_macos/Sources/native_image_picker_macos/NativeImagePickerPlugin.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import FlutterMacOS
44
public class NativeImagePickerPlugin: NSObject, FlutterPlugin {
55
public static func register(with registrar: FlutterPluginRegistrar) {
66
let messenger = registrar.messenger
7-
let api = ImagePickerImpl()
7+
let api = ImagePickerImpl(view: registrar.view)
88
ImagePickerApiSetup.setUp(binaryMessenger: messenger, api: api)
99
}
1010
}

pigeons/messages.dart

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ enum ImagePickerError {
6666
phpickerUnsupported,
6767

6868
/// Could not show the picker due to the missing window.
69+
/// This May occur if the `NSView` is `nil`, for instance in a headless environment.
6970
windowNotFound,
7071

7172
/// When a `PHPickerResult` can't load `NSImage`. This error should not be reached

0 commit comments

Comments
 (0)