Browse Source

Add ID to filedrop handler

Jonathan Kelley 1 year ago
parent
commit
ec3eaa6b26

+ 1 - 1
packages/desktop/headless_tests/rendering.rs

@@ -2,8 +2,8 @@ use dioxus::prelude::*;
 use dioxus_desktop::DesktopContext;
 
 pub(crate) fn check_app_exits(app: Component) {
-    use dioxus_desktop::tao::window::WindowBuilder;
     use dioxus_desktop::Config;
+    use tao::window::WindowBuilder;
     // This is a deadman's switch to ensure that the app exits
     let should_panic = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
     let should_panic_clone = should_panic.clone();

+ 1 - 1
packages/desktop/src/app.rs

@@ -6,7 +6,7 @@ pub use crate::desktop_context::{
 use crate::desktop_context::{EventData, UserWindowEvent, WebviewQueue, WindowEventHandlers};
 use crate::element::DesktopElement;
 use crate::eval::init_eval;
-use crate::events::{IpcMessage, KnownIpcMethod};
+use crate::events::{IpcMessage, IpcMethod};
 use crate::file_upload;
 pub use crate::protocol::{
     use_asset_handler, AssetFuture, AssetHandler, AssetRequest, AssetResponse,

+ 3 - 3
packages/desktop/src/cfg.rs

@@ -1,7 +1,7 @@
 use std::borrow::Cow;
 use std::path::PathBuf;
 
-use tao::window::{Icon, Window, WindowBuilder};
+use tao::window::{Icon, WindowBuilder, WindowId};
 use wry::{
     http::{Request as HttpRequest, Response as HttpResponse},
     FileDropEvent,
@@ -37,7 +37,7 @@ pub struct Config {
     pub(crate) enable_default_menu_bar: bool,
 }
 
-type DropHandler = Box<dyn Fn(FileDropEvent) -> bool>;
+type DropHandler = Box<dyn Fn(WindowId, FileDropEvent) -> bool>;
 
 pub(crate) type WryProtocol = (
     String,
@@ -119,7 +119,7 @@ impl Config {
     /// Set a file drop handler
     pub fn with_file_drop_handler(
         mut self,
-        handler: impl Fn(FileDropEvent) -> bool + 'static,
+        handler: impl Fn(WindowId, FileDropEvent) -> bool + 'static,
     ) -> Self {
         self.file_drop_handler = Some(Box::new(handler));
         self

+ 8 - 8
packages/desktop/src/events.rs

@@ -9,7 +9,7 @@ pub struct IpcMessage {
 }
 
 #[derive(Deserialize, Serialize, Debug, Clone)]
-pub enum KnownIpcMethod<'a> {
+pub enum IpcMethod<'a> {
     FileDialog,
     UserEvent,
     Query,
@@ -19,15 +19,15 @@ pub enum KnownIpcMethod<'a> {
 }
 
 impl IpcMessage {
-    pub(crate) fn method(&self) -> KnownIpcMethod {
+    pub(crate) fn method(&self) -> IpcMethod {
         match self.method.as_str() {
             // todo: this is a misspelling
-            "file_diolog" => KnownIpcMethod::FileDialog,
-            "user_event" => KnownIpcMethod::UserEvent,
-            "query" => KnownIpcMethod::Query,
-            "browser_open" => KnownIpcMethod::BrowserOpen,
-            "initialize" => KnownIpcMethod::Initialize,
-            _ => KnownIpcMethod::Other(&self.method),
+            "file_diolog" => IpcMethod::FileDialog,
+            "user_event" => IpcMethod::UserEvent,
+            "query" => IpcMethod::Query,
+            "browser_open" => IpcMethod::BrowserOpen,
+            "initialize" => IpcMethod::Initialize,
+            _ => IpcMethod::Other(&self.method),
         }
     }
 

+ 8 - 7
packages/desktop/src/lib.rs

@@ -30,9 +30,10 @@ pub use desktop_context::{
 };
 use desktop_context::{EventData, UserWindowEvent};
 use dioxus_core::*;
-use events::KnownIpcMethod;
+use events::IpcMethod;
 pub use protocol::{use_asset_handler, AssetFuture, AssetHandler, AssetRequest, AssetResponse};
 pub use shortcut::{use_global_shortcut, ShortcutHandle, ShortcutId, ShortcutRegistryError};
+pub use tao;
 pub use tao::dpi::{LogicalSize, PhysicalSize};
 use tao::event::{Event, StartCause, WindowEvent};
 pub use tao::window::WindowBuilder;
@@ -136,12 +137,12 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
                 EventData::CloseWindow => app.handle_close_msg(id),
                 EventData::HotReloadEvent(msg) => app.handle_hot_reload_msg(msg),
                 EventData::Ipc(msg) => match msg.method() {
-                    KnownIpcMethod::FileDialog => app.handle_file_dialog_msg(msg, id),
-                    KnownIpcMethod::UserEvent => app.handle_user_event_msg(msg, id),
-                    KnownIpcMethod::Query => app.handle_query_msg(msg, id),
-                    KnownIpcMethod::BrowserOpen => app.handle_browser_open(msg),
-                    KnownIpcMethod::Initialize => app.handle_initialize_msg(id),
-                    KnownIpcMethod::Other(_) => {}
+                    IpcMethod::FileDialog => app.handle_file_dialog_msg(msg, id),
+                    IpcMethod::UserEvent => app.handle_user_event_msg(msg, id),
+                    IpcMethod::Query => app.handle_query_msg(msg, id),
+                    IpcMethod::BrowserOpen => app.handle_browser_open(msg),
+                    IpcMethod::Initialize => app.handle_initialize_msg(id),
+                    IpcMethod::Other(_) => {}
                 },
             },
             _ => {}

+ 2 - 30
packages/desktop/src/protocol.rs

@@ -1,6 +1,5 @@
-use crate::{use_window, DesktopContext};
+use crate::DesktopContext;
 use dioxus_core::ScopeState;
-use dioxus_interpreter_js::INTERPRETER_JS;
 use slab::Slab;
 use std::{
     borrow::Cow,
@@ -17,7 +16,7 @@ use tokio::{
 };
 use wry::{
     http::{status::StatusCode, Request, Response},
-    RequestAsyncResponder, Result,
+    Result,
 };
 
 use crate::desktop_context::EditQueue;
@@ -25,33 +24,6 @@ use crate::desktop_context::EditQueue;
 static MINIFIED: &str = include_str!("./minified.js");
 
 fn module_loader(root_name: &str, headless: bool) -> String {
-    let js = INTERPRETER_JS.replace(
-        "/*POST_HANDLE_EDITS*/",
-        r#"// Prevent file inputs from opening the file dialog on click
-    let inputs = document.querySelectorAll("input");
-    for (let input of inputs) {
-      if (!input.getAttribute("data-dioxus-file-listener")) {
-        // prevent file inputs from opening the file dialog on click
-        const type = input.getAttribute("type");
-        if (type === "file") {
-          input.setAttribute("data-dioxus-file-listener", true);
-          input.addEventListener("click", (event) => {
-            let target = event.target;
-            let target_id = find_real_id(target);
-            if (target_id !== null) {
-              const send = (event_name) => {
-                const message = serializeIpcMessage("file_diolog", { accept: target.getAttribute("accept"), directory: target.getAttribute("webkitdirectory") === "true", multiple: target.hasAttribute("multiple"), target: parseInt(target_id), bubbles: event_bubbles(event_name), event: event_name });
-                window.ipc.postMessage(message);
-              };
-              send("change&input");
-            }
-            event.preventDefault();
-          });
-        }
-      }
-    }"#,
-    );
-
     format!(
         r#"
 <script type="module">

+ 1 - 1
packages/desktop/src/webview.rs

@@ -81,7 +81,7 @@ pub(crate) fn build(
         .with_file_drop_handler(move |event| {
             file_handler
                 .as_ref()
-                .map(|handler| handler(event))
+                .map(|handler| handler(window_id, event))
                 .unwrap_or_default()
         })
         .with_web_context(&mut web_context);