From 5c26325bb6f9d2d8a476aaf0d47d071bbec4914e Mon Sep 17 00:00:00 2001 From: Jannis Date: Mon, 5 Feb 2024 22:37:51 +0100 Subject: [PATCH] Use ui.add_enabled instead of custom UI module (#22) * Use ui.add_enabled instead of custom ui module * Update CHANGELOG.md --- CHANGELOG.md | 1 + src/create_directory_dialog.rs | 7 +++--- src/file_dialog.rs | 41 +++++++++++++++++++------------- src/lib.rs | 1 - src/ui.rs | 43 ---------------------------------- 5 files changed, 30 insertions(+), 63 deletions(-) delete mode 100644 src/ui.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index a51428f4..6488b87a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ ### 🔧 Changes - Removed the version of `egui-file-dialog` in the examples [#8](https://github.com/fluxxcode/egui-file-dialog/pull/8) +- Use `ui.add_enabled` instead of custom `ui.rs` module [#22](https://github.com/fluxxcode/egui-file-dialog/pull/22) ### 📚 Documentation - Fix syntax highlighting on crates.io [#9](https://github.com/fluxxcode/egui-file-dialog/pull/9) diff --git a/src/create_directory_dialog.rs b/src/create_directory_dialog.rs index 6036b178..7027c588 100644 --- a/src/create_directory_dialog.rs +++ b/src/create_directory_dialog.rs @@ -1,8 +1,6 @@ use std::fs; use std::path::{Path, PathBuf}; -use crate::ui; - pub struct CreateDirectoryResponse { /// Contains the path to the directory that was created. directory: Option, @@ -100,7 +98,10 @@ impl CreateDirectoryDialog { self.error = self.validate_input(); } - if ui::button_enabled_disabled(ui, "✔", self.error.is_none()) { + if ui + .add_enabled(self.error.is_none(), egui::Button::new("✔")) + .clicked() + { result = self.create_directory(); } diff --git a/src/file_dialog.rs b/src/file_dialog.rs index 0bfdc847..cfb44a91 100644 --- a/src/file_dialog.rs +++ b/src/file_dialog.rs @@ -6,7 +6,6 @@ use std::{fs, io}; use crate::create_directory_dialog::CreateDirectoryDialog; use crate::data::{DirectoryContent, DirectoryEntry, Disks, UserDirectories}; -use crate::ui; /// Represents the mode the file dialog is currently in. #[derive(Debug, PartialEq, Eq, Clone, Copy)] @@ -452,37 +451,31 @@ impl FileDialog { ui.horizontal(|ui| { // Navigation buttons if let Some(x) = self.current_directory() { - if ui::button_sized_enabled_disabled(ui, NAV_BUTTON_SIZE, "⏶", x.parent().is_some()) - { + if self.ui_button_sized(ui, x.parent().is_some(), NAV_BUTTON_SIZE, "⏶") { let _ = self.load_parent_directory(); } } else { - let _ = ui::button_sized_enabled_disabled(ui, NAV_BUTTON_SIZE, "⏶", false); + let _ = self.ui_button_sized(ui, false, NAV_BUTTON_SIZE, "⏶"); } - if ui::button_sized_enabled_disabled( + if self.ui_button_sized( ui, + self.directory_offset + 1 < self.directory_stack.len(), NAV_BUTTON_SIZE, "⏴", - self.directory_offset + 1 < self.directory_stack.len(), ) { let _ = self.load_previous_directory(); } - if ui::button_sized_enabled_disabled( - ui, - NAV_BUTTON_SIZE, - "⏵", - self.directory_offset != 0, - ) { + if self.ui_button_sized(ui, self.directory_offset != 0, NAV_BUTTON_SIZE, "⏵") { let _ = self.load_next_directory(); } - if ui::button_sized_enabled_disabled( + if self.ui_button_sized( ui, + !self.create_directory_dialog.is_open(), NAV_BUTTON_SIZE, "+", - !self.create_directory_dialog.is_open(), ) { if let Some(x) = self.current_directory() { self.create_directory_dialog.open(x.to_path_buf()); @@ -655,8 +648,7 @@ impl FileDialog { DialogMode::SaveFile => "📥 Save", }; - if ui::button_sized_enabled_disabled(ui, BUTTON_SIZE, label, self.is_selection_valid()) - { + if self.ui_button_sized(ui, self.is_selection_valid(), BUTTON_SIZE, label) { match &self.mode { DialogMode::SelectDirectory | DialogMode::SelectFile => { // self.selected_item should always contain a value, @@ -863,6 +855,23 @@ impl FileDialog { self.system_disks = disks; } + /// Helper function to add a sized button that can be enabled or disabled + fn ui_button_sized( + &self, + ui: &mut egui::Ui, + enabled: bool, + size: egui::Vec2, + label: &str, + ) -> bool { + let mut clicked = false; + + ui.add_enabled_ui(enabled, |ui| { + clicked = ui.add_sized(size, egui::Button::new(label)).clicked(); + }); + + clicked + } + /// Resets the dialog to use default values. /// Configuration variables such as `initial_directory` are retained. fn reset(&mut self) { diff --git a/src/lib.rs b/src/lib.rs index ed303999..a9d556d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,4 +58,3 @@ pub use file_dialog::{DialogMode, DialogState, FileDialog}; mod create_directory_dialog; mod data; -mod ui; diff --git a/src/ui.rs b/src/ui.rs deleted file mode 100644 index 173533ab..00000000 --- a/src/ui.rs +++ /dev/null @@ -1,43 +0,0 @@ -/// Adds a dynamically sized button to the UI that can be enabled or disabled. -/// Returns true if the button is clicked. Otherwise None is returned. -pub fn button_enabled_disabled(ui: &mut egui::Ui, text: &str, enabled: bool) -> bool { - if !enabled { - let button = egui::Button::new(text) - .stroke(egui::Stroke::NONE) - .fill(get_disabled_fill_color(ui)); - - let _ = ui.add(button); - - return false; - } - - ui.add(egui::Button::new(text)).clicked() -} - -/// Adds a fixed sized button to the UI that can be enabled or disabled. -/// Returns true if the button is clicked. Otherwise None is returned. -pub fn button_sized_enabled_disabled( - ui: &mut egui::Ui, - size: egui::Vec2, - text: &str, - enabled: bool, -) -> bool { - if !enabled { - let button = egui::Button::new(text) - .stroke(egui::Stroke::NONE) - .fill(get_disabled_fill_color(ui)); - - let _ = ui.add_sized(size, button); - - return false; - } - - ui.add_sized(size, egui::Button::new(text)).clicked() -} - -/// Returns the fill color of disabled buttons -#[inline] -fn get_disabled_fill_color(ui: &egui::Ui) -> egui::Color32 { - let c = ui.style().visuals.widgets.noninteractive.bg_fill; - egui::Color32::from_rgba_premultiplied(c.r(), c.g(), c.b(), 100) -}