Skip to content

Commit

Permalink
create metadata struct
Browse files Browse the repository at this point in the history
  • Loading branch information
hacknus committed Nov 26, 2024
1 parent 0bdb134 commit 975a0e6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
4 changes: 2 additions & 2 deletions examples/select_file_with_information_view/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ impl MyApp {
information_panel: InformationPanel::default()
.add_file_preview("csv", |ui, item| {
ui.label("CSV preview:");
if let Some(content) = item.content() {
if let Some(mut content) = item.content() {
egui::ScrollArea::vertical()
.max_height(150.0)
.show(ui, |ui| {
ui.add(
egui::TextEdit::multiline(&mut content.clone()).code_editor(),
egui::TextEdit::multiline(&mut content).code_editor(),
);
});
}
Expand Down
68 changes: 27 additions & 41 deletions src/data/directory_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ pub fn format_pixels(pixels: u32) -> String {
}

/// Contains the metadata of a directory item.
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Metadata {
pub size: Option<u64>,
pub last_modified: Option<SystemTime>,
pub created: Option<SystemTime>,
pub file_type: Option<String>,
}

/// Contains the information of a directory item.
///
/// This struct is mainly there so that the metadata can be loaded once and not that
/// This struct is mainly there so that the information and metadata can be loaded once and not that
/// a request has to be sent to the OS every frame using, for example, `path.is_file()`.
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct DirectoryEntry {
path: PathBuf,
size: Option<u64>,
last_modified: Option<SystemTime>,
created: Option<SystemTime>,
file_type: Option<String>,
metadata: Metadata,
is_directory: bool,
is_system_file: bool,
#[cfg(feature = "metadata_view")]
Expand All @@ -41,24 +48,18 @@ pub struct DirectoryEntry {
impl DirectoryEntry {
/// Creates a new directory entry from a path
pub fn from_path(config: &FileDialogConfig, path: &Path) -> Self {
let mut size = None;
let mut last_modified = None;
let mut created = None;
let mut file_type = None;

if let Ok(metadata) = fs::metadata(path) {
size = Some(metadata.len());
last_modified = metadata.modified().ok();
created = metadata.created().ok();
file_type = Some(format!("{:?}", metadata.file_type()));
let mut metadata = Metadata::default();

if let Ok(md) = fs::metadata(path) {
metadata.size = Some(md.len());
metadata.last_modified = md.modified().ok();
metadata.created = md.created().ok();
metadata.file_type = Some(format!("{:?}", md.file_type()));
}

Self {
path: path.to_path_buf(),
size,
last_modified,
created,
file_type,
metadata,
is_directory: path.is_dir(),
is_system_file: !path.is_dir() && !path.is_file(),
#[cfg(feature = "metadata_view")]
Expand All @@ -68,6 +69,11 @@ impl DirectoryEntry {
}
}

/// Returns the metadata of the directory entry.
pub const fn metadata(&self) -> &Metadata {
&self.metadata
}

/// Checks if the path of the current directory entry matches the other directory entry.
pub fn path_eq(&self, other: &Self) -> bool {
other.as_path() == self.as_path()
Expand Down Expand Up @@ -107,26 +113,6 @@ impl DirectoryEntry {
self.path.clone()
}

/// Returns the size of the directory item.
pub const fn size(&self) -> Option<u64> {
self.size
}

/// Clones the `FileType` struct of the directory item.
pub fn file_type(&self) -> Option<String> {
self.file_type.clone()
}

/// Returns the created date of the directory item.
pub const fn created(&self) -> Option<SystemTime> {
self.created
}

/// Returns the last modified date of the directory item.
pub const fn last_modified(&self) -> Option<SystemTime> {
self.last_modified
}

/// Returns the file name of the directory item.
pub fn file_name(&self) -> &str {
self.path
Expand Down Expand Up @@ -171,8 +157,8 @@ impl DirectoryEntry {
#[cfg(feature = "metadata_view")]
impl DirectoryEntry {
/// Clones the content of the directory item, if available
pub fn content(&self) -> Option<String> {
self.content.clone()
pub fn content(&self) -> Option<&str> {
self.content.as_deref()
}

/// Sets the content of the directory item
Expand Down
14 changes: 7 additions & 7 deletions src/data/information_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ impl Default for InformationPanel {
supported_files.insert(
text_extension.to_string(),
Box::new(|ui: &mut Ui, item: &DirectoryEntry| {
if let Some(content) = item.content() {
if let Some(mut content) = item.content() {
egui::ScrollArea::vertical()
.max_height(100.0)
.show(ui, |ui| {
ui.add(
egui::TextEdit::multiline(&mut content.clone()).code_editor(),
egui::TextEdit::multiline(&mut content).code_editor(),
);
});
}
Expand Down Expand Up @@ -232,12 +232,12 @@ impl InformationPanel {
self.supported_preview_files.get_mut(&ext.to_lowercase())
{
show_preview(ui, item);
} else if let Some(content) = item.content() {
} else if let Some(mut content) = item.content() {
egui::ScrollArea::vertical()
.max_height(100.0)
.show(ui, |ui| {
ui.add(
egui::TextEdit::multiline(&mut content.clone()).code_editor(),
egui::TextEdit::multiline(&mut content).code_editor(),
);
});
} else {
Expand Down Expand Up @@ -268,7 +268,7 @@ impl InformationPanel {
ui.label(item.file_name().to_string());
ui.end_row();

if let Some(size) = item.size() {
if let Some(size) = item.metadata().size {
ui.label("File Size: ");
if item.is_file() {
ui.label(format_bytes(size));
Expand All @@ -278,14 +278,14 @@ impl InformationPanel {
ui.end_row();
}

if let Some(date) = item.created() {
if let Some(date) = item.metadata().created {
ui.label("Created: ");
let created: DateTime<Local> = date.into();
ui.label(format!("{}", created.format("%Y-%m-%d %H:%M:%S")));
ui.end_row();
}

if let Some(date) = item.last_modified() {
if let Some(date) = item.metadata().last_modified {
ui.label("Last Modified: ");
let modified: DateTime<Local> = date.into();
ui.label(format!("{}", modified.format("%Y-%m-%d %H:%M:%S")));
Expand Down

0 comments on commit 975a0e6

Please sign in to comment.