file_explorer.rs 3.1 KB

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