feat: overhaul examples and clean things up

This commit is contained in:
Jonathan Kelley committed 2022-01-03 00:42:17 -05:00
1 parent 601632fc7e
commit 420a30e5d4
33 files changed
+1089 -1417

No files matched your search

+51 -30
View File
@@ -11,37 +11,53 @@
use dioxus::prelude::*;
fn main() {
dioxus::desktop::launch_cfg(app, |c| {
c.with_window(|w| {
w.with_resizable(true)
.with_inner_size(dioxus::desktop::tao::dpi::LogicalSize::new(400.0, 800.0))
})
});
dioxus::desktop::launch_cfg(app, |c| c.with_window(|w| w.with_resizable(true)));
}
fn app(cx: Scope) -> Element {
let files = use_ref(&cx, Files::new);
let files = use_ref(&cx, || Files::new());
cx.render(rsx!(
h1 { "Files: " }
h3 { "Cur dir: " [files.read().current()] }
button { onclick: move |_| files.write().go_up(), "go up" }
ol {
files.read().path_names.iter().enumerate().map(|(dir_id, path)| rsx!(
li { key: "{path}",
a { href: "#", onclick: move |_| files.write().enter_dir(dir_id),
"{path}",
}
}
))
rsx!(cx, div {
link { href:"https://fonts.googleapis.com/icon?family=Material+Icons", rel:"stylesheet", }
style { [include_str!("./assets/fileexplorer.css")] }
header {
i { class: "material-icons icon-menu", "menu" }
h1 { "Files: " [files.read().current()] }
span { }
i { class: "material-icons", onclick: move |_| files.write().go_up(), "logout" }
}
files.read().err.as_ref().map(|err| rsx!(
div {
code { "{err}" }
button { onclick: move |_| files.write().clear_err(), "x" }
}
))
))
main {
files.read().path_names.iter().enumerate().map(|(dir_id, path)| {
let path_end = path.split('/').last().unwrap_or(path.as_str());
let icon_type = if path_end.contains(".") {
"description"
} else {
"folder"
};
rsx! (
div {
class: "folder",
key: "{path}",
i { class: "material-icons",
onclick: move |_| files.write().enter_dir(dir_id),
"{icon_type}"
p { class: "cooltip", "0 folders / 0 files" }
}
h1 { "{path_end}" }
}
)
}),
files.read().err.as_ref().map(|err| {
rsx! (
div {
code { "{err}" }
button { onclick: move |_| files.write().clear_err(), "x" }
}
)
})
}
})
}
struct Files {
@@ -64,21 +80,26 @@ impl Files {
}
fn reload_path_list(&mut self) {
let paths = match std::fs::read_dir(self.path_stack.last().unwrap()) {
let cur_path = self.path_stack.last().unwrap();
let paths = match std::fs::read_dir(cur_path) {
Ok(e) => e,
Err(err) => {
self.err = Some(format!("An error occured: {:?}", err));
let err = format!("An error occured: {:?}", err);
self.err = Some(err);
self.path_stack.pop();
return;
}
};
let collected = paths.collect::<Vec<_>>();
// clear the current state
self.clear_err();
self.path_names.clear();
self.path_names
.extend(paths.map(|path| path.unwrap().path().display().to_string()));
for path in collected {
self.path_names
.push(path.unwrap().path().display().to_string());
}
}
fn go_up(&mut self) {