Skip to content

Commit

Permalink
refactor: move info box to different module.
Browse files Browse the repository at this point in the history
  • Loading branch information
THEGOLDENPRO committed Oct 17, 2024
1 parent 6028079 commit 5456d28
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 64 deletions.
68 changes: 4 additions & 64 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -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<alloc::System> = Cap::new(alloc::System, usize::max_value());

pub struct Roseate {
theme: Theme,
image: Option<Image>,
pub theme: Theme,
pub image: Option<Image>,
image_scale_factor: f32,
resize_timer: Option<Instant>,
last_window_rect: Rect,
Expand All @@ -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)
Expand Down
66 changes: 66 additions & 0 deletions src/info_box.rs
Original file line number Diff line number Diff line change
@@ -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<alloc::System> = 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)
));
});
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 5456d28

Please sign in to comment.