ailloli_ui_editor/paint/selection_painter.rs
1//! Selection geometry conversion into neutral paint items.
2
3use crate::input::selection::selection_rects_for_run;
4use crate::layout::EditorTextRun;
5use crate::paint::EditorPaintItem;
6use crate::EditorStyle;
7
8/// Converts a run-local selection into neutral selection paint items.
9///
10/// One item is emitted per intersected visual line. Empty/reversed selections
11/// emit none, and every item uses [`EditorStyle::selection_bg`].
12///
13/// # Examples
14///
15/// ```
16/// use ailloli_ui_core::{Color, FontId, TextStyle};
17/// use ailloli_ui_editor::{layout::EditorTextRun, paint::selection_painter::selection_items_for_run, EditorStyle};
18/// use ailloli_ui_text::{TextLayoutParams, TextSystem};
19/// let mut system = TextSystem::new();
20/// let layout = system.layout_cached(TextLayoutParams::new("abc", TextStyle::new(FontId::Mono, 13, Color::WHITE)));
21/// let run = EditorTextRun { index: 0, byte_range: 0..3, baseline_y: 12.0, layout };
22/// assert_eq!(selection_items_for_run(0.0, 0.0, &run, 0, 2, EditorStyle::default()).len(), 1);
23/// ```
24pub fn selection_items_for_run(
25 content_x: f32,
26 content_y: f32,
27 run: &EditorTextRun,
28 lo_local: usize,
29 hi_local: usize,
30 style: EditorStyle,
31) -> Vec<EditorPaintItem> {
32 selection_rects_for_run(content_x, content_y, run, lo_local, hi_local, style)
33 .into_iter()
34 .map(|rect| EditorPaintItem::Selection {
35 rect,
36 color: style.selection_bg,
37 })
38 .collect()
39}