mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 08:02:59 +00:00
Move event initialization, WebAssembly bindings, Fetch values, interface constants and WebIDL conversions into moli-webapi. Reuse existing native owners for dispatch, resources and realm installation.
158 lines
4.6 KiB
Rust
158 lines
4.6 KiB
Rust
use moli_webidl as webidl;
|
|
|
|
#[derive(webidl::WebIdlEnum)]
|
|
#[webidl(name = "ScrollBehavior", rename_all = "kebab-case")]
|
|
enum ScrollBehavior {
|
|
Auto,
|
|
Instant,
|
|
Smooth,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlDictionary)]
|
|
#[webidl(prefix = "ScrollOptions")]
|
|
struct ScrollOptions {
|
|
// The native scroll algorithm retains its existing immediate-scroll policy.
|
|
#[webidl(name = "behavior", converter = "enum", default = ScrollBehavior::Auto)]
|
|
_behavior: ScrollBehavior,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlDictionary)]
|
|
#[webidl(prefix = "ScrollToOptions")]
|
|
struct ScrollToOptions {
|
|
#[webidl(inherit)]
|
|
_base: ScrollOptions,
|
|
#[webidl(converter = "unrestricted_double")]
|
|
left: Option<f64>,
|
|
#[webidl(converter = "unrestricted_double")]
|
|
top: Option<f64>,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlArgs)]
|
|
#[webidl(prefix = "scroll")]
|
|
struct ScrollToArgs {
|
|
#[webidl(dictionary)]
|
|
options: ScrollToOptions,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlArgs)]
|
|
#[webidl(prefix = "scroll")]
|
|
struct ScrollNumericArgs {
|
|
#[webidl(required, converter = "unrestricted_double")]
|
|
x: f64,
|
|
#[webidl(required, converter = "unrestricted_double")]
|
|
y: f64,
|
|
}
|
|
|
|
pub struct ScrollCoordinates {
|
|
left: Option<f64>,
|
|
top: Option<f64>,
|
|
}
|
|
|
|
impl ScrollCoordinates {
|
|
pub fn resolve(self, current_x: f64, current_y: f64, relative: bool) -> (f64, f64) {
|
|
let normalize = |value: f64| if value.is_finite() { value } else { 0.0 };
|
|
let left = self.left.map(normalize);
|
|
let top = self.top.map(normalize);
|
|
if relative {
|
|
(
|
|
current_x + left.unwrap_or(0.0),
|
|
current_y + top.unwrap_or(0.0),
|
|
)
|
|
} else {
|
|
(left.unwrap_or(current_x), top.unwrap_or(current_y))
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn coordinates<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: &v8::FunctionCallbackArguments<'s>,
|
|
) -> Option<ScrollCoordinates> {
|
|
// The legacy scroll bindings silently cancel failed argument conversion.
|
|
// Keep the catch here so native layout failures remain visible to callers.
|
|
let try_catch = std::pin::pin!(v8::TryCatch::new(scope));
|
|
let mut scope = try_catch.init();
|
|
let parsed = if args.length() >= 2 {
|
|
webidl::try_parse_args::<ScrollNumericArgs>(&mut scope, args).map(|args| {
|
|
ScrollCoordinates {
|
|
left: Some(args.x),
|
|
top: Some(args.y),
|
|
}
|
|
})
|
|
} else {
|
|
webidl::try_parse_args::<ScrollToArgs>(&mut scope, args).map(|args| ScrollCoordinates {
|
|
left: args.options.left,
|
|
top: args.options.top,
|
|
})
|
|
};
|
|
if scope.has_caught() {
|
|
scope.reset();
|
|
return None;
|
|
}
|
|
parsed.ok()
|
|
}
|
|
|
|
#[derive(Clone, Copy, webidl::WebIdlEnum)]
|
|
#[webidl(name = "ScrollLogicalPosition", rename_all = "kebab-case")]
|
|
pub enum ScrollLogicalPosition {
|
|
Start,
|
|
Center,
|
|
End,
|
|
Nearest,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlEnum)]
|
|
#[webidl(name = "ScrollIntoViewContainer", rename_all = "kebab-case")]
|
|
enum ScrollIntoViewContainer {
|
|
All,
|
|
Nearest,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlDictionary)]
|
|
#[webidl(prefix = "ScrollIntoViewOptions")]
|
|
struct ScrollIntoViewOptions {
|
|
#[webidl(inherit)]
|
|
_base: ScrollOptions,
|
|
#[webidl(converter = "enum", default = ScrollLogicalPosition::Start)]
|
|
block: ScrollLogicalPosition,
|
|
// The native bubbling algorithm retains its existing all-containers policy.
|
|
#[webidl(name = "container", converter = "enum", default = ScrollIntoViewContainer::All)]
|
|
_container: ScrollIntoViewContainer,
|
|
#[webidl(converter = "enum", default = ScrollLogicalPosition::Nearest)]
|
|
inline: ScrollLogicalPosition,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlArgs)]
|
|
#[webidl(prefix = "Element.scrollIntoView")]
|
|
struct ScrollIntoViewArgs {
|
|
#[webidl(dictionary)]
|
|
options: ScrollIntoViewOptions,
|
|
}
|
|
|
|
pub fn into_view_alignments<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: &v8::FunctionCallbackArguments<'s>,
|
|
) -> Option<(ScrollLogicalPosition, ScrollLogicalPosition)> {
|
|
let value = args.get(0);
|
|
if !value.is_null_or_undefined() && !value.is_object() {
|
|
return Some((
|
|
ScrollLogicalPosition::Nearest,
|
|
if value.boolean_value(scope) {
|
|
ScrollLogicalPosition::Start
|
|
} else {
|
|
ScrollLogicalPosition::End
|
|
},
|
|
));
|
|
}
|
|
let try_catch = std::pin::pin!(v8::TryCatch::new(scope));
|
|
let mut scope = try_catch.init();
|
|
let parsed = webidl::try_parse_args::<ScrollIntoViewArgs>(&mut scope, args);
|
|
if scope.has_caught() {
|
|
scope.reset();
|
|
return None;
|
|
}
|
|
parsed
|
|
.ok()
|
|
.map(|args| (args.options.inline, args.options.block))
|
|
}
|