fix font zooming stuff

This commit is contained in:
candle 2025-07-15 09:53:45 -04:00
parent 1ed06bbe37
commit 46bca1c6cb
8 changed files with 49 additions and 20 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ced" name = "ced"
version = "0.0.3" version = "0.0.4"
edition = "2024" edition = "2024"
[dependencies] [dependencies]

View File

@ -46,6 +46,7 @@ impl TextEditor {
previous_cursor_char_index: None, previous_cursor_char_index: None,
current_cursor_line: 0, current_cursor_line: 0,
previous_cursor_line: 0, previous_cursor_line: 0,
font_settings_changed: false,
} }
} }

View File

@ -46,6 +46,7 @@ impl Default for TextEditor {
previous_cursor_char_index: None, previous_cursor_char_index: None,
current_cursor_line: 0, current_cursor_line: 0,
previous_cursor_line: 0, previous_cursor_line: 0,
font_settings_changed: false,
} }
} }
} }

View File

@ -70,4 +70,5 @@ pub struct TextEditor {
pub(crate) previous_cursor_char_index: Option<usize>, pub(crate) previous_cursor_char_index: Option<usize>,
pub(crate) current_cursor_line: usize, // Track current line number incrementally pub(crate) current_cursor_line: usize, // Track current line number incrementally
pub(crate) previous_cursor_line: usize, // Track previous line for comparison 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
} }

View File

@ -32,11 +32,13 @@ impl TextEditor {
egui::FontId::new(self.font_size, font_family) egui::FontId::new(self.font_size, font_family)
} }
/// Immediately apply theme and save to configuration
pub fn set_theme(&mut self, ctx: &egui::Context) { pub fn set_theme(&mut self, ctx: &egui::Context) {
theme::apply(self.theme, ctx); theme::apply(self.theme, ctx);
self.save_config(); self.save_config();
} }
/// Apply font settings with immediate text reprocessing
pub fn apply_font_settings(&mut self, ctx: &egui::Context) { pub fn apply_font_settings(&mut self, ctx: &egui::Context) {
let font_family = match self.font_family.as_str() { let font_family = match self.font_family.as_str() {
"Monospace" => egui::FontFamily::Monospace, "Monospace" => egui::FontFamily::Monospace,
@ -50,9 +52,27 @@ impl TextEditor {
); );
ctx.set_style(style); ctx.set_style(style);
self.font_settings_changed = true;
self.save_config(); 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 /// Calculates the available width for the text editor, accounting for line numbers and separator
pub fn calculate_editor_dimensions(&self, ui: &egui::Ui) -> EditorDimensions { pub fn calculate_editor_dimensions(&self, ui: &egui::Ui) -> EditorDimensions {
let total_available_width = ui.available_width(); 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 processing_result = self.get_text_processing_result();
let line_count = processing_result.line_count; let line_count = processing_result.line_count;
// Calculate base line number width
let font_id = self.get_font_id(); let font_id = self.get_font_id();
let line_count_digits = line_count.to_string().len(); let line_count_digits = line_count.to_string().len();
let sample_text = "9".repeat(line_count_digits); let sample_text = "9".repeat(line_count_digits);
@ -80,7 +98,6 @@ impl TextEditor {
.x .x
}); });
// Add padding based on line_side setting
let line_number_width = if self.line_side { let line_number_width = if self.line_side {
base_line_number_width + 20.0 base_line_number_width + 20.0
} else { } else {
@ -108,10 +125,8 @@ impl TextEditor {
return self.calculate_editor_dimensions(ui).text_width; 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 + self.font_size;
let longest_line_width = processing_result.longest_line_pixel_width + 20.0;
// Return the larger of the calculated width or minimum available width
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)
} }

View File

@ -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 !word_wrap {
if let Some(cursor_pos) = current_cursor_pos { if let Some(cursor_pos) = current_cursor_pos {
let cursor_moved = Some(cursor_pos) != app.previous_cursor_position; let cursor_moved = Some(cursor_pos) != app.previous_cursor_position;

View File

@ -172,7 +172,7 @@ pub(crate) fn menu_bar(app: &mut TextEditor, ctx: &egui::Context) {
ui.menu_button("View", |ui| { ui.menu_button("View", |ui| {
app.menu_interaction_active = true; app.menu_interaction_active = true;
if ui if ui
.checkbox(&mut app.show_line_numbers, "Toggle Line Numbers") .checkbox(&mut app.show_line_numbers, "Show Line Numbers")
.clicked() .clicked()
{ {
app.save_config(); app.save_config();
@ -272,7 +272,7 @@ pub(crate) fn menu_bar(app: &mut TextEditor, ctx: &egui::Context) {
ui.close_menu(); ui.close_menu();
} }
}); });
if app.auto_hide_tab_bar { if app.auto_hide_tab_bar {
let tab_title = if let Some(tab) = app.get_active_tab() { let tab_title = if let Some(tab) = app.get_active_tab() {
tab.title.clone() 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); let empty_tab = crate::app::tab::Tab::new_empty(1);
empty_tab.title.clone() empty_tab.title.clone()
}; };
let window_width = ctx.screen_rect().width(); let window_width = ctx.screen_rect().width();
let text_galley = ui.fonts(|fonts| { let text_galley = ui.fonts(|fonts| {
fonts.layout_job(egui::text::LayoutJob::simple_singleline( fonts.layout_job(egui::text::LayoutJob::simple_singleline(
tab_title.clone(), tab_title,
app.get_font_id(), ui.style().text_styles[&egui::TextStyle::Body].clone(),
ui.style().visuals.text_color(), ui.style().visuals.text_color(),
)) ))
}); });
let text_width = text_galley.size().x; let text_width = text_galley.size().x;
let text_height = text_galley.size().y; let text_height = text_galley.size().y;
let window_center_x = window_width / 2.0; let window_center_x = window_width / 2.0;
let text_x = (window_center_x - text_width / 2.0).max(0.0); let text_x = (window_center_x - text_width / 2.0).max(0.0);
let cursor_pos = ui.cursor().left_top(); let cursor_pos = ui.cursor().left_top();
ui.painter().galley( ui.painter().galley(
egui::pos2(text_x, cursor_pos.y), egui::pos2(text_x, cursor_pos.y),
text_galley, text_galley,
ui.style().visuals.text_color(), ui.style().visuals.text_color(),
); );
ui.allocate_exact_size(egui::vec2(0.0, text_height), egui::Sense::hover()); ui.allocate_exact_size(egui::vec2(0.0, text_height), egui::Sense::hover());
} }
}); });

View File

@ -58,7 +58,7 @@ pub(crate) fn preferences_window(app: &mut TextEditor, ctx: &egui::Context) {
}); });
if changed { 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); let clamped_size = new_size.clamp(8.0, 32.0);
if (app.font_size - clamped_size).abs() > 0.1 { if (app.font_size - clamped_size).abs() > 0.1 {
app.font_size = clamped_size; app.font_size = clamped_size;
app.apply_font_settings(ctx); app.apply_font_settings_with_ui(ctx, ui);
} }
} }
app.font_size_input = None; app.font_size_input = None;