main.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //! Example: File Explorer
  2. //!
  3. //! This is a fun little desktop application that lets you explore the file system.
  4. //!
  5. //! This example is interesting because it's mixing filesystem operations and GUI, which is typically hard for UI to do.
  6. //! We store the state entirely in a single signal, making the explorer logic fairly easy to reason about.
  7. use std::env::current_dir;
  8. use std::path::PathBuf;
  9. use dioxus::desktop::{Config, WindowBuilder};
  10. use dioxus::prelude::*;
  11. fn main() {
  12. dioxus::LaunchBuilder::desktop()
  13. .with_cfg(Config::new().with_window(WindowBuilder::new().with_resizable(true)))
  14. .launch(app)
  15. }
  16. fn app() -> Element {
  17. let mut files = use_signal(Files::new);
  18. rsx! {
  19. head::Link {
  20. rel: "stylesheet",
  21. href: asset!("./assets/fileexplorer.css")
  22. }
  23. div {
  24. head::Link { href: "https://fonts.googleapis.com/icon?family=Material+Icons", rel: "stylesheet" }
  25. header {
  26. i { class: "material-icons icon-menu", "menu" }
  27. h1 { "Files: " {files.read().current()} }
  28. span { }
  29. i { class: "material-icons", onclick: move |_| files.write().go_up(), "logout" }
  30. }
  31. main {
  32. for (dir_id, path) in files.read().path_names.iter().enumerate() {
  33. {
  34. let path_end = path.components().last().map(|p|p.as_os_str()).unwrap_or(path.as_os_str()).to_string_lossy();
  35. let path = path.display();
  36. rsx! {
  37. div { class: "folder", key: "{path}",
  38. i { class: "material-icons",
  39. onclick: move |_| files.write().enter_dir(dir_id),
  40. if path_end.contains('.') {
  41. "description"
  42. } else {
  43. "folder"
  44. }
  45. p { class: "cooltip", "0 folders / 0 files" }
  46. }
  47. h1 { "{path_end}" }
  48. }
  49. }
  50. }
  51. }
  52. if let Some(err) = files.read().err.as_ref() {
  53. div {
  54. code { "{err}" }
  55. button { onclick: move |_| files.write().clear_err(), "x" }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. /// A simple little struct to hold the file explorer state
  63. ///
  64. /// We don't use any fancy signals or memoization here - Dioxus is so fast that even a file explorer can be done with a
  65. /// single signal.
  66. struct Files {
  67. current_path: PathBuf,
  68. path_names: Vec<PathBuf>,
  69. err: Option<String>,
  70. }
  71. impl Files {
  72. fn new() -> Self {
  73. let mut files = Self {
  74. current_path: std::path::absolute(current_dir().unwrap()).unwrap(),
  75. path_names: vec![],
  76. err: None,
  77. };
  78. files.reload_path_list();
  79. files
  80. }
  81. fn reload_path_list(&mut self) {
  82. let paths = match std::fs::read_dir(&self.current_path) {
  83. Ok(e) => e,
  84. Err(err) => {
  85. let err = format!("An error occurred: {err:?}");
  86. self.err = Some(err);
  87. return;
  88. }
  89. };
  90. let collected = paths.collect::<Vec<_>>();
  91. // clear the current state
  92. self.clear_err();
  93. self.path_names.clear();
  94. for path in collected {
  95. self.path_names.push(path.unwrap().path().to_path_buf());
  96. }
  97. }
  98. fn go_up(&mut self) {
  99. self.current_path = match self.current_path.parent() {
  100. Some(path) => path.to_path_buf(),
  101. None => {
  102. self.err = Some("Cannot go up from the root directory".to_string());
  103. return;
  104. }
  105. };
  106. self.reload_path_list();
  107. }
  108. fn enter_dir(&mut self, dir_id: usize) {
  109. let path = &self.path_names[dir_id];
  110. self.current_path.clone_from(path);
  111. self.reload_path_list();
  112. }
  113. fn current(&self) -> String {
  114. self.current_path.display().to_string()
  115. }
  116. fn clear_err(&mut self) {
  117. self.err = None;
  118. }
  119. }