Skip to content

Commit

Permalink
fix: scroll to selection (fluxxcode#199)
Browse files Browse the repository at this point in the history
* Cleanup dopped files

* Fix scroll to selection
  • Loading branch information
fluxxcode authored Nov 19, 2024
1 parent e983dd0 commit 8ac548b
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ pub struct FileDialog {
/// This is used to prevent the dialog from closing when pressing the escape key
/// inside a text input.
any_focused_last_frame: bool,

/// flag to set if files have been dropped
new_file_dropped: bool,
}

/// This tests if file dialog is send and sync.
Expand Down Expand Up @@ -232,7 +229,6 @@ impl FileDialog {
init_search: false,

any_focused_last_frame: false,
new_file_dropped: false,
}
}

Expand Down Expand Up @@ -1140,28 +1136,30 @@ impl FileDialog {
self.cancel();
}

let mut repaint = false;

// Collect dropped files:
ctx.input(|i| {
// check if files were dropped
// Check if files were dropped
if let Some(dropped_file) = i.raw.dropped_files.last() {
if let Some(path) = &dropped_file.path {
if path.is_dir() {
// if we dropped a directory, go there
// If we dropped a directory, go there
self.load_directory(path.as_path());
self.new_file_dropped = true;
repaint = true;
} else if let Some(parent) = path.parent() {
// else, go to the parent directory
// Else, go to the parent directory
self.load_directory(parent);
self.select_item(&mut DirectoryEntry::from_path(&self.config, path));
self.scroll_to_selection = true;
self.new_file_dropped = true;
repaint = true;
}
}
}
});

// update GUI if we dropped a file
if self.new_file_dropped {
// Update GUI if we dropped a file
if repaint {
ctx.request_repaint();
}
}
Expand Down Expand Up @@ -2052,15 +2050,11 @@ impl FileDialog {
true
}
DirectoryContentState::Finished => {
// don't override the selected dropped item with the active directory
if self.new_file_dropped {
// clear the flag and return
self.new_file_dropped = false;
return true;
}
if let Some(dir) = self.current_directory() {
let mut dir_entry = DirectoryEntry::from_path(&self.config, dir);
self.select_item(&mut dir_entry);
if self.mode == DialogMode::SelectDirectory {
if let Some(dir) = self.current_directory() {
let mut dir_entry = DirectoryEntry::from_path(&self.config, dir);
self.select_item(&mut dir_entry);
}
}

false
Expand Down Expand Up @@ -2089,9 +2083,13 @@ impl FileDialog {
ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
let scroll_area = egui::containers::ScrollArea::vertical().auto_shrink([false, false]);

if self.search_value.is_empty() && !self.create_directory_dialog.is_open() {
// Only update visible items when the search value is empty
// and the create directory dialog is not open.
if self.search_value.is_empty()
&& !self.create_directory_dialog.is_open()
&& !self.scroll_to_selection
{
// Only update visible items when the search value is empty,
// the create directory dialog is closed and we are currently not scrolling
// to the current item.
scroll_area.show_rows(
ui,
ui.text_style_height(&egui::TextStyle::Body),
Expand Down

0 comments on commit 8ac548b

Please sign in to comment.