file_explorer.rs 4.0 KB

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