main.rs - icy_draw - icy_draw is the successor to mystic draw. fork / mirror
HTML git clone https://git.drkhsh.at/icy_draw.git
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
main.rs (5587B)
---
1 #![cfg_attr(not(debug_assertions), deny(warnings))] // Forbid warnings in release builds
2 #![warn(clippy::all, rust_2018_idioms)]
3 #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
4
5 use std::path::PathBuf;
6
7 mod model;
8 mod paint;
9 mod plugins;
10 mod ui;
11 mod util;
12
13 use log::LevelFilter;
14 use log4rs::{
15 append::{
16 console::{ConsoleAppender, Target},
17 file::FileAppender,
18 },
19 config::{Appender, Config, Root},
20 encode::pattern::PatternEncoder,
21 filter::threshold::ThresholdFilter,
22 };
23
24 use i18n_embed::{
25 fluent::{fluent_language_loader, FluentLanguageLoader},
26 DesktopLanguageRequester,
27 };
28 use rust_embed::RustEmbed;
29 use semver::Version;
30 pub use ui::*;
31
32 lazy_static::lazy_static! {
33 static ref VERSION: Version = Version::parse( env!("CARGO_PKG_VERSION")).unwrap();
34 static ref DEFAULT_TITLE: String = format!("iCY DRAW {}", *crate::VERSION);
35 }
36
37 #[derive(RustEmbed)]
38 #[folder = "i18n"] // path to the compiled localization resources
39 struct Localizations;
40
41 use once_cell::sync::Lazy;
42 static LANGUAGE_LOADER: Lazy<FluentLanguageLoader> = Lazy::new(|| {
43 let loader = fluent_language_loader!();
44 let requested_languages = DesktopLanguageRequester::requested_languages();
45 let _result = i18n_embed::select(&loader, &Localizations, &requested_languages);
46 loader
47 });
48 use clap::Parser;
49
50 #[derive(Parser, Debug)]
51 pub struct Cli {
52 path: Option<PathBuf>,
53 }
54
55 // When compiling natively:
56 #[cfg(not(target_arch = "wasm32"))]
57 fn main() {
58 use std::fs;
59
60 let args = Cli::parse();
61
62 use crate::plugins::Plugin;
63 let options = eframe::NativeOptions {
64 //initial_window_size: Some(egui::vec2(1280., 841.)),
65 multisampling: 0,
66 renderer: eframe::Renderer::Glow,
67 //icon_data: Some(IconData::try_from_png_bytes(&include_bytes!("../build/linux/256x256.png")[..]).unwrap()),
68 ..Default::default()
69 };
70
71 if let Ok(log_file) = Settings::get_log_file() {
72 // delete log file when it is too big
73 if let Ok(data) = fs::metadata(&log_file) {
74 if data.len() > 1024 * 256 {
75 fs::remove_file(&log_file).unwrap();
76 }
77 }
78
79 let level = log::LevelFilter::Info;
80
81 // Build a stderr logger.
82 let stderr = ConsoleAppender::builder().target(Target::Stderr).build();
83
84 // Logging to log file.
85 let logfile = FileAppender::builder()
86 // Pattern: https://docs.rs/log4rs/*/log4rs/encode/pattern/index.html
87 .encoder(Box::new(PatternEncoder::new("{l} - {m}\n")))
88 .build(log_file)
89 .unwrap();
90
91 let config = Config::builder()
92 .appender(Appender::builder().build("logfile", Box::new(logfile)))
93 .appender(
94 Appender::builder()
95 .filter(Box::new(ThresholdFilter::new(level)))
96 .build("stderr", Box::new(stderr)),
97 )
98 .build(Root::builder().appender("logfile").appender("stderr").build(LevelFilter::Info))
99 .unwrap();
100
101 // Use this to change log levels at runtime.
102 // This means you can change the default log level to trace
103 // if you are trying to debug an issue and need more logs on then turn it off
104 // once you are done.
105 let _handle = log4rs::init_config(config);
106 } else {
107 eprintln!("Failed to create log file");
108 }
109
110 if let Ok(settings_file) = Settings::get_settings_file() {
111 if settings_file.exists() {
112 if let Ok(settings) = Settings::load(&settings_file) {
113 unsafe {
114 SETTINGS = settings;
115 }
116 }
117 }
118 }
119
120 if let Ok(settings_file) = KeyBindings::get_keybindings_file() {
121 if settings_file.exists() {
122 if let Ok(key_bindings) = KeyBindings::load(&settings_file) {
123 unsafe {
124 KEYBINDINGS = key_bindings;
125 }
126 }
127 }
128 }
129
130 if let Ok(settings_file) = CharacterSets::get_character_sets_file() {
131 if settings_file.exists() {
132 if let Ok(character_sets) = CharacterSets::load(&settings_file) {
133 unsafe {
134 CHARACTER_SETS = character_sets;
135 }
136 }
137 }
138 }
139
140 if let Ok(settings_file) = MostRecentlyUsedFiles::get_mru_file() {
141 if settings_file.exists() {
142 if let Ok(character_sets) = MostRecentlyUsedFiles::load(&settings_file) {
143 unsafe {
144 MRU_FILES = character_sets;
145 }
146 }
147 }
148 }
149
150 unsafe {
151 if KEYBINDINGS.key_bindings.is_empty() {
152 KEYBINDINGS.key_bindings = Commands::default_keybindings();
153 }
154
155 if CHARACTER_SETS.character_sets.is_empty() {
156 CHARACTER_SETS.character_sets.push(CharSetMapping::default());
157 }
158 }
159
160 log::info!("Starting iCY DRAW {}", *VERSION);
161 Plugin::read_plugin_directory();
162 if let Err(err) = eframe::run_native(
163 &DEFAULT_TITLE,
164 options,
165 Box::new(|cc| {
166 let mut window = MainWindow::new(cc);
167 if let Some(mut path) = args.path {
168 if path.is_relative() {
169 path = std::env::current_dir().unwrap().join(path);
170 }
171 window.open_file(&path, false);
172 }
173 Box::new(window)
174 }),
175 ) {
176 log::error!("Error returned by run_native: {}", err);
177 }
178 log::info!("shutting down.");
179 }