cfg out globalhotkey

This commit is contained in:
Jonathan Kelley 2024-03-06 16:42:45 -08:00
parent f7f416ee3a
commit 81f38a0bc5
No known key found for this signature in database
GPG key ID: 1FBB50F7EB0A08BE
11 changed files with 37 additions and 26 deletions

4
Cargo.lock generated
View file

@ -4055,9 +4055,9 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]] [[package]]
name = "global-hotkey" name = "global-hotkey"
version = "0.4.2" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "927a00fd7c31d82029f99ce2481a8de1ae974758017d6a55ebbe7f22edcd1617" checksum = "34300b13d16b1593de1b6bd571a376704820a1e6f6fe57be2f106ded8d164030"
dependencies = [ dependencies = [
"crossbeam-channel", "crossbeam-channel",
"keyboard-types", "keyboard-types",

View file

@ -20,6 +20,7 @@ struct UploadedFile {
fn app() -> Element { fn app() -> Element {
let mut enable_directory_upload = use_signal(|| false); let mut enable_directory_upload = use_signal(|| false);
let mut files_uploaded = use_signal(|| Vec::new() as Vec<UploadedFile>); let mut files_uploaded = use_signal(|| Vec::new() as Vec<UploadedFile>);
let mut hovered = use_signal(|| false);
let read_files = move |file_engine: Arc<dyn FileEngine>| async move { let read_files = move |file_engine: Arc<dyn FileEngine>| async move {
let files = file_engine.files(); let files = file_engine.files();
@ -39,18 +40,12 @@ fn app() -> Element {
} }
}; };
let handle_file_drop = move |evt: DragEvent| async move {
if let Some(file_engine) = evt.files() {
read_files(file_engine).await;
}
};
rsx! { rsx! {
style { {include_str!("./assets/file_upload.css")} } style { {include_str!("./assets/file_upload.css")} }
h1 { "File Upload Example" } h1 { "File Upload Example" }
p { "Drop a .txt, .rs, or .js file here to read it" } p { "Drop a .txt, .rs, or .js file here to read it" }
button { onclick: move |_| files_uploaded.write().clear(), "Clear files" }
div { div {
label { r#for: "directory-upload", "Enable directory upload" } label { r#for: "directory-upload", "Enable directory upload" }
@ -75,14 +70,17 @@ fn app() -> Element {
} }
div { div {
// cheating with a little bit of JS...
"ondragover": "this.style.backgroundColor='#88FF88';",
"ondragleave": "this.style.backgroundColor='#FFFFFF';",
"ondrop": "this.style.backgroundColor='#FFFFFF';",
id: "drop-zone", id: "drop-zone",
prevent_default: "ondragover", prevent_default: "ondragover ondrop",
prevent_default: "ondrop", background_color: if hovered() { "lightblue" } else { "lightgray" },
ondrop: handle_file_drop, ondragover: move |_| hovered.set(true),
ondragleave: move |_| hovered.set(false),
ondrop: move |evt| async move {
hovered.set(false);
if let Some(file_engine) = evt.files() {
read_files(file_engine).await;
}
},
"Drop files here" "Drop files here"
} }

View file

@ -52,8 +52,8 @@ async-trait = "0.1.68"
tao = { version = "0.24.0", features = ["rwh_05"] } tao = { version = "0.24.0", features = ["rwh_05"] }
[target.'cfg(any(target_os = "windows",target_os = "macos",target_os = "linux",target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))'.dependencies] [target.'cfg(any(target_os = "windows",target_os = "macos",target_os = "linux",target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
global-hotkey = "0.5.0"
rfd = "0.12" rfd = "0.12"
global-hotkey = "0.4.1"
muda = "0.11.3" muda = "0.11.3"
[target.'cfg(target_os = "ios")'.dependencies] [target.'cfg(target_os = "ios")'.dependencies]

View file

@ -5,7 +5,7 @@ use crate::{
file_upload::{DesktopFileDragEvent, DesktopFileUploadForm, FileDialogRequest}, file_upload::{DesktopFileDragEvent, DesktopFileUploadForm, FileDialogRequest},
ipc::{IpcMessage, UserWindowEvent}, ipc::{IpcMessage, UserWindowEvent},
query::QueryResult, query::QueryResult,
shortcut::{GlobalHotKeyEvent, ShortcutRegistry}, shortcut::ShortcutRegistry,
webview::WebviewInstance, webview::WebviewInstance,
}; };
use dioxus_core::ElementId; use dioxus_core::ElementId;
@ -77,6 +77,7 @@ impl App {
dioxus_html::set_event_converter(Box::new(crate::events::SerializedHtmlEventConverter)); dioxus_html::set_event_converter(Box::new(crate::events::SerializedHtmlEventConverter));
// Wire up the global hotkey handler // Wire up the global hotkey handler
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
app.set_global_hotkey_handler(); app.set_global_hotkey_handler();
// Allow hotreloading to work - but only in debug mode // Allow hotreloading to work - but only in debug mode
@ -93,7 +94,8 @@ impl App {
.apply_event(window_event, &self.shared.target); .apply_event(window_event, &self.shared.target);
} }
pub fn handle_global_hotkey(&self, event: GlobalHotKeyEvent) { #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
pub fn handle_global_hotkey(&self, event: global_hotkey::GlobalHotKeyEvent) {
self.shared.shortcut_manager.call_handlers(event); self.shared.shortcut_manager.call_handlers(event);
} }
@ -322,6 +324,7 @@ impl App {
view.poll_vdom(); view.poll_vdom();
} }
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
fn set_global_hotkey_handler(&self) { fn set_global_hotkey_handler(&self) {
let receiver = self.shared.proxy.clone(); let receiver = self.shared.proxy.clone();

View file

@ -10,7 +10,7 @@ use dioxus_html::{
prelude::{SerializedMouseData, SerializedPointInteraction}, prelude::{SerializedMouseData, SerializedPointInteraction},
FileEngine, HasDragData, HasFileData, HasFormData, HasMouseData, FileEngine, HasDragData, HasFileData, HasFormData, HasMouseData,
}; };
use muda::accelerator::Modifiers;
use serde::Deserialize; use serde::Deserialize;
use std::{ use std::{
cell::{Cell, RefCell}, cell::{Cell, RefCell},
@ -223,7 +223,7 @@ impl InteractionElementOffset for DesktopFileDragEvent {
} }
impl ModifiersInteraction for DesktopFileDragEvent { impl ModifiersInteraction for DesktopFileDragEvent {
fn modifiers(&self) -> Modifiers { fn modifiers(&self) -> dioxus_html::prelude::Modifiers {
self.mouse.modifiers() self.mouse.modifiers()
} }
} }

View file

@ -1,11 +1,11 @@
use global_hotkey::GlobalHotKeyEvent;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tao::window::WindowId; use tao::window::WindowId;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum UserWindowEvent { pub enum UserWindowEvent {
/// A global hotkey event /// A global hotkey event
GlobalHotKeyEvent(GlobalHotKeyEvent), #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
GlobalHotKeyEvent(global_hotkey::GlobalHotKeyEvent),
/// Poll the virtualdom /// Poll the virtualdom
Poll(WindowId), Poll(WindowId),

View file

@ -32,9 +32,13 @@ pub fn launch_virtual_dom_blocking(virtual_dom: VirtualDom, desktop_config: Conf
UserWindowEvent::Poll(id) => app.poll_vdom(id), UserWindowEvent::Poll(id) => app.poll_vdom(id),
UserWindowEvent::NewWindow => app.handle_new_window(), UserWindowEvent::NewWindow => app.handle_new_window(),
UserWindowEvent::CloseWindow(id) => app.handle_close_msg(id), UserWindowEvent::CloseWindow(id) => app.handle_close_msg(id),
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
UserWindowEvent::GlobalHotKeyEvent(evnt) => app.handle_global_hotkey(evnt), UserWindowEvent::GlobalHotKeyEvent(evnt) => app.handle_global_hotkey(evnt),
#[cfg(all(feature = "hot-reload", debug_assertions))] #[cfg(all(feature = "hot-reload", debug_assertions))]
UserWindowEvent::HotReloadEvent(msg) => app.handle_hot_reload_msg(msg), UserWindowEvent::HotReloadEvent(msg) => app.handle_hot_reload_msg(msg),
UserWindowEvent::Ipc { id, msg } => match msg.method() { UserWindowEvent::Ipc { id, msg } => match msg.method() {
IpcMethod::Initialize => app.handle_initialize_msg(id), IpcMethod::Initialize => app.handle_initialize_msg(id),
IpcMethod::FileDialog => app.handle_file_dialog_msg(msg, id), IpcMethod::FileDialog => app.handle_file_dialog_msg(msg, id),

View file

@ -64,6 +64,7 @@ impl ShortcutRegistry {
} }
} }
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
pub(crate) fn call_handlers(&self, id: GlobalHotKeyEvent) { pub(crate) fn call_handlers(&self, id: GlobalHotKeyEvent) {
if let Some(ShortcutInner { callbacks, .. }) = self.shortcuts.borrow_mut().get_mut(&id.id) { if let Some(ShortcutInner { callbacks, .. }) = self.shortcuts.borrow_mut().get_mut(&id.id) {
for (_, callback) in callbacks.iter_mut() { for (_, callback) in callbacks.iter_mut() {

View file

@ -147,7 +147,7 @@ fn deserialize_raw(
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
impl HtmlEvent { impl HtmlEvent {
pub fn bubbles(&self) -> bool { pub fn bubbles(&self) -> bool {
event_bubbles(&self.name) self.bubbles
} }
} }

View file

@ -75,7 +75,12 @@ mod js {
"{let node = document.createElement('pre'); node.hidden = true; this.stack.push(node); this.nodes[$id$] = node;}" "{let node = document.createElement('pre'); node.hidden = true; this.stack.push(node); this.nodes[$id$] = node;}"
} }
fn new_event_listener(event_name: &str<u8, evt>, id: u32, bubbles: u8) { fn new_event_listener(event_name: &str<u8, evt>, id: u32, bubbles: u8) {
r#"let node = this.nodes[id]; if(node.listening){node.listening += 1;}else{node.listening = 1;} node.setAttribute('data-dioxus-id', `\${id}`); this.createListener($event_name$, node, $bubbles$);"# r#"
let node = this.nodes[id];
if(node.listening){node.listening += 1;}else{node.listening = 1;}
node.setAttribute('data-dioxus-id', `\${id}`);
this.createListener($event_name$, node, $bubbles$);
"#
} }
fn remove_event_listener(event_name: &str<u8, evt>, id: u32, bubbles: u8) { fn remove_event_listener(event_name: &str<u8, evt>, id: u32, bubbles: u8) {
"{let node = this.nodes[$id$]; node.listening -= 1; node.removeAttribute('data-dioxus-id'); this.removeListener(node, $event_name$, $bubbles$);}" "{let node = this.nodes[$id$]; node.listening -= 1; node.removeAttribute('data-dioxus-id'); this.removeListener(node, $event_name$, $bubbles$);}"

View file

@ -73,7 +73,7 @@ impl WebsysDom {
move |event: &web_sys::Event| { move |event: &web_sys::Event| {
let name = event.type_(); let name = event.type_();
let element = walk_event_for_id(event); let element = walk_event_for_id(event);
let bubbles = dioxus_html::event_bubbles(name.as_str()); let bubbles = event.bubbles();
let Some((element, target)) = element else { let Some((element, target)) = element else {
return; return;