Skip to content

Commit

Permalink
feat: support w3c DeviceOrientationEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
yifei8 committed Apr 17, 2024
1 parent 34e3229 commit 1acc5f4
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/device_orientation_event.cc
core/events/ui_event.cc
core/events/focus_event.cc
core/events/gesture_event.cc
Expand Down Expand Up @@ -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_device_orientation_event.cc
out/qjs_device_orientation_event_init.cc
out/qjs_intersection_change_event.cc
out/qjs_intersection_change_event_init.cc
out/qjs_touch.cc
Expand Down
2 changes: 2 additions & 0 deletions bridge/bindings/qjs/binding_initializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "qjs_input_event.h"
#include "qjs_intersection_change_event.h"
#include "qjs_keyboard_event.h"
#include "qjs_device_orientation_event.h"
#include "qjs_location.h"
#include "qjs_message_event.h"
#include "qjs_module_manager.h"
Expand Down Expand Up @@ -129,6 +130,7 @@ void InstallBindings(ExecutingContext* context) {
QJSTransitionEvent::Install(context);
QJSIntersectionChangeEvent::Install(context);
QJSKeyboardEvent::Install(context);
QJSDeviceOrientationEvent::Install(context);
QJSNode::Install(context);
QJSNodeList::Install(context);
QJSDocument::Install(context);
Expand Down
66 changes: 66 additions & 0 deletions bridge/core/events/device_orientation_event.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2024-present The WebF authors. All rights reserved.
*/

#include "device_orientation_event.h"
#include "qjs_device_orientation_event.h"

namespace webf {

DeviceOrientationEvent* DeviceOrientationEvent::Create(ExecutingContext* context,
const AtomicString& type,
ExceptionState& exception_state) {
return MakeGarbageCollected<DeviceOrientationEvent>(context, type, exception_state);
}

DeviceOrientationEvent* DeviceOrientationEvent::Create(ExecutingContext* context,
const AtomicString& type,
const std::shared_ptr<DeviceOrientationEventInit>& initializer,
ExceptionState& exception_state) {
return MakeGarbageCollected<DeviceOrientationEvent>(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<DeviceOrientationEventInit>& 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
11 changes: 11 additions & 0 deletions bridge/core/events/device_orientation_event.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Event} from "../dom/events/event";
import {GestureEventInit} 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;
}
55 changes: 55 additions & 0 deletions bridge/core/events/device_orientation_event.h
Original file line number Diff line number Diff line change
@@ -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<DeviceOrientationEventInit>& 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<DeviceOrientationEventInit>& 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_
10 changes: 10 additions & 0 deletions bridge/core/events/device_orientation_event_init.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 25 additions & 0 deletions webf/lib/src/dom/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<NativeType> toRaw([int extraLength = 0, bool isCustomEvent = false]) {
List<int> methods = [
doubleToUint64(alpha),
doubleToUint64(beta),
doubleToUint64(gamma)
];
Pointer<RawEvent> rawEvent = super.toRaw(methods.length).cast<RawEvent>();
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;
Expand Down

0 comments on commit 1acc5f4

Please sign in to comment.