Skip to main content

azul_css/props/style/
selection.rs

1//! CSS properties for styling text selections (`-azul-selection-*`).
2//!
3//! Defines the following properties (analogous to the `::selection` pseudo-element):
4//!
5//! - `-azul-selection-background-color` ([`SelectionBackgroundColor`])
6//! - `-azul-selection-color` ([`SelectionColor`])
7//! - `-azul-selection-radius` ([`SelectionRadius`])
8
9use alloc::string::String;
10
11use crate::props::{
12    basic::color::{parse_css_color, ColorU, CssColorParseError, CssColorParseErrorOwned},
13    formatter::PrintAsCssValue,
14};
15
16/// Default selection highlight background — light blue, similar to macOS.
17const DEFAULT_SELECTION_BG: ColorU = ColorU::new(173, 214, 255, 255);
18
19// --- -azul-selection-background-color ---
20
21/// Parsed value for the `-azul-selection-background-color` CSS property.
22#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23#[repr(C)]
24pub struct SelectionBackgroundColor {
25    pub inner: ColorU,
26}
27
28impl Default for SelectionBackgroundColor {
29    fn default() -> Self {
30        Self {
31            inner: DEFAULT_SELECTION_BG,
32        }
33    }
34}
35
36impl PrintAsCssValue for SelectionBackgroundColor {
37    fn print_as_css_value(&self) -> String {
38        self.inner.to_hash()
39    }
40}
41
42impl crate::codegen::format::FormatAsRustCode for SelectionBackgroundColor {
43    fn format_as_rust_code(&self, _tabs: usize) -> String {
44        format!(
45            "SelectionBackgroundColor {{ inner: {} }}",
46            crate::codegen::format::format_color_value(&self.inner)
47        )
48    }
49}
50
51/// Parses a `-azul-selection-background-color` CSS value.
52#[cfg(feature = "parser")]
53/// # Errors
54///
55/// Returns an error if `input` is not a valid CSS `selection-background-color` value.
56pub fn parse_selection_background_color(
57    input: &str,
58) -> Result<SelectionBackgroundColor, CssColorParseError<'_>> {
59    parse_css_color(input).map(|inner| SelectionBackgroundColor { inner })
60}
61
62// --- -azul-selection-color ---
63
64/// Parsed value for the `-azul-selection-color` CSS property.
65#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
66#[repr(C)]
67pub struct SelectionColor {
68    pub inner: ColorU,
69}
70
71impl Default for SelectionColor {
72    fn default() -> Self {
73        Self {
74            inner: ColorU::BLACK,
75        }
76    }
77}
78
79impl PrintAsCssValue for SelectionColor {
80    fn print_as_css_value(&self) -> String {
81        self.inner.to_hash()
82    }
83}
84
85impl crate::codegen::format::FormatAsRustCode for SelectionColor {
86    fn format_as_rust_code(&self, _tabs: usize) -> String {
87        format!(
88            "SelectionColor {{ inner: {} }}",
89            crate::codegen::format::format_color_value(&self.inner)
90        )
91    }
92}
93
94/// Parses a `-azul-selection-color` CSS value.
95#[cfg(feature = "parser")]
96/// # Errors
97///
98/// Returns an error if `input` is not a valid CSS `selection-color` value.
99pub fn parse_selection_color(input: &str) -> Result<SelectionColor, CssColorParseError<'_>> {
100    parse_css_color(input).map(|inner| SelectionColor { inner })
101}
102
103// --- -azul-selection-radius ---
104
105use crate::props::basic::{
106    pixel::{parse_pixel_value, CssPixelValueParseError, CssPixelValueParseErrorOwned, PixelValue},
107    SizeMetric,
108};
109
110/// Parsed value for the `-azul-selection-radius` CSS property.
111#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
112#[repr(C)]
113pub struct SelectionRadius {
114    pub inner: PixelValue,
115}
116
117impl Default for SelectionRadius {
118    fn default() -> Self {
119        Self {
120            inner: PixelValue::zero(),
121        }
122    }
123}
124
125impl PrintAsCssValue for SelectionRadius {
126    fn print_as_css_value(&self) -> String {
127        self.inner.to_string()
128    }
129}
130
131impl crate::codegen::format::FormatAsRustCode for SelectionRadius {
132    fn format_as_rust_code(&self, _tabs: usize) -> String {
133        // Use the Display implementation of PixelValue to get a string like "5px" or "1em"
134        format!(
135            "SelectionRadius {{ inner: PixelValue::from_metric(SizeMetric::{:?}, {}) }}",
136            self.inner.metric,
137            self.inner.number.get()
138        )
139    }
140}
141
142/// Parses a `-azul-selection-radius` CSS value.
143#[cfg(feature = "parser")]
144/// # Errors
145///
146/// Returns an error if `input` is not a valid CSS `selection-radius` value.
147pub fn parse_selection_radius(input: &str) -> Result<SelectionRadius, CssPixelValueParseError<'_>> {
148    parse_pixel_value(input).map(|inner| SelectionRadius { inner })
149}