Skip to content

Commit

Permalink
WIP: add event constructor codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
ShinyRaine committed Feb 8, 2025
1 parent 57d23c8 commit 3cda47c
Show file tree
Hide file tree
Showing 31 changed files with 609 additions and 178 deletions.
2 changes: 1 addition & 1 deletion bridge/core/dom/events/event.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ interface Event {

[key: string]: any;

new(type: string, options?: EventInit) : Event;
new(type: string, init?: EventInit) : Event;
}
23 changes: 11 additions & 12 deletions bridge/include/plugin_api/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ class Event;
typedef struct ScriptValueRef ScriptValueRef;
enum class EventType {
kEvent = 0,
kCustomEvent = 1,
kAnimationEvent = 2,
kCloseEvent = 3,
kGestureEvent = 4,
kHashchangeEvent = 5,
kIntersectionChangeEvent = 6,
kTransitionEvent = 7,
kUIEvent = 8,
kFocusEvent = 9,
kInputEvent = 10,
kMouseEvent = 11,
kPointerEvent = 12,
kAnimationEvent = 1,
kCloseEvent = 2,
kGestureEvent = 3,
kHashchangeEvent = 4,
kIntersectionChangeEvent = 5,
kTransitionEvent = 6,
kUIEvent = 7,
kFocusEvent = 8,
kInputEvent = 9,
kMouseEvent = 10,
kPointerEvent = 11,
};
using PublicEventGetBubbles = int32_t (*)(Event*);
using PublicEventGetCancelBubble = int32_t (*)(Event*);
Expand Down
142 changes: 142 additions & 0 deletions bridge/include/plugin_api/executing_context.h

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions bridge/include/plugin_api/focus_event_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ struct WebFFocusEventInit {
double detail;
WebFValue<Window, WindowPublicMethods> view;
double which;
int32_t bubbles;
int32_t cancelable;
int32_t composed;
WebFValue<EventTarget, EventTargetPublicMethods> related_target;
};
} // namespace webf
Expand Down
3 changes: 3 additions & 0 deletions bridge/include/plugin_api/input_event_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct WebFInputEventInit {
double detail;
WebFValue<Window, WindowPublicMethods> view;
double which;
int32_t bubbles;
int32_t cancelable;
int32_t composed;
const char* input_type;
const char* data;
};
Expand Down
3 changes: 3 additions & 0 deletions bridge/include/plugin_api/intersection_change_event_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct WebFIntersectionChangeEventInit {
double detail;
WebFValue<Window, WindowPublicMethods> view;
double which;
int32_t bubbles;
int32_t cancelable;
int32_t composed;
double intersection_ratio;
};
} // namespace webf
Expand Down
3 changes: 3 additions & 0 deletions bridge/include/plugin_api/keyboard_event_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct WebFKeyboardEventInit {
double detail;
WebFValue<Window, WindowPublicMethods> view;
double which;
int32_t bubbles;
int32_t cancelable;
int32_t composed;
int32_t alt_key;
double char_code;
const char* code;
Expand Down
3 changes: 3 additions & 0 deletions bridge/include/plugin_api/mouse_event_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct WebFMouseEventInit {
double detail;
WebFValue<Window, WindowPublicMethods> view;
double which;
int32_t bubbles;
int32_t cancelable;
int32_t composed;
};
} // namespace webf
#endif // WEBF_CORE_WEBF_API_PLUGIN_API_MOUSE_EVENT_INIT_H_
8 changes: 8 additions & 0 deletions bridge/include/plugin_api/pointer_event_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
#include <stdint.h>
#include "webf_value.h"
namespace webf {
typedef struct Window Window;
typedef struct WindowPublicMethods WindowPublicMethods;
struct WebFPointerEventInit {
double detail;
WebFValue<Window, WindowPublicMethods> view;
double which;
int32_t bubbles;
int32_t cancelable;
int32_t composed;
int32_t is_primary;
double pointer_id;
const char* pointer_type;
Expand Down
120 changes: 0 additions & 120 deletions bridge/rusty_webf_sys/src/dom/events/custom_event.rs

This file was deleted.

55 changes: 33 additions & 22 deletions bridge/rusty_webf_sys/src/dom/events/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ use crate::*;
#[repr(C)]
enum EventType {
Event = 0,
CustomEvent = 1,
AnimationEvent = 2,
CloseEvent = 3,
GestureEvent = 4,
HashchangeEvent = 5,
IntersectionChangeEvent = 6,
TransitionEvent = 7,
UIEvent = 8,
FocusEvent = 9,
InputEvent = 10,
MouseEvent = 11,
PointerEvent = 12,
AnimationEvent = 1,
CloseEvent = 2,
GestureEvent = 3,
HashchangeEvent = 4,
IntersectionChangeEvent = 5,
TransitionEvent = 6,
UIEvent = 7,
FocusEvent = 8,
InputEvent = 9,
MouseEvent = 10,
PointerEvent = 11,
}
#[repr(C)]
pub struct EventRustMethods {
Expand Down Expand Up @@ -171,16 +170,6 @@ impl Event {
}
Ok(())
}
pub fn as_custom_event(&self) -> Result<CustomEvent, &str> {
let raw_ptr = unsafe {
assert!(!(*((*self).status)).disposed, "The underline C++ impl of this ptr({:?}) had been disposed", (self.method_pointer));
((*self.method_pointer).dynamic_to)(self.ptr, EventType::CustomEvent)
};
if (raw_ptr.value == std::ptr::null()) {
return Err("The type value of Event does not belong to the CustomEvent type.");
}
Ok(CustomEvent::initialize(raw_ptr.value, self.context, raw_ptr.method_pointer as *const CustomEventRustMethods, raw_ptr.status))
}
pub fn as_animation_event(&self) -> Result<AnimationEvent, &str> {
let raw_ptr = unsafe {
assert!(!(*((*self).status)).disposed, "The underline C++ impl of this ptr({:?}) had been disposed", (self.method_pointer));
Expand Down Expand Up @@ -367,3 +356,25 @@ impl EventMethods for Event {
self
}
}
impl ExecutingContext {
pub fn create_event(&self, event_type: &str, exception_state: &ExceptionState) -> Result<Event, String> {
let event_type_c_string = CString::new(event_type).unwrap();
let new_event = unsafe {
((*self.method_pointer).create_event)(self.ptr, event_type_c_string.as_ptr(), exception_state.ptr)
};
if exception_state.has_exception() {
return Err(exception_state.stringify(self));
}
return Ok(Event::initialize(new_event.value, self, new_event.method_pointer, new_event.status));
}
pub fn create_event_with_options(&self, event_type: &str, options: &EventInit, exception_state: &ExceptionState) -> Result<Event, String> {
let event_type_c_string = CString::new(event_type).unwrap();
let new_event = unsafe {
((*self.method_pointer).create_event_with_options)(self.ptr, event_type_c_string.as_ptr(), options, exception_state.ptr)
};
if exception_state.has_exception() {
return Err(exception_state.stringify(self));
}
return Ok(Event::initialize(new_event.value, self, new_event.method_pointer, new_event.status));
}
}
2 changes: 0 additions & 2 deletions bridge/rusty_webf_sys/src/dom/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
* Copyright (C) 2022-present The WebF authors. All rights reserved.
*/
pub mod add_event_listener_options;
pub mod custom_event;
pub mod event_init;
pub mod event_listener_options;
pub mod event_target;
pub mod event;

pub use add_event_listener_options::*;
pub use custom_event::*;
pub use event_init::*;
pub use event_listener_options::*;
pub use event_target::*;
Expand Down
22 changes: 22 additions & 0 deletions bridge/rusty_webf_sys/src/events/animation_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,25 @@ impl EventMethods for AnimationEvent {
&self.event
}
}
impl ExecutingContext {
pub fn create_animation_event(&self, event_type: &str, exception_state: &ExceptionState) -> Result<AnimationEvent, String> {
let event_type_c_string = CString::new(event_type).unwrap();
let new_event = unsafe {
((*self.method_pointer).create_animation_event)(self.ptr, event_type_c_string.as_ptr(), exception_state.ptr)
};
if exception_state.has_exception() {
return Err(exception_state.stringify(self));
}
return Ok(AnimationEvent::initialize(new_event.value, self, new_event.method_pointer, new_event.status));
}
pub fn create_animation_event_with_options(&self, event_type: &str, options: &AnimationEventInit, exception_state: &ExceptionState) -> Result<AnimationEvent, String> {
let event_type_c_string = CString::new(event_type).unwrap();
let new_event = unsafe {
((*self.method_pointer).create_animation_event_with_options)(self.ptr, event_type_c_string.as_ptr(), options, exception_state.ptr)
};
if exception_state.has_exception() {
return Err(exception_state.stringify(self));
}
return Ok(AnimationEvent::initialize(new_event.value, self, new_event.method_pointer, new_event.status));
}
}
22 changes: 22 additions & 0 deletions bridge/rusty_webf_sys/src/events/close_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,25 @@ impl EventMethods for CloseEvent {
&self.event
}
}
impl ExecutingContext {
pub fn create_close_event(&self, event_type: &str, exception_state: &ExceptionState) -> Result<CloseEvent, String> {
let event_type_c_string = CString::new(event_type).unwrap();
let new_event = unsafe {
((*self.method_pointer).create_close_event)(self.ptr, event_type_c_string.as_ptr(), exception_state.ptr)
};
if exception_state.has_exception() {
return Err(exception_state.stringify(self));
}
return Ok(CloseEvent::initialize(new_event.value, self, new_event.method_pointer, new_event.status));
}
pub fn create_close_event_with_options(&self, event_type: &str, options: &CloseEventInit, exception_state: &ExceptionState) -> Result<CloseEvent, String> {
let event_type_c_string = CString::new(event_type).unwrap();
let new_event = unsafe {
((*self.method_pointer).create_close_event_with_options)(self.ptr, event_type_c_string.as_ptr(), options, exception_state.ptr)
};
if exception_state.has_exception() {
return Err(exception_state.stringify(self));
}
return Ok(CloseEvent::initialize(new_event.value, self, new_event.method_pointer, new_event.status));
}
}
22 changes: 22 additions & 0 deletions bridge/rusty_webf_sys/src/events/focus_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,25 @@ impl EventMethods for FocusEvent {
&self.ui_event.event
}
}
impl ExecutingContext {
pub fn create_focus_event(&self, event_type: &str, exception_state: &ExceptionState) -> Result<FocusEvent, String> {
let event_type_c_string = CString::new(event_type).unwrap();
let new_event = unsafe {
((*self.method_pointer).create_focus_event)(self.ptr, event_type_c_string.as_ptr(), exception_state.ptr)
};
if exception_state.has_exception() {
return Err(exception_state.stringify(self));
}
return Ok(FocusEvent::initialize(new_event.value, self, new_event.method_pointer, new_event.status));
}
pub fn create_focus_event_with_options(&self, event_type: &str, options: &FocusEventInit, exception_state: &ExceptionState) -> Result<FocusEvent, String> {
let event_type_c_string = CString::new(event_type).unwrap();
let new_event = unsafe {
((*self.method_pointer).create_focus_event_with_options)(self.ptr, event_type_c_string.as_ptr(), options, exception_state.ptr)
};
if exception_state.has_exception() {
return Err(exception_state.stringify(self));
}
return Ok(FocusEvent::initialize(new_event.value, self, new_event.method_pointer, new_event.status));
}
}
Loading

0 comments on commit 3cda47c

Please sign in to comment.