Clean up file uploads in desktop/liveview, remove minify

This commit is contained in:
Jonathan Kelley committed 2024-03-05 13:00:27 -08:00
1 parent 56a365742c
commit 403e8e2f49
14 files changed
+169 -156

No files matched your search

+7 -24
View File
@@ -2,9 +2,8 @@ use crate::{
config::{Config, WindowCloseBehaviour},
element::DesktopElement,
event_handlers::WindowEventHandlers,
file_upload::FileDialogRequest,
ipc::IpcMessage,
ipc::{EventData, UserWindowEvent},
file_upload::{DesktopFileUploadForm, FileDialogRequest},
ipc::{EventData, IpcMessage, UserWindowEvent},
query::QueryResult,
shortcut::{GlobalHotKeyEvent, ShortcutRegistry},
webview::WebviewInstance,
@@ -12,10 +11,7 @@ use crate::{
use crossbeam_channel::Receiver;
use dioxus_core::ElementId;
use dioxus_core::VirtualDom;
use dioxus_html::{
native_bind::NativeFileEngine, FileEngine, HasFileData, HasFormData, HtmlEvent,
PlatformEventData,
};
use dioxus_html::{native_bind::NativeFileEngine, HtmlEvent, PlatformEventData};
use std::{
cell::{Cell, RefCell},
collections::HashMap,
@@ -270,30 +266,17 @@ impl App {
let Ok(file_dialog) = serde_json::from_value::<FileDialogRequest>(msg.params()) else {
return;
};
struct DesktopFileUploadForm {
files: Arc<NativeFileEngine>,
}
impl HasFileData for DesktopFileUploadForm {
fn files(&self) -> Option<Arc<dyn FileEngine>> {
Some(self.files.clone())
}
}
impl HasFormData for DesktopFileUploadForm {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
let id = ElementId(file_dialog.target);
let event_name = &file_dialog.event;
let event_bubbles = file_dialog.bubbles;
let files = file_dialog.get_file_event();
let data = Rc::new(PlatformEventData::new(Box::new(DesktopFileUploadForm {
let as_any = Box::new(DesktopFileUploadForm {
files: Arc::new(NativeFileEngine::new(files)),
})));
});
let data = Rc::new(PlatformEventData::new(as_any));
let view = self.webviews.get_mut(&window).unwrap();
+8 -2
View File
@@ -1,6 +1,6 @@
//! Convert a serialized event to an event trigger
use crate::element::DesktopElement;
use crate::{element::DesktopElement, file_upload::DesktopFileUploadForm};
use dioxus_html::*;
pub(crate) struct SerializedHtmlEventConverter;
@@ -47,8 +47,14 @@ impl HtmlEventConverter for SerializedHtmlEventConverter {
}
fn convert_form_data(&self, event: &PlatformEventData) -> FormData {
// Attempt a simple serialized form data conversion
if let Some(_data) = event.downcast::<SerializedFormData>() {
return _data.clone().into();
}
// If that failed then it's a file upload form
event
.downcast::<SerializedFormData>()
.downcast::<DesktopFileUploadForm>()
.cloned()
.unwrap()
.into()
+19 -1
View File
@@ -1,7 +1,8 @@
#![allow(unused)]
use dioxus_html::{native_bind::NativeFileEngine, FileEngine, HasFileData, HasFormData};
use serde::Deserialize;
use std::{path::PathBuf, str::FromStr};
use std::{path::PathBuf, str::FromStr, sync::Arc};
#[derive(Debug, Deserialize)]
pub(crate) struct FileDialogRequest {
@@ -124,3 +125,20 @@ impl FromStr for Filters {
}
}
}
#[derive(Clone)]
pub(crate) struct DesktopFileUploadForm {
pub files: Arc<NativeFileEngine>,
}
impl HasFileData for DesktopFileUploadForm {
fn files(&self) -> Option<Arc<dyn FileEngine>> {
Some(self.files.clone())
}
}
impl HasFormData for DesktopFileUploadForm {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
+12 -18
View File
@@ -13,18 +13,6 @@ const EDITS_PATH: &str = "http://dioxus.index.html/edits";
#[cfg(not(any(target_os = "android", target_os = "windows")))]
const EDITS_PATH: &str = "dioxus://index.html/edits";
// const PREVENT_FILE_UPLOAD: &str = include_str!("../js/prevent_file_upload.js");
fn handle_edits_code() -> String {
format!(
r#"// Poll for requests
{SLEDGEHAMMER_JS}
{NATIVE_JS}
window.interpreter = new NativeInterpreter("{EDITS_PATH}");
"#
)
}
static DEFAULT_INDEX: &str = include_str!("./index.html");
/// Build the index.html file we use for bootstrapping a new app
@@ -184,20 +172,26 @@ fn serve_from_fs(path: PathBuf) -> Result<Response<Vec<u8>>> {
/// - headless: is this page being loaded but invisible? Important because not all windows are visible and the
/// interpreter can't connect until the window is ready.
fn module_loader(root_id: &str, headless: bool) -> String {
let js = handle_edits_code();
format!(
r#"
<script type="module">
{js}
// Wait for the page to load
// Bring the sledgehammer code
{SLEDGEHAMMER_JS}
// And then extend it with our native bindings
{NATIVE_JS}
// The nativeinterprerter extends the sledgehammer interpreter with a few extra methods that we use for IPC
window.interpreter = new NativeInterpreter("{EDITS_PATH}");
// Wait for the page to load before sending the initialize message
window.onload = function() {{
let rootname = "{root_id}";
let root_element = window.document.getElementById(rootname);
let root_element = window.document.getElementById("{root_id}");
if (root_element != null) {{
window.interpreter.initialize(root_element);
window.ipc.postMessage(window.interpreter.serializeIpcMessage("initialize"));
}}
window.interpreter.wait_for_request({headless});
window.interpreter.waitForRequest({headless});
}}
</script>
"#