Add directory support for file input.
This commit is contained in:
1 parent
0fec47db72
commit
37a8fb5391
5 files changed
+31
-11
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
|
use tokio::time::sleep;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
dioxus_desktop::launch(App);
|
dioxus_desktop::launch(App);
|
||||||
@@ -13,20 +14,22 @@ fn App(cx: Scope) -> Element {
|
|||||||
r#type: "file",
|
r#type: "file",
|
||||||
accept: ".txt, .rs",
|
accept: ".txt, .rs",
|
||||||
multiple: true,
|
multiple: true,
|
||||||
|
directory: true,
|
||||||
onchange: |evt| {
|
onchange: |evt| {
|
||||||
to_owned![files_uploaded];
|
to_owned![files_uploaded];
|
||||||
async move {
|
async move {
|
||||||
if let Some(file_engine) = &evt.files {
|
if let Some(file_engine) = &evt.files {
|
||||||
let files = file_engine.files();
|
let files = file_engine.files();
|
||||||
for file_name in &files {
|
for file_name in files {
|
||||||
if let Some(file) = file_engine.read_file_to_string(file_name).await{
|
sleep(std::time::Duration::from_secs(1)).await;
|
||||||
files_uploaded.write().push(file);
|
files_uploaded.write().push(file_name);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
|
|
||||||
|
div { "progress: {files_uploaded.read().len()}" },
|
||||||
|
|
||||||
ul {
|
ul {
|
||||||
for file in files_uploaded.read().iter() {
|
for file in files_uploaded.read().iter() {
|
||||||
|
|||||||
@@ -7,14 +7,21 @@ pub(crate) struct FileDiologRequest {
|
|||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
accept: Option<String>,
|
accept: Option<String>,
|
||||||
multiple: bool,
|
multiple: bool,
|
||||||
|
directory: bool,
|
||||||
pub event: String,
|
pub event: String,
|
||||||
pub target: usize,
|
pub target: usize,
|
||||||
pub bubbles: bool,
|
pub bubbles: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_file_event(request: &FileDiologRequest) -> Vec<PathBuf> {
|
fn get_file_event_for_folder(request: &FileDiologRequest, dialog: rfd::FileDialog) -> Vec<PathBuf> {
|
||||||
let mut dialog = rfd::FileDialog::new();
|
if request.multiple {
|
||||||
|
dialog.pick_folders().into_iter().flatten().collect()
|
||||||
|
} else {
|
||||||
|
dialog.pick_folder().into_iter().collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_file_event_for_file(request: &FileDiologRequest, mut dialog: rfd::FileDialog) -> Vec<PathBuf> {
|
||||||
let filters: Vec<_> = request
|
let filters: Vec<_> = request
|
||||||
.accept
|
.accept
|
||||||
.as_deref()
|
.as_deref()
|
||||||
@@ -30,13 +37,21 @@ pub(crate) fn get_file_event(request: &FileDiologRequest) -> Vec<PathBuf> {
|
|||||||
|
|
||||||
dialog = dialog.add_filter("name", file_extensions.as_slice());
|
dialog = dialog.add_filter("name", file_extensions.as_slice());
|
||||||
|
|
||||||
let files: Vec<_> = if request.multiple {
|
if request.multiple {
|
||||||
dialog.pick_files().into_iter().flatten().collect()
|
dialog.pick_files().into_iter().flatten().collect()
|
||||||
} else {
|
} else {
|
||||||
dialog.pick_file().into_iter().collect()
|
dialog.pick_file().into_iter().collect()
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
files
|
pub(crate) fn get_file_event(request: &FileDiologRequest) -> Vec<PathBuf> {
|
||||||
|
let dialog = rfd::FileDialog::new();
|
||||||
|
|
||||||
|
if request.directory {
|
||||||
|
get_file_event_for_folder(request, dialog)
|
||||||
|
} else {
|
||||||
|
get_file_event_for_file(request, dialog)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Filters {
|
enum Filters {
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ fn module_loader(root_name: &str) -> String {
|
|||||||
let target_id = find_real_id(target);
|
let target_id = find_real_id(target);
|
||||||
if (target_id !== null) {
|
if (target_id !== null) {
|
||||||
const send = (event_name) => {
|
const send = (event_name) => {
|
||||||
const message = serializeIpcMessage("file_diolog", { accept: target.getAttribute("accept"), multiple: target.hasAttribute("multiple"), target: parseInt(target_id), bubbles: event_bubbles(event_name), event: event_name });
|
const message = serializeIpcMessage("file_diolog", { accept: target.getAttribute("accept"), directory: target.hasAttribute("directory"), multiple: target.hasAttribute("multiple"), target: parseInt(target_id), bubbles: event_bubbles(event_name), event: event_name });
|
||||||
window.ipc.postMessage(message);
|
window.ipc.postMessage(message);
|
||||||
};
|
};
|
||||||
send("change&input");
|
send("change&input");
|
||||||
|
|||||||
@@ -1098,6 +1098,7 @@ builder_constructors! {
|
|||||||
autofocus: Bool DEFAULT,
|
autofocus: Bool DEFAULT,
|
||||||
capture: String DEFAULT,
|
capture: String DEFAULT,
|
||||||
checked: Bool DEFAULT,
|
checked: Bool DEFAULT,
|
||||||
|
directory: Bool DEFAULT,
|
||||||
disabled: Bool DEFAULT,
|
disabled: Bool DEFAULT,
|
||||||
form: Id DEFAULT,
|
form: Id DEFAULT,
|
||||||
formaction: Uri DEFAULT,
|
formaction: Uri DEFAULT,
|
||||||
|
|||||||
@@ -143,6 +143,7 @@ mod js {
|
|||||||
autofocus: true,
|
autofocus: true,
|
||||||
autoplay: true,
|
autoplay: true,
|
||||||
checked: true,
|
checked: true,
|
||||||
|
directory: true,
|
||||||
controls: true,
|
controls: true,
|
||||||
default: true,
|
default: true,
|
||||||
defer: true,
|
defer: true,
|
||||||
|
|||||||
Reference in new issue
Block a user