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
//! The colors of selected text: Compose's `TextSelectionColors` and
//! `LocalTextSelectionColors`.
use cranpose_core::{CompositionLocal, compositionLocalOf};
use cranpose_ui_graphics::Color;
/// The colors that show selected text: the handles that drag the ends of a
/// selection, and the highlight behind the selected text. Compose's
/// `TextSelectionColors`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TextSelectionColors {
/// The color of the handles at the ends of a selection.
pub handle_color: Color,
/// The color drawn behind selected text.
pub background_color: Color,
}
// The default blue: handles in it solid, the highlight at about a third.
pub(crate) const DEFAULT_TEXT_SELECTION_COLORS: TextSelectionColors = TextSelectionColors {
handle_color: Color(0.0, 0.478, 1.0, 1.0),
background_color: Color(0.0, 0.478, 1.0, 0.32),
};
impl Default for TextSelectionColors {
fn default() -> Self {
DEFAULT_TEXT_SELECTION_COLORS
}
}
/// The selection colors that the text fields and the text in a
/// [`SelectionContainer`](crate::widgets::SelectionContainer) below show:
/// Compose's `LocalTextSelectionColors`.
///
/// ```
/// use cranpose_core::CompositionLocalProvider;
/// use cranpose_ui::{widgets::SelectionContainer, *};
///
/// #[composable]
/// fn GoldSelection() {
/// let gold = TextSelectionColors {
/// handle_color: Color(0.85, 0.65, 0.2, 1.0),
/// background_color: Color(0.85, 0.65, 0.2, 0.5),
/// };
/// CompositionLocalProvider([local_text_selection_colors().provides(gold)], || {
/// SelectionContainer(Modifier::empty(), || {
/// Text(
/// "Selected text shows gold.",
/// Modifier::empty(),
/// TextStyle::default(),
/// );
/// });
/// });
/// }
/// ```
pub fn local_text_selection_colors() -> CompositionLocal<TextSelectionColors> {
crate::environment_locals::cached_local(
|locals| &locals.text_selection_colors,
|| compositionLocalOf(TextSelectionColors::default),
)
}