file_explorer.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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::desktop::wry::application::dpi::LogicalSize;
  8. use dioxus::prelude::*;
  9. use std::fs::{self, DirEntry};
  10. fn main() {
  11. env_logger::init();
  12. dioxus::desktop::launch(App, |c| {
  13. c.with_resizable(false)
  14. .with_inner_size(LogicalSize::new(800.0, 400.0))
  15. })
  16. .unwrap();
  17. }
  18. static App: FC<()> = |cx| {
  19. let files = use_state(cx, || Files::new());
  20. let file_list = files.path_names.iter().enumerate().map(|(dir_id, path)| {
  21. rsx! (
  22. li { a {"{path}", onclick: move |_| files.get_mut().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 |_| files.get_mut().clear_err() }
  30. }
  31. }
  32. });
  33. let cur = files.current();
  34. cx.render(rsx! {
  35. div {
  36. h1 {"Files: "}
  37. h3 {"Cur dir: {cur}"}
  38. button { "go up", onclick: move |_| files.get_mut().go_up() }
  39. ol { {file_list} }
  40. {err_disp}
  41. }
  42. })
  43. };
  44. // right now, this gets cloned every time. It might be a bit better to use im_rc's collections instead
  45. #[derive(Clone)]
  46. struct Files {
  47. path_stack: Vec<String>,
  48. path_names: Vec<String>,
  49. err: Option<String>,
  50. }
  51. impl Files {
  52. fn new() -> Self {
  53. let mut files = Self {
  54. path_stack: vec!["./".to_string()],
  55. path_names: vec![],
  56. err: None,
  57. };
  58. files.reload_path_list();
  59. files
  60. }
  61. fn reload_path_list(&mut self) {
  62. let cur_path = self.path_stack.last().unwrap();
  63. let paths = match 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. // clear the current state
  73. self.clear_err();
  74. self.path_names.clear();
  75. for path in paths {
  76. self.path_names
  77. .push(path.unwrap().path().display().to_string());
  78. }
  79. }
  80. fn go_up(&mut self) {
  81. if self.path_stack.len() > 1 {
  82. self.path_stack.pop();
  83. }
  84. self.reload_path_list();
  85. }
  86. fn enter_dir(&mut self, dir_id: usize) {
  87. let path = &self.path_names[dir_id];
  88. self.path_stack.push(path.clone());
  89. self.reload_path_list();
  90. }
  91. fn current(&self) -> &str {
  92. self.path_stack.last().unwrap()
  93. }
  94. fn clear_err(&mut self) {
  95. self.err = None;
  96. }
  97. }