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

Add image dimension and file size information #21675

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/image_viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ theme.workspace = true
ui.workspace = true
util.workspace = true
workspace.workspace = true
image = "0.25.5"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's elevate this dependency, and the one in GPUI, to a workspace dependency

46 changes: 45 additions & 1 deletion crates/image_viewer/src/image_viewer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use image::GenericImageView;
use std::fs::metadata;
use std::path::PathBuf;

use anyhow::Context as _;
Expand Down Expand Up @@ -122,8 +124,21 @@ impl Item for ImageView {

fn breadcrumbs(&self, _theme: &Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
let text = breadcrumbs_text_for_image(self.project.read(cx), self.image_item.read(cx), cx);

let img_info = image_info(self.image_item.read(cx), self.project.read(cx), cx)
.map(|(width, height, size)| {
format!(
"{} | Dimension: {}x{} | Image size: {:.2} KB",
text,
width,
height,
size as f64 / 1024.0
)
})
.unwrap_or_else(|err| format!("{} | Image info is not available: {}", text, err));

Some(vec![BreadcrumbText {
text,
text: img_info,
highlights: None,
font: None,
}])
Expand All @@ -145,6 +160,35 @@ impl Item for ImageView {
}
}

fn image_info(
image: &ImageItem,
project: &Project,
cx: &AppContext,
) -> Result<(u32, u32, u64), String> {
let worktree = project
.worktree_for_id(image.project_path(cx).worktree_id, cx)
.ok_or_else(|| "Could not find worktree for image".to_string())?;
let worktree_root = worktree.read(cx).abs_path();

let path = if image.path().is_absolute() {
image.path().to_path_buf()
} else {
worktree_root.join(image.path())
};

if !path.exists() {
return Err(format!("File does not exist at path: {:?}", path));
}

let img = image::open(&path).map_err(|e| format!("Failed to open image: {}", e))?;
kaf-lamed-beyt marked this conversation as resolved.
Show resolved Hide resolved
let dimensions = img.dimensions();

let file_metadata = metadata(&path).map_err(|e| format!("Cannot access image data: {}", e))?;
let file_size = file_metadata.len();

Ok((dimensions.0, dimensions.1, file_size))
}

fn breadcrumbs_text_for_image(project: &Project, image: &ImageItem, cx: &AppContext) -> String {
let path = image.path();
if project.visible_worktrees(cx).count() <= 1 {
Expand Down
Loading