2021-07-27 04:27:07 +00:00
|
|
|
//! Example: File Explorer
|
|
|
|
//! -------------------------
|
|
|
|
//!
|
|
|
|
//! This is a fun little desktop application that lets you explore the file system.
|
|
|
|
//!
|
|
|
|
//! This example is interesting because it's mixing filesystem operations and GUI, which is typically hard for UI to do.
|
2021-12-30 08:14:47 +00:00
|
|
|
//!
|
|
|
|
//! It also uses `use_ref` to maintain a model, rather than `use_state`. That way,
|
|
|
|
//! we dont need to clutter our code with `read` commands.
|
2021-07-27 04:27:07 +00:00
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
2022-09-13 23:22:27 +00:00
|
|
|
use dioxus_desktop::{Config, WindowBuilder};
|
2021-07-27 04:27:07 +00:00
|
|
|
|
|
|
|
fn main() {
|
2022-09-13 23:22:27 +00:00
|
|
|
dioxus_desktop::launch_cfg(
|
|
|
|
app,
|
|
|
|
Config::new().with_window(WindowBuilder::new().with_resizable(true)),
|
|
|
|
);
|
2021-07-27 04:27:07 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 08:14:47 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-12-07 21:11:40 +00:00
|
|
|
let files = use_ref(cx, Files::new);
|
2021-09-24 06:37:51 +00:00
|
|
|
|
2022-11-16 07:59:19 +00:00
|
|
|
cx.render(rsx! {
|
2022-09-25 08:05:16 +00:00
|
|
|
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" }
|
|
|
|
}
|
|
|
|
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" }
|
2022-01-03 05:42:17 +00:00
|
|
|
}
|
2022-09-25 08:05:16 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2021-12-15 20:56:53 +00:00
|
|
|
}
|
2022-11-16 07:59:19 +00:00
|
|
|
})
|
2021-12-30 08:14:47 +00:00
|
|
|
}
|
2021-07-27 04:27:07 +00:00
|
|
|
|
|
|
|
struct Files {
|
|
|
|
path_stack: Vec<String>,
|
|
|
|
path_names: Vec<String>,
|
|
|
|
err: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Files {
|
|
|
|
fn new() -> Self {
|
|
|
|
let mut files = Self {
|
|
|
|
path_stack: vec!["./".to_string()],
|
|
|
|
path_names: vec![],
|
|
|
|
err: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
files.reload_path_list();
|
|
|
|
|
|
|
|
files
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reload_path_list(&mut self) {
|
2022-01-03 05:42:17 +00:00
|
|
|
let cur_path = self.path_stack.last().unwrap();
|
|
|
|
let paths = match std::fs::read_dir(cur_path) {
|
2021-07-27 04:27:07 +00:00
|
|
|
Ok(e) => e,
|
|
|
|
Err(err) => {
|
2023-01-28 02:35:46 +00:00
|
|
|
let err = format!("An error occured: {err:?}");
|
2022-01-03 05:42:17 +00:00
|
|
|
self.err = Some(err);
|
2021-07-27 04:27:07 +00:00
|
|
|
self.path_stack.pop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2022-01-03 05:42:17 +00:00
|
|
|
let collected = paths.collect::<Vec<_>>();
|
2021-07-27 04:27:07 +00:00
|
|
|
|
|
|
|
// clear the current state
|
|
|
|
self.clear_err();
|
|
|
|
self.path_names.clear();
|
|
|
|
|
2022-01-03 05:42:17 +00:00
|
|
|
for path in collected {
|
|
|
|
self.path_names
|
|
|
|
.push(path.unwrap().path().display().to_string());
|
|
|
|
}
|
2021-07-27 04:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn go_up(&mut self) {
|
|
|
|
if self.path_stack.len() > 1 {
|
|
|
|
self.path_stack.pop();
|
|
|
|
}
|
|
|
|
self.reload_path_list();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enter_dir(&mut self, dir_id: usize) {
|
|
|
|
let path = &self.path_names[dir_id];
|
|
|
|
self.path_stack.push(path.clone());
|
|
|
|
self.reload_path_list();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current(&self) -> &str {
|
|
|
|
self.path_stack.last().unwrap()
|
|
|
|
}
|
|
|
|
fn clear_err(&mut self) {
|
|
|
|
self.err = None;
|
|
|
|
}
|
|
|
|
}
|