-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[image_picker] Add desktop support - platform interface #4161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
auto-submit
merged 3 commits into
flutter:main
from
stuartmorgan-g:image-picker-macos-linux-platform-interface
Jun 8, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/image_picker/image_picker_platform_interface/lib/src/types/camera_delegate.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:cross_file/cross_file.dart'; | ||
import 'package:flutter/foundation.dart' show immutable; | ||
|
||
import 'camera_device.dart'; | ||
|
||
/// Options for [ImagePickerCameraDelegate] methods. | ||
/// | ||
/// New options may be added in the future. | ||
@immutable | ||
class ImagePickerCameraDelegateOptions { | ||
/// Creates a new set of options for taking an image or video. | ||
const ImagePickerCameraDelegateOptions({ | ||
this.preferredCameraDevice = CameraDevice.rear, | ||
this.maxVideoDuration, | ||
}); | ||
|
||
/// The camera device to default to, if available. | ||
/// | ||
/// Defaults to [CameraDevice.rear]. | ||
final CameraDevice preferredCameraDevice; | ||
|
||
/// The maximum duration to allow when recording a video. | ||
/// | ||
/// Defaults to null, meaning no maximum duration. | ||
final Duration? maxVideoDuration; | ||
} | ||
|
||
/// A delegate for `ImagePickerPlatform` implementations that do not provide | ||
/// a camera implementation, or that have a default but allow substituting an | ||
/// alternate implementation. | ||
abstract class ImagePickerCameraDelegate { | ||
/// Takes a photo with the given [options] and returns an [XFile] to the | ||
/// resulting image file. | ||
/// | ||
/// Returns null if the photo could not be taken, or the user cancelled. | ||
Future<XFile?> takePhoto({ | ||
ImagePickerCameraDelegateOptions options = | ||
const ImagePickerCameraDelegateOptions(), | ||
}); | ||
|
||
/// Records a video with the given [options] and returns an [XFile] to the | ||
/// resulting video file. | ||
/// | ||
/// Returns null if the video could not be recorded, or the user cancelled. | ||
Future<XFile?> takeVideo({ | ||
ImagePickerCameraDelegateOptions options = | ||
const ImagePickerCameraDelegateOptions(), | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/image_picker/image_picker_platform_interface/test/image_picker_platform_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart'; | ||
|
||
void main() { | ||
group('ImagePickerPlatform', () { | ||
test('supportsImageSource defaults to true for original values', () async { | ||
final ImagePickerPlatform implementation = FakeImagePickerPlatform(); | ||
|
||
expect(implementation.supportsImageSource(ImageSource.camera), true); | ||
expect(implementation.supportsImageSource(ImageSource.gallery), true); | ||
}); | ||
}); | ||
|
||
group('CameraDelegatingImagePickerPlatform', () { | ||
test( | ||
'supportsImageSource returns false for camera when there is no delegate', | ||
() async { | ||
final FakeCameraDelegatingImagePickerPlatform implementation = | ||
FakeCameraDelegatingImagePickerPlatform(); | ||
|
||
expect(implementation.supportsImageSource(ImageSource.camera), false); | ||
}); | ||
|
||
test('supportsImageSource returns true for camera when there is a delegate', | ||
() async { | ||
final FakeCameraDelegatingImagePickerPlatform implementation = | ||
FakeCameraDelegatingImagePickerPlatform(); | ||
implementation.cameraDelegate = FakeCameraDelegate(); | ||
|
||
expect(implementation.supportsImageSource(ImageSource.camera), true); | ||
}); | ||
|
||
test('getImageFromSource for camera throws if delegate is not set', | ||
() async { | ||
final FakeCameraDelegatingImagePickerPlatform implementation = | ||
FakeCameraDelegatingImagePickerPlatform(); | ||
|
||
await expectLater( | ||
implementation.getImageFromSource(source: ImageSource.camera), | ||
throwsStateError); | ||
}); | ||
|
||
test('getVideo for camera throws if delegate is not set', () async { | ||
final FakeCameraDelegatingImagePickerPlatform implementation = | ||
FakeCameraDelegatingImagePickerPlatform(); | ||
|
||
await expectLater(implementation.getVideo(source: ImageSource.camera), | ||
throwsStateError); | ||
}); | ||
|
||
test('getImageFromSource for camera calls delegate if set', () async { | ||
const String fakePath = '/tmp/foo'; | ||
final FakeCameraDelegatingImagePickerPlatform implementation = | ||
FakeCameraDelegatingImagePickerPlatform(); | ||
implementation.cameraDelegate = | ||
FakeCameraDelegate(result: XFile(fakePath)); | ||
|
||
expect( | ||
(await implementation.getImageFromSource(source: ImageSource.camera))! | ||
.path, | ||
fakePath); | ||
}); | ||
|
||
test('getVideo for camera calls delegate if set', () async { | ||
const String fakePath = '/tmp/foo'; | ||
final FakeCameraDelegatingImagePickerPlatform implementation = | ||
FakeCameraDelegatingImagePickerPlatform(); | ||
implementation.cameraDelegate = | ||
FakeCameraDelegate(result: XFile(fakePath)); | ||
|
||
expect((await implementation.getVideo(source: ImageSource.camera))!.path, | ||
fakePath); | ||
}); | ||
}); | ||
} | ||
|
||
class FakeImagePickerPlatform extends ImagePickerPlatform {} | ||
|
||
class FakeCameraDelegatingImagePickerPlatform | ||
extends CameraDelegatingImagePickerPlatform {} | ||
|
||
class FakeCameraDelegate extends ImagePickerCameraDelegate { | ||
FakeCameraDelegate({this.result}); | ||
|
||
XFile? result; | ||
|
||
@override | ||
Future<XFile?> takePhoto( | ||
{ImagePickerCameraDelegateOptions options = | ||
const ImagePickerCameraDelegateOptions()}) async { | ||
return result; | ||
} | ||
|
||
@override | ||
Future<XFile?> takeVideo( | ||
{ImagePickerCameraDelegateOptions options = | ||
const ImagePickerCameraDelegateOptions()}) async { | ||
return result; | ||
} | ||
} |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because Dart won't promote nullable fields (because in general it can't guarantee that you aren't running this code in the context of a subclass that overrode the field with a getter that does something crazy like return null every other time you call it), you have two options for cases like this:
!
every time you access it.I strongly prefer the former since it relies on the compiler to ensure that things are still right any time someone changes the logic, while the latter relies on humans to ensure it.