file_explorer.rs 3.8 KB

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