Skip to content

Commit ee90351

Browse files
committed
Merge branch 'show_meta_data' into sort_by_metadata
2 parents f00b153 + 2260c5a commit ee90351

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ cargo run --example save_file
7979

8080

8181
## Pick File with Information View
82+
8283
Example showing how to pick a file and display file information using the `InformationView`.
8384

8485
Requires the feature `information_view` as well as these dependencies:
@@ -92,7 +93,7 @@ image = { version = "0.25.5", features = ["bmp", "jpeg", "gif", "png", "tiff", "
9293
```
9394

9495
```shell
95-
cargo run --example select_file_with_information_view
96+
cargo run --example pick_file_with_information_view
9697
```
9798

9899
![Screenshot](../media/examples/information_view.png)
File renamed without changes.

src/file_dialog.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,11 @@ impl FileDialog {
12391239
pub fn state(&self) -> DialogState {
12401240
self.state.clone()
12411241
}
1242+
1243+
/// Get the window Id
1244+
pub const fn get_window_id(&self) -> egui::Id {
1245+
self.window_id
1246+
}
12421247
}
12431248

12441249
/// UI methods

src/information_panel.rs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{DirectoryEntry, FileDialog};
44
use chrono::{DateTime, Local};
55
use egui::ahash::{HashMap, HashMapExt};
6-
use egui::Ui;
6+
use egui::{Direction, Layout, Ui, Vec2};
77
use indexmap::{IndexMap, IndexSet};
88
use std::fs::File;
99
use std::io::{self, Read};
@@ -139,47 +139,23 @@ impl Default for InformationPanel {
139139
"jpg".to_string(),
140140
Box::new(
141141
|ui: &mut Ui, item: &InfoPanelEntry, stored_images: &mut IndexSet<String>| {
142-
ui.label("Image");
143-
stored_images.insert(format!("{}", item.directory_entry.as_path().display()));
144-
let image = egui::Image::new(format!(
145-
"file://{}",
146-
item.directory_entry.as_path().display()
147-
));
148-
ui.vertical_centered(|ui| {
149-
ui.add(image.max_height(ui.available_width()));
150-
});
142+
Self::show_image_preview(ui, item, stored_images);
151143
},
152144
) as Box<dyn FnMut(&mut Ui, &InfoPanelEntry, &mut IndexSet<String>)>,
153145
);
154146
supported_images.insert(
155147
"jpeg".to_string(),
156148
Box::new(
157149
|ui: &mut Ui, item: &InfoPanelEntry, stored_images: &mut IndexSet<String>| {
158-
ui.label("Image");
159-
stored_images.insert(format!("{}", item.directory_entry.as_path().display()));
160-
let image = egui::Image::new(format!(
161-
"file://{}",
162-
item.directory_entry.as_path().display()
163-
));
164-
ui.vertical_centered(|ui| {
165-
ui.add(image.max_height(ui.available_width()));
166-
});
150+
Self::show_image_preview(ui, item, stored_images);
167151
},
168152
) as Box<dyn FnMut(&mut Ui, &InfoPanelEntry, &mut IndexSet<String>)>,
169153
);
170154
supported_images.insert(
171155
"png".to_string(),
172156
Box::new(
173157
|ui: &mut Ui, item: &InfoPanelEntry, stored_images: &mut IndexSet<String>| {
174-
ui.label("Image");
175-
stored_images.insert(format!("{}", item.directory_entry.as_path().display()));
176-
let image = egui::Image::new(format!(
177-
"file://{}",
178-
item.directory_entry.as_path().display()
179-
));
180-
ui.vertical_centered(|ui| {
181-
ui.add(image.max_height(ui.available_width()));
182-
});
158+
Self::show_image_preview(ui, item, stored_images);
183159
},
184160
) as Box<dyn FnMut(&mut Ui, &InfoPanelEntry, &mut IndexSet<String>)>,
185161
);
@@ -199,6 +175,30 @@ impl Default for InformationPanel {
199175
}
200176

201177
impl InformationPanel {
178+
fn show_image_preview(
179+
ui: &mut Ui,
180+
item: &InfoPanelEntry,
181+
stored_images: &mut IndexSet<String>,
182+
) {
183+
ui.label("Image");
184+
stored_images.insert(format!("{}", item.directory_entry.as_path().display()));
185+
let image = egui::Image::new(format!(
186+
"file://{}",
187+
item.directory_entry.as_path().display()
188+
));
189+
let size = Vec2 {
190+
x: ui.available_width(),
191+
y: ui.available_width() / 4.0 * 3.0,
192+
};
193+
ui.allocate_ui_with_layout(
194+
size,
195+
Layout::centered_and_justified(Direction::TopDown),
196+
|ui| {
197+
ui.add(image);
198+
},
199+
);
200+
}
201+
202202
/// Adds support for previewing a custom file type.
203203
///
204204
/// # Arguments
@@ -294,15 +294,15 @@ impl InformationPanel {
294294
ui.add_space(spacing);
295295

296296
// show all metadata
297-
self.display_meta_data(ui, width, item);
297+
self.display_meta_data(ui, file_dialog.get_window_id(), width, item);
298298
}
299299
}
300300

301301
fn display_preview(&mut self, ui: &mut Ui, item: &DirectoryEntry) {
302302
if item.is_dir() {
303303
// show folder icon
304304
ui.vertical_centered(|ui| {
305-
ui.label(egui::RichText::from(item.icon()).size(120.0));
305+
ui.label(egui::RichText::from(item.icon()).size(ui.available_width() / 3.0));
306306
});
307307
} else {
308308
// Display file content preview based on its extension
@@ -322,21 +322,23 @@ impl InformationPanel {
322322
}
323323
} else if let Some(mut content) = panel_entry.content() {
324324
egui::ScrollArea::vertical()
325-
.max_height(100.0)
325+
.max_height(ui.available_width() / 3.0)
326326
.show(ui, |ui| {
327327
ui.add(egui::TextEdit::multiline(&mut content).code_editor());
328328
});
329329
} else {
330330
// if now preview is available, show icon
331331
ui.vertical_centered(|ui| {
332-
ui.label(egui::RichText::from(item.icon()).size(120.0));
332+
ui.label(
333+
egui::RichText::from(item.icon()).size(ui.available_width() / 3.0),
334+
);
333335
});
334336
}
335337
}
336338
} else {
337339
// if now ext is available, show icon anyway
338340
ui.vertical_centered(|ui| {
339-
ui.label(egui::RichText::from(item.icon()).size(120.0));
341+
ui.label(egui::RichText::from(item.icon()).size(ui.available_width() / 3.0));
340342
});
341343
}
342344
}
@@ -383,11 +385,11 @@ impl InformationPanel {
383385
}
384386
}
385387

386-
fn display_meta_data(&self, ui: &mut Ui, width: f32, item: &DirectoryEntry) {
388+
fn display_meta_data(&self, ui: &mut Ui, id: egui::Id, width: f32, item: &DirectoryEntry) {
387389
egui::ScrollArea::vertical()
388-
.id_salt("meta_data_scroll")
390+
.id_salt(id.with("meta_data_scroll"))
389391
.show(ui, |ui| {
390-
egui::Grid::new("meta_data")
392+
egui::Grid::new(id.with("meta_data_grid"))
391393
.num_columns(2)
392394
.striped(true)
393395
.min_col_width(width)

0 commit comments

Comments
 (0)