fix font zooming stuff
This commit is contained in:
parent
1ed06bbe37
commit
46bca1c6cb
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ced"
|
||||
version = "0.0.3"
|
||||
version = "0.0.4"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@ -46,6 +46,7 @@ impl TextEditor {
|
||||
previous_cursor_char_index: None,
|
||||
current_cursor_line: 0,
|
||||
previous_cursor_line: 0,
|
||||
font_settings_changed: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,4 +70,5 @@ pub struct TextEditor {
|
||||
pub(crate) previous_cursor_char_index: Option<usize>,
|
||||
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
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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();
|
||||
@ -285,8 +285,8 @@ pub(crate) fn menu_bar(app: &mut TextEditor, ctx: &egui::Context) {
|
||||
|
||||
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(),
|
||||
))
|
||||
});
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user