Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

5.0 KB
Raw Blame
use super::*;
use crate::webidl;

const EVENT_MODIFIERS_SLOT: &str = "__moliEventModifiers";
const MODIFIER_KEYS: [&str; 14] = [
    "Alt",
    "Control",
    "Meta",
    "AltGraph",
    "CapsLock",
    "Fn",
    "FnLock",
    "Hyper",
    "NumLock",
    "ScrollLock",
    "Super",
    "Symbol",
    "SymbolLock",
    "Shift",
];

/// The UIEventInit ancestor is converted before the modifier members.
#[derive(Default, webidl::WebIdlDictionary)]
#[webidl(prefix = "EventModifierInit")]
pub(super) struct EventModifierInit<'s> {
    #[webidl(inherit)]
    ui: super::ui::UiEventInit<'s>,
    #[webidl(default = false)]
    alt_key: bool,
    #[webidl(default = false)]
    ctrl_key: bool,
    #[webidl(default = false)]
    meta_key: bool,
    #[webidl(default = false)]
    modifier_alt_graph: bool,
    #[webidl(default = false)]
    modifier_caps_lock: bool,
    #[webidl(default = false)]
    modifier_fn: bool,
    #[webidl(default = false)]
    modifier_fn_lock: bool,
    #[webidl(default = false)]
    modifier_hyper: bool,
    #[webidl(default = false)]
    modifier_num_lock: bool,
    #[webidl(default = false)]
    modifier_scroll_lock: bool,
    #[webidl(default = false)]
    modifier_super: bool,
    #[webidl(default = false)]
    modifier_symbol: bool,
    #[webidl(default = false)]
    modifier_symbol_lock: bool,
    #[webidl(default = false)]
    shift_key: bool,
}

#[derive(WebApiObject)]
#[webapi(plain, data_properties, enumerable)]
struct EventModifierProperties {
    alt_key: bool,
    ctrl_key: bool,
    meta_key: bool,
    shift_key: bool,
}

impl<'s> EventModifierInit<'s> {
    pub(super) fn event_flags(&self) -> (bool, bool, bool) {
        self.ui.event_flags()
    }

    pub(super) fn initialize(
        &self,
        scope: &mut v8::PinScope<'s, '_>,
        event: v8::Local<'s, v8::Object>,
    ) {
        self.ui.initialize(scope, event);
        self.initialize_modifiers(scope, event);
    }

    // TouchEvent keeps its key accessors in private state. Installing the own
    // modifier properties used by Mouse/Keyboard events would change its shape.
    pub(super) fn initialize_touch_event_base(
        &self,
        scope: &mut v8::PinScope<'s, '_>,
        event: v8::Local<'s, v8::Object>,
    ) -> (bool, bool, bool, bool) {
        self.ui.initialize(scope, event);
        (self.alt_key, self.meta_key, self.ctrl_key, self.shift_key)
    }

    fn initialize_modifiers(
        &self,
        scope: &mut v8::PinScope<'s, '_>,
        event: v8::Local<'s, v8::Object>,
    ) {
        let event = event_backing(scope, event);
        let values = [
            self.alt_key,
            self.ctrl_key,
            self.meta_key,
            self.modifier_alt_graph,
            self.modifier_caps_lock,
            self.modifier_fn,
            self.modifier_fn_lock,
            self.modifier_hyper,
            self.modifier_num_lock,
            self.modifier_scroll_lock,
            self.modifier_super,
            self.modifier_symbol,
            self.modifier_symbol_lock,
            self.shift_key,
        ];
        let bits = values
            .into_iter()
            .enumerate()
            .fold(0, |bits, (index, active)| {
                bits | (u32::from(active) << index)
            });
        set_private_value(
            scope,
            event,
            EVENT_MODIFIERS_SLOT,
            v8::Integer::new_from_unsigned(scope, bits).into(),
        );
        EventModifierProperties::new(self.alt_key, self.ctrl_key, self.meta_key, self.shift_key)
            .initialize(scope, event)
            .expect("event modifier properties should initialize");
    }
}

pub(in crate::context_bootstrap) fn initialize_legacy_event_modifiers<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    event: v8::Local<'s, v8::Object>,
    ctrl_key: bool,
    alt_key: bool,
    shift_key: bool,
    meta_key: bool,
) {
    EventModifierInit {
        ctrl_key,
        alt_key,
        shift_key,
        meta_key,
        ..Default::default()
    }
    .initialize_modifiers(scope, event);
}

#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "getModifierState")]
struct GetModifierStateArgs {
    #[webidl(required)]
    key_arg: String,
}

pub(in crate::context_bootstrap) fn event_get_modifier_state_callback<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(parsed) = webidl::parse_args::<GetModifierStateArgs>(scope, &args) else {
        return;
    };
    // Retain the legacy accelerator alias for the native platform.
    let key = match parsed.key_arg.as_str() {
        "Accel" if cfg!(target_os = "macos") => "Meta",
        "Accel" => "Control",
        key => key,
    };
    let bits = event_private_value(scope, args.this(), EVENT_MODIFIERS_SLOT)
        .and_then(|value| value.uint32_value(scope))
        .unwrap_or_default();
    let active = MODIFIER_KEYS
        .iter()
        .position(|name| *name == key)
        .is_some_and(|index| bits & (1 << index) != 0);
    rv.set(v8::Boolean::new(scope, active).into());
}