tessera_ui_basic_components/selection_highlight_rect.rs
1//! Provides a rectangular highlight component for visually indicating selected regions,
2//! typically in text editors or similar UI elements. This module enables rendering of
3//! sharp-cornered, shadowless rectangles with configurable size and color, suitable for
4//! marking text selections or other highlighted areas. For multi-line or complex selections,
5//! multiple highlight rectangles can be composed to cover the desired region.
6use tessera_ui::{Color, ComputedData, Px};
7use tessera_ui_macros::tessera;
8
9use crate::pipelines::ShapeCommand;
10
11/// Draws a rectangular highlight, typically used to indicate selected text regions in a text editor.
12///
13/// This component renders a single contiguous rectangle with sharp corners and no shadow,
14/// suitable for visually marking selected areas. To highlight selections spanning multiple lines
15/// or with complex shapes, use multiple `selection_highlight_rect` components, each representing
16/// a segment of the selection.
17///
18/// # Parameters
19///
20/// - `width`: The width of the highlight rectangle, in physical pixels (`Px`).
21/// - `height`: The height of the highlight rectangle, in physical pixels (`Px`).
22/// - `color`: The fill color of the rectangle, including alpha for transparency (`Color`).
23///
24/// # Example
25///
26/// ```
27/// use tessera_ui::{Color, Px};
28/// use tessera_ui_basic_components::selection_highlight_rect::selection_highlight_rect;
29///
30/// // Renders a selection highlight rectangle with a width of 100px, a height of 20px,
31/// // and a semi-transparent blue color.
32/// selection_highlight_rect(
33/// Px(100),
34/// Px(20),
35/// Color::new(0.2, 0.4, 1.0, 0.3),
36/// );
37/// ```
38///
39#[tessera]
40pub fn selection_highlight_rect(
41 width: Px,
42 height: Px,
43 color: Color, // RGBA color with alpha for transparency
44) {
45 measure(Box::new(move |input| {
46 let drawable = ShapeCommand::Rect {
47 color,
48 corner_radius: 0.0, // Sharp corners for text selection
49 g2_k_value: 3.0, // g2-like corners
50 shadow: None, // No shadow for selection highlight
51 };
52
53 input.metadata_mut().push_draw_command(drawable);
54
55 // Return the specified size
56 Ok(ComputedData { width, height })
57 }));
58}