much better error handling
This commit is contained in:
parent
5fedd82dda
commit
b7b924d6b4
|
@ -1,11 +1,12 @@
|
|||
use eframe::egui;
|
||||
use egui::{text::CCursor, text_edit::CCursorRange, Rangef};
|
||||
use std::{cmp::max, env, path::Path};
|
||||
use std::{cmp::max, env, path::Path, path::PathBuf};
|
||||
|
||||
use crate::tools;
|
||||
use crate::Calcifer;
|
||||
use crate::MAX_TABS;
|
||||
use crate::PATH_ROOT;
|
||||
use tools::hex_str_to_color;
|
||||
|
||||
pub mod code_editor;
|
||||
use code_editor::CodeEditor;
|
||||
|
@ -77,12 +78,9 @@ impl Calcifer {
|
|||
.resizable(true)
|
||||
.show(ctx, |ui| {
|
||||
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
|
||||
let command_color = egui::Color32::from_hex(self.theme.functions)
|
||||
.expect("Theme color issue (functions)");
|
||||
let entry_color = egui::Color32::from_hex(self.theme.literals)
|
||||
.expect("Theme color issue (literals)");
|
||||
let bg_color =
|
||||
egui::Color32::from_hex(self.theme.bg).expect("Theme color issue (bg)");
|
||||
let command_color = hex_str_to_color(self.theme.functions);
|
||||
let entry_color = hex_str_to_color(self.theme.literals);
|
||||
let bg_color = hex_str_to_color(self.theme.bg);
|
||||
|
||||
ui.label("");
|
||||
|
||||
|
@ -95,7 +93,7 @@ impl Calcifer {
|
|||
ui.colored_label(
|
||||
command_color,
|
||||
tools::format_path(
|
||||
&env::current_dir().expect("Could not find Shell Environnment"),
|
||||
&env::current_dir().unwrap_or_else(|_| PathBuf::from("/")),
|
||||
),
|
||||
);
|
||||
let response = ui.add(
|
||||
|
@ -148,21 +146,16 @@ impl Calcifer {
|
|||
.show(ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.style_mut().visuals.selection.bg_fill =
|
||||
egui::Color32::from_hex(self.theme.functions)
|
||||
.expect("Could not convert color");
|
||||
ui.style_mut().visuals.hyperlink_color =
|
||||
egui::Color32::from_hex(self.theme.functions)
|
||||
.expect("Could not convert color");
|
||||
hex_str_to_color(self.theme.functions);
|
||||
ui.style_mut().visuals.hyperlink_color = hex_str_to_color(self.theme.functions);
|
||||
for (index, tab) in self.tabs.clone().iter().enumerate() {
|
||||
let mut title = tab.get_name();
|
||||
if !tab.saved {
|
||||
title += " ~";
|
||||
}
|
||||
if self.selected_tab == tools::TabNumber::from_index(index) {
|
||||
ui.style_mut().visuals.override_text_color = Some(
|
||||
egui::Color32::from_hex(self.theme.bg)
|
||||
.expect("Could not convert color"),
|
||||
);
|
||||
ui.style_mut().visuals.override_text_color =
|
||||
Some(hex_str_to_color(self.theme.bg));
|
||||
}
|
||||
ui.selectable_value(
|
||||
&mut self.selected_tab,
|
||||
|
|
|
@ -9,6 +9,7 @@ use crate::MAX_TABS;
|
|||
use crate::PATH_ROOT;
|
||||
use crate::SAVE_PATH;
|
||||
use crate::TIME_LABELS;
|
||||
use tools::hex_str_to_color;
|
||||
|
||||
impl Calcifer {
|
||||
pub fn handle_confirm(&mut self) {
|
||||
|
@ -27,9 +28,7 @@ impl Calcifer {
|
|||
if self.tabs[self.selected_tab.to_index()]
|
||||
.path
|
||||
.file_name()
|
||||
.expect("Could not get Tab Name")
|
||||
.to_string_lossy()
|
||||
== "untitled"
|
||||
.map_or(true, |name| name.to_string_lossy() == "untitled")
|
||||
{
|
||||
self.save_tab_as()
|
||||
} else {
|
||||
|
@ -77,11 +76,9 @@ impl Calcifer {
|
|||
};
|
||||
|
||||
for path in app_state.tabs {
|
||||
if path
|
||||
if !path
|
||||
.file_name()
|
||||
.expect("Could not get Tab Name")
|
||||
.to_string_lossy()
|
||||
!= "untitled"
|
||||
.map_or(true, |name| name.to_string_lossy() == "untitled")
|
||||
{
|
||||
new.open_file(Some(&path));
|
||||
}
|
||||
|
@ -195,15 +192,11 @@ impl Calcifer {
|
|||
let text_color: Color32;
|
||||
|
||||
if display {
|
||||
bg_color = Color32::from_hex(self.theme.functions)
|
||||
.expect("Could not convert color to hex (functions)");
|
||||
text_color =
|
||||
Color32::from_hex(self.theme.bg).expect("Could not convert color to hex (bg)");
|
||||
bg_color = hex_str_to_color(self.theme.functions);
|
||||
text_color = hex_str_to_color(self.theme.bg);
|
||||
} else {
|
||||
bg_color =
|
||||
Color32::from_hex(self.theme.bg).expect("Could not convert color to hex (bg)");
|
||||
text_color = Color32::from_hex(self.theme.literals)
|
||||
.expect("Could not convert color to hex (literals)");
|
||||
bg_color = hex_str_to_color(self.theme.bg);
|
||||
text_color = hex_str_to_color(self.theme.literals);
|
||||
};
|
||||
|
||||
ui.style_mut().visuals.override_text_color = Some(text_color);
|
||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -37,7 +37,8 @@ const DISPLAY_PATH_DEPTH: usize = 3;
|
|||
const MAX_TABS: usize = 20;
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
let icon_data = tools::load_icon();
|
||||
let icon_data = tools::load_icon().unwrap_or_default();
|
||||
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default()
|
||||
.with_inner_size([1200.0, 800.0])
|
||||
|
@ -47,12 +48,12 @@ fn main() -> Result<(), eframe::Error> {
|
|||
|
||||
// Attempt to load previous state
|
||||
let app_state: tools::AppState = if Path::new(SAVE_PATH).exists() {
|
||||
tools::load_state(SAVE_PATH).expect("Failed to load the save")
|
||||
} else {
|
||||
tools::AppState {
|
||||
tabs: vec![],
|
||||
theme: 0,
|
||||
match tools::load_state(SAVE_PATH) {
|
||||
Ok(app_state) => app_state,
|
||||
Err(_) => tools::AppState::default(),
|
||||
}
|
||||
} else {
|
||||
tools::AppState::default()
|
||||
};
|
||||
|
||||
eframe::run_native(
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
use crate::calcifer::code_editor::Syntax;
|
||||
use crate::DISPLAY_PATH_DEPTH;
|
||||
use eframe::egui;
|
||||
use egui::Color32;
|
||||
use image::GenericImageView;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
cmp::Ordering, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write, path::Component,
|
||||
path::Path, path::PathBuf,
|
||||
cmp::Ordering, error::Error, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write,
|
||||
path::Component, path::Path, path::PathBuf,
|
||||
};
|
||||
|
||||
//my tools;
|
||||
|
@ -25,6 +27,15 @@ pub struct AppState {
|
|||
pub theme: usize,
|
||||
}
|
||||
|
||||
impl Default for AppState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
tabs: vec![],
|
||||
theme: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_state(state: &AppState, file_path: &str) -> Result<(), std::io::Error> {
|
||||
let serialized_state = serde_json::to_string(state)?;
|
||||
|
||||
|
@ -51,22 +62,20 @@ pub fn load_state(file_path: &str) -> Result<AppState, std::io::Error> {
|
|||
Ok(serde_json::from_str(&serialized_state)?)
|
||||
}
|
||||
|
||||
pub fn load_icon() -> egui::IconData {
|
||||
pub fn load_icon() -> Result<egui::IconData, Box<dyn Error>> {
|
||||
let (icon_rgba, icon_width, icon_height) = {
|
||||
let icon = include_bytes!("../../assets/icon.png");
|
||||
let image = image::load_from_memory(icon)
|
||||
.expect("Failed to open icon path")
|
||||
.into_rgba8();
|
||||
let image = image::load_from_memory(icon)?;
|
||||
let rgba = image.clone().into_rgba8().to_vec();
|
||||
let (width, height) = image.dimensions();
|
||||
let rgba = image.into_raw();
|
||||
(rgba, width, height)
|
||||
};
|
||||
|
||||
egui::IconData {
|
||||
Ok(egui::IconData {
|
||||
rgba: icon_rgba,
|
||||
width: icon_width,
|
||||
height: icon_height,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn to_syntax(language: &str) -> Syntax {
|
||||
|
@ -114,5 +123,9 @@ pub fn format_path(path: &Path) -> String {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn hex_str_to_color(hex_str: &str) -> Color32 {
|
||||
Color32::from_hex(hex_str).unwrap_or_else(|_| Color32::BLACK)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
|
@ -70,9 +70,9 @@ impl Tab {
|
|||
pub fn get_name(&self) -> String {
|
||||
self.path
|
||||
.file_name()
|
||||
.expect("Could not get Tab Name")
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
.map_or("untitled".to_string(), |name| {
|
||||
name.to_string_lossy().to_string()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn refresh(&mut self) {
|
||||
|
|
Loading…
Reference in a new issue