Skip to content

Commit

Permalink
Move CreateDirectoryDialog to seperate struct
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxxcode committed Jan 23, 2024
1 parent 6d5a1dd commit b106dfc
Showing 1 changed file with 58 additions and 31 deletions.
89 changes: 58 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ pub struct FileExplorer {
directory_offset: usize,
directory_content: Vec<PathBuf>,

create_folder_dialog: bool,
create_folder_input: String,
create_directory_dialog: CreateDirectoryDialog,

selected_item: Option<PathBuf>,
search_value: String
Expand All @@ -42,8 +41,7 @@ impl FileExplorer {
directory_offset: 0,
directory_content: vec![],

create_folder_dialog: false,
create_folder_input: String::new(),
create_directory_dialog: CreateDirectoryDialog::new(),

selected_item: None,
search_value: String::new(),
Expand Down Expand Up @@ -112,9 +110,9 @@ impl FileExplorer {
let _ = self.load_next_directory();
}

if ui_button_enabled_disabled(ui, NAV_BUTTON_SIZE, "+", !self.create_folder_dialog) {
self.create_folder_input.clear();
self.create_folder_dialog = true;
if ui_button_enabled_disabled(ui, NAV_BUTTON_SIZE, "+",
!self.create_directory_dialog.is_open()) {
self.create_directory_dialog.open();
}

// Current path display
Expand Down Expand Up @@ -286,24 +284,7 @@ impl FileExplorer {

self.directory_content = data;

if self.create_folder_dialog {
self.update_create_folder_dialog(ui);
}
});
}

fn update_create_folder_dialog(&mut self, ui: &mut egui::Ui) {
ui.horizontal(|ui| {
// TODO: Add "Create new folder" icon
ui.text_edit_singleline(&mut self.create_folder_input).scroll_to_me(None);
// TODO: Validate that the filename is unique
if ui.button("✔").clicked() {
// TODO: Create folder and append it to the end of directory_content
self.reset_create_folder_dialog();
}
if ui.button("✖").clicked() {
self.reset_create_folder_dialog();
}
self.create_directory_dialog.update(ui);
});
}

Expand Down Expand Up @@ -355,11 +336,6 @@ impl FileExplorer {
}
}

fn reset_create_folder_dialog(&mut self) {
self.create_folder_input.clear();
self.create_folder_dialog = false;
}

fn current_directory(&self) -> Option<&Path> {
if let Some(x) = self.directory_stack.iter().nth_back(self.directory_offset) {
return Some(x.as_path())
Expand Down Expand Up @@ -434,7 +410,7 @@ impl FileExplorer {
fn load_directory_content(&mut self, path: &Path) -> io::Result<()> {
let paths = fs::read_dir(path)?;

self.reset_create_folder_dialog();
self.create_directory_dialog.close();

self.directory_content.clear();

Expand All @@ -452,6 +428,57 @@ impl FileExplorer {
}
}

struct CreateDirectoryDialog {
open: bool,
input: String
}

impl CreateDirectoryDialog {
pub fn new() -> Self {
Self {
open: false,
input: String::new()
}
}

pub fn open(&mut self) {
self.reset();
self.open = true;
}

pub fn close(&mut self) {
self.reset();
}

pub fn update(&mut self, ui: &mut egui::Ui) {
if !self.open {
return;
}

ui.horizontal(|ui| {
// TODO: Add "Create new folder" icon
ui.text_edit_singleline(&mut self.input).scroll_to_me(None);
// TODO: Validate that the filename is unique
if ui.button("✔").clicked() {
// TODO: Create folder and append it to the end of directory_content
self.reset();
}
if ui.button("✖").clicked() {
self.reset();
}
});
}

pub fn is_open(&self) -> bool {
self.open
}

fn reset(&mut self) {
self.open = false;
self.input.clear();
}
}

fn ui_button_enabled_disabled(ui: &mut egui::Ui, size: egui::Vec2, text: &str,
active: bool) -> bool {
if !active {
Expand Down

0 comments on commit b106dfc

Please sign in to comment.