1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.foundation.text.selection
18 
19 import androidx.compose.runtime.Immutable
20 import androidx.compose.runtime.Stable
21 import androidx.compose.runtime.compositionLocalOf
22 import androidx.compose.ui.graphics.Color
23 
24 /**
25  * Represents the colors used for text selection by text and text field components.
26  *
27  * See [LocalTextSelectionColors] to provide new values for this throughout the hierarchy.
28  *
29  * @property handleColor the color used for the selection handles on either side of the selection
30  *   region.
31  * @property backgroundColor the color used to draw the background behind the selected region. This
32  *   color should have alpha applied to keep the text legible - this alpha is typically 0.4f (40%)
33  *   but this may need to be reduced in order to meet contrast requirements depending on the color
34  *   used for text, selection background, and the background behind the selection background.
35  */
36 @Immutable
37 class TextSelectionColors(val handleColor: Color, val backgroundColor: Color) {
equalsnull38     override fun equals(other: Any?): Boolean {
39         if (this === other) return true
40         if (other !is TextSelectionColors) return false
41 
42         if (handleColor != other.handleColor) return false
43         if (backgroundColor != other.backgroundColor) return false
44 
45         return true
46     }
47 
hashCodenull48     override fun hashCode(): Int {
49         var result = handleColor.hashCode()
50         result = 31 * result + backgroundColor.hashCode()
51         return result
52     }
53 
toStringnull54     override fun toString(): String {
55         return "SelectionColors(selectionHandleColor=$handleColor, " +
56             "selectionBackgroundColor=$backgroundColor)"
57     }
58 }
59 
60 /**
61  * CompositionLocal used to change the [TextSelectionColors] used by text and text field components
62  * in the hierarchy.
63  */
<lambda>null64 val LocalTextSelectionColors = compositionLocalOf { DefaultTextSelectionColors }
65 
66 /** Default color used is the blue from the Compose logo, b/172679845 for context */
67 private val DefaultSelectionColor = Color(0xFF4286F4)
68 
69 @Stable
70 private val DefaultTextSelectionColors =
71     TextSelectionColors(
72         handleColor = DefaultSelectionColor,
73         backgroundColor = DefaultSelectionColor.copy(alpha = 0.4f)
74     )
75