Skip to content

Commit 6d5fd26

Browse files
committed
[WIP]feat: support w3c deviceOrientationEvent
- add device orientation Event in bridge
1 parent 34e3229 commit 6d5fd26

12 files changed

+180
-0
lines changed

bridge/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ if ($ENV{WEBF_JS_ENGINE} MATCHES "quickjs")
337337
core/events/message_event.cc
338338
core/events/animation_event.cc
339339
core/events/close_event.cc
340+
core/events/deviceorientation_event.cc
340341
core/events/ui_event.cc
341342
core/events/focus_event.cc
342343
core/events/gesture_event.cc
@@ -431,6 +432,8 @@ if ($ENV{WEBF_JS_ENGINE} MATCHES "quickjs")
431432
out/qjs_hashchange_event.cc
432433
out/qjs_hashchange_event_init.cc
433434
out/qjs_gesture_event_init.cc
435+
out/qjs_deviceorientation_event.cc
436+
out/qjs_deviceorientation_event_init.cc
434437
out/qjs_intersection_change_event.cc
435438
out/qjs_intersection_change_event_init.cc
436439
out/qjs_touch.cc

bridge/bindings/qjs/binding_initializer.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qjs_event_target.h"
3434
#include "qjs_focus_event.h"
3535
#include "qjs_gesture_event.h"
36+
#include "qjs_deviceorientation_event.h"
3637
#include "qjs_hashchange_event.h"
3738
#include "qjs_html_all_collection.h"
3839
#include "qjs_html_anchor_element.h"
@@ -120,6 +121,7 @@ void InstallBindings(ExecutingContext* context) {
120121
QJSFocusEvent::Install(context);
121122
QJSGestureEvent::Install(context);
122123
QJSHashchangeEvent::Install(context);
124+
QJSDeviceorientationEvent::Install(context);
123125
QJSInputEvent::Install(context);
124126
QJSCustomEvent::Install(context);
125127
QJSMouseEvent::Install(context);

bridge/bindings/qjs/wrapper_type_info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum {
3333
JS_CLASS_FOCUS_EVENT,
3434
JS_CLASS_GESTURE_EVENT,
3535
JS_CLASS_HASHCHANGE_EVENT,
36+
JS_CLASS_DEVICEORIENTATION_EVENT,
3637
JS_CLASS_POP_STATE_EVENT,
3738
JS_CLASS_INTERSECTION_CHANGE_EVENT,
3839
JS_CLASS_KEYBOARD_EVENT,

bridge/core/dom/events/event.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ bool Event::IsHashChangeEvent() const {
306306
return false;
307307
}
308308

309+
bool Event::IsDeviceorientationEvent() const {
310+
return false;
311+
}
312+
309313
bool Event::IsIntersectionchangeEvent() const {
310314
return false;
311315
}

bridge/core/dom/events/event.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class Event : public ScriptWrappable {
172172
virtual bool IsPopstateEvent() const;
173173
virtual bool IsIntersectionchangeEvent() const;
174174
virtual bool IsHashChangeEvent() const;
175+
virtual bool IsDeviceorientationEvent() const;
175176

176177
// Drag events are a subset of mouse events.
177178
virtual bool IsDragEvent() const;

bridge/core/events/dart_created_events.json5

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
]
3939
},
4040
"hashchange",
41+
"deviceorientation",
4142
"input",
4243
{
4344
"class": "FocusEvent",
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2024-present The WebF authors. All rights reserved.
3+
*/
4+
5+
#include "deviceorientation_event.h"
6+
#include "qjs_device_orientation_event.h"
7+
8+
namespace webf {
9+
10+
DeviceorientationEvent* DeviceorientationEvent::Create(ExecutingContext* context,
11+
const AtomicString& type,
12+
ExceptionState& exception_state) {
13+
return MakeGarbageCollected<DeviceorientationEvent>(context, type, exception_state);
14+
}
15+
16+
DeviceorientationEvent* DeviceorientationEvent::Create(ExecutingContext* context,
17+
const AtomicString& type,
18+
const std::shared_ptr<DeviceorientationEventInit>& initializer,
19+
ExceptionState& exception_state) {
20+
return MakeGarbageCollected<DeviceorientationEvent>(context, type, initializer, exception_state);
21+
}
22+
23+
DeviceorientationEvent::DeviceorientationEvent(ExecutingContext* context, const AtomicString& type, ExceptionState& exception_state)
24+
: Event(context, type) {}
25+
26+
DeviceorientationEvent::DeviceorientationEvent(ExecutingContext* context,
27+
const AtomicString& type,
28+
const std::shared_ptr<DeviceorientationEventInit>& initializer,
29+
ExceptionState& exception_state)
30+
: Event(context, type),
31+
absolute_(initializer->hasAbsolute ? initializer->absolute()),
32+
alpha_(initializer->hasAlpha() ? initializer->alpha() : 0.0),
33+
beta_(initializer->hasBeta() ? initializer->beta() : 0.0),
34+
gamma_(initializer->hasGamma() ? initializer->gamma() : 0.0) {}
35+
36+
DeviceorientationEvent::DeviceorientationEvent(ExecutingContext* context,
37+
const AtomicString& type,
38+
NativeDeviceorientationEvent* native_orientation_event)
39+
: Event(context, type, &native_orientation_event->native_event),
40+
absolute_(native_orientation_event->absolute),
41+
alpha_(native_orientation_event->alpha),
42+
beta_(native_orientation_event->beta),
43+
gamma_(native_orientation_event->gamma) {
44+
}
45+
46+
bool DeviceorientationEvent::IsDeviceorientationEvent() const {
47+
return true;
48+
}
49+
50+
bool DeviceorientationEvent::absolute() const {
51+
return absolute_;
52+
}
53+
54+
double DeviceorientationEvent::alpha() const {
55+
return alpha_;
56+
}
57+
58+
double DeviceorientationEvent::beta() const {
59+
return beta_;
60+
}
61+
62+
double DeviceorientationEvent::gamma() const {
63+
return gamma_;
64+
}
65+
66+
} // namespace webf
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {Event} from "../dom/events/event";
2+
import {DeviceorientationEventInit} from "./device_orientation_event_init";
3+
4+
interface DeviceorientationEvent extends Event {
5+
readonly absolute: boolean;
6+
readonly alpha: number;
7+
readonly beta: number;
8+
readonly gamma: number;
9+
[key: string]: any;
10+
new(type: string, init?: DeviceorientationEventInit): DeviceorientationEvent;
11+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2024-present The WebF authors. All rights reserved.
3+
*/
4+
5+
#ifndef BRIDGE_CORE_EVENTS_GESTURE_EVENT_H_
6+
#define BRIDGE_CORE_EVENTS_GESTURE_EVENT_H_
7+
8+
#include "bindings/qjs/dictionary_base.h"
9+
#include "bindings/qjs/source_location.h"
10+
#include "core/dom/events/event.h"
11+
#include "qjs_device_orientation_event_init.h"
12+
13+
namespace webf {
14+
15+
struct NativeDeviceorientationEvent;
16+
17+
class DeviceorientationEvent : public Event {
18+
DEFINE_WRAPPERTYPEINFO();
19+
20+
public:
21+
using ImplType = DeviceorientationEvent*;
22+
23+
static DeviceorientationEvent* Create(ExecutingContext* context, const AtomicString& type, ExceptionState& exception_state);
24+
25+
static DeviceorientationEvent* Create(ExecutingContext* context,
26+
const AtomicString& type,
27+
const std::shared_ptr<DeviceorientationEventInit>& initializer,
28+
ExceptionState& exception_state);
29+
30+
explicit DeviceorientationEvent(ExecutingContext* context, const AtomicString& type, ExceptionState& exception_state);
31+
32+
explicit DeviceorientationEvent(ExecutingContext* context,
33+
const AtomicString& type,
34+
const std::shared_ptr<DeviceorientationEventInit>& initializer,
35+
ExceptionState& exception_state);
36+
37+
explicit DeviceorientationEvent(ExecutingContext* context, const AtomicString& type, NativeDeviceorientationEvent* native_orientation_event);
38+
39+
bool absolute() const;
40+
double alpha() const;
41+
double beta() const;
42+
double gamma() const;
43+
44+
bool IsDeviceorientationEvent() const override;
45+
46+
private:
47+
bool absolute_;
48+
double alpha_;
49+
double beta_;
50+
double gamma_;
51+
};
52+
53+
} // namespace webf
54+
55+
#endif // BRIDGE_CORE_EVENTS_GESTURE_EVENT_H_
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { EventInit } from "../dom/events/event_init";
2+
3+
// @ts-ignore
4+
@Dictionary()
5+
export interface DeviceorientationEventInit extends EventInit {
6+
absolute?: boolean;
7+
alpha?: number;
8+
beta?: number;
9+
gamma?: number;
10+
}

bridge/core/events/event_type_names.json5

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
"wheel",
225225
"zoom",
226226
"intersectionchange",
227+
"deviceorientation",
227228
"gcopen"
228229
]
229230
}

webf/lib/src/dom/event.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,31 @@ class MouseEvent extends UIEvent {
393393
}
394394
}
395395

396+
class DeviceOrientationEvent extends Event {
397+
DeviceOrientationEvent(this.alpha, this.beta, this.gamma) : super(EVENT_DEVICE_ORIENTATION);
398+
399+
final bool absolute = true;
400+
final double alpha;
401+
final double beta;
402+
final double gamma;
403+
404+
@override
405+
Pointer<NativeType> toRaw([int extraLength = 0, bool isCustomEvent = false]) {
406+
List<int> methods = [
407+
doubleToUint64(alpha),
408+
doubleToUint64(beta),
409+
doubleToUint64(gamma)
410+
];
411+
Pointer<RawEvent> rawEvent = super.toRaw(methods.length).cast<RawEvent>();
412+
int currentStructSize = rawEvent.ref.length + methods.length;
413+
Uint64List bytes = rawEvent.ref.bytes.asTypedList(currentStructSize);
414+
bytes.setAll(rawEvent.ref.length, methods);
415+
rawEvent.ref.length = currentStructSize;
416+
417+
return rawEvent;
418+
}
419+
}
420+
396421
/// reference: https://developer.mozilla.org/en-US/docs/Web/API/GestureEvent
397422
class GestureEvent extends Event {
398423
final String state;

0 commit comments

Comments
 (0)