file_explorer.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 dioxus::desktop::{Config, WindowBuilder};
  8. use dioxus::prelude::*;
  9. fn main() {
  10. LaunchBuilder::desktop()
  11. .with_cfg(Config::new().with_window(WindowBuilder::new().with_resizable(true)))
  12. .launch(app)
  13. }
  14. #[cfg(not(feature = "collect-assets"))]
  15. const _STYLE: &str = include_str!("../examples/assets/fileexplorer.css");
  16. #[cfg(feature = "collect-assets")]
  17. const _STYLE: &str = manganis::mg!(file("./examples/assets/fileexplorer.css"));
  18. fn app() -> Element {
  19. let mut 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. style { "{_STYLE}" }
  30. main {
  31. for (dir_id, path) in files.read().path_names.iter().enumerate() {
  32. {
  33. let path_end = path.split('/').last().unwrap_or(path.as_str());
  34. rsx! {
  35. div { class: "folder", 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. }
  50. if let Some(err) = files.read().err.as_ref() {
  51. div {
  52. code { "{err}" }
  53. button { onclick: move |_| files.write().clear_err(), "x" }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. /// A simple little struct to hold the file explorer state
  61. ///
  62. /// We don't use any fancy signals or memoization here - Dioxus is so fast that even a file explorer can be done with a
  63. /// single signal.
  64. struct Files {
  65. path_stack: Vec<String>,
  66. path_names: Vec<String>,
  67. err: Option<String>,
  68. }
  69. impl Files {
  70. fn new() -> Self {
  71. let mut files = Self {
  72. path_stack: vec!["./".to_string()],
  73. path_names: vec![],
  74. err: None,
  75. };
  76. files.reload_path_list();
  77. files
  78. }
  79. fn reload_path_list(&mut self) {
  80. let cur_path = self.path_stack.last().unwrap();
  81. let paths = match std::fs::read_dir(cur_path) {
  82. Ok(e) => e,
  83. Err(err) => {
  84. let err = format!("An error occurred: {err:?}");
  85. self.err = Some(err);
  86. self.path_stack.pop();
  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
  96. .push(path.unwrap().path().display().to_string());
  97. }
  98. }
  99. fn go_up(&mut self) {
  100. if self.path_stack.len() > 1 {
  101. self.path_stack.pop();
  102. }
  103. self.reload_path_list();
  104. }
  105. fn enter_dir(&mut self, dir_id: usize) {
  106. let path = &self.path_names[dir_id];
  107. self.path_stack.push(path.clone());
  108. self.reload_path_list();
  109. }
  110. fn current(&self) -> &str {
  111. self.path_stack.last().unwrap()
  112. }
  113. fn clear_err(&mut self) {
  114. self.err = None;
  115. }
  116. }