From 5456d28ad2e859eecd1e687e8e0cf97af6060ef5 Mon Sep 17 00:00:00 2001 From: Goldy <66202304+THEGOLDENPRO@users.noreply.github.com> Date: Thu, 17 Oct 2024 23:55:42 +0100 Subject: [PATCH] refactor: move info box to different module. --- src/app.rs | 68 +++---------------------------------------------- src/info_box.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 3 files changed, 71 insertions(+), 64 deletions(-) create mode 100644 src/info_box.rs diff --git a/src/app.rs b/src/app.rs index 16c096b..022b019 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,19 +1,14 @@ use std::time::{Duration, Instant}; -use cap::Cap; -use cirrus_theming::Theme; -use std::alloc; use rdev::display_size; -use eframe::egui::{self, pos2, Color32, ImageSource, Key, Margin, Rect, Shadow, Style}; +use cirrus_theming::Theme; +use eframe::egui::{self, Color32, ImageSource, Key, Margin, Rect}; use crate::image::{Image, ImageOptimization}; -#[global_allocator] -static ALLOCATOR: Cap = Cap::new(alloc::System, usize::max_value()); - pub struct Roseate { - theme: Theme, - image: Option, + pub theme: Theme, + pub image: Option, image_scale_factor: f32, resize_timer: Option, last_window_rect: Rect, @@ -34,61 +29,6 @@ impl Roseate { } } - fn show_info_box(&mut self, ctx: &egui::Context) { - - let mut custom_frame = egui::Frame::window(&ctx.style()); - custom_frame.fill = Color32::from_hex(&self.theme.hex_code).unwrap().gamma_multiply(3.0); - custom_frame.shadow = Shadow::NONE; - - egui::Window::new( - egui::WidgetText::RichText( - egui::RichText::new("ℹ Info").size(15.0) - ) - ) - .default_pos(pos2(200.0, 200.0)) - .title_bar(true) - .resizable(false) - .frame(custom_frame) - .show(ctx, |ui| { - let mem_allocated = ALLOCATOR.allocated(); - - egui::Frame::group(&Style::default()).inner_margin(Margin::same(1.0)).show( - ui, |ui| { - egui::Grid::new("info_box_grid") - .num_columns(2) - .spacing([20.0, 4.0]) - .striped(true) - .max_col_width(130.0) - .show(ui, |ui| { - if self.image.is_some() { - let image = self.image.as_ref().unwrap(); // safe to unwrap as we know this is Some(). - - ui.label("Name:"); - ui.label( - image.image_path.file_name().expect("Failed to retrieve image name from path!").to_string_lossy() - ); - ui.end_row(); - - ui.label("Dimensions: "); - ui.label( - format!( - "{}x{}", image.image_size.width, image.image_size.height - ) - ); - ui.end_row(); - } - }); - } - ); - - ui.add_space(3.0); - ui.label(format!( - "Memory Allocated: {}", - re_format::format_bytes(mem_allocated as f64) - )); - }); - } - fn scale_image_on_window_resize(&mut self, window_rect: &Rect) { if let Some(timer) = self.resize_timer { // If the timer has expired (no new resize events) diff --git a/src/info_box.rs b/src/info_box.rs new file mode 100644 index 0000000..ad084a5 --- /dev/null +++ b/src/info_box.rs @@ -0,0 +1,66 @@ +use std::alloc; + +use cap::Cap; +use eframe::egui::{self, pos2, Color32, Margin, Shadow, Style}; + +use crate::app::Roseate; + +#[global_allocator] +static ALLOCATOR: Cap = Cap::new(alloc::System, usize::max_value()); + +impl Roseate { + pub fn show_info_box(&mut self, ctx: &egui::Context) { + + let mut custom_frame = egui::Frame::window(&ctx.style()); + custom_frame.fill = Color32::from_hex(&self.theme.hex_code).unwrap().gamma_multiply(3.0); + custom_frame.shadow = Shadow::NONE; + + egui::Window::new( + egui::WidgetText::RichText( + egui::RichText::new("ℹ Info").size(15.0) + ) + ) + .default_pos(pos2(200.0, 200.0)) + .title_bar(true) + .resizable(false) + .frame(custom_frame) + .show(ctx, |ui| { + let mem_allocated = ALLOCATOR.allocated(); + + egui::Frame::group(&Style::default()).inner_margin(Margin::same(1.0)).show( + ui, |ui| { + egui::Grid::new("info_box_grid") + .num_columns(2) + .spacing([20.0, 4.0]) + .striped(true) + .max_col_width(130.0) + .show(ui, |ui| { + if self.image.is_some() { + let image = self.image.as_ref().unwrap(); // safe to unwrap as we know this is Some(). + + ui.label("Name:"); + ui.label( + image.image_path.file_name().expect("Failed to retrieve image name from path!").to_string_lossy() + ); + ui.end_row(); + + ui.label("Dimensions: "); + ui.label( + format!( + "{}x{}", image.image_size.width, image.image_size.height + ) + ); + ui.end_row(); + } + }); + } + ); + + ui.add_space(3.0); + ui.label(format!( + "Memory Allocated: {}", + re_format::format_bytes(mem_allocated as f64) + )); + }); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3ec1348..121c3d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use image::Image; mod app; mod image; +mod info_box; /// 🌹 A small and simple but fancy image viewer built with Rust that's cross-platform. #[derive(Parser, Debug)]