file_explorer.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //! Example: File Explorer
  2. //! -------------------------
  3. //!
  4. //! This is a fun little desktop application that lets you explore the file system.
  5. //!
  6. //! This example is interesting because it's mixing filesystem operations and GUI, which is typically hard for UI to do.
  7. //!
  8. //! It also uses `use_ref` to maintain a model, rather than `use_state`. That way,
  9. //! we dont need to clutter our code with `read` commands.
  10. use dioxus::prelude::*;
  11. use dioxus_desktop::{Config, WindowBuilder};
  12. fn main() {
  13. dioxus_desktop::launch_cfg(
  14. app,
  15. Config::new().with_window(WindowBuilder::new().with_resizable(true)),
  16. );
  17. }
  18. fn app(cx: Scope) -> Element {
  19. let files = use_ref(&cx, Files::new);
  20. rsx!(cx, div {
  21. link { href:"https://fonts.googleapis.com/icon?family=Material+Icons", rel:"stylesheet", }
  22. style { include_str!("./assets/fileexplorer.css") }
  23. header {
  24. i { class: "material-icons icon-menu", "menu" }
  25. h1 { "Files: ", files.read().current() }
  26. span { }
  27. i { class: "material-icons", onclick: move |_| files.write().go_up(), "logout" }
  28. }
  29. main {
  30. files.read().path_names.iter().enumerate().map(|(dir_id, path)| {
  31. let path_end = path.split('/').last().unwrap_or(path.as_str());
  32. let icon_type = if path_end.contains('.') {
  33. "description"
  34. } else {
  35. "folder"
  36. };
  37. rsx! (
  38. div {
  39. class: "folder",
  40. key: "{path}",
  41. i { class: "material-icons",
  42. onclick: move |_| files.write().enter_dir(dir_id),
  43. "{icon_type}"
  44. p { class: "cooltip", "0 folders / 0 files" }
  45. }
  46. h1 { "{path_end}" }
  47. }
  48. )
  49. }),
  50. files.read().err.as_ref().map(|err| {
  51. rsx! (
  52. div {
  53. code { "{err}" }
  54. button { onclick: move |_| files.write().clear_err(), "x" }
  55. }
  56. )
  57. })
  58. }
  59. })
  60. }
  61. struct Files {
  62. path_stack: Vec<String>,
  63. path_names: Vec<String>,
  64. err: Option<String>,
  65. }
  66. impl Files {
  67. fn new() -> Self {
  68. let mut files = Self {
  69. path_stack: vec!["./".to_string()],
  70. path_names: vec![],
  71. err: None,
  72. };
  73. files.reload_path_list();
  74. files
  75. }
  76. fn reload_path_list(&mut self) {
  77. let cur_path = self.path_stack.last().unwrap();
  78. let paths = match std::fs::read_dir(cur_path) {
  79. Ok(e) => e,
  80. Err(err) => {
  81. let err = format!("An error occured: {:?}", err);
  82. self.err = Some(err);
  83. self.path_stack.pop();
  84. return;
  85. }
  86. };
  87. let collected = paths.collect::<Vec<_>>();
  88. // clear the current state
  89. self.clear_err();
  90. self.path_names.clear();
  91. for path in collected {
  92. self.path_names
  93. .push(path.unwrap().path().display().to_string());
  94. }
  95. }
  96. fn go_up(&mut self) {
  97. if self.path_stack.len() > 1 {
  98. self.path_stack.pop();
  99. }
  100. self.reload_path_list();
  101. }
  102. fn enter_dir(&mut self, dir_id: usize) {
  103. let path = &self.path_names[dir_id];
  104. self.path_stack.push(path.clone());
  105. self.reload_path_list();
  106. }
  107. fn current(&self) -> &str {
  108. self.path_stack.last().unwrap()
  109. }
  110. fn clear_err(&mut self) {
  111. self.err = None;
  112. }
  113. }