Skip to content

Commit

Permalink
Fix modal (#121)
Browse files Browse the repository at this point in the history
* Remove keep_focus option

* Fix modal
  • Loading branch information
fluxxcode authored May 29, 2024
1 parent f93954c commit f22c325
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
## Unreleased - v0.6.0
### 🚨 Breaking Changes
- Added new labels to `FileDialogLabels` [#100](https://github.com/fluxxcode/egui-file-dialog/pull/100), [#111](https://github.com/fluxxcode/egui-file-dialog/pull/111)
- Added new configuration values to `FileDialogConfig` [#100](https://github.com/fluxxcode/egui-file-dialog/pull/100), [#104](https://github.com/fluxxcode/egui-file-dialog/pull/104), [#106](https://github.com/fluxxcode/egui-file-dialog/pull/106), [#110](https://github.com/fluxxcode/egui-file-dialog/pull/110), [#111](https://github.com/fluxxcode/egui-file-dialog/pull/111), [#112](https://github.com/fluxxcode/egui-file-dialog/pull/112), [#118](https://github.com/fluxxcode/egui-file-dialog/pull/118)
- Added new configuration values to `FileDialogConfig` [#100](https://github.com/fluxxcode/egui-file-dialog/pull/100), [#104](https://github.com/fluxxcode/egui-file-dialog/pull/104), [#106](https://github.com/fluxxcode/egui-file-dialog/pull/106), [#110](https://github.com/fluxxcode/egui-file-dialog/pull/110), [#111](https://github.com/fluxxcode/egui-file-dialog/pull/111), [#118](https://github.com/fluxxcode/egui-file-dialog/pull/118)

### ✨ Features
- Added the ability to pin folders to the left sidebar and enable or disable the feature with `FileDialog::show_pinned_folders` [#100](https://github.com/fluxxcode/egui-file-dialog/pull/100)
- Added `FileDialogConfig::storage`, `FileDialog::storage` and `FileDialog::storage_mut` to be able to save and load persistent data [#104](https://github.com/fluxxcode/egui-file-dialog/pull/104) and [#105](https://github.com/fluxxcode/egui-file-dialog/pull/105)
- Added new modal and option `FileDialog::allow_file_overwrite` to allow overwriting an already existing file when the dialog is in `DialogMode::SaveFile` mode [#106](https://github.com/fluxxcode/egui-file-dialog/pull/106)
- Implemented customizable keyboard navigation using `FileDialogKeybindings` and `FileDialog::keybindings` [#110](https://github.com/fluxxcode/egui-file-dialog/pull/110)
- Implemented show hidden files and folders option [#111](https://github.com/fluxxcode/egui-file-dialog/pull/111)
- Implemented `FileDialog::keep_focus` which keeps the file dialog in focus so it appears on top of all other windows, even if the user clicks outside the window [#112](https://github.com/fluxxcode/egui-file-dialog/pull/112)
- The dialog is now displayed as a modal window by default. This can be disabled with `FileDialog::as_modal`. The color of the modal overlay can be adjusted using `FileDialog::modal_overlay_color`. [#118](https://github.com/fluxxcode/egui-file-dialog/pull/118)

### ☢️ Deprecated
Expand Down
4 changes: 0 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ pub struct FileDialogConfig {
pub as_modal: bool,
/// Color of the overlay that is displayed under the modal to prevent user interaction.
pub modal_overlay_color: egui::Color32,
/// If the file dialog window should keep focus and appear on top of all other windows,
/// even if the user clicks outside the window.
pub keep_focus: bool,
/// The first directory that will be opened when the dialog opens.
pub initial_directory: PathBuf,
/// The default filename when opening the dialog in `DialogMode::SaveFile` mode.
Expand Down Expand Up @@ -192,7 +189,6 @@ impl Default for FileDialogConfig {

as_modal: true,
modal_overlay_color: egui::Color32::from_rgba_premultiplied(0, 0, 0, 120),
keep_focus: true,
initial_directory: std::env::current_dir().unwrap_or_default(),
default_file_name: String::new(),
allow_file_overwrite: true,
Expand Down
57 changes: 27 additions & 30 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,6 @@ impl FileDialog {
self
}

/// If the file dialog window should keep focus and appear on top of all other windows,
/// even if the user clicks outside the window.
/// However, this does not prevent the user from using other widgets outside of the
/// file dialog.
pub fn keep_focus(mut self, keep_focus: bool) -> Self {
self.config.keep_focus = keep_focus;
self
}

/// Sets the first loaded directory when the dialog opens.
/// If the path is a file, the file's parent directory is used. If the path then has no
/// parent directory or cannot be loaded, the user will receive an error.
Expand Down Expand Up @@ -861,29 +852,11 @@ impl FileDialog {
let mut is_open = true;

if self.config.as_modal {
let re_area = egui::Area::new(egui::Id::from("Test"))
.interactable(true)
.fixed_pos(egui::Pos2::ZERO)
.show(ctx, |ui| {
let screen_rect = ctx.input(|i| i.screen_rect);

ui.allocate_response(screen_rect.size(), egui::Sense::click());

ui.painter().rect_filled(
screen_rect,
egui::Rounding::ZERO,
self.config.modal_overlay_color,
);
});

ctx.move_to_top(re_area.response.layer_id);
let re = self.ui_update_modal_background(ctx);
ctx.move_to_top(re.response.layer_id);
}

self.create_window(&mut is_open).show(ctx, |ui| {
if self.config.keep_focus || self.config.as_modal {
ui.ctx().move_to_top(ui.layer_id());
}

let re = self.create_window(&mut is_open).show(ctx, |ui| {
if !self.modals.is_empty() {
self.ui_update_modals(ui);
return;
Expand Down Expand Up @@ -918,6 +891,12 @@ impl FileDialog {
});
});

if self.config.as_modal {
if let Some(inner_response) = re {
ctx.move_to_top(inner_response.response.layer_id);
}
}

self.any_focused_last_frame = ctx.memory(|r| r.focused()).is_some();

// User closed the window without finishing the dialog
Expand All @@ -926,6 +905,24 @@ impl FileDialog {
}
}

/// Updates the main modal background of the file dialog window.
fn ui_update_modal_background(&self, ctx: &egui::Context) -> egui::InnerResponse<()> {
egui::Area::new(egui::Id::from("fe_modal_overlay"))
.interactable(true)
.fixed_pos(egui::Pos2::ZERO)
.show(ctx, |ui| {
let screen_rect = ctx.input(|i| i.screen_rect);

ui.allocate_response(screen_rect.size(), egui::Sense::click());

ui.painter().rect_filled(
screen_rect,
egui::Rounding::ZERO,
self.config.modal_overlay_color,
);
})
}

fn ui_update_modals(&mut self, ui: &mut egui::Ui) {
// Currently, a rendering error occurs when only a single central panel is rendered
// inside a window. Therefore, when rendering a modal, we render an invisible bottom panel,
Expand Down

0 comments on commit f22c325

Please sign in to comment.