use std::rc::Rc;
use crate::{
assets::*, ipc::UserWindowEvent, shortcut::IntoAccelerator, window, DesktopContext,
ShortcutHandle, ShortcutRegistryError, WryEventHandler,
};
use dioxus_core::{
prelude::{consume_context, use_hook_with_cleanup},
use_hook, Runtime,
};
use dioxus_hooks::use_callback;
use tao::{event::Event, event_loop::EventLoopWindowTarget};
use wry::RequestAsyncResponder;
pub fn use_window() -> DesktopContext {
use_hook(consume_context::<DesktopContext>)
}
pub fn use_wry_event_handler(
mut handler: impl FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
) -> WryEventHandler {
use dioxus_core::prelude::current_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(),
)
}
#[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);
}
})
}
#[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_tray_menu_event_handler(
mut handler: impl FnMut(&tray_icon::menu::MenuEvent) + 'static,
) -> WryEventHandler {
use_wry_event_handler(move |event, _| {
if let Event::UserEvent(UserWindowEvent::TrayMenuEvent(event)) = event {
handler(event);
}
})
}
#[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_tray_icon_event_handler(
mut handler: impl FnMut(&tray_icon::TrayIconEvent) + 'static,
) -> WryEventHandler {
use_wry_event_handler(move |event, _| {
if let Event::UserEvent(UserWindowEvent::TrayIconEvent(event)) = event {
handler(event);
}
})
}
pub fn use_asset_handler(
name: &str,
mut handler: impl FnMut(AssetRequest, RequestAsyncResponder) + 'static,
) {
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())
},
move |name| {
_ = crate::window().asset_handlers.remove_handler(name.as_ref());
},
);
}
pub fn use_global_shortcut(
accelerator: impl IntoAccelerator,
mut handler: impl FnMut() + 'static,
) -> Result<ShortcutHandle, ShortcutRegistryError> {
let cb = use_callback(move |_| handler());
use_hook_with_cleanup(
move || window().create_shortcut(accelerator.accelerator(), move || cb(())),
|handle| {
if let Ok(handle) = handle {
handle.remove();
}
},
)
}