file_explorer.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. use dioxus::prelude::*;
  8. fn main() {
  9. dioxus::desktop::launch_cfg(App, |c| {
  10. c.with_window(|w| {
  11. w.with_resizable(true).with_inner_size(
  12. dioxus::desktop::wry::application::dpi::LogicalSize::new(400.0, 800.0),
  13. )
  14. })
  15. });
  16. }
  17. static App: Component = |cx| {
  18. let file_manager = use_ref(&cx, Files::new);
  19. let files = file_manager.read();
  20. let file_list = files.path_names.iter().enumerate().map(|(dir_id, path)| {
  21. rsx! (
  22. li { a {"{path}", onclick: move |_| file_manager.write().enter_dir(dir_id), href: "#"} }
  23. )
  24. });
  25. let err_disp = files.err.as_ref().map(|err| {
  26. rsx! (
  27. div {
  28. code {"{err}"}
  29. button {"x", onclick: move |_| file_manager.write().clear_err() }
  30. }
  31. )
  32. });
  33. let current_dir = files.current();
  34. cx.render(rsx!(
  35. div {
  36. h1 {"Files: "}
  37. h3 {"Cur dir: {current_dir}"}
  38. button { "go up", onclick: move |_| file_manager.write().go_up() }
  39. ol { {file_list} }
  40. {err_disp}
  41. }
  42. ))
  43. };
  44. struct Files {
  45. path_stack: Vec<String>,
  46. path_names: Vec<String>,
  47. err: Option<String>,
  48. }
  49. impl Files {
  50. fn new() -> Self {
  51. let mut files = Self {
  52. path_stack: vec!["./".to_string()],
  53. path_names: vec![],
  54. err: None,
  55. };
  56. files.reload_path_list();
  57. files
  58. }
  59. fn reload_path_list(&mut self) {
  60. let cur_path = self.path_stack.last().unwrap();
  61. log::info!("Reloading path list for {:?}", cur_path);
  62. let paths = match std::fs::read_dir(cur_path) {
  63. Ok(e) => e,
  64. Err(err) => {
  65. let err = format!("An error occured: {:?}", err);
  66. self.err = Some(err);
  67. self.path_stack.pop();
  68. return;
  69. }
  70. };
  71. let collected = paths.collect::<Vec<_>>();
  72. log::info!("Path list reloaded {:#?}", collected);
  73. // clear the current state
  74. self.clear_err();
  75. self.path_names.clear();
  76. for path in collected {
  77. self.path_names
  78. .push(path.unwrap().path().display().to_string());
  79. }
  80. log::info!("path namees are {:#?}", self.path_names);
  81. }
  82. fn go_up(&mut self) {
  83. if self.path_stack.len() > 1 {
  84. self.path_stack.pop();
  85. }
  86. self.reload_path_list();
  87. }
  88. fn enter_dir(&mut self, dir_id: usize) {
  89. let path = &self.path_names[dir_id];
  90. self.path_stack.push(path.clone());
  91. self.reload_path_list();
  92. }
  93. fn current(&self) -> &str {
  94. self.path_stack.last().unwrap()
  95. }
  96. fn clear_err(&mut self) {
  97. self.err = None;
  98. }
  99. }