pub fn add_event_listener<E: wasm_bindgen::JsCast>(
        &self,
        target: &EventTarget,
        event_name: &'static str,
        mut closure: impl FnMut(E, MutexGuard<'_, AppRunner>) + 'static,
    ) -> Result<(), JsValue> {
        use wasm_bindgen::JsCast;

        // Create a JS closure based on the FnMut provided
        let closure = Closure::wrap({
            // Clone atomics
            let runner_ref = self.runner.clone();
            let panicked = self.panicked.clone();

            Box::new(move |event: web_sys::Event| {
                // Only call the wrapped closure if the egui code has not panicked
                if !panicked.load(Ordering::SeqCst) {
                    // Cast the event to the expected event type
                    let event = event.unchecked_into::<E>();

                    closure(event, runner_ref.lock());
                }
            }) as Box<dyn FnMut(_)>
        });

        // Add the event listener to the target
        target.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;

        // Bypass closure drop so that event handler can call the closure
        closure.forget();

        Ok(())
    }
/// Convenience function to reduce boilerplate and ensure that all event handlers
/// are dealt with in the same way
Convenience function to reduce boilerplate and ensure that all event handlers
are dealt with in the same way
Convenience function to reduce boilerplate and ensure that all event handlers are dealt with in the same way
pub fn add_event_listener<E: wasm_bindgen::JsCast>(
        &self,
        target: &EventTarget,
        event_name: &'static str,
        mut closure: impl FnMut(E, MutexGuard<'_, AppRunner>) + 'static,
    ) -> Result<(), JsValue> {
        use wasm_bindgen::JsCast;
        // Create a JS closure based on the FnMut provided
        let closure = Closure::wrap({
            // Clone atomics
            let runner_ref = self.runner.clone();
            let panicked = self.panicked.clone();
            Box::new(move |event: web_sys::Event| {
                // Only call the wrapped closure if the egui code has not panicked
                if !panicked.load(Ordering::SeqCst) {
                    // Cast the event to the expected event type
                    let event = event.unchecked_into::<E>();
                    closure(event, runner_ref.lock());
                }
            }) as Box<dyn FnMut(_)>
        });
        // Add the event listener to the target
        target.add_event_listener_with_callback(event_name, closure.as_ref().unchecked_ref())?;
        // Bypass closure drop so that event handler can call the closure
        closure.forget();
        Ok(())
    }
pub fn add_event_listener < E : wasm_bindgen :: JsCast > ( & self , target : & EventTarget , event_name : & ' static str , mut closure : impl FnMut ( E , MutexGuard < ' _ , AppRunner > ) + ' static , ) -> Result < ( ) , JsValue > { use wasm_bindgen :: JsCast ; // Create a JS closure based on the FnMut provided let closure = Closure :: wrap ( { // Clone atomics let runner_ref = self . runner . clone ( ) ; let panicked = self . panicked . clone ( ) ; Box :: new ( move | event : web_sys :: Event | { // Only call the wrapped closure if the egui code has not panicked if ! panicked . load ( Ordering :: SeqCst ) { // Cast the event to the expected event type let event = event . unchecked_into :: < E > ( ) ; closure ( event , runner_ref . lock ( ) ) ; } } ) as Box < dyn FnMut ( _ ) > } ) ; // Add the event listener to the target target . add_event_listener_with_callback ( event_name , closure . as_ref ( ) . unchecked_ref ( ) ) ? ; // Bypass closure drop so that event handler can call the closure closure . forget ( ) ; Ok ( ( ) ) }
Convenience function to reduce boilerplate and ensure that all event handlers
are dealt with in the same way
Convenience function to reduce boilerplate and ensure that all event handlers are dealt with in the same way
&self
target
&EventTarget
event_name
&'static str
closure
impl FnMut(E, MutexGuard<'_, AppRunner>) + 'static
&self
target
&EventTarget
event_name
&'static str
closure
impl FnMut(E, MutexGuard<'_, AppRunner>) + 'static