calcifer/src/main.rs

118 lines
2.9 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-21 12:59:54 +01:00
use std::{path::Path, sync::Arc};
2024-01-22 09:11:54 +01:00
use tools::Demo;
2024-01-22 20:00:17 +01:00
use calcifer::code_editor::themes::DEFAULT_THEMES;
2024-01-16 08:05:46 +01:00
const TERMINAL_HEIGHT : f32 = 200.0;
2024-01-19 17:20:15 +01:00
const RED : egui::Color32 = egui::Color32::from_rgb(235, 108, 99);
2024-01-21 12:28:10 +01:00
const SAVE_PATH : &str = "calcifer_save.json";
2024-01-15 09:14:27 +01:00
2024-01-14 10:56:21 +01:00
fn main() -> Result<(), eframe::Error> {
2024-01-17 21:03:41 +01:00
tools::loaded();
2024-01-21 10:24:16 +01:00
let icon_data = tools::load_icon();
2024-01-14 10:56:21 +01:00
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
2024-01-15 10:06:20 +01:00
.with_inner_size([1200.0, 800.0])
2024-01-21 10:24:16 +01:00
.with_icon(Arc::new(icon_data)),
2024-01-14 10:56:21 +01:00
..Default::default()
};
2024-01-21 12:28:10 +01:00
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,
};
}
2024-01-14 10:56:21 +01:00
eframe::run_native(
2024-01-23 09:27:41 +01:00
"Calcifer v1.0.2",
2024-01-14 10:56:21 +01:00
options,
2024-01-21 12:28:10 +01:00
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-19 19:21:16 +01:00
selected_tab : tools::TabNumber,
tabs: Vec<tools::Tab>,
2024-01-22 09:11:54 +01:00
2024-01-16 11:07:53 +01:00
command: String,
2024-01-19 17:20:15 +01:00
command_history: Vec<tools::CommandEntry>,
2024-01-22 09:11:54 +01:00
2024-01-20 15:23:57 +01:00
theme: ColorTheme,
2024-01-22 09:11:54 +01:00
search: tools::search::SearchWindow,
searching: bool,
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-14 11:50:25 +01:00
fn default() -> Self {
2024-01-21 12:59:54 +01:00
Self {
selected_tab: tools::TabNumber::Zero,
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-22 09:11:54 +01:00
search: tools::search::SearchWindow::default(),
searching: false,
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-14 10:56:21 +01:00
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
2024-01-20 19:08:23 +01:00
if ctx.input( |i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
2024-01-21 12:59:54 +01:00
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-22 09:11:54 +01:00
if ctx.input( |i| i.key_pressed(egui::Key::F) && i.modifiers.ctrl) {
self.searching = !self.searching.clone();
2024-01-23 09:27:41 +01:00
if self.searching {
self.search.initialized = false;
}
2024-01-22 09:11:54 +01:00
}
2024-01-20 19:08:23 +01:00
2024-01-20 15:23:57 +01:00
self.draw_settings(ctx);
2024-01-17 16:20:22 +01:00
self.draw_tree_panel(ctx);
self.draw_terminal_panel(ctx);
2024-01-19 19:21:16 +01:00
self.draw_tab_panel(ctx);
2024-01-20 15:23:57 +01:00
self.draw_content_panel(ctx);
2024-01-22 09:11:54 +01:00
if self.searching {
self.search.show(ctx, &mut self.searching, &mut self.tabs, &mut self.selected_tab);
}
2024-01-22 17:03:57 +01:00
if !self.search.tab_selected && self.search.get_tab() != self.selected_tab {
self.selected_tab = self.search.get_tab();
2024-01-23 09:27:41 +01:00
println!("changed tab to {}", self.selected_tab.to_index());
2024-01-22 17:03:57 +01:00
}
self.search.tab_selected = true;
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-17 16:20:22 +01:00
}