dioxus/examples/file_explorer.rs

133 lines
4.2 KiB
Rust
Raw Normal View History

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.
2024-02-14 20:33:07 +00:00
//! We store the state entirely in a single signal, making the explorer logic fairly easy to reason about.
2021-07-27 04:27:07 +00:00
2024-01-20 00:36:40 +00:00
use dioxus::desktop::{Config, WindowBuilder};
2021-07-27 04:27:07 +00:00
use dioxus::prelude::*;
fn main() {
LaunchBuilder::desktop()
.with_cfg(Config::new().with_window(WindowBuilder::new().with_resizable(true)))
2024-01-18 20:32:01 +00:00
.launch(app)
2021-07-27 04:27:07 +00:00
}
2024-01-18 20:36:42 +00:00
#[cfg(not(feature = "collect-assets"))]
const _STYLE: &str = include_str!("../examples/assets/fileexplorer.css");
#[cfg(feature = "collect-assets")]
2024-01-08 22:54:02 +00:00
const _STYLE: &str = manganis::mg!(file("./examples/assets/fileexplorer.css"));
2024-01-15 22:40:56 +00:00
fn app() -> Element {
let mut files = use_signal(Files::new);
2021-09-24 06:37:51 +00:00
2024-01-16 19:18:46 +00:00
rsx! {
div {
2024-01-15 21:06:05 +00:00
link { href:"https://fonts.googleapis.com/icon?family=Material+Icons", rel:"stylesheet" }
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" }
}
2024-01-18 20:36:42 +00:00
style { "{_STYLE}" }
main {
2024-02-14 20:33:07 +00:00
for (dir_id, path) in files.read().path_names.iter().enumerate() {
{
let path_end = path.split('/').last().unwrap_or(path.as_str());
rsx! {
div { class: "folder", key: "{path}",
i { class: "material-icons",
onclick: move |_| files.write().enter_dir(dir_id),
if path_end.contains('.') {
"description"
} else {
"folder"
}
p { class: "cooltip", "0 folders / 0 files" }
}
2024-02-14 20:33:07 +00:00
h1 { "{path_end}" }
}
}
2024-02-14 20:33:07 +00:00
}
}
if let Some(err) = files.read().err.as_ref() {
div {
code { "{err}" }
button { onclick: move |_| files.write().clear_err(), "x" }
}
}
}
}
2024-01-14 05:12:21 +00:00
}
}
2021-07-27 04:27:07 +00:00
2024-02-14 20:33:07 +00:00
/// A simple little struct to hold the file explorer state
///
/// We don't use any fancy signals or memoization here - Dioxus is so fast that even a file explorer can be done with a
/// single signal.
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) {
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) => {
let err = format!("An error occurred: {err:?}");
self.err = Some(err);
2021-07-27 04:27:07 +00:00
self.path_stack.pop();
return;
}
};
let collected = paths.collect::<Vec<_>>();
2021-07-27 04:27:07 +00:00
// clear the current state
self.clear_err();
self.path_names.clear();
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;
}
}