file_explorer.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_window(|w| {
  14. w.with_resizable(false)
  15. .with_inner_size(LogicalSize::new(800.0, 400.0))
  16. })
  17. })
  18. .unwrap();
  19. }
  20. static App: FC<()> = |cx, props| {
  21. let files = use_state(cx, || Files::new());
  22. let file_list = files.path_names.iter().enumerate().map(|(dir_id, path)| {
  23. rsx! (
  24. li { a {"{path}", onclick: move |_| files.modify().enter_dir(dir_id), href: "#"} }
  25. )
  26. });
  27. let err_disp = files.err.as_ref().map(|err| {
  28. rsx! {
  29. div {
  30. code {"{err}"}
  31. button {"x", onclick: move |_| files.modify().clear_err() }
  32. }
  33. }
  34. });
  35. let cur = files.current();
  36. cx.render(rsx! {
  37. div {
  38. h1 {"Files: "}
  39. h3 {"Cur dir: {cur}"}
  40. button { "go up", onclick: move |_| files.modify().go_up() }
  41. ol { {file_list} }
  42. {err_disp}
  43. }
  44. })
  45. };
  46. // right now, this gets cloned every time. It might be a bit better to use im_rc's collections instead
  47. #[derive(Clone)]
  48. struct Files {
  49. path_stack: Vec<String>,
  50. path_names: Vec<String>,
  51. err: Option<String>,
  52. }
  53. impl Files {
  54. fn new() -> Self {
  55. let mut files = Self {
  56. path_stack: vec!["./".to_string()],
  57. path_names: vec![],
  58. err: None,
  59. };
  60. files.reload_path_list();
  61. files
  62. }
  63. fn reload_path_list(&mut self) {
  64. let cur_path = self.path_stack.last().unwrap();
  65. let paths = match fs::read_dir(cur_path) {
  66. Ok(e) => e,
  67. Err(err) => {
  68. let err = format!("An error occured: {:?}", err);
  69. self.err = Some(err);
  70. self.path_stack.pop();
  71. return;
  72. }
  73. };
  74. // clear the current state
  75. self.clear_err();
  76. self.path_names.clear();
  77. for path in paths {
  78. self.path_names
  79. .push(path.unwrap().path().display().to_string());
  80. }
  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. }