2023-04-21 03:45:59 +03:00
|
|
|
// Copyright 2023 The Grim Developers
|
2023-04-10 16:02:53 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-04-21 03:45:59 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rust_i18n;
|
2023-07-11 16:23:10 +03:00
|
|
|
|
2024-04-24 11:29:38 +03:00
|
|
|
use egui::{Context, Stroke};
|
2023-10-04 16:16:30 +03:00
|
|
|
|
2023-07-11 16:23:10 +03:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
use winit::platform::android::activity::AndroidApp;
|
|
|
|
|
2024-04-24 18:24:21 +03:00
|
|
|
pub use settings::AppConfig;
|
2023-10-04 16:16:30 +03:00
|
|
|
pub use settings::Settings;
|
2023-07-11 16:23:10 +03:00
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
use crate::gui::{Colors, PlatformApp};
|
2023-07-11 21:02:36 +03:00
|
|
|
use crate::gui::platform::PlatformCallbacks;
|
2023-10-04 16:16:30 +03:00
|
|
|
use crate::gui::views::View;
|
2023-07-11 16:23:10 +03:00
|
|
|
use crate::node::Node;
|
|
|
|
|
2023-04-21 03:45:59 +03:00
|
|
|
i18n!("locales");
|
|
|
|
|
2023-07-11 16:26:39 +03:00
|
|
|
mod node;
|
2023-04-10 16:02:53 +03:00
|
|
|
mod wallet;
|
2024-04-25 15:15:00 +03:00
|
|
|
mod tor;
|
2023-07-28 00:00:57 +03:00
|
|
|
mod settings;
|
2023-07-11 16:23:10 +03:00
|
|
|
pub mod gui;
|
2023-06-15 23:54:31 +03:00
|
|
|
|
2023-07-28 00:00:57 +03:00
|
|
|
// Include build information.
|
|
|
|
pub mod built_info {
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/built.rs"));
|
|
|
|
}
|
2023-07-11 16:23:10 +03:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
#[no_mangle]
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Android platform entry point.
|
2023-07-11 16:23:10 +03:00
|
|
|
fn android_main(app: AndroidApp) {
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
std::env::set_var("RUST_BACKTRACE", "full");
|
|
|
|
let log_config = android_logger::Config::default()
|
|
|
|
.with_max_level(log::LevelFilter::Info)
|
|
|
|
.with_tag("grim");
|
|
|
|
android_logger::init_once(log_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
use gui::platform::Android;
|
|
|
|
use gui::PlatformApp;
|
|
|
|
|
|
|
|
let platform = Android::new(app.clone());
|
|
|
|
use winit::platform::android::EventLoopBuilderExtAndroid;
|
2024-04-16 15:24:22 +03:00
|
|
|
|
|
|
|
let width = app.config().screen_width_dp().unwrap() as f32;
|
|
|
|
let height = app.config().screen_height_dp().unwrap() as f32;
|
2024-04-25 15:15:00 +03:00
|
|
|
let size = egui::emath::vec2(width, height);
|
2024-04-16 15:24:22 +03:00
|
|
|
let mut options = eframe::NativeOptions {
|
2024-04-25 15:15:00 +03:00
|
|
|
viewport: egui::ViewportBuilder::default().with_inner_size(size),
|
2024-04-16 15:24:22 +03:00
|
|
|
..Default::default()
|
|
|
|
};
|
2023-07-14 00:55:31 +03:00
|
|
|
// Setup limits that are guaranteed to be compatible with Android devices.
|
2023-07-21 03:58:58 +03:00
|
|
|
options.wgpu_options.device_descriptor = std::sync::Arc::new(|adapter| {
|
2023-07-13 18:44:37 +03:00
|
|
|
let base_limits = wgpu::Limits::downlevel_webgl2_defaults();
|
|
|
|
wgpu::DeviceDescriptor {
|
|
|
|
label: Some("egui wgpu device"),
|
2024-04-16 15:24:22 +03:00
|
|
|
required_features: wgpu::Features::default(),
|
|
|
|
required_limits: wgpu::Limits {
|
2023-07-13 18:44:37 +03:00
|
|
|
max_texture_dimension_2d: 8192,
|
|
|
|
..base_limits
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
2023-07-11 16:23:10 +03:00
|
|
|
options.event_loop_builder = Some(Box::new(move |builder| {
|
|
|
|
builder.with_android_app(app);
|
|
|
|
}));
|
|
|
|
|
|
|
|
start(options, app_creator(PlatformApp::new(platform)));
|
|
|
|
}
|
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
/// [`PlatformApp`] setup for [`eframe`].
|
2023-07-11 16:26:39 +03:00
|
|
|
pub fn app_creator<T: 'static>(app: PlatformApp<T>) -> eframe::AppCreator
|
2023-07-11 21:02:36 +03:00
|
|
|
where PlatformApp<T>: eframe::App, T: PlatformCallbacks {
|
2023-07-11 16:23:10 +03:00
|
|
|
Box::new(|cc| {
|
2023-10-04 16:16:30 +03:00
|
|
|
// Setup images support.
|
|
|
|
egui_extras::install_image_loaders(&cc.egui_ctx);
|
|
|
|
// Setup visuals.
|
2023-07-14 00:55:31 +03:00
|
|
|
setup_visuals(&cc.egui_ctx);
|
2023-10-04 16:16:30 +03:00
|
|
|
// Setup fonts.
|
2023-07-14 00:55:31 +03:00
|
|
|
setup_fonts(&cc.egui_ctx);
|
2023-10-04 16:16:30 +03:00
|
|
|
// Return app instance.
|
2023-07-11 16:23:10 +03:00
|
|
|
Box::new(app)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Entry point to start ui with [`eframe`].
|
2023-07-11 16:26:39 +03:00
|
|
|
pub fn start(mut options: eframe::NativeOptions, app_creator: eframe::AppCreator) {
|
|
|
|
options.default_theme = eframe::Theme::Light;
|
|
|
|
options.renderer = eframe::Renderer::Wgpu;
|
2023-07-25 03:42:52 +03:00
|
|
|
// Setup translations.
|
2023-07-11 16:23:10 +03:00
|
|
|
setup_i18n();
|
2023-07-25 03:42:52 +03:00
|
|
|
// Start integrated node if needed.
|
2023-07-11 16:23:10 +03:00
|
|
|
if Settings::app_config_to_read().auto_start_node {
|
|
|
|
Node::start();
|
|
|
|
}
|
2023-07-25 03:42:52 +03:00
|
|
|
// Launch graphical interface.
|
2023-07-28 00:00:57 +03:00
|
|
|
eframe::run_native("Grim", options, app_creator).unwrap();
|
2023-07-11 16:23:10 +03:00
|
|
|
}
|
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Setup application [`egui::Style`] and [`egui::Visuals`].
|
|
|
|
pub fn setup_visuals(ctx: &Context) {
|
|
|
|
let mut style = (*ctx.style()).clone();
|
|
|
|
// Setup spacing for buttons.
|
|
|
|
style.spacing.button_padding = egui::vec2(12.0, 8.0);
|
2024-04-17 01:30:28 +03:00
|
|
|
// Make scroll-bar thinner and lighter.
|
2024-04-16 15:24:22 +03:00
|
|
|
style.spacing.scroll.bar_width = 4.0;
|
2024-04-17 01:30:28 +03:00
|
|
|
style.spacing.scroll.bar_outer_margin = -2.0;
|
|
|
|
style.spacing.scroll.foreground_color = false;
|
2023-07-14 00:55:31 +03:00
|
|
|
// Disable spacing between items.
|
|
|
|
style.spacing.item_spacing = egui::vec2(0.0, 0.0);
|
|
|
|
// Setup radio button/checkbox size and spacing.
|
|
|
|
style.spacing.icon_width = 24.0;
|
|
|
|
style.spacing.icon_width_inner = 14.0;
|
|
|
|
style.spacing.icon_spacing = 10.0;
|
|
|
|
// Setup style
|
|
|
|
ctx.set_style(style);
|
|
|
|
|
|
|
|
let mut visuals = egui::Visuals::light();
|
|
|
|
// Setup selection color.
|
|
|
|
visuals.selection.stroke = Stroke { width: 1.0, color: Colors::TEXT };
|
|
|
|
visuals.selection.bg_fill = Colors::GOLD;
|
2023-08-09 02:22:16 +03:00
|
|
|
// Disable stroke around panels by default.
|
2023-07-14 00:55:31 +03:00
|
|
|
visuals.widgets.noninteractive.bg_stroke = Stroke::NONE;
|
2023-08-09 02:22:16 +03:00
|
|
|
// Setup stroke around inactive widgets.
|
|
|
|
visuals.widgets.inactive.bg_stroke = View::DEFAULT_STROKE;
|
2024-05-17 21:37:29 +03:00
|
|
|
// Setup background and foreground stroke color for widgets like pull-to-refresher.
|
|
|
|
visuals.widgets.inactive.bg_fill = Colors::YELLOW;
|
|
|
|
visuals.widgets.inactive.fg_stroke.color = Colors::ITEM_BUTTON;
|
2023-07-14 00:55:31 +03:00
|
|
|
// Setup visuals
|
|
|
|
ctx.set_visuals(visuals);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Setup application fonts.
|
|
|
|
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.30,
|
|
|
|
y_offset: 0.0,
|
2023-10-16 22:39:40 +03:00
|
|
|
baseline_offset_factor: 0.50,
|
2023-07-14 00:55:31 +03:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
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,
|
|
|
|
baseline_offset_factor: 0.17,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
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 = [
|
2023-07-14 03:51:06 +03:00
|
|
|
(Heading, FontId::new(19.0, Proportional)),
|
2023-07-14 00:55:31 +03:00
|
|
|
(Body, FontId::new(16.0, Proportional)),
|
2023-07-14 03:51:06 +03:00
|
|
|
(Button, FontId::new(17.0, Proportional)),
|
|
|
|
(Small, FontId::new(15.0, Proportional)),
|
2023-07-14 00:55:31 +03:00
|
|
|
(Monospace, FontId::new(16.0, Proportional)),
|
|
|
|
].into();
|
|
|
|
|
|
|
|
ctx.set_style(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Setup translations.
|
2023-07-11 16:23:10 +03:00
|
|
|
fn setup_i18n() {
|
2024-04-24 11:29:38 +03:00
|
|
|
// Set saved locale or get from system.
|
|
|
|
if let Some(lang) = AppConfig::locale() {
|
|
|
|
if rust_i18n::available_locales!().contains(&lang.as_str()) {
|
|
|
|
rust_i18n::set_locale(lang.as_str());
|
|
|
|
}
|
2023-07-11 16:23:10 +03:00
|
|
|
} else {
|
2024-04-24 18:24:21 +03:00
|
|
|
let locale = sys_locale::get_locale().unwrap_or(String::from(AppConfig::DEFAULT_LOCALE));
|
2024-04-24 11:29:38 +03:00
|
|
|
let locale_str = if locale.contains("-") {
|
2024-04-24 18:24:21 +03:00
|
|
|
locale.split("-").next().unwrap_or(AppConfig::DEFAULT_LOCALE)
|
2024-04-24 11:29:38 +03:00
|
|
|
} else {
|
|
|
|
locale.as_str()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set best possible locale.
|
|
|
|
if rust_i18n::available_locales!().contains(&locale_str) {
|
|
|
|
rust_i18n::set_locale(locale_str);
|
|
|
|
} else {
|
2024-04-24 18:24:21 +03:00
|
|
|
rust_i18n::set_locale(AppConfig::DEFAULT_LOCALE);
|
2024-04-24 11:29:38 +03:00
|
|
|
}
|
2023-07-11 16:23:10 +03:00
|
|
|
}
|
|
|
|
}
|