From 871542ecd7c250a30dcc81e13f218451302a24d3 Mon Sep 17 00:00:00 2001 From: Vitaliy Grabovets Date: Fri, 4 Aug 2023 22:05:01 +0300 Subject: [PATCH] Add file size to image metadata tooltip --- src/appstate.rs | 2 ++ src/main.rs | 21 +++++++++++++++------ src/utils.rs | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/appstate.rs b/src/appstate.rs index 6f4d22e7..03ca5f25 100644 --- a/src/appstate.rs +++ b/src/appstate.rs @@ -57,6 +57,7 @@ pub struct OculanteState { pub cursor: Vector2, pub cursor_relative: Vector2, pub image_dimension: (u32, u32), + pub image_size: u64, pub sampled_color: [f32; 4], pub mouse_delta: Vector2, pub texture_channel: (Sender, Receiver), @@ -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, diff --git a/src/main.rs b/src/main.rs index 11abfc0d..a2febdc2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -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 @@ -1005,7 +1013,7 @@ 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, @@ -1013,10 +1021,11 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O 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), )); }, ); diff --git a/src/utils.rs b/src/utils.rs index 64f9616c..c8f7d7c4 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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) + } +}