ced/src/ui/central_panel/find_highlight.rs

81 lines
2.4 KiB
Rust
Raw Normal View History

2025-07-05 14:42:45 -04:00
use eframe::egui;
pub(super) fn _draw_find_highlight(
2025-07-05 14:42:45 -04:00
ui: &mut egui::Ui,
content: &str,
start_pos: usize,
end_pos: usize,
editor_rect: egui::Rect,
font_size: f32,
) {
let font_id = ui
.style()
.text_styles
.get(&egui::TextStyle::Monospace)
.unwrap_or(&egui::FontId::monospace(font_size))
.clone();
let text_up_to_start = &content[..start_pos.min(content.len())];
let start_line = text_up_to_start.chars().filter(|&c| c == '\n').count();
let line_start_byte_pos = text_up_to_start.rfind('\n').map(|pos| pos + 1).unwrap_or(0);
let line_start_char_pos = content[..line_start_byte_pos].chars().count();
let start_char_pos = content[..start_pos].chars().count();
let start_col = start_char_pos - line_start_char_pos;
let lines: Vec<&str> = content.lines().collect();
if start_line >= lines.len() {
return;
}
let line_text = lines[start_line];
let text_before_match: String = line_text.chars().take(start_col).collect();
let line_height = ui.fonts(|fonts| fonts.row_height(&font_id));
let horizontal_margin = ui.spacing().button_padding.x - 4.0;
let vertical_margin = ui.spacing().button_padding.y - 1.0;
let text_area_left = editor_rect.left() + horizontal_margin;
let text_area_top = editor_rect.top() + vertical_margin;
let text_before_width = ui.fonts(|fonts| {
fonts
.layout(
text_before_match,
font_id.clone(),
egui::Color32::WHITE,
f32::INFINITY,
)
.size()
.x
});
let start_y = text_area_top + (start_line as f32 * line_height);
let start_x = text_area_left + text_before_width;
{
let match_text = &content[start_pos..end_pos.min(content.len())];
let match_width = ui.fonts(|fonts| {
fonts
.layout(
match_text.to_string(),
font_id.clone(),
ui.visuals().text_color(),
f32::INFINITY,
)
.size()
.x
});
let highlight_rect = egui::Rect::from_min_size(
egui::pos2(start_x, start_y),
egui::vec2(match_width, line_height),
);
ui.painter()
.rect_filled(highlight_rect, 0.0, ui.visuals().selection.bg_fill);
2025-07-05 14:42:45 -04:00
}
}