cranpose_ui/text_selection_colors.rs
1//! The colors of selected text: Compose's `TextSelectionColors` and
2//! `LocalTextSelectionColors`.
3
4use cranpose_core::{CompositionLocal, compositionLocalOf};
5use cranpose_ui_graphics::Color;
6
7/// The colors that show selected text: the handles that drag the ends of a
8/// selection, and the highlight behind the selected text. Compose's
9/// `TextSelectionColors`.
10#[derive(Clone, Copy, Debug, PartialEq)]
11pub struct TextSelectionColors {
12 /// The color of the handles at the ends of a selection.
13 pub handle_color: Color,
14 /// The color drawn behind selected text.
15 pub background_color: Color,
16}
17
18// The default blue: handles in it solid, the highlight at about a third.
19pub(crate) const DEFAULT_TEXT_SELECTION_COLORS: TextSelectionColors = TextSelectionColors {
20 handle_color: Color(0.0, 0.478, 1.0, 1.0),
21 background_color: Color(0.0, 0.478, 1.0, 0.32),
22};
23
24impl Default for TextSelectionColors {
25 fn default() -> Self {
26 DEFAULT_TEXT_SELECTION_COLORS
27 }
28}
29
30/// The selection colors that the text fields and the text in a
31/// [`SelectionContainer`](crate::widgets::SelectionContainer) below show:
32/// Compose's `LocalTextSelectionColors`.
33///
34/// ```
35/// use cranpose_core::CompositionLocalProvider;
36/// use cranpose_ui::{widgets::SelectionContainer, *};
37///
38/// #[composable]
39/// fn GoldSelection() {
40/// let gold = TextSelectionColors {
41/// handle_color: Color(0.85, 0.65, 0.2, 1.0),
42/// background_color: Color(0.85, 0.65, 0.2, 0.5),
43/// };
44/// CompositionLocalProvider([local_text_selection_colors().provides(gold)], || {
45/// SelectionContainer(Modifier::empty(), || {
46/// Text(
47/// "Selected text shows gold.",
48/// Modifier::empty(),
49/// TextStyle::default(),
50/// );
51/// });
52/// });
53/// }
54/// ```
55pub fn local_text_selection_colors() -> CompositionLocal<TextSelectionColors> {
56 crate::environment_locals::cached_local(
57 |locals| &locals.text_selection_colors,
58 || compositionLocalOf(TextSelectionColors::default),
59 )
60}