Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show and hide individual sections of left panel #57

Merged
merged 5 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### ✨ Features
- Added `FileDialog::take_selected` as an alternative to `FileDialog::selected` [#52](https://github.com/fluxxcode/egui-file-dialog/pull/52)
- Added `FileDialog::show_left_panel` to show or hide the left panel with the shortcut directories such as “Home”, “Documents”, etc. [#54](https://github.com/fluxxcode/egui-file-dialog/pull/54)
- Added `FileDialog::show_places`, `FileDialog::show_devices` and `FileDialog::show_removable_devices` to show or hide individual section of the left panel [#57](https://github.com/fluxxcode/egui-file-dialog/pull/57)

### 🔧 Changes
- Cleanup and restructure `FileDialog` UI methods [#56](https://github.com/fluxxcode/egui-file-dialog/pull/56)
Expand Down
97 changes: 87 additions & 10 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ pub struct FileDialog {
/// If the sidebar with the shortcut directories such as
/// “Home”, “Documents” etc. should be visible.
show_left_panel: bool,
/// If the Places section in the left sidebar should be visible.
show_places: bool,
/// If the Devices section in the left sidebar should be visible.
show_devices: bool,
/// If the Removable Devices section in the left sidebar should be visible.
show_removable_devices: bool,

/// The dialog that is shown when the user wants to create a new directory.
create_directory_dialog: CreateDirectoryDialog,
Expand Down Expand Up @@ -192,6 +198,9 @@ impl FileDialog {
window_title_bar: true,

show_left_panel: true,
show_places: true,
show_devices: true,
show_removable_devices: true,

create_directory_dialog: CreateDirectoryDialog::new(),

Expand Down Expand Up @@ -453,6 +462,33 @@ impl FileDialog {
self
}

/// Sets if the "Places" section should be visible in the left sidebar.
/// The Places section contains the user directories such as Home or Documents.
///
/// Has no effect when `FileDialog::show_left_panel` is disabled.
pub fn show_places(mut self, show_places: bool) -> Self {
self.show_places = show_places;
self
}

/// Sets if the "Devices" section should be visible in the left sidebar.
/// The Devices section contains the non removable system disks.
///
/// Has no effect when `FileDialog::show_left_panel` is disabled.
pub fn show_devices(mut self, show_devices: bool) -> Self {
self.show_devices = show_devices;
self
}

/// Sets if the "Removable Devices" section should be visible in the left sidebar.
/// The Removable Devices section contains the removable disks like USB disks.
///
/// Has no effect when `FileDialog::show_left_panel` is disabled.
pub fn show_removable_devices(mut self, show_removable_devices: bool) -> Self {
self.show_removable_devices = show_removable_devices;
self
}

// -------------------------------------------------
// Getter:

Expand Down Expand Up @@ -733,29 +769,43 @@ impl FileDialog {
}

/// Updates the left panel of the dialog. Including the list of the user directories (Places)
/// and system disks.
/// and system disks (Devices, Removable Devices).
fn ui_update_left_panel(&mut self, ui: &mut egui::Ui) {
ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
egui::containers::ScrollArea::vertical()
.auto_shrink([false, false])
.show(ui, |ui| {
ui.add_space(ui.ctx().style().spacing.item_spacing.y * 2.0);
let mut spacing = ui.ctx().style().spacing.item_spacing.y * 2.0;

self.ui_update_user_directories(ui);
if self.show_places && self.ui_update_user_directories(ui, spacing) {
spacing = ui.ctx().style().spacing.item_spacing.y * 4.0;
}

let disks = std::mem::take(&mut self.system_disks);

self.ui_update_devices(ui, &disks);
self.ui_update_removable_devices(ui, &disks);
if self.show_devices && self.ui_update_devices(ui, spacing, &disks) {
spacing = ui.ctx().style().spacing.item_spacing.y * 4.0;
}

if self.show_removable_devices
&& self.ui_update_removable_devices(ui, spacing, &disks)
{
// Add this when we add a new section after removable devices
// spacing = ui.ctx().style().spacing.item_spacing.y * 4.0;
}

self.system_disks = disks;
});
});
}

/// Updates the list of the user directories (Places).
fn ui_update_user_directories(&mut self, ui: &mut egui::Ui) {
///
/// Returns true if at least one directory was included in the list and the
/// heading is visible. If no directory was listed, false is returned.
fn ui_update_user_directories(&mut self, ui: &mut egui::Ui, spacing: f32) -> bool {
if let Some(dirs) = self.user_directories.clone() {
ui.add_space(spacing);
ui.label("Places");

if let Some(path) = dirs.home_dir() {
Expand Down Expand Up @@ -815,31 +865,58 @@ impl FileDialog {
let _ = self.load_directory(path);
}
}

return true;
}

false
}

/// Updates the list of devices like system disks
fn ui_update_devices(&mut self, ui: &mut egui::Ui, disks: &Disks) {
///
/// Returns true if at least one device was included in the list and the
/// heading is visible. If no device was listed, false is returned.
fn ui_update_devices(&mut self, ui: &mut egui::Ui, spacing: f32, disks: &Disks) -> bool {
let mut visible = false;

for (i, disk) in disks.iter().filter(|x| !x.is_removable()).enumerate() {
if i == 0 {
ui.add_space(ui.style().spacing.item_spacing.y * 4.0);
ui.add_space(spacing);
ui.label("Devices");

visible = true;
}

self.ui_update_device_entry(ui, disk);
}

visible
}

/// Updates the list of removable devices like USB drives
fn ui_update_removable_devices(&mut self, ui: &mut egui::Ui, disks: &Disks) {
///
/// Returns true if at least one device was included in the list and the
/// heading is visible. If no device was listed, false is returned.
fn ui_update_removable_devices(
&mut self,
ui: &mut egui::Ui,
spacing: f32,
disks: &Disks,
) -> bool {
let mut visible = false;

for (i, disk) in disks.iter().filter(|x| x.is_removable()).enumerate() {
if i == 0 {
ui.add_space(ui.style().spacing.item_spacing.y * 4.0);
ui.add_space(spacing);
ui.label("Removable Devices");

visible = true;
}

self.ui_update_device_entry(ui, disk);
}

visible
}

/// Updates a device entry of a device list like "Devices" or "Removable Devices".
Expand Down
Loading