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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
#![no_std] use js::*; extern crate alloc; use alloc::boxed::Box; use alloc::string::String; use web_common::*; pub fn get_element_by_id(id: &str) -> JSObject { js!("function(strPtr,strLen){ let el = document.getElementById(this.readUtf8FromMemory(strPtr,strLen)); return this.storeObject(el); }") .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); js!("function(el,strPtr,strLen,callback){ el = this.getObject(el); const event = this.readUtf8FromMemory(strPtr,strLen); el.addEventListener(event,this.createCallback(callback)); }") .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 { js!("function(ev){ ev = this.getObject(ev); return ev.keyCode; }") .invoke_1(&self.handle) as u32 } } pub fn attach_shadow(el: impl Into<f64>, open: bool) -> JSObject { js!(r#"function(el,open){ el = this.getObject(el); el.attachShadow({mode:open?"open":"closed"}); return this.storeObject(el.shadowRoot); }"#) .invoke_2(el.into(), if open { 1.0 } else { 0.0 }) .into() } pub fn set_inner_html(el: impl Into<f64>, html: &str) { js!(r#"function(el,strPtr,strLen){ el = this.getObject(el); el.innerHTML = this.readUtf8FromMemory(strPtr,strLen); }"#) .invoke_3(el.into(), html.as_ptr() as u32, html.len() as u32); } pub fn get_attribute(el: impl Into<f64>, name: &str) -> Option<alloc::string::String> { let attr = js!(r#"function(el,strPtr,strLen){ el = this.getObject(el); const a = el.getAttribute(this.readUtf8FromMemory(strPtr,strLen)); if(a === null){ return -1; } return this.writeCStringToMemory(a); }"#) .invoke_3(el.into(), name.as_ptr() as u32, name.len() as u32); if attr == -1.0 { return None; } else { Some(cstr_to_string(attr as i32)) } } pub struct KeyEventHandler { pub handler: Option<Box<dyn Sync + FnMut(KeyEvent) + 'static + Send>>, } impl KeyEventHandler { pub fn new(f: impl Sync + FnMut(KeyEvent) + 'static + Send) -> KeyEventHandler { KeyEventHandler { handler: Some(Box::new(f)), } } } pub struct KeyEvent { obj: JSObject, } impl KeyEvent { pub fn new(o: f64) -> KeyEvent { KeyEvent { obj: JSObject::from(o), } } pub fn key_code(&self) -> usize { js!("function(o){ return this.getObject(o).keyCode; }") .invoke_1(self.obj.handle) as usize } pub fn target(&self) -> JSObject { let r = js!("function(o){ return this.storeObject(this.getObject(o).target); }") .invoke_1(self.obj.handle); JSObject::from(r) } } pub struct InputElement { obj: JSObject, } impl InputElement { pub fn new(o: f64) -> InputElement { InputElement { obj: JSObject::from(o), } } pub fn from(o: JSObject) -> InputElement { InputElement { obj: o } } pub fn value(&self) -> Option<String> { get_property(&self.obj, "value") } pub fn set_value(&mut self, s: &str) { set_property(&self.obj, "value", s) } } pub struct MouseEventHandler { pub handler: Option<Box<dyn Sync + FnMut(MouseEvent) + 'static + Send>>, } impl MouseEventHandler { pub fn new(f: impl Sync + FnMut(MouseEvent) + 'static + Send) -> MouseEventHandler { MouseEventHandler { handler: Some(Box::new(f)), } } } pub struct MouseEvent { obj: JSObject, } impl MouseEvent { pub fn new(o: f64) -> MouseEvent { MouseEvent { obj: JSObject::from(o), } } pub fn target(&self) -> JSObject { let r = js!("function(o){ return this.storeObject(this.getObject(o).target); }") .invoke_1(self.obj.handle); JSObject::from(r) } } pub struct EventHandler { pub handler: Option<Box<dyn Sync + FnMut(Event) + 'static + Send>>, } impl EventHandler { pub fn new(f: impl Sync + FnMut(Event) + 'static + Send) -> EventHandler { EventHandler { handler: Some(Box::new(f)), } } } pub struct Event { obj: JSObject, } impl Event { pub fn new(o: f64) -> Event { Event { obj: JSObject::from(o), } } pub fn target(&self) -> JSObject { get_object(self.obj.handle) } }