ced/src/app/state/ui.rs

135 lines
4.6 KiB
Rust
Raw Normal View History

2025-07-05 14:42:45 -04:00
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 { "" };
2025-07-15 00:42:01 -04:00
format!(
"{}{} - {}",
tab.title,
modified_indicator,
env!("CARGO_PKG_NAME")
)
2025-07-05 14:42:45 -04:00
} else {
2025-07-15 00:42:01 -04:00
format!("{} - {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
2025-07-05 14:42:45 -04:00
}
}
/// Get the configured font ID based on the editor's font settings
2025-07-15 00:42:01 -04:00
pub fn get_font_id(&self) -> egui::FontId {
2025-07-05 14:42:45 -04:00
let font_family = match self.font_family.as_str() {
"Monospace" => egui::FontFamily::Monospace,
_ => egui::FontFamily::Proportional,
};
egui::FontId::new(self.font_size, font_family)
}
2025-07-15 09:53:45 -04:00
/// Immediately apply theme and save to configuration
2025-07-05 14:42:45 -04:00
pub fn set_theme(&mut self, ctx: &egui::Context) {
theme::apply(self.theme, ctx);
self.save_config();
}
2025-07-15 09:53:45 -04:00
/// Apply font settings with immediate text reprocessing
2025-07-05 14:42:45 -04:00
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);
2025-07-15 09:53:45 -04:00
self.font_settings_changed = true;
2025-07-05 14:42:45 -04:00
self.save_config();
}
2025-07-15 09:53:45 -04:00
/// 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);
}
}
}
2025-07-05 14:42:45 -04:00
/// 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();
2025-07-15 00:42:01 -04:00
2025-07-05 14:42:45 -04:00
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;
2025-07-15 00:42:01 -04:00
2025-07-05 14:42:45 -04:00
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 {
2025-07-15 00:42:01 -04:00
base_line_number_width + 20.0
2025-07-05 14:42:45 -04:00
} else {
2025-07-15 00:42:01 -04:00
base_line_number_width + 8.0
2025-07-05 14:42:45 -04:00
};
// Separator space (7.0 for separator + 3.0 spacing = 10.0 total)
let separator_width = 10.0;
2025-07-15 00:42:01 -04:00
2025-07-05 14:42:45 -04:00
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,
}
}
2025-07-15 00:42:01 -04:00
/// Calculate the available width for non-word-wrapped content based on processed text data
2025-07-05 14:42:45 -04:00
pub fn calculate_content_based_width(&self, ui: &egui::Ui) -> f32 {
2025-07-15 00:42:01 -04:00
let processing_result = self.get_text_processing_result();
2025-07-05 14:42:45 -04:00
2025-07-15 00:42:01 -04:00
if processing_result.longest_line_length == 0 {
return self.calculate_editor_dimensions(ui).text_width;
}
2025-07-05 14:42:45 -04:00
let longest_line_width =
processing_result.longest_line_pixel_width + (self.font_size * 2.0);
2025-07-05 14:42:45 -04:00
2025-07-15 00:42:01 -04:00
let dimensions = self.calculate_editor_dimensions(ui);
longest_line_width.max(dimensions.text_width)
2025-07-05 14:42:45 -04:00
}
}