added save state
This commit is contained in:
parent
121c1b4a4e
commit
6b6c84db44
25
Cargo.lock
generated
25
Cargo.lock
generated
|
@ -578,6 +578,8 @@ dependencies = [
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"image",
|
"image",
|
||||||
"rfd",
|
"rfd",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1648,6 +1650,12 @@ dependencies = [
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "jni"
|
name = "jni"
|
||||||
version = "0.21.1"
|
version = "0.21.1"
|
||||||
|
@ -2367,6 +2375,12 @@ dependencies = [
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "same-file"
|
name = "same-file"
|
||||||
version = "1.0.6"
|
version = "1.0.6"
|
||||||
|
@ -2421,6 +2435,17 @@ dependencies = [
|
||||||
"syn 2.0.48",
|
"syn 2.0.48",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.111"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_repr"
|
name = "serde_repr"
|
||||||
version = "0.1.18"
|
version = "0.1.18"
|
||||||
|
|
|
@ -12,7 +12,9 @@ env_logger = { version = "0.10.1", default-features = false, features = [
|
||||||
"humantime",
|
"humantime",
|
||||||
] }
|
] }
|
||||||
rfd = "0.12.1"
|
rfd = "0.12.1"
|
||||||
egui_extras = { version = "0.25.0" }
|
egui_extras = "0.25.0"
|
||||||
egui_code_editor = "0.2.3"
|
egui_code_editor = {version = "0.2.3"}
|
||||||
image = "0.24.8"
|
image = "0.24.8"
|
||||||
#syntect = "4.6"
|
serde = { version = "1.0.195", features = ["derive"] }
|
||||||
|
serde_json = "1.0.111"
|
||||||
|
|
||||||
|
|
1
calcifer_save.json
Normal file
1
calcifer_save.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"tabs":["/home/penwing/Documents/projects/rust/calcifer/src/main.rs"],"theme":6}
|
65
src/main.rs
65
src/main.rs
|
@ -8,6 +8,7 @@ use std::{path::Path, path::PathBuf, fs, io, env, cmp::max, cmp::min, sync::Arc}
|
||||||
const TERMINAL_HEIGHT : f32 = 200.0;
|
const TERMINAL_HEIGHT : f32 = 200.0;
|
||||||
const RED : egui::Color32 = egui::Color32::from_rgb(235, 108, 99);
|
const RED : egui::Color32 = egui::Color32::from_rgb(235, 108, 99);
|
||||||
const HISTORY_LENGTH : usize = 2;
|
const HISTORY_LENGTH : usize = 2;
|
||||||
|
const SAVE_PATH : &str = "calcifer_save.json";
|
||||||
|
|
||||||
|
|
||||||
fn main() -> Result<(), eframe::Error> {
|
fn main() -> Result<(), eframe::Error> {
|
||||||
|
@ -22,10 +23,22 @@ fn main() -> Result<(), eframe::Error> {
|
||||||
.with_icon(Arc::new(icon_data)),
|
.with_icon(Arc::new(icon_data)),
|
||||||
..Default::default()
|
..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(
|
eframe::run_native(
|
||||||
"Calcifer",
|
"Calcifer",
|
||||||
options,
|
options,
|
||||||
Box::new(|_cc| Box::<Calcifer>::default()),
|
Box::new(move |_cc| Box::from(Calcifer::from_app_state(app_state))),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +97,10 @@ impl eframe::App for Calcifer {
|
||||||
self.draw_tab_panel(ctx);
|
self.draw_tab_panel(ctx);
|
||||||
self.draw_content_panel(ctx);
|
self.draw_content_panel(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn on_exit(&mut self, _gl : std::option::Option<&eframe::glow::Context>) {
|
||||||
|
self.save_state();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -99,13 +116,10 @@ impl Calcifer {
|
||||||
.show_ui(ui, |ui| {
|
.show_ui(ui, |ui| {
|
||||||
ui.style_mut().wrap = Some(false);
|
ui.style_mut().wrap = Some(false);
|
||||||
ui.set_min_width(60.0);
|
ui.set_min_width(60.0);
|
||||||
ui.selectable_value(&mut self.theme, ColorTheme::SONOKAI, "Sonokai");
|
for i in 0..tools::themes::CustomColorTheme::max() {
|
||||||
ui.selectable_value(&mut self.theme, ColorTheme::AYU_DARK, "Ayu Dark");
|
let theme = tools::themes::CustomColorTheme::from_index(i);
|
||||||
ui.selectable_value(&mut self.theme, ColorTheme::AYU_MIRAGE, "Ayu Mirage");
|
ui.selectable_value(&mut self.theme, theme, theme.name);
|
||||||
ui.selectable_value(&mut self.theme, ColorTheme::GITHUB_DARK, "Github Dark");
|
}
|
||||||
ui.selectable_value(&mut self.theme, ColorTheme::GRUVBOX, "Gruvbox");
|
|
||||||
ui.selectable_value(&mut self.theme, tools::themes::CustomColorTheme::fire(), "Fire");
|
|
||||||
ui.selectable_value(&mut self.theme, tools::themes::CustomColorTheme::ash(), "Ash");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -329,5 +343,40 @@ impl Calcifer {
|
||||||
current_tab.code = current_tab.history[current_tab.history.len() - 2].clone();
|
current_tab.code = current_tab.history[current_tab.history.len() - 2].clone();
|
||||||
current_tab.history.pop();
|
current_tab.history.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_app_state(app_state: tools::AppState) -> Self {
|
||||||
|
let mut new = Self::default();
|
||||||
|
new.theme = tools::themes::CustomColorTheme::from_index(app_state.theme);
|
||||||
|
|
||||||
|
new.tabs = vec![];
|
||||||
|
|
||||||
|
for path in app_state.tabs {
|
||||||
|
if path.file_name().expect("Could not get Tab Name").to_string_lossy().to_string() != "untitled" {
|
||||||
|
new.open_file(&path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if new.tabs == vec![] {
|
||||||
|
new.new_tab();
|
||||||
|
}
|
||||||
|
|
||||||
|
new
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_state(&self) {
|
||||||
|
let state_theme = tools::themes::CustomColorTheme::to_index(self.theme);
|
||||||
|
|
||||||
|
let mut state_tabs = vec![];
|
||||||
|
|
||||||
|
for tab in &self.tabs {
|
||||||
|
state_tabs.push(tab.path.clone());
|
||||||
|
}
|
||||||
|
let app_state = tools::AppState {
|
||||||
|
tabs: state_tabs,
|
||||||
|
theme: state_theme,
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = tools::save_state(&app_state, SAVE_PATH);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
//use eframe::egui;
|
use std::{process::Command, cmp::Ordering, env, path::PathBuf, fs::read_to_string, fs::write};
|
||||||
//use std::io;
|
|
||||||
use std::{process::Command, cmp::Ordering, env, path::PathBuf};
|
|
||||||
use egui_code_editor::Syntax;
|
use egui_code_editor::Syntax;
|
||||||
use eframe::egui::IconData;
|
use eframe::egui::IconData;
|
||||||
//use std::fs;
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
pub mod themes;
|
pub mod themes;
|
||||||
|
|
||||||
|
@ -51,7 +49,7 @@ impl TabNumber {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, PartialEq)]
|
||||||
pub struct Tab {
|
pub struct Tab {
|
||||||
pub path : PathBuf,
|
pub path : PathBuf,
|
||||||
pub code : String,
|
pub code : String,
|
||||||
|
@ -100,6 +98,31 @@ impl Default for CommandEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct AppState {
|
||||||
|
pub tabs: Vec<PathBuf>,
|
||||||
|
pub theme: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn save_state(state: &AppState, file_path: &str) -> Result<(), std::io::Error> {
|
||||||
|
let serialized_state = serde_json::to_string(state)?;
|
||||||
|
|
||||||
|
write(file_path, serialized_state)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn load_state(file_path: &str) -> Result<AppState, std::io::Error> {
|
||||||
|
let serialized_state = read_to_string(file_path)?;
|
||||||
|
|
||||||
|
let state: AppState = serde_json::from_str(&serialized_state)?;
|
||||||
|
|
||||||
|
Ok(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn loaded() {
|
pub fn loaded() {
|
||||||
println!("Tools loaded");
|
println!("Tools loaded");
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,4 +44,34 @@ impl CustomColorTheme {
|
||||||
|
|
||||||
theme
|
theme
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_index(n : usize) -> ColorTheme {
|
||||||
|
match n {
|
||||||
|
0 => ColorTheme::SONOKAI,
|
||||||
|
1 => ColorTheme::GRUVBOX,
|
||||||
|
2 => ColorTheme::GITHUB_DARK,
|
||||||
|
3 => ColorTheme::AYU_MIRAGE,
|
||||||
|
4 => ColorTheme::AYU_DARK,
|
||||||
|
5 => CustomColorTheme::ash(),
|
||||||
|
6 => CustomColorTheme::fire(),
|
||||||
|
_ => CustomColorTheme::ash(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn max() -> usize {
|
||||||
|
7
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_index(theme : ColorTheme) -> usize {
|
||||||
|
match theme.name {
|
||||||
|
"Sonokai" => 0,
|
||||||
|
"Gruvbox" => 1,
|
||||||
|
"Github Dark" => 2,
|
||||||
|
"Ayu Mirage" => 3,
|
||||||
|
"Ayu Dark" => 4,
|
||||||
|
"Ash" => 5,
|
||||||
|
"Fire" => 6,
|
||||||
|
_ => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue