calcifer/src/main.rs

237 lines
6.8 KiB
Rust
Raw Normal View History

2024-01-17 21:03:41 +01:00
mod tools;
2024-01-21 12:59:54 +01:00
mod calcifer;
2024-01-15 09:14:27 +01:00
2024-01-14 10:56:21 +01:00
use eframe::egui;
2024-01-22 20:00:17 +01:00
use calcifer::code_editor::ColorTheme;
2024-01-25 10:54:42 +01:00
use std::{path::Path, sync::Arc, time, thread, ops::Range};
2024-01-25 08:48:32 +01:00
use egui::FontFamily::Proportional;
use egui::FontId;
use egui::TextStyle::{Small, Button, Body, Heading, Monospace};
2024-01-23 22:09:56 +01:00
2024-01-22 20:00:17 +01:00
use calcifer::code_editor::themes::DEFAULT_THEMES;
2024-01-16 08:05:46 +01:00
2024-01-24 22:34:25 +01:00
#[cfg(debug_assertions)]
2024-01-24 23:10:26 +01:00
mod build {
2024-01-25 20:16:35 +01:00
pub const SAVE_PATH : &str = "/home/penwing/Documents/.save/debug/calcifer_save.json";
2024-01-24 23:10:26 +01:00
pub const TITLE: &str = " debug";
}
2024-01-24 22:34:25 +01:00
#[cfg(not(debug_assertions))]
2024-01-24 23:10:26 +01:00
mod build {
2024-01-25 20:31:17 +01:00
pub const SAVE_PATH : &str = "/home/penwing/Documents/.save/calcifer_save.json";
2024-01-24 23:10:26 +01:00
pub const TITLE: &str = "";
}
use build::SAVE_PATH;
use build::TITLE;
2024-01-24 22:34:25 +01:00
2024-01-16 08:05:46 +01:00
const TERMINAL_HEIGHT : f32 = 200.0;
2024-01-25 10:54:42 +01:00
const TERMINAL_RANGE : Range<f32> = 100.0..500.0;
2024-01-19 17:20:15 +01:00
const RED : egui::Color32 = egui::Color32::from_rgb(235, 108, 99);
2024-01-25 08:48:32 +01:00
const TIME_LABELS : [&str; 7] = ["input", "settings", "tree", "terminal", "tabs", "content", "windows"];
2024-01-23 22:43:23 +01:00
const MAX_FPS : f32 = 30.0;
2024-01-24 13:26:52 +01:00
const PATH_ROOT : &str = "/home/penwing/Documents/";
2024-01-24 14:53:24 +01:00
const DISPLAY_PATH_DEPTH : usize = 3;
2024-01-24 16:30:28 +01:00
const MAX_TABS : usize = 20;
2024-01-15 09:14:27 +01:00
2024-01-14 10:56:21 +01:00
fn main() -> Result<(), eframe::Error> {
2024-01-21 10:24:16 +01:00
let icon_data = tools::load_icon();
2024-01-23 22:09:56 +01:00
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([1200.0, 800.0])
2024-01-21 10:24:16 +01:00
.with_icon(Arc::new(icon_data)),
2024-01-23 22:09:56 +01:00
..Default::default()
};
let app_state: tools::AppState;
// Attempt to load previous state
if Path::new(SAVE_PATH).exists() {
app_state = tools::load_state(SAVE_PATH).expect("Failed to load the save");
} else {
app_state = tools::AppState {
tabs: vec![],
theme: 0,
};
}
eframe::run_native(
2024-01-25 19:10:23 +01:00
&format!("Calcifer{}{}", tools::version(), TITLE),
2024-01-23 22:09:56 +01:00
options,
Box::new(move |_cc| Box::from(Calcifer::from_app_state(app_state))),
)
2024-01-14 10:56:21 +01:00
}
2024-01-17 16:20:22 +01:00
2024-01-20 15:23:57 +01:00
struct Calcifer {
2024-01-23 22:09:56 +01:00
selected_tab : tools::TabNumber,
tabs: Vec<tools::Tab>,
2024-01-22 09:11:54 +01:00
2024-01-23 22:09:56 +01:00
command: String,
command_history: Vec<tools::CommandEntry>,
2024-01-22 09:11:54 +01:00
2024-01-23 22:09:56 +01:00
theme: ColorTheme,
2024-01-24 13:26:52 +01:00
font_size: f32,
2024-01-25 10:54:42 +01:00
tree_visible: bool,
profiler_visible: bool,
terminal_visible: bool,
2024-01-24 19:29:08 +01:00
close_tab_confirm: tools::confirm::ConfirmWindow,
tab_to_close: usize,
refresh_confirm: tools::confirm::ConfirmWindow,
2024-01-25 13:11:49 +01:00
exit_confirm: tools::confirm::ConfirmWindow,
2024-01-25 08:48:32 +01:00
2024-01-25 13:11:49 +01:00
search_menu: tools::search::SearchWindow,
2024-01-25 08:48:32 +01:00
settings_menu: tools::settings::SettingsWindow,
2024-01-25 10:54:42 +01:00
shortcuts_menu: tools::shortcuts::ShortcutsWindow,
2024-01-25 08:48:32 +01:00
time_watch: Vec<f32>,
next_frame: time::Instant,
2024-01-14 11:50:25 +01:00
}
2024-01-16 12:53:03 +01:00
2024-01-20 15:23:57 +01:00
impl Default for Calcifer {
2024-01-23 22:09:56 +01:00
fn default() -> Self {
2024-01-21 12:59:54 +01:00
Self {
2024-01-24 16:30:28 +01:00
selected_tab: tools::TabNumber::from_index(0),
2024-01-21 12:59:54 +01:00
tabs: vec![tools::Tab::default()],
2024-01-22 09:11:54 +01:00
2024-01-21 12:59:54 +01:00
command: String::new(),
command_history: Vec::new(),
2024-01-22 09:11:54 +01:00
2024-01-22 20:00:17 +01:00
theme: DEFAULT_THEMES[0],
2024-01-24 13:26:52 +01:00
font_size: 14.0,
2024-01-25 10:54:42 +01:00
tree_visible: false,
profiler_visible: false,
terminal_visible: false,
2024-01-24 19:29:08 +01:00
close_tab_confirm: tools::confirm::ConfirmWindow::new("You have some unsaved changes, Do you still want to close this document ?", "Confirm Close"),
tab_to_close: 0,
refresh_confirm: tools::confirm::ConfirmWindow::new("You have some unsaved changes, Do you still want to refresh this document ?", "Confirm Refresh"),
2024-01-25 13:11:49 +01:00
exit_confirm: tools::confirm::ConfirmWindow::new("", "Confirm Exit"),
2024-01-25 08:48:32 +01:00
2024-01-25 13:11:49 +01:00
search_menu: tools::search::SearchWindow::default(),
2024-01-25 08:48:32 +01:00
settings_menu: tools::settings::SettingsWindow::new(DEFAULT_THEMES[0]),
2024-01-25 10:54:42 +01:00
shortcuts_menu: tools::shortcuts::ShortcutsWindow::new(),
2024-01-25 08:48:32 +01:00
time_watch: vec![0.0; TIME_LABELS.len()],
next_frame: time::Instant::now(),
2024-01-21 12:59:54 +01:00
}
}
2024-01-14 11:50:25 +01:00
}
2024-01-15 10:06:20 +01:00
2024-01-20 15:23:57 +01:00
impl eframe::App for Calcifer {
2024-01-23 22:09:56 +01:00
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
2024-01-23 22:43:23 +01:00
thread::sleep(time::Duration::from_secs_f32(((1.0/MAX_FPS) - self.next_frame.elapsed().as_secs_f32()).max(0.0)));
self.next_frame = time::Instant::now();
let mut watch = time::Instant::now();
2024-01-23 22:09:56 +01:00
2024-01-25 08:48:32 +01:00
let mut style = (*ctx.style()).clone();
2024-01-25 10:54:42 +01:00
style.text_styles = [
(Heading, FontId::new(self.font_size * 1.6, Proportional)),
(Body, FontId::new(self.font_size, Proportional)),
(Monospace, FontId::new(self.font_size, Proportional)),
(Button, FontId::new(self.font_size, Proportional)),
(Small, FontId::new(self.font_size, Proportional)),
]
.into();
ctx.set_style(style);
if ctx.input( |i| i.key_pressed(egui::Key::R) && i.modifiers.ctrl) && !self.refresh_confirm.visible {
2024-01-24 19:29:08 +01:00
if self.tabs[self.selected_tab.to_index()].saved {
self.tabs[self.selected_tab.to_index()].refresh();
} else {
self.refresh_confirm.ask();
}
2024-01-23 22:09:56 +01:00
}
2024-01-24 13:26:52 +01:00
if ctx.input( |i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
self.handle_save_file(self.save_tab());
}
2024-01-20 19:08:23 +01:00
if ctx.input( |i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl && i.modifiers.shift) {
2024-01-21 12:59:54 +01:00
self.handle_save_file(self.save_tab_as());
2024-01-20 19:08:23 +01:00
}
2024-01-24 13:26:52 +01:00
if ctx.input( |i| i.key_pressed(egui::Key::ArrowLeft) && i.modifiers.alt) {
self.move_through_tabs(false);
}
if ctx.input( |i| i.key_pressed(egui::Key::ArrowRight) && i.modifiers.alt) {
self.move_through_tabs(true);
}
if ctx.input( |i| i.zoom_delta() > 1.0) {
self.font_size = (self.font_size * 1.1).min(30.0);
}
if ctx.input( |i| i.zoom_delta() < 1.0) {
self.font_size = (self.font_size / 1.1).max(10.0);
}
2024-01-22 09:11:54 +01:00
if ctx.input( |i| i.key_pressed(egui::Key::F) && i.modifiers.ctrl) {
2024-01-25 13:11:49 +01:00
self.search_menu.visible = !self.search_menu.visible.clone();
self.search_menu.initialized = !self.search_menu.visible.clone();
}
if ctx.input(|i| i.viewport().close_requested()) {
let mut unsaved_tabs : Vec<usize> = vec![];
for (index, tab) in self.tabs.iter().enumerate() {
if !tab.saved {
unsaved_tabs.push(index);
}
}
if unsaved_tabs.len() > 0 {
let mut unsaved_tabs_names : String = "".to_string();
for index in unsaved_tabs.iter() {
unsaved_tabs_names.push_str(&self.tabs[*index].get_name());
}
egui::Context::send_viewport_cmd(ctx, egui::ViewportCommand::CancelClose);
self.exit_confirm.prompt = format!("You have some unsaved changes :\n{}\nDo you still want to exit ?", unsaved_tabs_names);
self.exit_confirm.ask();
}
2024-01-22 09:11:54 +01:00
}
2024-01-20 19:08:23 +01:00
2024-01-25 08:48:32 +01:00
self.time_watch[0] = watch.elapsed().as_micros() as f32 / 1000.0;
watch = time::Instant::now();
2024-01-20 15:23:57 +01:00
self.draw_settings(ctx);
2024-01-23 22:09:56 +01:00
2024-01-25 08:48:32 +01:00
self.time_watch[1] = watch.elapsed().as_micros() as f32 / 1000.0;
2024-01-23 22:43:23 +01:00
watch = time::Instant::now();
2024-01-23 22:09:56 +01:00
self.draw_tree_panel(ctx);
2024-01-25 08:48:32 +01:00
self.time_watch[2] = watch.elapsed().as_micros() as f32 / 1000.0;
2024-01-23 22:43:23 +01:00
watch = time::Instant::now();
2024-01-23 22:09:56 +01:00
2024-01-25 10:54:42 +01:00
self.draw_bottom_tray(ctx);
2024-01-23 22:09:56 +01:00
self.draw_terminal_panel(ctx);
2024-01-25 08:48:32 +01:00
self.time_watch[3] = watch.elapsed().as_micros() as f32 / 1000.0;
2024-01-23 22:43:23 +01:00
watch = time::Instant::now();
2024-01-23 22:09:56 +01:00
self.draw_tab_panel(ctx);
2024-01-25 08:48:32 +01:00
self.time_watch[4] = watch.elapsed().as_micros() as f32 / 1000.0;
2024-01-23 22:43:23 +01:00
watch = time::Instant::now();
2024-01-23 22:09:56 +01:00
self.draw_content_panel(ctx);
2024-01-25 08:48:32 +01:00
self.time_watch[5] = watch.elapsed().as_micros() as f32 / 1000.0;
watch = time::Instant::now();
self.draw_windows(ctx);
2024-01-24 19:29:08 +01:00
2024-01-25 08:48:32 +01:00
self.time_watch[6] = watch.elapsed().as_micros() as f32 / 1000.0;
2024-01-20 19:08:23 +01:00
}
2024-01-21 12:28:10 +01:00
fn on_exit(&mut self, _gl : std::option::Option<&eframe::glow::Context>) {
self.save_state();
}
2024-01-25 19:10:23 +01:00
}