Skip to content

Commit

Permalink
Use ui.add_enabled instead of custom UI module (#22)
Browse files Browse the repository at this point in the history
* Use ui.add_enabled instead of custom ui module

* Update CHANGELOG.md
  • Loading branch information
fluxxcode authored Feb 5, 2024
1 parent 3f795fc commit 5c26325
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 63 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/create_directory_dialog.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>,
Expand Down Expand Up @@ -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();
}

Expand Down
41 changes: 25 additions & 16 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,3 @@ pub use file_dialog::{DialogMode, DialogState, FileDialog};

mod create_directory_dialog;
mod data;
mod ui;
43 changes: 0 additions & 43 deletions src/ui.rs

This file was deleted.

0 comments on commit 5c26325

Please sign in to comment.