diff --git a/bridge/CMakeLists.txt b/bridge/CMakeLists.txt index 9be240c44b..02ebde6e93 100644 --- a/bridge/CMakeLists.txt +++ b/bridge/CMakeLists.txt @@ -337,6 +337,7 @@ if ($ENV{WEBF_JS_ENGINE} MATCHES "quickjs") core/events/message_event.cc core/events/animation_event.cc core/events/close_event.cc + core/events/deviceorientation_event.cc core/events/ui_event.cc core/events/focus_event.cc core/events/gesture_event.cc @@ -431,6 +432,8 @@ if ($ENV{WEBF_JS_ENGINE} MATCHES "quickjs") out/qjs_hashchange_event.cc out/qjs_hashchange_event_init.cc out/qjs_gesture_event_init.cc + out/qjs_deviceorientation_event.cc + out/qjs_deviceorientation_event_init.cc out/qjs_intersection_change_event.cc out/qjs_intersection_change_event_init.cc out/qjs_touch.cc diff --git a/bridge/bindings/qjs/binding_initializer.cc b/bridge/bindings/qjs/binding_initializer.cc index 8302f58453..64b2dfd526 100644 --- a/bridge/bindings/qjs/binding_initializer.cc +++ b/bridge/bindings/qjs/binding_initializer.cc @@ -33,6 +33,7 @@ #include "qjs_event_target.h" #include "qjs_focus_event.h" #include "qjs_gesture_event.h" +#include "qjs_deviceorientation_event.h" #include "qjs_hashchange_event.h" #include "qjs_html_all_collection.h" #include "qjs_html_anchor_element.h" @@ -120,6 +121,7 @@ void InstallBindings(ExecutingContext* context) { QJSFocusEvent::Install(context); QJSGestureEvent::Install(context); QJSHashchangeEvent::Install(context); + QJSDeviceorientationEvent::Install(context); QJSInputEvent::Install(context); QJSCustomEvent::Install(context); QJSMouseEvent::Install(context); diff --git a/bridge/bindings/qjs/wrapper_type_info.h b/bridge/bindings/qjs/wrapper_type_info.h index a77603ecfc..1f1828311d 100644 --- a/bridge/bindings/qjs/wrapper_type_info.h +++ b/bridge/bindings/qjs/wrapper_type_info.h @@ -33,6 +33,7 @@ enum { JS_CLASS_FOCUS_EVENT, JS_CLASS_GESTURE_EVENT, JS_CLASS_HASHCHANGE_EVENT, + JS_CLASS_DEVICEORIENTATION_EVENT, JS_CLASS_POP_STATE_EVENT, JS_CLASS_INTERSECTION_CHANGE_EVENT, JS_CLASS_KEYBOARD_EVENT, diff --git a/bridge/core/dom/events/event.cc b/bridge/core/dom/events/event.cc index 7b6ef8c407..0bbd7cc001 100644 --- a/bridge/core/dom/events/event.cc +++ b/bridge/core/dom/events/event.cc @@ -306,6 +306,10 @@ bool Event::IsHashChangeEvent() const { return false; } +bool Event::IsDeviceorientationEvent() const { + return false; +} + bool Event::IsIntersectionchangeEvent() const { return false; } diff --git a/bridge/core/dom/events/event.h b/bridge/core/dom/events/event.h index c5856cf7c3..9fa5f8de37 100644 --- a/bridge/core/dom/events/event.h +++ b/bridge/core/dom/events/event.h @@ -172,6 +172,7 @@ class Event : public ScriptWrappable { virtual bool IsPopstateEvent() const; virtual bool IsIntersectionchangeEvent() const; virtual bool IsHashChangeEvent() const; + virtual bool IsDeviceorientationEvent() const; // Drag events are a subset of mouse events. virtual bool IsDragEvent() const; diff --git a/bridge/core/events/dart_created_events.json5 b/bridge/core/events/dart_created_events.json5 index 0bce923918..abb3074b7c 100644 --- a/bridge/core/events/dart_created_events.json5 +++ b/bridge/core/events/dart_created_events.json5 @@ -38,6 +38,7 @@ ] }, "hashchange", + "deviceorientation", "input", { "class": "FocusEvent", diff --git a/bridge/core/events/deviceorientation_event.cc b/bridge/core/events/deviceorientation_event.cc new file mode 100644 index 0000000000..e63ce0ba39 --- /dev/null +++ b/bridge/core/events/deviceorientation_event.cc @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2024-present The WebF authors. All rights reserved. + */ + +#include "deviceorientation_event.h" +#include "qjs_device_orientation_event.h" + +namespace webf { + +DeviceorientationEvent* DeviceorientationEvent::Create(ExecutingContext* context, + const AtomicString& type, + ExceptionState& exception_state) { + return MakeGarbageCollected(context, type, exception_state); +} + +DeviceorientationEvent* DeviceorientationEvent::Create(ExecutingContext* context, + const AtomicString& type, + const std::shared_ptr& initializer, + ExceptionState& exception_state) { + return MakeGarbageCollected(context, type, initializer, exception_state); +} + +DeviceorientationEvent::DeviceorientationEvent(ExecutingContext* context, const AtomicString& type, ExceptionState& exception_state) + : Event(context, type) {} + +DeviceorientationEvent::DeviceorientationEvent(ExecutingContext* context, + const AtomicString& type, + const std::shared_ptr& initializer, + ExceptionState& exception_state) + : Event(context, type), + absolute_(initializer->hasAbsolute ? initializer->absolute()), + alpha_(initializer->hasAlpha() ? initializer->alpha() : 0.0), + beta_(initializer->hasBeta() ? initializer->beta() : 0.0), + gamma_(initializer->hasGamma() ? initializer->gamma() : 0.0) {} + +DeviceorientationEvent::DeviceorientationEvent(ExecutingContext* context, + const AtomicString& type, + NativeDeviceorientationEvent* native_orientation_event) + : Event(context, type, &native_orientation_event->native_event), + absolute_(native_orientation_event->absolute), + alpha_(native_orientation_event->alpha), + beta_(native_orientation_event->beta), + gamma_(native_orientation_event->gamma) { +} + +bool DeviceorientationEvent::IsDeviceorientationEvent() const { + return true; +} + +bool DeviceorientationEvent::absolute() const { + return absolute_; +} + +double DeviceorientationEvent::alpha() const { + return alpha_; +} + +double DeviceorientationEvent::beta() const { + return beta_; +} + +double DeviceorientationEvent::gamma() const { + return gamma_; +} + +} // namespace webf diff --git a/bridge/core/events/deviceorientation_event.d.ts b/bridge/core/events/deviceorientation_event.d.ts new file mode 100644 index 0000000000..cd8f162eb6 --- /dev/null +++ b/bridge/core/events/deviceorientation_event.d.ts @@ -0,0 +1,11 @@ +import {Event} from "../dom/events/event"; +import {DeviceorientationEventInit} from "./device_orientation_event_init"; + +interface DeviceorientationEvent extends Event { + readonly absolute: boolean; + readonly alpha: number; + readonly beta: number; + readonly gamma: number; + [key: string]: any; + new(type: string, init?: DeviceorientationEventInit): DeviceorientationEvent; +} diff --git a/bridge/core/events/deviceorientation_event.h b/bridge/core/events/deviceorientation_event.h new file mode 100644 index 0000000000..cf3e3ce6fb --- /dev/null +++ b/bridge/core/events/deviceorientation_event.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2024-present The WebF authors. All rights reserved. + */ + +#ifndef BRIDGE_CORE_EVENTS_GESTURE_EVENT_H_ +#define BRIDGE_CORE_EVENTS_GESTURE_EVENT_H_ + +#include "bindings/qjs/dictionary_base.h" +#include "bindings/qjs/source_location.h" +#include "core/dom/events/event.h" +#include "qjs_device_orientation_event_init.h" + +namespace webf { + +struct NativeDeviceorientationEvent; + +class DeviceorientationEvent : public Event { + DEFINE_WRAPPERTYPEINFO(); + + public: + using ImplType = DeviceorientationEvent*; + + static DeviceorientationEvent* Create(ExecutingContext* context, const AtomicString& type, ExceptionState& exception_state); + + static DeviceorientationEvent* Create(ExecutingContext* context, + const AtomicString& type, + const std::shared_ptr& initializer, + ExceptionState& exception_state); + + explicit DeviceorientationEvent(ExecutingContext* context, const AtomicString& type, ExceptionState& exception_state); + + explicit DeviceorientationEvent(ExecutingContext* context, + const AtomicString& type, + const std::shared_ptr& initializer, + ExceptionState& exception_state); + + explicit DeviceorientationEvent(ExecutingContext* context, const AtomicString& type, NativeDeviceorientationEvent* native_orientation_event); + + bool absolute() const; + double alpha() const; + double beta() const; + double gamma() const; + + bool IsDeviceorientationEvent() const override; + + private: + bool absolute_; + double alpha_; + double beta_; + double gamma_; +}; + +} // namespace webf + +#endif // BRIDGE_CORE_EVENTS_GESTURE_EVENT_H_ diff --git a/bridge/core/events/deviceorientation_event_init.d.ts b/bridge/core/events/deviceorientation_event_init.d.ts new file mode 100644 index 0000000000..d63fb59f9e --- /dev/null +++ b/bridge/core/events/deviceorientation_event_init.d.ts @@ -0,0 +1,10 @@ +import { EventInit } from "../dom/events/event_init"; + +// @ts-ignore +@Dictionary() +export interface DeviceorientationEventInit extends EventInit { + absolute?: boolean; + alpha?: number; + beta?: number; + gamma?: number; +} diff --git a/bridge/core/events/event_type_names.json5 b/bridge/core/events/event_type_names.json5 index 4185ec5f53..b3f9c2274a 100644 --- a/bridge/core/events/event_type_names.json5 +++ b/bridge/core/events/event_type_names.json5 @@ -224,6 +224,7 @@ "wheel", "zoom", "intersectionchange", + "deviceorientation", "gcopen" ] } diff --git a/webf/lib/src/dom/event.dart b/webf/lib/src/dom/event.dart index 35c065f6e4..5cf7848cd7 100644 --- a/webf/lib/src/dom/event.dart +++ b/webf/lib/src/dom/event.dart @@ -393,6 +393,31 @@ class MouseEvent extends UIEvent { } } +class DeviceOrientationEvent extends Event { + DeviceOrientationEvent(this.alpha, this.beta, this.gamma) : super(EVENT_DEVICE_ORIENTATION); + + final bool absolute = true; + final double alpha; + final double beta; + final double gamma; + + @override + Pointer toRaw([int extraLength = 0, bool isCustomEvent = false]) { + List methods = [ + doubleToUint64(alpha), + doubleToUint64(beta), + doubleToUint64(gamma) + ]; + Pointer rawEvent = super.toRaw(methods.length).cast(); + int currentStructSize = rawEvent.ref.length + methods.length; + Uint64List bytes = rawEvent.ref.bytes.asTypedList(currentStructSize); + bytes.setAll(rawEvent.ref.length, methods); + rawEvent.ref.length = currentStructSize; + + return rawEvent; + } +} + /// reference: https://developer.mozilla.org/en-US/docs/Web/API/GestureEvent class GestureEvent extends Event { final String state;