ced/src/app/state/config.rs

109 lines
3.6 KiB
Rust

use super::editor::TextEditor;
use crate::app::config::Config;
use crate::app::theme;
use crate::io;
use std::path::PathBuf;
impl TextEditor {
pub fn from_config(config: Config) -> Self {
Self {
state_cache: config.state_cache,
show_line_numbers: config.show_line_numbers,
word_wrap: config.word_wrap,
auto_hide_toolbar: config.auto_hide_toolbar,
hide_tab_bar: config.hide_tab_bar,
theme: config.theme,
line_side: config.line_side,
font_family: config.font_family,
font_size: config.font_size,
syntax_highlighting: config.syntax_highlighting,
..Default::default()
}
}
pub fn from_config_with_context(
config: Config,
cc: &eframe::CreationContext<'_>,
initial_paths: Vec<PathBuf>,
) -> Self {
let mut editor = Self::from_config(config);
if let Err(e) = editor.load_state_cache() {
eprintln!("Failed to load state cache: {e}");
}
if !initial_paths.is_empty() {
let mut opened_any = false;
for path in initial_paths {
if path.is_file() {
match io::open_file_from_path(&mut editor, path.clone()) {
Ok(()) => opened_any = true,
Err(e) => eprintln!("Error opening file {}: {}", path.display(), e),
}
} else if path.is_dir() {
match io::open_files_from_directory(&mut editor, path.clone()) {
Ok(count) => {
opened_any = true;
println!("Opened {} files from directory {}", count, path.display());
}
Err(e) => eprintln!("Error opening directory {}: {}", path.display(), e),
}
} else {
eprintln!("Warning: Path does not exist: {}", path.display());
}
}
if opened_any {
editor.active_tab_index = editor.tabs.len().saturating_sub(1);
}
}
theme::apply(editor.theme, &cc.egui_ctx);
cc.egui_ctx.options_mut(|o| o.zoom_with_keyboard = false);
let mut style = (*cc.egui_ctx.style()).to_owned();
style
.text_styles
.insert(egui::TextStyle::Body, egui::FontId::proportional(16.0));
style
.text_styles
.insert(egui::TextStyle::Button, egui::FontId::proportional(16.0));
style
.text_styles
.insert(egui::TextStyle::Heading, egui::FontId::proportional(22.0));
style
.text_styles
.insert(egui::TextStyle::Small, egui::FontId::proportional(14.0));
cc.egui_ctx.set_style(style);
editor.apply_font_settings(&cc.egui_ctx);
editor
}
pub fn get_config(&self) -> Config {
Config {
state_cache: self.state_cache,
auto_hide_toolbar: self.auto_hide_toolbar,
show_line_numbers: self.show_line_numbers,
hide_tab_bar: self.hide_tab_bar,
word_wrap: self.word_wrap,
theme: self.theme,
line_side: self.line_side,
font_family: self.font_family.to_string(),
font_size: self.font_size,
syntax_highlighting: self.syntax_highlighting,
// vim_mode: self.vim_mode,
}
}
pub fn save_config(&self) {
let config = self.get_config();
if let Err(e) = config.save() {
eprintln!("Failed to save configuration: {e}");
}
}
}