file_explorer.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. fn main() {
  12. dioxus::desktop::launch_cfg(app, |c| c.with_window(|w| w.with_resizable(true)));
  13. }
  14. fn app(cx: Scope) -> Element {
  15. let files = use_ref(&cx, Files::new);
  16. rsx!(cx, div {
  17. link { href:"https://fonts.googleapis.com/icon?family=Material+Icons", rel:"stylesheet", }
  18. style { [include_str!("./assets/fileexplorer.css")] }
  19. header {
  20. i { class: "material-icons icon-menu", "menu" }
  21. h1 { "Files: " [files.read().current()] }
  22. span { }
  23. i { class: "material-icons", onclick: move |_| files.write().go_up(), "logout" }
  24. }
  25. main {
  26. files.read().path_names.iter().enumerate().map(|(dir_id, path)| {
  27. let path_end = path.split('/').last().unwrap_or_else(|| path.as_str());
  28. let icon_type = if path_end.contains('.') {
  29. "description"
  30. } else {
  31. "folder"
  32. };
  33. rsx! (
  34. div {
  35. class: "folder",
  36. key: "{path}",
  37. i { class: "material-icons",
  38. onclick: move |_| files.write().enter_dir(dir_id),
  39. "{icon_type}"
  40. p { class: "cooltip", "0 folders / 0 files" }
  41. }
  42. h1 { "{path_end}" }
  43. }
  44. )
  45. }),
  46. files.read().err.as_ref().map(|err| {
  47. rsx! (
  48. div {
  49. code { "{err}" }
  50. button { onclick: move |_| files.write().clear_err(), "x" }
  51. }
  52. )
  53. })
  54. }
  55. })
  56. }
  57. struct Files {
  58. path_stack: Vec<String>,
  59. path_names: Vec<String>,
  60. err: Option<String>,
  61. }
  62. impl Files {
  63. fn new() -> Self {
  64. let mut files = Self {
  65. path_stack: vec!["./".to_string()],
  66. path_names: vec![],
  67. err: None,
  68. };
  69. files.reload_path_list();
  70. files
  71. }
  72. fn reload_path_list(&mut self) {
  73. let cur_path = self.path_stack.last().unwrap();
  74. let paths = match std::fs::read_dir(cur_path) {
  75. Ok(e) => e,
  76. Err(err) => {
  77. let err = format!("An error occured: {:?}", err);
  78. self.err = Some(err);
  79. self.path_stack.pop();
  80. return;
  81. }
  82. };
  83. let collected = paths.collect::<Vec<_>>();
  84. // clear the current state
  85. self.clear_err();
  86. self.path_names.clear();
  87. for path in collected {
  88. self.path_names
  89. .push(path.unwrap().path().display().to_string());
  90. }
  91. }
  92. fn go_up(&mut self) {
  93. if self.path_stack.len() > 1 {
  94. self.path_stack.pop();
  95. }
  96. self.reload_path_list();
  97. }
  98. fn enter_dir(&mut self, dir_id: usize) {
  99. let path = &self.path_names[dir_id];
  100. self.path_stack.push(path.clone());
  101. self.reload_path_list();
  102. }
  103. fn current(&self) -> &str {
  104. self.path_stack.last().unwrap()
  105. }
  106. fn clear_err(&mut self) {
  107. self.err = None;
  108. }
  109. }