config checking/merging, slightly more generous scroll width for giga fast typing

This commit is contained in:
candle 2025-07-15 11:20:52 -04:00
parent 3ee73c3d9b
commit c1d0c7af1e
2 changed files with 46 additions and 14 deletions

View File

@ -5,22 +5,38 @@ use super::theme::Theme;
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config { pub struct Config {
#[serde(default = "default_auto_hide_toolbar")]
pub auto_hide_toolbar: bool, pub auto_hide_toolbar: bool,
#[serde(default = "default_auto_hide_tab_bar")]
pub auto_hide_tab_bar: bool, pub auto_hide_tab_bar: bool,
#[serde(default = "default_show_line_numbers")]
pub show_line_numbers: bool, pub show_line_numbers: bool,
#[serde(default = "default_word_wrap")]
pub word_wrap: bool, pub word_wrap: bool,
#[serde(default = "Theme::default")]
pub theme: Theme, pub theme: Theme,
#[serde(default = "default_line_side")]
pub line_side: bool, pub line_side: bool,
#[serde(default = "default_font_family")]
pub font_family: String, pub font_family: String,
#[serde(default = "default_font_size")]
pub font_size: f32, pub font_size: f32,
// pub vim_mode: bool, // pub vim_mode: bool,
} }
fn default_auto_hide_toolbar() -> bool { false }
fn default_auto_hide_tab_bar() -> bool { true }
fn default_show_line_numbers() -> bool { false }
fn default_word_wrap() -> bool { true }
fn default_line_side() -> bool { false }
fn default_font_family() -> String { "Proportional".to_string() }
fn default_font_size() -> f32 { 14.0 }
impl Default for Config { impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
auto_hide_toolbar: false, auto_hide_toolbar: false,
auto_hide_tab_bar: false, auto_hide_tab_bar: true,
show_line_numbers: false, show_line_numbers: false,
word_wrap: true, word_wrap: true,
theme: Theme::default(), theme: Theme::default(),
@ -35,9 +51,9 @@ impl Default for Config {
impl Config { impl Config {
pub fn config_path() -> Option<PathBuf> { pub fn config_path() -> Option<PathBuf> {
let config_dir = if let Some(config_dir) = dirs::config_dir() { let config_dir = if let Some(config_dir) = dirs::config_dir() {
config_dir.join("ced") config_dir.join(format!("{}", env!("CARGO_PKG_NAME")))
} else if let Some(home_dir) = dirs::home_dir() { } else if let Some(home_dir) = dirs::home_dir() {
home_dir.join(".config").join("ced") home_dir.join(".config").join(format!("{}", env!("CARGO_PKG_NAME")))
} else { } else {
return None; return None;
}; };
@ -60,16 +76,22 @@ impl Config {
} }
match std::fs::read_to_string(&config_path) { match std::fs::read_to_string(&config_path) {
Ok(content) => match toml::from_str::<Config>(&content) { Ok(content) => {
Ok(config) => config, let mut config = match toml::from_str::<Config>(&content) {
Err(e) => { Ok(config) => config,
eprintln!( Err(e) => {
"Failed to parse config file {}: {}", eprintln!(
config_path.display(), "Failed to parse config file {}: {}",
e config_path.display(),
); e
Self::default() );
} return Self::default();
}
};
let default_config = Self::default();
config.merge_with_default(default_config);
config
}, },
Err(e) => { Err(e) => {
eprintln!( eprintln!(
@ -82,6 +104,16 @@ impl Config {
} }
} }
fn merge_with_default(&mut self, default: Config) {
if self.font_family.is_empty() {
self.font_family = default.font_family;
}
if self.font_size <= 0.0 {
self.font_size = default.font_size;
}
}
pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> { pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
let config_path = Self::config_path().ok_or("Cannot determine config directory")?; let config_path = Self::config_path().ok_or("Cannot determine config directory")?;

View File

@ -125,7 +125,7 @@ impl TextEditor {
return self.calculate_editor_dimensions(ui).text_width; return self.calculate_editor_dimensions(ui).text_width;
} }
let longest_line_width = processing_result.longest_line_pixel_width + self.font_size; let longest_line_width = processing_result.longest_line_pixel_width + (self.font_size * 2.0);
let dimensions = self.calculate_editor_dimensions(ui); let dimensions = self.calculate_editor_dimensions(ui);
longest_line_width.max(dimensions.text_width) longest_line_width.max(dimensions.text_width)