Skip to content

Commit

Permalink
Add FileDialog::anchor()
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxxcode committed Feb 4, 2024
1 parent 1e34e5a commit c884dd9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
3 changes: 2 additions & 1 deletion examples/sandbox/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ struct MyApp {
impl MyApp {
pub fn new(_cc: &eframe::CreationContext) -> Self {
Self {
file_dialog: FileDialog::new(),
file_dialog: FileDialog::new()
.anchor(egui::Align2::CENTER_CENTER, egui::Vec2::new(0.0, 0.0)),

selected_directory: None,
selected_file: None,
Expand Down
73 changes: 46 additions & 27 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub struct FileDialog {
window_title: String,
/// The default size of the window.
default_window_size: egui::Vec2,
/// The anchor of the window.
window_anchor: Option<(egui::Align2, egui::Vec2)>,

/// The dialog that is shown when the user wants to create a new directory.
create_directory_dialog: CreateDirectoryDialog,
Expand Down Expand Up @@ -146,6 +148,7 @@ impl FileDialog {

window_title: String::from("Select directory"),
default_window_size: egui::Vec2::new(650.0, 370.0),
window_anchor: None,

create_directory_dialog: CreateDirectoryDialog::new(),

Expand Down Expand Up @@ -177,6 +180,12 @@ impl FileDialog {
self
}

/// Sets the anchor of the window.
pub fn anchor(mut self, align: egui::Align2, offset: egui::Vec2) -> Self {
self.window_anchor = Some((align, offset));
self
}

/// Sets the default file name when opening the dialog in `DialogMode::SaveFile` mode.
pub fn default_file_name(mut self, name: &str) -> Self {
self.default_file_name = name.to_string();
Expand Down Expand Up @@ -284,37 +293,31 @@ impl FileDialog {

let mut is_open = true;

egui::Window::new(&self.window_title)
.open(&mut is_open)
.default_size(self.default_window_size)
.min_width(335.0)
.min_height(200.0)
.collapsible(false)
.show(ctx, |ui| {
egui::TopBottomPanel::top("fe_top_panel")
.resizable(false)
.show_inside(ui, |ui| {
self.ui_update_top_panel(ctx, ui);
});

egui::SidePanel::left("fe_left_panel")
.resizable(true)
.default_width(150.0)
.width_range(90.0..=250.0)
.show_inside(ui, |ui| {
self.ui_update_left_panel(ctx, ui);
});
self.create_window(&mut is_open).show(ctx, |ui| {
egui::TopBottomPanel::top("fe_top_panel")
.resizable(false)
.show_inside(ui, |ui| {
self.ui_update_top_panel(ctx, ui);
});

egui::TopBottomPanel::bottom("fe_bottom_panel")
.resizable(false)
.show_inside(ui, |ui| {
self.ui_update_bottom_panel(ctx, ui);
});
egui::SidePanel::left("fe_left_panel")
.resizable(true)
.default_width(150.0)
.width_range(90.0..=250.0)
.show_inside(ui, |ui| {
self.ui_update_left_panel(ctx, ui);
});

egui::CentralPanel::default().show_inside(ui, |ui| {
self.ui_update_central_panel(ui);
egui::TopBottomPanel::bottom("fe_bottom_panel")
.resizable(false)
.show_inside(ui, |ui| {
self.ui_update_bottom_panel(ctx, ui);
});

egui::CentralPanel::default().show_inside(ui, |ui| {
self.ui_update_central_panel(ui);
});
});

// User closed the window without finishing the dialog
if !is_open {
Expand All @@ -324,6 +327,22 @@ impl FileDialog {
self
}

/// Creates a new egui window with the configured options.
fn create_window<'a>(&self, is_open: &'a mut bool) -> egui::Window<'a> {
let mut window = egui::Window::new(&self.window_title)
.open(is_open)
.default_size(self.default_window_size)
.min_width(355.0)
.min_height(200.0)
.collapsible(false);

if let Some((anchor, offset)) = self.window_anchor {
window = window.anchor(anchor, offset);
}

window
}

/// Updates the top panel of the dialog. Including the navigation buttons,
/// the current path display, the reload button and the search field.
fn ui_update_top_panel(&mut self, ctx: &egui::Context, ui: &mut egui::Ui) {
Expand Down

0 comments on commit c884dd9

Please sign in to comment.