Skip to content

Commit

Permalink
Add file size to image metadata tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrabovets committed Aug 4, 2023
1 parent 0873960 commit 871542e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/appstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct OculanteState {
pub cursor: Vector2<f32>,
pub cursor_relative: Vector2<f32>,
pub image_dimension: (u32, u32),
pub image_size: u64,
pub sampled_color: [f32; 4],
pub mouse_delta: Vector2<f32>,
pub texture_channel: (Sender<Frame>, Receiver<Frame>),
Expand Down Expand Up @@ -166,6 +167,7 @@ impl Default for OculanteState {
cursor: Default::default(),
cursor_relative: Default::default(),
image_dimension: (0, 0),
image_size: 0,
sampled_color: [0., 0., 0., 0.],
player: Player::new(tx_channel.0.clone(), 20, 16384),
texture_channel: tx_channel,
Expand Down
21 changes: 15 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use round::round;
use shortcuts::key_pressed;
use std::collections::HashSet;
use std::ffi::OsStr;
use std::fs::File;
use std::fs::{self, File};
use std::io::{self, BufRead, Write};
use std::path::{Path, PathBuf};
use std::sync::mpsc;
Expand Down Expand Up @@ -754,6 +754,14 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
} else {
state.current_image_is_favourite = false;
}

match fs::metadata(current_path) {
Ok(metadata) => {
state.image_size = metadata.len();
}
Err(_) => state.send_message_err("Couldn't get metadata"),
}

}

// fill image sequence
Expand Down Expand Up @@ -1005,18 +1013,19 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
// the top menu bar

if state.show_metadata_tooltip && !state.pointer_over_ui && state.cursor_within_image() {
let pos_y = TOP_MENU_HEIGHT + 5. + if state.current_image_is_favourite {STAR.y} else {0.};
let pos_y = TOP_MENU_HEIGHT + 5. + if state.current_image_is_favourite { STAR.y } else { 0. };

show_tooltip_at(
ctx,
egui::Id::new("my_tooltip"),
Some(pos2(10., pos_y)),
|ui| {
ui.label(format!(
"scale: {scale}\nwidth: {width}\nheight: {height}",
scale=round(state.image_geometry.scale as f64, 2),
width=state.image_dimension.0,
height=state.image_dimension.1,
"scale: {scale}\nwidth: {width}\nheight: {height}\nsize: {size}",
scale = round(state.image_geometry.scale as f64, 2),
width = state.image_dimension.0,
height = state.image_dimension.1,
size = format_bytes(state.image_size as f64),
));
},
);
Expand Down
16 changes: 16 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,19 @@ pub fn toggle_zen_mode(state: &mut OculanteState, app: &mut App) {
}
set_title(app, state);
}

pub fn format_bytes(bytes: f64) -> String {
const KILOBYTE: f64 = 1000.;
const MEGABYTE: f64 = KILOBYTE * 1000.;
const GIGABYTE: f64 = MEGABYTE * 1000.;

return if bytes < KILOBYTE {
format!("{} bytes", bytes)
} else if bytes < MEGABYTE {
format!("{:.1} KB", bytes / KILOBYTE)
} else if bytes < GIGABYTE {
format!("{:.1} MB", bytes / MEGABYTE)
} else {
format!("{:.1} GB", bytes / GIGABYTE)
}
}

0 comments on commit 871542e

Please sign in to comment.