mirror of
https://github.com/DioxusLabs/dioxus
synced 2025-02-18 14:48:26 +00:00
cfg out globalhotkey
This commit is contained in:
parent
f7f416ee3a
commit
81f38a0bc5
11 changed files with 37 additions and 26 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -4055,9 +4055,9 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
|
|||
|
||||
[[package]]
|
||||
name = "global-hotkey"
|
||||
version = "0.4.2"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "927a00fd7c31d82029f99ce2481a8de1ae974758017d6a55ebbe7f22edcd1617"
|
||||
checksum = "34300b13d16b1593de1b6bd571a376704820a1e6f6fe57be2f106ded8d164030"
|
||||
dependencies = [
|
||||
"crossbeam-channel",
|
||||
"keyboard-types",
|
||||
|
|
|
@ -20,6 +20,7 @@ struct UploadedFile {
|
|||
fn app() -> Element {
|
||||
let mut enable_directory_upload = use_signal(|| false);
|
||||
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 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! {
|
||||
style { {include_str!("./assets/file_upload.css")} }
|
||||
|
||||
h1 { "File Upload Example" }
|
||||
p { "Drop a .txt, .rs, or .js file here to read it" }
|
||||
|
||||
button { onclick: move |_| files_uploaded.write().clear(), "Clear files" }
|
||||
|
||||
div {
|
||||
label { r#for: "directory-upload", "Enable directory upload" }
|
||||
|
@ -75,14 +70,17 @@ fn app() -> Element {
|
|||
}
|
||||
|
||||
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",
|
||||
prevent_default: "ondragover",
|
||||
prevent_default: "ondrop",
|
||||
ondrop: handle_file_drop,
|
||||
prevent_default: "ondragover ondrop",
|
||||
background_color: if hovered() { "lightblue" } else { "lightgray" },
|
||||
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"
|
||||
}
|
||||
|
||||
|
|
|
@ -52,8 +52,8 @@ async-trait = "0.1.68"
|
|||
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]
|
||||
global-hotkey = "0.5.0"
|
||||
rfd = "0.12"
|
||||
global-hotkey = "0.4.1"
|
||||
muda = "0.11.3"
|
||||
|
||||
[target.'cfg(target_os = "ios")'.dependencies]
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
file_upload::{DesktopFileDragEvent, DesktopFileUploadForm, FileDialogRequest},
|
||||
ipc::{IpcMessage, UserWindowEvent},
|
||||
query::QueryResult,
|
||||
shortcut::{GlobalHotKeyEvent, ShortcutRegistry},
|
||||
shortcut::ShortcutRegistry,
|
||||
webview::WebviewInstance,
|
||||
};
|
||||
use dioxus_core::ElementId;
|
||||
|
@ -77,6 +77,7 @@ impl App {
|
|||
dioxus_html::set_event_converter(Box::new(crate::events::SerializedHtmlEventConverter));
|
||||
|
||||
// Wire up the global hotkey handler
|
||||
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
|
||||
app.set_global_hotkey_handler();
|
||||
|
||||
// Allow hotreloading to work - but only in debug mode
|
||||
|
@ -93,7 +94,8 @@ impl App {
|
|||
.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);
|
||||
}
|
||||
|
||||
|
@ -322,6 +324,7 @@ impl App {
|
|||
view.poll_vdom();
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
|
||||
fn set_global_hotkey_handler(&self) {
|
||||
let receiver = self.shared.proxy.clone();
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ use dioxus_html::{
|
|||
prelude::{SerializedMouseData, SerializedPointInteraction},
|
||||
FileEngine, HasDragData, HasFileData, HasFormData, HasMouseData,
|
||||
};
|
||||
use muda::accelerator::Modifiers;
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
cell::{Cell, RefCell},
|
||||
|
@ -223,7 +223,7 @@ impl InteractionElementOffset for DesktopFileDragEvent {
|
|||
}
|
||||
|
||||
impl ModifiersInteraction for DesktopFileDragEvent {
|
||||
fn modifiers(&self) -> Modifiers {
|
||||
fn modifiers(&self) -> dioxus_html::prelude::Modifiers {
|
||||
self.mouse.modifiers()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use global_hotkey::GlobalHotKeyEvent;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tao::window::WindowId;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum UserWindowEvent {
|
||||
/// 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(WindowId),
|
||||
|
|
|
@ -32,9 +32,13 @@ pub fn launch_virtual_dom_blocking(virtual_dom: VirtualDom, desktop_config: Conf
|
|||
UserWindowEvent::Poll(id) => app.poll_vdom(id),
|
||||
UserWindowEvent::NewWindow => app.handle_new_window(),
|
||||
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),
|
||||
|
||||
#[cfg(all(feature = "hot-reload", debug_assertions))]
|
||||
UserWindowEvent::HotReloadEvent(msg) => app.handle_hot_reload_msg(msg),
|
||||
|
||||
UserWindowEvent::Ipc { id, msg } => match msg.method() {
|
||||
IpcMethod::Initialize => app.handle_initialize_msg(id),
|
||||
IpcMethod::FileDialog => app.handle_file_dialog_msg(msg, id),
|
||||
|
|
|
@ -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) {
|
||||
if let Some(ShortcutInner { callbacks, .. }) = self.shortcuts.borrow_mut().get_mut(&id.id) {
|
||||
for (_, callback) in callbacks.iter_mut() {
|
||||
|
|
|
@ -147,7 +147,7 @@ fn deserialize_raw(
|
|||
#[cfg(feature = "serialize")]
|
||||
impl HtmlEvent {
|
||||
pub fn bubbles(&self) -> bool {
|
||||
event_bubbles(&self.name)
|
||||
self.bubbles
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -75,7 +75,12 @@ mod js {
|
|||
"{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) {
|
||||
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) {
|
||||
"{let node = this.nodes[$id$]; node.listening -= 1; node.removeAttribute('data-dioxus-id'); this.removeListener(node, $event_name$, $bubbles$);}"
|
||||
|
|
|
@ -73,7 +73,7 @@ impl WebsysDom {
|
|||
move |event: &web_sys::Event| {
|
||||
let name = event.type_();
|
||||
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 {
|
||||
return;
|
||||
|
|
Loading…
Add table
Reference in a new issue