ui: refactor app creation and styles setup for different platforms

This commit is contained in:
ardocrat 2023-06-02 22:55:07 +03:00
parent ffc68bc6ae
commit de7be791a9
3 changed files with 89 additions and 108 deletions

View file

@ -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<T: 'static>(app: PlatformApp<T>) -> AppCreator where PlatformApp<T>: 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,
),
)
}
}

View file

@ -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);
}
}

View file

@ -63,109 +63,13 @@ impl PlatformCallbacks for Android {
}
}
//TODO
// pub trait PlatformSetup<T> {
// fn new(cc: &eframe::CreationContext<'_>, platform: T) -> Box<Self> {
// Self::setup_visuals(&cc.egui_ctx);
// return Self {
// app: App::default(),
// platform
// }
// }
// fn setup_visuals(ctx: &egui::Context);
//
// }
//
// impl PlatformSetup<Android> for PlatformApp<Android> {
// fn setup_visuals(ctx: &Context) {
//
// }
// }
impl PlatformApp<Android> {
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<Android> {