1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Provides a rectangular highlight component for visually indicating selected
//! regions, typically in text editors or similar UI elements. This module
//! enables rendering of sharp-cornered, shadowless rectangles with configurable
//! size and color, suitable for marking text selections or other highlighted
//! areas. For multi-line or complex selections, multiple highlight rectangles
//! can be composed to cover the desired region.
use tessera_ui::{
Color, ComputedData, LayoutInput, LayoutOutput, LayoutSpec, MeasurementError, Px, RenderInput,
tessera,
};
use crate::pipelines::shape::command::ShapeCommand;
/// Draws a rectangular highlight, typically used to indicate selected text
/// regions in a text editor.
///
/// This component renders a single contiguous rectangle with sharp corners and
/// no shadow, suitable for visually marking selected areas. To highlight
/// selections spanning multiple lines or with complex shapes, use multiple
/// `selection_highlight_rect` components, each representing a segment of the
/// selection.
///
/// # Parameters
///
/// - `width`: The width of the highlight rectangle, in physical pixels (`Px`).
/// - `height`: The height of the highlight rectangle, in physical pixels
/// (`Px`).
/// - `color`: The fill color of the rectangle, including alpha for transparency
/// (`Color`).
#[tessera]
pub fn selection_highlight_rect(
width: Px,
height: Px,
color: Color, // RGBA color with alpha for transparency
) {
layout(SelectionHighlightLayout {
width,
height,
color,
});
}
#[derive(Clone, PartialEq)]
struct SelectionHighlightLayout {
width: Px,
height: Px,
color: Color,
}
impl LayoutSpec for SelectionHighlightLayout {
fn measure(
&self,
_input: &LayoutInput<'_>,
_output: &mut LayoutOutput<'_>,
) -> Result<ComputedData, MeasurementError> {
Ok(ComputedData {
width: self.width,
height: self.height,
})
}
fn record(&self, input: &RenderInput<'_>) {
let drawable = ShapeCommand::Rect {
color: self.color,
corner_radii: glam::Vec4::ZERO.into(),
corner_g2: [3.0; 4],
shadow: None,
};
input.metadata_mut().push_draw_command(drawable);
}
}