From f22c325ccb55113e4ef933b6553c9b1eccb8aac9 Mon Sep 17 00:00:00 2001 From: Jannis Date: Wed, 29 May 2024 21:11:27 +0200 Subject: [PATCH] Fix modal (#121) * Remove keep_focus option * Fix modal --- CHANGELOG.md | 3 +-- src/config/mod.rs | 4 ---- src/file_dialog.rs | 57 ++++++++++++++++++++++------------------------ 3 files changed, 28 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0499c9fe..8a1c69b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## 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) @@ -11,7 +11,6 @@ - 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 diff --git a/src/config/mod.rs b/src/config/mod.rs index 00eac768..ce0d7f38 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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. @@ -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, diff --git a/src/file_dialog.rs b/src/file_dialog.rs index cf454dbc..3dbc683b 100644 --- a/src/file_dialog.rs +++ b/src/file_dialog.rs @@ -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. @@ -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; @@ -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 @@ -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,