Repository: DioxusLabs/dioxus
File: packages/desktop/src/hooks.rs (Rust)

Code before:
    // move the runtime into the event handler closure
    let runtime = Runtime::current().unwrap();

    use_hook_with_cleanup(
        move || {
            window().create_wry_event_handler(move |event, target| {
                let _runtime_guard = RuntimeGuard::new(runtime.clone());
                handler(event, target)
            })
        },
        move |handler| handler.remove(),
    )
}

/// Register an event handler that runs when a muda event is processed.
#[cfg_attr(
    docsrs,
    doc(cfg(any(target_os = "windows", target_os = "linux", target_os = "macos")))
)]
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
pub fn use_muda_event_handler(
    mut handler: impl FnMut(&muda::MenuEvent) + 'static,
) -> WryEventHandler {
    use dioxus_core::prelude::current_scope_id;

    // Capture the current runtime and scope ID.
    let runtime = Runtime::current().unwrap();
    let scope_id = current_scope_id().unwrap();

    use_wry_event_handler(move |event, _| {
        runtime.on_scope(scope_id, || {
            if let Event::UserEvent(UserWindowEvent::MudaMenuEvent(event)) = event {
                handler(event);
            }
        });
    })
}

/// Provide a callback to handle asset loading yourself.
///
/// The callback takes a path as requested by the web view, and it should return `Some(response)`
/// if you want to load the asset, and `None` if you want to fallback on the default behavior.
pub fn use_asset_handler(
    name: &str,
    mut handler: impl FnMut(AssetRequest, RequestAsyncResponder) + 'static,
) {
    // wrap the user's handler in something that keeps it up to date
    let cb = use_callback(move |(asset, responder)| handler(asset, responder));

    use_hook_with_cleanup(
        || {

Review comment (suggestion):
Instead of patching use_muda_event_handler, can you apply this patch to use_wry_event_handler so that both carry the scope with them. If the use_muda_event_handler closure has the runtime and scope, then use_wry_event_handler closure should inherit those fixes

Code after:
    use dioxus_core::prelude::current_scope_id;

    // Capture the current runtime and scope ID.
    let runtime = Runtime::current().unwrap();
    let scope_id = current_scope_id().unwrap();

    use_hook_with_cleanup(
        move || {
            window().create_wry_event_handler(move |event, target| {
                runtime.on_scope(scope_id, || handler(event, target))
            })
        },
        move |handler| handler.remove(),
    )
}

/// Register an event handler that runs when a muda event is processed.
#[cfg_attr(
    docsrs,
    doc(cfg(any(target_os = "windows", target_os = "linux", target_os = "macos")))
)]
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
pub fn use_muda_event_handler(
    mut handler: impl FnMut(&muda::MenuEvent) + 'static,
) -> WryEventHandler {
    use_wry_event_handler(move |event, _| {
        if let Event::UserEvent(UserWindowEvent::MudaMenuEvent(event)) = event {
            handler(event);
        }
    })
}

/// Provide a callback to handle asset loading yourself.
///
/// The callback takes a path as requested by the web view, and it should return `Some(response)`
/// if you want to load the asset, and `None` if you want to fallback on the default behavior.
pub fn use_asset_handler(
    name: &str,
    mut handler: impl FnMut(AssetRequest, RequestAsyncResponder) + 'static,
) {
    // wrap the user's handler in something that keeps it up to date
    let cb = use_callback(move |(asset, responder)| handler(asset, responder));

    use_hook_with_cleanup(
        || {
            crate::window()
                .asset_handlers
                .register_handler(name.to_string(), cb);

            Rc::new(name.to_string())
        },