Skip to main content

azul_css/props/style/
selection.rs

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