Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FileDialog::{active_entry, update_with_custom_right_panel} #170

Merged
merged 10 commits into from
Oct 25, 2024
60 changes: 58 additions & 2 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ impl Debug for dyn FileDialogModal + Send + Sync {
}
}

/// Callback type to inject a custom egui ui inside the file dialog's ui.
///
/// Also gives access to the file dialog, since it would otherwise be inaccessible
/// inside the closure.
type FileDialogUiCallback<'a> = dyn FnMut(&mut egui::Ui, &mut FileDialog) + 'a;
fluxxcode marked this conversation as resolved.
Show resolved Hide resolved

impl FileDialog {
// ------------------------------------------------------------------------
// Creation:
Expand Down Expand Up @@ -383,7 +389,33 @@ impl FileDialog {
}

self.update_keybindings(ctx);
self.update_ui(ctx);
self.update_ui(ctx, None);

self
}

/// Do an [update](`Self::update`) with a custom right panel ui.
///
/// Example use cases:
/// - Show custom information for a file (size, MIME type, etc.)
/// - Embed a preview, like a thumbnail for an image
/// - Add controls for custom open options, like open as read-only, etc.
///
/// See [`active_entry`](Self::active_entry) to get the active directory entry
/// to show the information for.
///
/// This function has no effect if the dialog state is currently not `DialogState::Open`.
pub fn update_with_custom_right_panel(
fluxxcode marked this conversation as resolved.
Show resolved Hide resolved
&mut self,
ctx: &egui::Context,
f: &mut FileDialogUiCallback,
) -> &Self {
if self.state != DialogState::Open {
return self;
}

self.update_keybindings(ctx);
self.update_ui(ctx, Some(f));

self
}
Expand Down Expand Up @@ -964,6 +996,13 @@ impl FileDialog {
}
}

/// Returns the currently active directory entry.
///
/// If no file has been highlighted, it returns the active directory.
pub const fn active_entry(&self) -> Option<&DirectoryEntry> {
fluxxcode marked this conversation as resolved.
Show resolved Hide resolved
self.selected_item.as_ref()
}

/// Returns the ID of the operation for which the dialog is currently being used.
///
/// See `FileDialog::open` for more information.
Expand All @@ -985,7 +1024,13 @@ impl FileDialog {
/// UI methods
impl FileDialog {
/// Main update method of the UI
fn update_ui(&mut self, ctx: &egui::Context) {
///
/// Takes an optional callback to show a custom right panel.
fn update_ui(
&mut self,
ctx: &egui::Context,
right_panel_fn: Option<&mut FileDialogUiCallback>,
) {
let mut is_open = true;

if self.config.as_modal {
Expand Down Expand Up @@ -1017,6 +1062,17 @@ impl FileDialog {
});
}

// Optionally, show a custom right panel (see `update_with_custom_right_panel`)
if let Some(f) = right_panel_fn {
egui::SidePanel::right(self.window_id.with("right_panel"))
// Unlike the left panel, we have no control over the contents, so
// we don't restrict the width. It's up to the user to make the UI presentable.
.resizable(true)
.show_inside(ui, |ui| {
f(ui, self);
});
}

egui::TopBottomPanel::bottom(self.window_id.with("bottom_panel"))
.resizable(false)
.show_inside(ui, |ui| {
Expand Down
Loading