Skip to content

Commit b282e3e

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

12 files changed

+185
-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/device_orientation_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_device_orientation_event.cc
436+
out/qjs_device_orientation_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
@@ -58,6 +58,7 @@
5858
#include "qjs_input_event.h"
5959
#include "qjs_intersection_change_event.h"
6060
#include "qjs_keyboard_event.h"
61+
#include "qjs_device_orientation_event.h"
6162
#include "qjs_location.h"
6263
#include "qjs_message_event.h"
6364
#include "qjs_module_manager.h"
@@ -129,6 +130,7 @@ void InstallBindings(ExecutingContext* context) {
129130
QJSTransitionEvent::Install(context);
130131
QJSIntersectionChangeEvent::Install(context);
131132
QJSKeyboardEvent::Install(context);
133+
QJSDeviceOrientationEvent::Install(context);
132134
QJSNode::Install(context);
133135
QJSNodeList::Install(context);
134136
QJSDocument::Install(context);

bridge/bindings/qjs/wrapper_type_info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum {
3535
JS_CLASS_HASHCHANGE_EVENT,
3636
JS_CLASS_POP_STATE_EVENT,
3737
JS_CLASS_INTERSECTION_CHANGE_EVENT,
38+
JS_CLASS_DEVICE_ORIENTATION_EVENT,
3839
JS_CLASS_KEYBOARD_EVENT,
3940
JS_CLASS_PROMISE_REJECTION_EVENT,
4041
JS_CLASS_EVENT_TARGET,

bridge/core/dom/events/event.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ bool Event::IsIntersectionchangeEvent() const {
310310
return false;
311311
}
312312

313+
bool Event::IsDeviceorientationEvent() const {
314+
return false;
315+
}
316+
313317
bool Event::IsDragEvent() const {
314318
return false;
315319
}

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
"intersectionchange"
5555
]
5656
},
57+
{
58+
"class": "DeviceOrientationEvent",
59+
"types": [
60+
"deviceorientation"
61+
]
62+
},
5763
// {
5864
// "class": "KeyboardEvent",
5965
// "types": [
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 "device_orientation_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)