ced/src/app/state/ui.rs
2025-07-15 09:53:45 -04:00

134 lines
4.5 KiB
Rust

use super::editor::TextEditor;
use crate::app::theme;
use eframe::egui;
pub struct EditorDimensions {
pub text_width: f32,
pub line_number_width: f32,
pub total_reserved_width: f32,
}
impl TextEditor {
pub fn get_title(&self) -> String {
if let Some(tab) = self.get_active_tab() {
let modified_indicator = if tab.is_modified { "*" } else { "" };
format!(
"{}{} - {}",
tab.title,
modified_indicator,
env!("CARGO_PKG_NAME")
)
} else {
format!("{} - {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
}
}
/// Get the configured font ID based on the editor's font settings
pub fn get_font_id(&self) -> egui::FontId {
let font_family = match self.font_family.as_str() {
"Monospace" => egui::FontFamily::Monospace,
_ => egui::FontFamily::Proportional,
};
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,
_ => egui::FontFamily::Proportional,
};
let mut style = (*ctx.style()).clone();
style.text_styles.insert(
egui::TextStyle::Monospace,
egui::FontId::new(self.font_size, font_family),
);
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();
if !self.show_line_numbers {
return EditorDimensions {
text_width: total_available_width,
line_number_width: 0.0,
total_reserved_width: 0.0,
};
}
let processing_result = self.get_text_processing_result();
let line_count = processing_result.line_count;
let font_id = self.get_font_id();
let line_count_digits = line_count.to_string().len();
let sample_text = "9".repeat(line_count_digits);
let base_line_number_width = ui.fonts(|fonts| {
fonts
.layout(sample_text, font_id, egui::Color32::WHITE, f32::INFINITY)
.size()
.x
});
let line_number_width = if self.line_side {
base_line_number_width + 20.0
} else {
base_line_number_width + 8.0
};
// Separator space (7.0 for separator + 3.0 spacing = 10.0 total)
let separator_width = 10.0;
let total_reserved_width = line_number_width + separator_width;
let text_width = (total_available_width - total_reserved_width).max(100.0); // Minimum 100px for text
EditorDimensions {
text_width,
line_number_width,
total_reserved_width,
}
}
/// Calculate the available width for non-word-wrapped content based on processed text data
pub fn calculate_content_based_width(&self, ui: &egui::Ui) -> f32 {
let processing_result = self.get_text_processing_result();
if processing_result.longest_line_length == 0 {
return self.calculate_editor_dimensions(ui).text_width;
}
let longest_line_width = processing_result.longest_line_pixel_width + self.font_size;
let dimensions = self.calculate_editor_dimensions(ui);
longest_line_width.max(dimensions.text_width)
}
}