Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions packages/camera/camera_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.5+5

* Bumps minimum package:web dependency to 1.0.0 and removes pkg_web_tweaks.dart.

## 0.3.5+4

* Fixes a `TypeError` in `availableCameras()` caused by browsers (e.g. Firefox) returning
Expand Down
13 changes: 6 additions & 7 deletions packages/camera/camera_web/lib/src/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,14 @@ class Camera {

if (videoTracks.isNotEmpty) {
final web.MediaStreamTrack defaultVideoTrack = videoTracks.first;
final JSArray<JSBoolean>? torchCapability = defaultVideoTrack.getCapabilities().torchNullable;
final bool canEnableTorchMode =
defaultVideoTrack.getCapabilities().torchNullable?.toDart.first.toDart ?? false;
torchCapability != null &&
torchCapability.toDart.isNotEmpty &&
torchCapability.toDart.first.toDart;

if (canEnableTorchMode) {
defaultVideoTrack.applyWebTweakConstraints(
WebTweakMediaTrackConstraints(torch: enabled.toJS),
);
defaultVideoTrack.applyConstraints(MediaTrackConstraints(torch: enabled.toJS));
} else {
throw CameraWebException(
textureId,
Expand Down Expand Up @@ -385,9 +386,7 @@ class Camera {
);
}

zoomLevelCapability.videoTrack.applyWebTweakConstraints(
WebTweakMediaTrackConstraints(zoom: zoom.toJS),
);
zoomLevelCapability.videoTrack.applyConstraints(MediaTrackConstraints(zoom: zoom.toJS));
}

/// Returns a lens direction of this camera.
Expand Down
15 changes: 6 additions & 9 deletions packages/camera/camera_web/lib/src/camera_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class CameraService {
web.Window window = web.window;

/// The utility to manipulate JavaScript interop objects.
@visibleForTesting
JsUtil jsUtil = JsUtil();

/// Returns a media stream associated with the camera device
Expand Down Expand Up @@ -128,7 +127,7 @@ class CameraService {

/// The zoom level capability is represented by MediaSettingsRange.
/// See: https://developer.mozilla.org/en-US/docs/Web/API/MediaSettingsRange
final WebTweakMediaSettingsRange? zoomLevelCapability = defaultVideoTrack
final web.MediaSettingsRange? zoomLevelCapability = defaultVideoTrack
.getCapabilities()
.zoomNullable;

Expand Down Expand Up @@ -164,7 +163,7 @@ class CameraService {
.getSupportedConstraints();

// Return null if the facing mode is not supported.
if (!supportedConstraints.facingMode) {
if (!(supportedConstraints.facingModeNullable ?? false)) {
return null;
}

Expand Down Expand Up @@ -203,15 +202,13 @@ class CameraService {
// We use jsUtil.getProperty to safely read the raw JS value, then explicitly
// validate it is a JSArray before accessing its elements to prevent a TypeError.

final JSAny? facingModeCapabilities = jsUtil.getProperty(
videoTrackCapabilities,
'facingMode'.toJS,
);
if (facingModeCapabilities == null || !facingModeCapabilities.isA<JSArray>()) {
final JSArray<JSString>? facingModeCapabilities = videoTrackCapabilities.facingModeNullable;

if (facingModeCapabilities == null) {
return null;
}

final List<JSAny?> facingModes = (facingModeCapabilities as JSArray).toDart;
final List<JSAny?> facingModes = facingModeCapabilities.toDart;

if (facingModes.isNotEmpty && facingModes.first.isA<JSString>()) {
return (facingModes.first! as JSString).toDart;
Expand Down
3 changes: 1 addition & 2 deletions packages/camera/camera_web/lib/src/camera_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'package:web/web.dart' as web;

import 'camera.dart';
import 'camera_service.dart';
import 'pkg_web_tweaks.dart';
import 'types/types.dart';

// The default error message, when the error is an empty string.
Expand Down Expand Up @@ -388,7 +387,7 @@ class CameraPlugin extends CameraPlatform {
// See: https://w3c.github.io/screen-orientation/#interaction-with-fullscreen-api
// Recent versions of Dart changed requestFullscreen to return a Future instead of void.
// This wrapper allows use of both the old and new APIs.
dynamic fullScreen() => documentElement.requestFullScreenTweak();
dynamic fullScreen() => documentElement.requestFullscreen();
await fullScreen();
await screenOrientation.lock(orientationType).toDart;
} else {
Expand Down
87 changes: 35 additions & 52 deletions packages/camera/camera_web/lib/src/pkg_web_tweaks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,50 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'dart:js_interop';

import 'package:web/web.dart';

/// Adds missing fields to [Element].
extension FullScreenSupportMethods on Element {
@JS('requestFullscreen')
external JSPromise<JSAny?> requestFullScreenTweak([JSAny options]);
}
import 'package:web/web.dart' as web;

/// Adds missing fields to [MediaTrackSupportedConstraints].
extension NonStandardFieldsOnMediaTrackSupportedConstraints on MediaTrackSupportedConstraints {
@JS('zoom')
external bool? get zoomNullable;

@JS('torch')
external bool? get torchNullable;
}
import 'shims/dart_js_util.dart';

/// Adds missing fields to [MediaTrackCapabilities].
extension NonStandardFieldsOnMediaTrackCapabilities on MediaTrackCapabilities {
@JS('zoom')
external WebTweakMediaSettingsRange? get zoomNullable;
final JsUtil _jsUtil = JsUtil();

@JS('torch')
external JSArray<JSBoolean>? get torchNullable;

@JS('facingMode')
external JSArray<JSString>? get facingModeNullable;
}

/// Adds missing fields to [MediaTrackSettings]
extension NonStandardFieldsOnMediaTrackSettings on MediaTrackSettings {
@JS('facingMode')
external String? get facingModeNullable;
}
/// Adds safe helper getters to [web.MediaTrackSupportedConstraints].
extension NonStandardFieldsOnMediaTrackSupportedConstraints on web.MediaTrackSupportedConstraints {
/// Returns the zoom constraint support or null if not present on the JS object.
bool? get zoomNullable => _jsUtil.hasProperty(this, 'zoom'.toJS) ? zoom : null;

/// Brought over from package:web 1.0.0
extension type WebTweakMediaSettingsRange._(JSObject _) implements JSObject {
@JS('MediaSettingsRange')
external factory WebTweakMediaSettingsRange({num max, num min, num step});
/// Returns the torch constraint support or null if not present on the JS object.
bool? get torchNullable => _jsUtil.hasProperty(this, 'torch'.toJS) ? torch : null;

external double get max;
external set max(num value);
external double get min;
external set min(num value);
external double get step;
external set step(num value);
/// Returns the facingMode constraint support or null if not present on the JS object.
bool? get facingModeNullable => _jsUtil.hasProperty(this, 'facingMode'.toJS) ? facingMode : null;
}

/// Adds an applyConstraints method that accepts the WebTweakMediaTrackConstraints.
extension WebTweakMethodVersions on MediaStreamTrack {
@JS('applyConstraints')
external JSPromise<JSAny?> applyWebTweakConstraints([WebTweakMediaTrackConstraints constraints]);
/// Adds safe helper getters to [web.MediaTrackCapabilities].
extension NonStandardFieldsOnMediaTrackCapabilities on web.MediaTrackCapabilities {
/// Returns the zoom capability range or null if not present on the JS object.
web.MediaSettingsRange? get zoomNullable => _jsUtil.hasProperty(this, 'zoom'.toJS) ? zoom : null;

/// Returns the torch capability array or null if not present on the JS object.
JSArray<JSBoolean>? get torchNullable => _jsUtil.hasProperty(this, 'torch'.toJS) ? torch : null;

/// Returns the facingMode capability array or null if not present on the JS object or not a [JSArray].
JSArray<JSString>? get facingModeNullable {
if (!_jsUtil.hasProperty(this, 'facingMode'.toJS)) {
return null;
}
final JSAny? val = _jsUtil.getProperty(this, 'facingMode'.toJS);
if (val != null && val.isA<JSArray>()) {
return val as JSArray<JSString>;
}
return null;
}
}

/// Allows creating the MediaTrackConstraints that are needed.
/// Brought over from package:web 1.0.0
extension type WebTweakMediaTrackConstraints._(JSObject _) implements JSObject {
@JS('MediaTrackConstraints')
external factory WebTweakMediaTrackConstraints({JSAny zoom, ConstrainBoolean torch});
/// Adds safe helper getters to [web.MediaTrackSettings].
extension NonStandardFieldsOnMediaTrackSettings on web.MediaTrackSettings {
/// Returns the facingMode setting or null if not present on the JS object.
String? get facingModeNullable =>
_jsUtil.hasProperty(this, 'facingMode'.toJS) ? facingMode : null;
}
4 changes: 2 additions & 2 deletions packages/camera/camera_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_web
description: A Flutter plugin for getting information about and controlling the camera on Web.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.3.5+4
version: 0.3.5+5

environment:
sdk: ^3.10.0
Expand All @@ -23,7 +23,7 @@ dependencies:
flutter_web_plugins:
sdk: flutter
stream_transform: ^2.0.0
web: ">=0.5.1 <2.0.0"
web: ^1.0.0

dev_dependencies:
flutter_test:
Expand Down
Loading