macro_rules! ui_elements {
{$($element:ident : $base_ty:ty => $transform_ty:ty),* $(,)?} => { ... };
}Expand description
UI macro: define UI elements.
This macro is used as a convenience to define a struct called Elements
that contains all the UI HTML elements. A constructor
Elements::new(document: &Document) -> Result<Elements, JsValue> is also
defined by this macro.
Each member in Elements is defined by its HTML id (which also gives the
name of the member), the web_sys type of the corresponding HTML element,
and the type to which it is transformed as a member of Elements. The
latter is either an InputElement or an
Rc wrapping the HTML element type. See the example below
for details about the syntax.
ยงExample
use maia_wasm::{ui::input::{CheckboxInput, MHzPresentation, NumberInput},
ui_elements};
use std::rc::Rc;
use web_sys::{Document, HtmlButtonElement, HtmlInputElement};
ui_elements! {
my_checkbox: HtmlInputElement => CheckboxInput,
my_button: HtmlButtonElement => Rc<HtmlButtonElement>,
my_frequency: HtmlInputElement => NumberInput<f32, MHzPresentation>,
}
fn main() -> Result<(), wasm_bindgen::JsValue> {
let (_, document) = maia_wasm::get_window_and_document()?;
let elements = Elements::new(&document)?;
// elements.my_checkbox is a CheckboxInput
// elements.my_button is an Rc<HtmlButtonElement>
// etc.
Ok(())
}