Skip to content

Commit

Permalink
Refactor global margins and scroll area
Browse files Browse the repository at this point in the history
  • Loading branch information
breard-r committed Sep 5, 2024
1 parent d50c9a6 commit 4ad91fc
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 115 deletions.
27 changes: 23 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::i18n::I18n;
use crate::receipt::Receipt;
use crate::theme::Theme;
use crate::views::AppView;
use eframe::egui::{self, Context};
use eframe::egui::{self, Context, Image};
use std::collections::HashSet;
use std::path::Path;

Expand Down Expand Up @@ -80,9 +80,28 @@ impl eframe::App for ChecksumApp {
.show(ctx, |ui| {
self.theme.set_visuals(ui.visuals_mut());
self.update_status(ctx);
let view = self.view.to_owned();
view.display(self, ui);
view.handle_dropped_files(self, ctx);

egui::Frame::none()
.inner_margin(crate::UI_MARGIN_LARGE)
.show(ui, |ui| {
let (logo_name, logo_bytes) = self.theme.get_logo_bytes();
ui.add(Image::from_bytes(logo_name, logo_bytes).fit_to_original_size(1.0));
});

egui::ScrollArea::both().show(ui, |ui| {
egui::Frame::none()
.inner_margin(egui::Margin {
left: crate::UI_MARGIN_LARGE,
right: crate::UI_MARGIN_LARGE,
top: crate::UI_MARGIN_NONE,
bottom: crate::UI_MARGIN_LARGE,
})
.show(ui, |ui| {
let view = self.view.to_owned();
view.display(self, ui);
view.handle_dropped_files(self, ctx);
});
});
});
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ const NB_FILES_START: u32 = 1;
const WIN_WIDTH: f32 = 720.0;
const WIN_HEIGHT: f32 = 345.0;

const MAIN_ROUNDING: f32 = 7.0;
const UI_BTN_PADDING_H: f32 = 10.0;
const UI_BTN_PADDING_V: f32 = 6.0;
const UI_MARGIN_LARGE: f32 = 32.0;
const UI_MARGIN_MEDIUM: f32 = 16.0;
const UI_MARGIN_SMALL: f32 = 8.0;
const UI_MARGIN_NONE: f32 = 0.0;

fn main() {
let config = config::Config::init();
let viewport = eframe::egui::ViewportBuilder::default()
Expand Down
14 changes: 6 additions & 8 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use eframe::egui::{self, FontFamily, FontId, RichText, TextStyle};
use serde::{Deserialize, Serialize};

pub const AVAILABLE_THEMES: &[Theme] = &[Theme::Dark, Theme::Light];
pub const LARGE_PADDING: f32 = 6.0;
pub const MAIN_ROUNDING: f32 = 7.0;

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
Expand Down Expand Up @@ -145,7 +143,7 @@ impl Theme {

pub fn get_main_frame(&self) -> egui::Frame {
egui::Frame::default()
.inner_margin(32.0)
.inner_margin(crate::UI_MARGIN_NONE)
.fill(Color::MainFrameBackground.get(*self))
}

Expand All @@ -171,7 +169,7 @@ impl Theme {
width: 1.0,
color: Color::MainText.get(*self),
};
visuals.widgets.noninteractive.rounding = MAIN_ROUNDING.into();
visuals.widgets.noninteractive.rounding = crate::MAIN_ROUNDING.into();
visuals.widgets.noninteractive.fg_stroke = egui::Stroke {
width: 12.0,
color: Color::MainText.get(*self),
Expand All @@ -184,7 +182,7 @@ impl Theme {
width: 1.0,
color: Color::ButtonBorder.get(*self),
};
visuals.widgets.inactive.rounding = MAIN_ROUNDING.into();
visuals.widgets.inactive.rounding = crate::MAIN_ROUNDING.into();
visuals.widgets.inactive.fg_stroke = egui::Stroke {
width: 12.0,
color: Color::ButtonText.get(*self),
Expand All @@ -197,7 +195,7 @@ impl Theme {
width: 1.0,
color: Color::ButtonBorderHovered.get(*self),
};
visuals.widgets.hovered.rounding = MAIN_ROUNDING.into();
visuals.widgets.hovered.rounding = crate::MAIN_ROUNDING.into();
visuals.widgets.hovered.fg_stroke = egui::Stroke {
width: 12.0,
color: Color::ButtonTextHovered.get(*self),
Expand All @@ -210,7 +208,7 @@ impl Theme {
width: 1.0,
color: Color::ButtonBorderHovered.get(*self),
};
visuals.widgets.active.rounding = MAIN_ROUNDING.into();
visuals.widgets.active.rounding = crate::MAIN_ROUNDING.into();
visuals.widgets.active.fg_stroke = egui::Stroke {
width: 12.0,
color: Color::ButtonTextHovered.get(*self),
Expand All @@ -223,7 +221,7 @@ impl Theme {
width: 1.0,
color: Color::ButtonBorderHovered.get(*self),
};
visuals.widgets.open.rounding = MAIN_ROUNDING.into();
visuals.widgets.open.rounding = crate::MAIN_ROUNDING.into();
visuals.widgets.open.fg_stroke = egui::Stroke {
width: 12.0,
color: Color::ButtonTextHovered.get(*self),
Expand Down
4 changes: 2 additions & 2 deletions src/theme/infobox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use eframe::egui;
const INFOBOX_BORDER_SIZE: f32 = 1.0;
const INFOBOX_ICON_SIZE: f32 = 20.0;
const INFOBOX_MAIN_LEFT_BORDER_SIZE: f32 = 13.0;
const INFOBOX_PADDING: f32 = crate::theme::LARGE_PADDING;
const INFOBOX_ROUNDING: f32 = crate::theme::MAIN_ROUNDING;
const INFOBOX_PADDING: f32 = crate::UI_MARGIN_SMALL;
const INFOBOX_ROUNDING: f32 = crate::MAIN_ROUNDING;

#[derive(Clone, Copy, Debug)]
pub enum InfoBoxType {
Expand Down
5 changes: 0 additions & 5 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ mod check_errors;
mod config;
mod main;

const UI_BTN_PADDING_H: f32 = 10.0;
const UI_BTN_PADDING_V: f32 = 6.0;
const UI_EXTRA_SPACE: f32 = 8.0;
const UI_MARGIN_LARGE: f32 = 16.0;

#[derive(Clone)]
pub enum AppView {
CheckErrors,
Expand Down
15 changes: 5 additions & 10 deletions src/views/check_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::app::ChecksumApp;
use crate::checker::CheckResult;
use crate::theme::Button;
use crate::views::AppView;
use eframe::egui::{self, Image};
use eframe::egui;

macro_rules! file_error {
($app: ident, $ui: ident, $lst: expr, $title: literal) => {
if !$lst.is_empty() {
$ui.add_space(super::UI_MARGIN_LARGE);
$ui.add_space(crate::UI_MARGIN_MEDIUM);
$ui.label($app.theme.title(&$app.i18n.msg($title)));
$ui.add_space(super::UI_EXTRA_SPACE);
$ui.add_space(crate::UI_MARGIN_SMALL);
for f in &$lst {
$ui.label(f.path.display().to_string());
}
Expand All @@ -18,14 +18,9 @@ macro_rules! file_error {
}

pub fn display(app: &mut ChecksumApp, ui: &mut egui::Ui) {
let (logo_name, logo_bytes) = app.theme.get_logo_bytes();

let spacing = ui.spacing_mut();
app.default_padding = spacing.button_padding;
spacing.button_padding = egui::vec2(super::UI_BTN_PADDING_H, super::UI_BTN_PADDING_V);

ui.add(Image::from_bytes(logo_name, logo_bytes).fit_to_original_size(1.0));
ui.add_space(super::UI_MARGIN_LARGE);
spacing.button_padding = egui::vec2(crate::UI_BTN_PADDING_H, crate::UI_BTN_PADDING_V);

if let Some(CheckResult::CheckErrors(err)) = &app.file_check_result {
file_error!(app, ui, err.invalid_ctn_file, "title_invalid_ctn_file");
Expand All @@ -34,7 +29,7 @@ pub fn display(app: &mut ChecksumApp, ui: &mut egui::Ui) {
file_error!(app, ui, err.missing_receipt, "title_missing_receipt");
}

ui.add_space(super::UI_MARGIN_LARGE);
ui.add_space(crate::UI_MARGIN_MEDIUM);
if ui
.add(Button::new().text(app.i18n.msg("back")).render())
.clicked()
Expand Down
19 changes: 9 additions & 10 deletions src/views/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ use crate::config::Config;
use crate::i18n::I18n;
use crate::theme::{Button, Icon};
use crate::views::AppView;
use eframe::egui::{self, Image};
use eframe::egui;

pub fn display(app: &mut ChecksumApp, ui: &mut egui::Ui) {
let mut new_config = get_config(app);

let (logo_name, logo_bytes) = app.theme.get_logo_bytes();
ui.add(Image::from_bytes(logo_name, logo_bytes).fit_to_original_size(1.0));
ui.add_space(super::UI_MARGIN_LARGE);
ui.add_space(super::UI_MARGIN_LARGE);

ui.add_space(crate::UI_MARGIN_MEDIUM);
ui.label(app.theme.title(&app.i18n.msg("config_title")));
ui.add_space(super::UI_MARGIN_LARGE);
ui.add_space(crate::UI_MARGIN_MEDIUM);

egui::Grid::new("header_grid")
.num_columns(2)
.spacing(egui::Vec2 { x: 0.0, y: 6.0 })
.spacing(egui::Vec2 {
x: crate::UI_MARGIN_NONE,
y: crate::UI_MARGIN_SMALL,
})
.show(ui, |ui| {
// Content file name
let default_content_file_name = app.i18n.msg("content_file_name");
Expand Down Expand Up @@ -184,10 +183,10 @@ pub fn display(app: &mut ChecksumApp, ui: &mut egui::Ui) {

let spacing = ui.spacing_mut();
app.default_padding = spacing.button_padding;
spacing.button_padding = egui::vec2(super::UI_BTN_PADDING_H, super::UI_BTN_PADDING_V);
spacing.button_padding = egui::vec2(crate::UI_BTN_PADDING_H, crate::UI_BTN_PADDING_V);

app.tmp_config = Some(new_config.clone());
ui.add_space(super::UI_MARGIN_LARGE);
ui.add_space(crate::UI_MARGIN_MEDIUM);
ui.horizontal(|ui| {
if ui
.add(Button::new().text(app.i18n.msg("apply")).render())
Expand Down
Loading

0 comments on commit 4ad91fc

Please sign in to comment.