From 46bca1c6cbe4bb22869d5e80445f292b1fbba536 Mon Sep 17 00:00:00 2001 From: candle Date: Tue, 15 Jul 2025 09:53:45 -0400 Subject: [PATCH] fix font zooming stuff --- Cargo.toml | 2 +- src/app/state/config.rs | 1 + src/app/state/default.rs | 1 + src/app/state/editor.rs | 1 + src/app/state/ui.rs | 27 +++++++++++++++++++++------ src/ui/central_panel/editor.rs | 11 +++++++++++ src/ui/menu_bar.rs | 22 +++++++++++----------- src/ui/preferences_window.rs | 4 ++-- 8 files changed, 49 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e48f1ed..a3242f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ced" -version = "0.0.3" +version = "0.0.4" edition = "2024" [dependencies] diff --git a/src/app/state/config.rs b/src/app/state/config.rs index 58f6ca4..d7d8814 100644 --- a/src/app/state/config.rs +++ b/src/app/state/config.rs @@ -46,6 +46,7 @@ impl TextEditor { previous_cursor_char_index: None, current_cursor_line: 0, previous_cursor_line: 0, + font_settings_changed: false, } } diff --git a/src/app/state/default.rs b/src/app/state/default.rs index e4cd95c..2044f9f 100644 --- a/src/app/state/default.rs +++ b/src/app/state/default.rs @@ -46,6 +46,7 @@ impl Default for TextEditor { previous_cursor_char_index: None, current_cursor_line: 0, previous_cursor_line: 0, + font_settings_changed: false, } } } diff --git a/src/app/state/editor.rs b/src/app/state/editor.rs index 9b136a2..d9c082f 100644 --- a/src/app/state/editor.rs +++ b/src/app/state/editor.rs @@ -70,4 +70,5 @@ pub struct TextEditor { pub(crate) previous_cursor_char_index: Option, pub(crate) current_cursor_line: usize, // Track current line number incrementally pub(crate) previous_cursor_line: usize, // Track previous line for comparison + pub(crate) font_settings_changed: bool, // Flag to trigger text reprocessing when font changes } diff --git a/src/app/state/ui.rs b/src/app/state/ui.rs index a4951e7..cec69b0 100644 --- a/src/app/state/ui.rs +++ b/src/app/state/ui.rs @@ -32,11 +32,13 @@ impl TextEditor { egui::FontId::new(self.font_size, font_family) } + /// Immediately apply theme and save to configuration pub fn set_theme(&mut self, ctx: &egui::Context) { theme::apply(self.theme, ctx); self.save_config(); } + /// Apply font settings with immediate text reprocessing pub fn apply_font_settings(&mut self, ctx: &egui::Context) { let font_family = match self.font_family.as_str() { "Monospace" => egui::FontFamily::Monospace, @@ -50,9 +52,27 @@ impl TextEditor { ); ctx.set_style(style); + self.font_settings_changed = true; self.save_config(); } + /// Apply font settings with immediate text reprocessing + pub fn apply_font_settings_with_ui(&mut self, ctx: &egui::Context, ui: &egui::Ui) { + self.apply_font_settings(ctx); + self.reprocess_text_for_font_change(ui); + self.font_settings_changed = false; + } + + /// Trigger immediate text reprocessing when font settings change + pub fn reprocess_text_for_font_change(&mut self, ui: &egui::Ui) { + if let Some(active_tab) = self.get_active_tab() { + let content = active_tab.content.clone(); + if !content.is_empty() { + self.process_text_for_rendering(&content, ui); + } + } + } + /// Calculates the available width for the text editor, accounting for line numbers and separator pub fn calculate_editor_dimensions(&self, ui: &egui::Ui) -> EditorDimensions { let total_available_width = ui.available_width(); @@ -65,11 +85,9 @@ impl TextEditor { }; } - // Get line count from processing result let processing_result = self.get_text_processing_result(); let line_count = processing_result.line_count; - // Calculate base line number width let font_id = self.get_font_id(); let line_count_digits = line_count.to_string().len(); let sample_text = "9".repeat(line_count_digits); @@ -80,7 +98,6 @@ impl TextEditor { .x }); - // Add padding based on line_side setting let line_number_width = if self.line_side { base_line_number_width + 20.0 } else { @@ -108,10 +125,8 @@ impl TextEditor { return self.calculate_editor_dimensions(ui).text_width; } - // Use the pre-calculated pixel width with some padding - let longest_line_width = processing_result.longest_line_pixel_width + 20.0; + let longest_line_width = processing_result.longest_line_pixel_width + self.font_size; - // Return the larger of the calculated width or minimum available width let dimensions = self.calculate_editor_dimensions(ui); longest_line_width.max(dimensions.text_width) } diff --git a/src/ui/central_panel/editor.rs b/src/ui/central_panel/editor.rs index 738e213..6ece007 100644 --- a/src/ui/central_panel/editor.rs +++ b/src/ui/central_panel/editor.rs @@ -121,6 +121,17 @@ pub(super) fn editor_view_ui(ui: &mut egui::Ui, app: &mut TextEditor) -> egui::R } } + // Check if font settings changed and trigger reprocessing + if app.font_settings_changed { + if let Some(active_tab) = app.get_active_tab() { + let content = active_tab.content.clone(); + if !content.is_empty() { + app.process_text_for_rendering(&content, ui); + } + } + app.font_settings_changed = false; + } + if !word_wrap { if let Some(cursor_pos) = current_cursor_pos { let cursor_moved = Some(cursor_pos) != app.previous_cursor_position; diff --git a/src/ui/menu_bar.rs b/src/ui/menu_bar.rs index 60094ab..6dcd98b 100644 --- a/src/ui/menu_bar.rs +++ b/src/ui/menu_bar.rs @@ -172,7 +172,7 @@ pub(crate) fn menu_bar(app: &mut TextEditor, ctx: &egui::Context) { ui.menu_button("View", |ui| { app.menu_interaction_active = true; if ui - .checkbox(&mut app.show_line_numbers, "Toggle Line Numbers") + .checkbox(&mut app.show_line_numbers, "Show Line Numbers") .clicked() { app.save_config(); @@ -272,7 +272,7 @@ pub(crate) fn menu_bar(app: &mut TextEditor, ctx: &egui::Context) { ui.close_menu(); } }); - + if app.auto_hide_tab_bar { let tab_title = if let Some(tab) = app.get_active_tab() { tab.title.clone() @@ -280,31 +280,31 @@ pub(crate) fn menu_bar(app: &mut TextEditor, ctx: &egui::Context) { let empty_tab = crate::app::tab::Tab::new_empty(1); empty_tab.title.clone() }; - + let window_width = ctx.screen_rect().width(); - + let text_galley = ui.fonts(|fonts| { fonts.layout_job(egui::text::LayoutJob::simple_singleline( - tab_title.clone(), - app.get_font_id(), + tab_title, + ui.style().text_styles[&egui::TextStyle::Body].clone(), ui.style().visuals.text_color(), )) }); - + let text_width = text_galley.size().x; let text_height = text_galley.size().y; - + let window_center_x = window_width / 2.0; let text_x = (window_center_x - text_width / 2.0).max(0.0); - + let cursor_pos = ui.cursor().left_top(); - + ui.painter().galley( egui::pos2(text_x, cursor_pos.y), text_galley, ui.style().visuals.text_color(), ); - + ui.allocate_exact_size(egui::vec2(0.0, text_height), egui::Sense::hover()); } }); diff --git a/src/ui/preferences_window.rs b/src/ui/preferences_window.rs index 105f309..be33a80 100644 --- a/src/ui/preferences_window.rs +++ b/src/ui/preferences_window.rs @@ -58,7 +58,7 @@ pub(crate) fn preferences_window(app: &mut TextEditor, ctx: &egui::Context) { }); if changed { - app.apply_font_settings(ctx); + app.apply_font_settings_with_ui(ctx, ui); } }); @@ -93,7 +93,7 @@ pub(crate) fn preferences_window(app: &mut TextEditor, ctx: &egui::Context) { let clamped_size = new_size.clamp(8.0, 32.0); if (app.font_size - clamped_size).abs() > 0.1 { app.font_size = clamped_size; - app.apply_font_settings(ctx); + app.apply_font_settings_with_ui(ctx, ui); } } app.font_size_input = None;