From de7be791a92a0c2238e14a48f034617aa8ba5ca9 Mon Sep 17 00:00:00 2001 From: ardocrat Date: Fri, 2 Jun 2023 22:55:07 +0300 Subject: [PATCH] ui: refactor app creation and styles setup for different platforms --- src/grim.rs | 25 +++++---- src/gui/app.rs | 74 +++++++++++++++++++++++++ src/gui/platform/android/mod.rs | 98 +-------------------------------- 3 files changed, 89 insertions(+), 108 deletions(-) diff --git a/src/grim.rs b/src/grim.rs index b490b3a..fc8feb5 100644 --- a/src/grim.rs +++ b/src/grim.rs @@ -18,7 +18,7 @@ use log::LevelFilter::Info; #[cfg(target_os = "android")] use winit::platform::android::activity::AndroidApp; -use crate::gui::PlatformApp; +use crate::gui::{App, PlatformApp}; use crate::node::Node; #[allow(dead_code)] @@ -42,9 +42,7 @@ fn android_main(app: AndroidApp) { builder.with_android_app(app); })); - start(options, Box::new(|_cc| Box::new( - PlatformApp::new(_cc, platform) - ))); + start(options, app_creator(PlatformApp::new(platform))); } #[allow(dead_code)] @@ -52,15 +50,22 @@ fn android_main(app: AndroidApp) { fn main() { #[cfg(debug_assertions)] env_logger::builder() - .filter_level(Debug) + .filter_level(Info) .parse_default_env() .init(); use crate::gui::platform::Desktop; let options = NativeOptions::default(); - start(options, Box::new(|_cc| Box::new( - PlatformApp::new(_cc, Desktop::default()) - ))); + start(options, app_creator(Desktop::default())); +} + +fn app_creator(app: PlatformApp) -> AppCreator where PlatformApp: eframe::App { + Box::new(|cc| { + App::setup_visuals(&cc.egui_ctx); + App::setup_fonts(&cc.egui_ctx); + //TODO: Setup storage + Box::new(app) + }) } fn start(mut options: NativeOptions, app_creator: AppCreator) { @@ -68,8 +73,6 @@ fn start(mut options: NativeOptions, app_creator: AppCreator) { options.renderer = Renderer::Wgpu; setup_i18n(); - - //TODO: Take network type and server check from config Node::start(ChainTypes::Mainnet); eframe::run_native("Grim", options, app_creator); @@ -107,4 +110,4 @@ pub fn info_strings() -> (String, String) { built_info::FEATURES_STR, ), ) -} +} \ No newline at end of file diff --git a/src/gui/app.rs b/src/gui/app.rs index 88974ec..799c5db 100644 --- a/src/gui/app.rs +++ b/src/gui/app.rs @@ -121,5 +121,79 @@ impl App { OperatingSystem::Unknown => {} } } + + pub fn setup_visuals(ctx: &Context) { + // Setup style + let mut style = (*ctx.style()).clone(); + // Setup spacing for buttons. + style.spacing.button_padding = egui::vec2(12.0, 8.0); + // Make scroll-bar thinner. + style.spacing.scroll_bar_width = 4.0; + // Disable spacing between items. + style.spacing.item_spacing = egui::vec2(0.0, 0.0); + + ctx.set_style(style); + + // Setup visuals + let mut visuals = egui::Visuals::light(); + + // Disable stroke around panels by default + visuals.widgets.noninteractive.bg_stroke = Stroke::NONE; + ctx.set_visuals(visuals); + } + + pub fn setup_fonts(ctx: &Context) { + use egui::FontFamily::Proportional; + + let mut fonts = egui::FontDefinitions::default(); + + fonts.font_data.insert( + "phosphor".to_owned(), + egui::FontData::from_static(include_bytes!( + "../../fonts/phosphor.ttf" + )).tweak(egui::FontTweak { + scale: 1.0, + y_offset_factor: 0.14, + y_offset: 0.0 + }), + ); + fonts + .families + .entry(Proportional) + .or_default() + .insert(0, "phosphor".to_owned()); + + fonts.font_data.insert( + "noto".to_owned(), + egui::FontData::from_static(include_bytes!( + "../../fonts/noto_sc_reg.otf" + )).tweak(egui::FontTweak { + scale: 1.0, + y_offset_factor: -0.25, + y_offset: 0.0 + }), + ); + fonts + .families + .entry(Proportional) + .or_default() + .insert(0, "noto".to_owned()); + + ctx.set_fonts(fonts); + + use egui::FontId; + use egui::TextStyle::*; + + let mut style = (*ctx.style()).clone(); + style.text_styles = [ + (Heading, FontId::new(20.0, Proportional)), + (Body, FontId::new(16.0, Proportional)), + (Button, FontId::new(18.0, Proportional)), + (Small, FontId::new(12.0, Proportional)), + (Monospace, FontId::new(16.0, Proportional)), + ].into(); + + ctx.set_style(style); + } } diff --git a/src/gui/platform/android/mod.rs b/src/gui/platform/android/mod.rs index 04a8fe3..94909d8 100644 --- a/src/gui/platform/android/mod.rs +++ b/src/gui/platform/android/mod.rs @@ -63,109 +63,13 @@ impl PlatformCallbacks for Android { } } -//TODO - -// pub trait PlatformSetup { -// fn new(cc: &eframe::CreationContext<'_>, platform: T) -> Box { -// Self::setup_visuals(&cc.egui_ctx); -// return Self { -// app: App::default(), -// platform -// } -// } -// fn setup_visuals(ctx: &egui::Context); -// -// } -// -// impl PlatformSetup for PlatformApp { -// fn setup_visuals(ctx: &Context) { -// -// } -// } - impl PlatformApp { - pub fn new(cc: &eframe::CreationContext<'_>, platform: Android) -> Self { - Self::setup_visuals(&cc.egui_ctx); - Self::setup_fonts(&cc.egui_ctx); + pub fn new(platform: Android) -> Self { Self { app: App::default(), platform, } } - - fn setup_visuals(ctx: &egui::Context) { - // Setup style - let mut style = (*ctx.style()).clone(); - // Setup spacing for buttons. - style.spacing.button_padding = egui::vec2(12.0, 8.0); - // Make scroll-bar thinner. - style.spacing.scroll_bar_width = 4.0; - // Disable spacing between items. - style.spacing.item_spacing = egui::vec2(0.0, 0.0); - - ctx.set_style(style); - - // Setup visuals - let mut visuals = egui::Visuals::light(); - - // Disable stroke around panels by default - visuals.widgets.noninteractive.bg_stroke = Stroke::NONE; - ctx.set_visuals(visuals); - } - - fn setup_fonts(ctx: &egui::Context) { - use egui::FontFamily::Proportional; - - let mut fonts = egui::FontDefinitions::default(); - - fonts.font_data.insert( - "phosphor".to_owned(), - egui::FontData::from_static(include_bytes!( - "../../../../fonts/phosphor.ttf" - )).tweak(egui::FontTweak { - scale: 1.0, - y_offset_factor: 0.14, - y_offset: 0.0 - }), - ); - fonts - .families - .entry(Proportional) - .or_default() - .insert(0, "phosphor".to_owned()); - - fonts.font_data.insert( - "noto".to_owned(), - egui::FontData::from_static(include_bytes!( - "../../../../fonts/noto_sc_reg.otf" - )).tweak(egui::FontTweak { - scale: 1.0, - y_offset_factor: -0.25, - y_offset: 0.0 - }), - ); - fonts - .families - .entry(Proportional) - .or_default() - .insert(0, "noto".to_owned()); - - ctx.set_fonts(fonts); - - use egui::FontId; - use egui::TextStyle::*; - - let mut style = (*ctx.style()).clone(); - style.text_styles = [ - (Heading, FontId::new(20.0, Proportional)), - (Body, FontId::new(16.0, Proportional)), - (Button, FontId::new(18.0, Proportional)), - (Small, FontId::new(12.0, Proportional)), - (Monospace, FontId::new(16.0, Proportional)), - ].into(); - - ctx.set_style(style); - } } impl eframe::App for PlatformApp {