Skip to content

Commit 5647462

Browse files
committed
fix: sheet is smaller than expected #2, or might be displayed on the wrong window (or not displayed) #4
1 parent 4b91e98 commit 5647462

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

macos/native_image_picker_macos/Sources/native_image_picker_macos/ImagePickerImpl.swift

+17-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,11 +113,20 @@ 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+
let windowSize = window.frame.size
122+
123+
let scaleFactor = 0.80 // 80% of the parent window's size
124+
125+
var pickerWidth = windowSize.width * scaleFactor
126+
var pickerHeight = windowSize.height * scaleFactor
127+
128+
picker.view.frame = NSRect(x: 0, y: 0, width: pickerWidth, height: pickerHeight)
129+
115130
window.contentViewController?.presentAsSheet(picker)
116131
}
117132

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)