feat(text): add selected_text_color to the text style

This commit is contained in:
Hojjat 2026-09-11 11:29:04 -06:00 • committed by Michael Murphy
parent 2e12eba5cd
commit 9ef36c9604

View file

@ -379,45 +379,65 @@ where
let style = theme.style(&self.class); let style = theme.style(&self.class);
let bounds = layout.bounds(); let bounds = layout.bounds();
let paragraph = state.paragraph.raw(); let paragraph = state.paragraph.raw();
if let Some(sel) = &state.selection { let anchor = bounds.anchor(
let left = sel.anchor.min(sel.end); paragraph.min_bounds(),
let right = sel.anchor.max(sel.end); paragraph.align_x(),
let content: &str = self.fragment.as_ref(); paragraph.align_y(),
);
if left != right { let rects: Vec<Rectangle> = state
let lo_byte = grapheme_to_byte(content, left); .selection
let hi_byte = grapheme_to_byte(content, right); .as_ref()
.filter(|sel| sel.anchor != sel.end)
.map(|sel| {
let content: &str = self.fragment.as_ref();
let lo_byte =
grapheme_to_byte(content, sel.anchor.min(sel.end));
let hi_byte =
grapheme_to_byte(content, sel.anchor.max(sel.end));
let anchor = bounds.anchor( paragraph
paragraph.min_bounds(), .highlight(
paragraph.align_x(), 0,
paragraph.align_y(), (lo_byte, text::Affinity::After),
(hi_byte, text::Affinity::Before),
)
.into_iter()
.map(|r| Rectangle {
x: anchor.x + r.x,
y: anchor.y + r.y,
width: r.width,
height: r.height,
})
.collect()
})
.unwrap_or_default();
let fill_selection = |renderer: &mut Renderer| {
for r in &rects {
renderer.fill_quad(
renderer::Quad {
bounds: *r,
..renderer::Quad::default()
},
style.selected_fill,
); );
}
};
let rects = paragraph.highlight( if style.selected_text_color.is_none() {
0, fill_selection(renderer);
(lo_byte, text::Affinity::After), }
(hi_byte, text::Affinity::Before), draw(renderer, defaults, bounds, paragraph, style, viewport);
); if let Some(color) = style.selected_text_color {
fill_selection(renderer);
for r in rects { for r in &rects {
renderer.fill_quad( renderer.with_layer(*r, |renderer| {
renderer::Quad { renderer
bounds: Rectangle { .fill_paragraph(paragraph, anchor, color, *viewport);
x: anchor.x + r.x, });
y: anchor.y + r.y,
width: r.width,
height: r.height,
},
..renderer::Quad::default()
},
style.selected_fill,
);
}
} }
} }
draw(renderer, defaults, bounds, paragraph, style, viewport);
} }
fn update( fn update(
@ -931,6 +951,10 @@ pub struct Style {
pub color: Option<Color>, pub color: Option<Color>,
/// The fill [`Color`] of the selection highlight. /// The fill [`Color`] of the selection highlight.
pub selected_fill: Color, pub selected_fill: Color,
/// The [`Color`] of selected text.
///
/// The default, `None`, keeps the regular text color.
pub selected_text_color: Option<Color>,
} }
impl Default for Style { impl Default for Style {
@ -938,6 +962,7 @@ impl Default for Style {
Self { Self {
color: None, color: None,
selected_fill: DEFAULT_SELECTION_COLOR, selected_fill: DEFAULT_SELECTION_COLOR,
selected_text_color: None,
} }
} }
} }