Skip to content

Commit

Permalink
feat: implement this device orientation Event on the dart side
Browse files Browse the repository at this point in the history
  • Loading branch information
yifei8 committed Apr 19, 2024
1 parent 7789f09 commit 56820d3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions webf/lib/src/dom/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const String EVENT_STATE_START = 'start';
const String EVENT_STATE_UPDATE = 'update';
const String EVENT_STATE_END = 'end';
const String EVENT_STATE_CANCEL = 'cancel';
const String EVENT_DEVICE_ORIENTATION = 'deviceorientation';

mixin ElementEventMixin on ElementBase {
AppearEventType _prevAppearState = AppearEventType.none;
Expand Down
48 changes: 48 additions & 0 deletions webf/lib/src/dom/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
* Copyright (C) 2019-2022 The Kraken authors. All rights reserved.
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
import 'dart:math' as math;
import 'dart:async';
import 'dart:ui';

import 'package:sensors_plus/sensors_plus.dart';
import 'package:webf/bridge.dart';
import 'package:webf/dom.dart';
import 'package:webf/foundation.dart';
Expand Down Expand Up @@ -117,6 +120,9 @@ class Window extends EventTarget {
// Fired at the Document or element when the viewport or element is scrolled, respectively.
document.documentElement?.addEventListener(eventType, handler, addEventListenerOptions: addEventListenerOptions);
break;
case EVENT_DEVICE_ORIENTATION:
_registerGyroscope();
break;
}
}

Expand All @@ -127,6 +133,9 @@ class Window extends EventTarget {
case EVENT_SCROLL:
document.documentElement?.removeEventListener(eventType, handler);
break;
case EVENT_DEVICE_ORIENTATION:
_unRegisterGyroscope(false);
break;
}
}

Expand All @@ -135,4 +144,43 @@ class Window extends EventTarget {
void focus() {
// TODO
}

StreamSubscription<GyroscopeEvent>? _gyroscopeStream;
void _registerGyroscope() {
_gyroscopeStream ??= gyroscopeEventStream().listen((GyroscopeEvent event) {
dispatchDeviceOrientationEvent(event.x, event.y, event.z);
},
cancelOnError: true,
);
}

void _unRegisterGyroscope(bool dispose) {
if (dispose || !hasEventListener(EVENT_DEVICE_ORIENTATION)) {
_gyroscopeStream?.cancel();
_gyroscopeStream = null;
}
}

/// Convert gyroscope data obtained in Flutter into rotation angle in the Web
/// In the w3c standard:
/// alpha: A number representing the motion of the device around the z axis, express in degrees with values ranging from 0 (inclusive) to 360 (exclusive).
/// beta: A number representing the motion of the device around the x axis, expressed in degrees with values ranging from -180 (inclusive) to 180 (exclusive).
/// gamma: A number representing the motion of the device around the y axis, expressed in degrees with values ranging from -90 (inclusive) to 90 (exclusive).
void dispatchDeviceOrientationEvent(x, y, z) {
var xAxisAngle= math.atan2(y, z) * (180 / math.pi); // Angle of rotation around the X-axis
var yAxisAngle = math.atan2(-x, math.sqrt(y * y + z * z)) * (180 / math.pi); // Rotation angle around Y axis
var zAxisAngle = math.atan2(y, z) * (180 / math.pi); // Rotation angle around Z axis
var alpha = zAxisAngle + 180;
var beta = xAxisAngle;
var gamma = yAxisAngle;
if (hasEventListener(EVENT_DEVICE_ORIENTATION)) {
dispatchEvent(DeviceOrientationEvent(alpha, beta, gamma));
}
}

@override
void dispose() async {
_unRegisterGyroscope(true);
super.dispose();
}
}
1 change: 1 addition & 0 deletions webf/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies:
shared_preferences: 2.2.0 # No AndroidX used.
archive: ^3.3.7 # Pure dart module.
web_socket_channel: ^2.2.0
sensors_plus: ^4.0.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 56820d3

Please sign in to comment.