1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#![no_std] use callback::*; use js::*; pub fn get_element_by_id(id: &str) -> JSObject { lazy_static::lazy_static! { static ref FN: JSFunction= { register_function( "function(strPtr,strLen){ let el = document.getElementById(this.readUtf8FromMemory(strPtr,strLen)); return this.storeObject(el); }", ) };}; FN.invoke_2(id.as_ptr() as u32, id.len() as u32).into() } pub fn add_event_listener( dom: impl Into<f64>, event: &str, handler: impl FnMut(f64) -> () + Send + 'static, ) { let cb = create_callback_1(handler); lazy_static::lazy_static! { static ref FN: JSFunction= { register_function( "function(el,strPtr,strLen,callback){ el = this.getObject(el); const event = this.readUtf8FromMemory(strPtr,strLen); el.addEventListener(event,this.createCallback(callback)); }", ) };}; FN.invoke_4(dom.into(), event.as_ptr() as u32, event.len() as u32, cb); } pub struct KeyDownEvent { pub handle: JSObject, } impl KeyDownEvent { pub fn from_event(ev: impl Into<JSObject>) -> KeyDownEvent { KeyDownEvent { handle: ev.into() } } pub fn key_code(&self) -> u32 { lazy_static::lazy_static! { static ref FN: JSFunction= { register_function( "function(ev){ ev = this.getObject(ev); return ev.keyCode; }", ) };}; FN.invoke_1(&self.handle) as u32 } }