Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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+.
Expand Down Expand Up @@ -107,12 +113,24 @@ 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)

// A similar minimum sheet size to PhotosPicker in a macOS SwiftUI app.
picker.view.window?.contentMinSize = NSSize(width: 320, height: 200)
}

func openPhotosApp() -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions pigeons/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading