Add ID to filedrop handler

This commit is contained in:
Jonathan Kelley committed 2024-01-04 16:48:53 -08:00
1 parent 62a9583fff
commit ec3eaa6b26
7 files changed
+24 -51

No files matched your search

+1 -1
View File
@@ -2,8 +2,8 @@ use dioxus::prelude::*;
use dioxus_desktop::DesktopContext; use dioxus_desktop::DesktopContext;
pub(crate) fn check_app_exits(app: Component) { pub(crate) fn check_app_exits(app: Component) {
use dioxus_desktop::tao::window::WindowBuilder;
use dioxus_desktop::Config; use dioxus_desktop::Config;
use tao::window::WindowBuilder;
// This is a deadman's switch to ensure that the app exits // 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 = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
let should_panic_clone = should_panic.clone(); let should_panic_clone = should_panic.clone();
+1 -1
View File
@@ -6,7 +6,7 @@ pub use crate::desktop_context::{
use crate::desktop_context::{EventData, UserWindowEvent, WebviewQueue, WindowEventHandlers}; use crate::desktop_context::{EventData, UserWindowEvent, WebviewQueue, WindowEventHandlers};
use crate::element::DesktopElement; use crate::element::DesktopElement;
use crate::eval::init_eval; use crate::eval::init_eval;
use crate::events::{IpcMessage, KnownIpcMethod}; use crate::events::{IpcMessage, IpcMethod};
use crate::file_upload; use crate::file_upload;
pub use crate::protocol::{ pub use crate::protocol::{
use_asset_handler, AssetFuture, AssetHandler, AssetRequest, AssetResponse, use_asset_handler, AssetFuture, AssetHandler, AssetRequest, AssetResponse,
+3 -3
View File
@@ -1,7 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::path::PathBuf; use std::path::PathBuf;
use tao::window::{Icon, Window, WindowBuilder}; use tao::window::{Icon, WindowBuilder, WindowId};
use wry::{ use wry::{
http::{Request as HttpRequest, Response as HttpResponse}, http::{Request as HttpRequest, Response as HttpResponse},
FileDropEvent, FileDropEvent,
@@ -37,7 +37,7 @@ pub struct Config {
pub(crate) enable_default_menu_bar: bool, 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 = ( pub(crate) type WryProtocol = (
String, String,
@@ -119,7 +119,7 @@ impl Config {
/// Set a file drop handler /// Set a file drop handler
pub fn with_file_drop_handler( pub fn with_file_drop_handler(
mut self, mut self,
handler: impl Fn(FileDropEvent) -> bool + 'static, handler: impl Fn(WindowId, FileDropEvent) -> bool + 'static,
) -> Self { ) -> Self {
self.file_drop_handler = Some(Box::new(handler)); self.file_drop_handler = Some(Box::new(handler));
self self
+8 -8
View File
@@ -9,7 +9,7 @@ pub struct IpcMessage {
} }
#[derive(Deserialize, Serialize, Debug, Clone)] #[derive(Deserialize, Serialize, Debug, Clone)]
pub enum KnownIpcMethod<'a> { pub enum IpcMethod<'a> {
FileDialog, FileDialog,
UserEvent, UserEvent,
Query, Query,
@@ -19,15 +19,15 @@ pub enum KnownIpcMethod<'a> {
} }
impl IpcMessage { impl IpcMessage {
pub(crate) fn method(&self) -> KnownIpcMethod { pub(crate) fn method(&self) -> IpcMethod {
match self.method.as_str() { match self.method.as_str() {
// todo: this is a misspelling // todo: this is a misspelling
"file_diolog" => KnownIpcMethod::FileDialog, "file_diolog" => IpcMethod::FileDialog,
"user_event" => KnownIpcMethod::UserEvent, "user_event" => IpcMethod::UserEvent,
"query" => KnownIpcMethod::Query, "query" => IpcMethod::Query,
"browser_open" => KnownIpcMethod::BrowserOpen, "browser_open" => IpcMethod::BrowserOpen,
"initialize" => KnownIpcMethod::Initialize, "initialize" => IpcMethod::Initialize,
_ => KnownIpcMethod::Other(&self.method), _ => IpcMethod::Other(&self.method),
} }
} }
+8 -7
View File
@@ -30,9 +30,10 @@ pub use desktop_context::{
}; };
use desktop_context::{EventData, UserWindowEvent}; use desktop_context::{EventData, UserWindowEvent};
use dioxus_core::*; use dioxus_core::*;
use events::KnownIpcMethod; use events::IpcMethod;
pub use protocol::{use_asset_handler, AssetFuture, AssetHandler, AssetRequest, AssetResponse}; pub use protocol::{use_asset_handler, AssetFuture, AssetHandler, AssetRequest, AssetResponse};
pub use shortcut::{use_global_shortcut, ShortcutHandle, ShortcutId, ShortcutRegistryError}; pub use shortcut::{use_global_shortcut, ShortcutHandle, ShortcutId, ShortcutRegistryError};
pub use tao;
pub use tao::dpi::{LogicalSize, PhysicalSize}; pub use tao::dpi::{LogicalSize, PhysicalSize};
use tao::event::{Event, StartCause, WindowEvent}; use tao::event::{Event, StartCause, WindowEvent};
pub use tao::window::WindowBuilder; 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::CloseWindow => app.handle_close_msg(id),
EventData::HotReloadEvent(msg) => app.handle_hot_reload_msg(msg), EventData::HotReloadEvent(msg) => app.handle_hot_reload_msg(msg),
EventData::Ipc(msg) => match msg.method() { EventData::Ipc(msg) => match msg.method() {
KnownIpcMethod::FileDialog => app.handle_file_dialog_msg(msg, id), IpcMethod::FileDialog => app.handle_file_dialog_msg(msg, id),
KnownIpcMethod::UserEvent => app.handle_user_event_msg(msg, id), IpcMethod::UserEvent => app.handle_user_event_msg(msg, id),
KnownIpcMethod::Query => app.handle_query_msg(msg, id), IpcMethod::Query => app.handle_query_msg(msg, id),
KnownIpcMethod::BrowserOpen => app.handle_browser_open(msg), IpcMethod::BrowserOpen => app.handle_browser_open(msg),
KnownIpcMethod::Initialize => app.handle_initialize_msg(id), IpcMethod::Initialize => app.handle_initialize_msg(id),
KnownIpcMethod::Other(_) => {} IpcMethod::Other(_) => {}
}, },
}, },
_ => {} _ => {}
+2 -30
View File
@@ -1,6 +1,5 @@
use crate::{use_window, DesktopContext}; use crate::DesktopContext;
use dioxus_core::ScopeState; use dioxus_core::ScopeState;
use dioxus_interpreter_js::INTERPRETER_JS;
use slab::Slab; use slab::Slab;
use std::{ use std::{
borrow::Cow, borrow::Cow,
@@ -17,7 +16,7 @@ use tokio::{
}; };
use wry::{ use wry::{
http::{status::StatusCode, Request, Response}, http::{status::StatusCode, Request, Response},
RequestAsyncResponder, Result, Result,
}; };
use crate::desktop_context::EditQueue; use crate::desktop_context::EditQueue;
@@ -25,33 +24,6 @@ use crate::desktop_context::EditQueue;
static MINIFIED: &str = include_str!("./minified.js"); static MINIFIED: &str = include_str!("./minified.js");
fn module_loader(root_name: &str, headless: bool) -> String { 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!( format!(
r#" r#"
<script type="module"> <script type="module">
+1 -1
View File
@@ -81,7 +81,7 @@ pub(crate) fn build(
.with_file_drop_handler(move |event| { .with_file_drop_handler(move |event| {
file_handler file_handler
.as_ref() .as_ref()
.map(|handler| handler(event)) .map(|handler| handler(window_id, event))
.unwrap_or_default() .unwrap_or_default()
}) })
.with_web_context(&mut web_context); .with_web_context(&mut web_context);