1 /*
<lambda>null2  * 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.glance.text
18 
19 import androidx.compose.runtime.Stable
20 
21 /** Defines a horizontal line to be drawn on the text. */
22 @JvmInline
23 value class TextDecoration internal constructor(private val mask: Int) {
24     companion object {
25         val None: TextDecoration = TextDecoration(0x0)
26 
27         /** Draws a horizontal line below the text. */
28         val Underline: TextDecoration = TextDecoration(0x1)
29 
30         /**
31          * Draws a horizontal line over the text.
32          *
33          * Note: This will have no effect if used on Wear Tiles.
34          */
35         val LineThrough: TextDecoration = TextDecoration(0x2)
36 
37         /**
38          * Creates a decoration that includes all the given decorations.
39          *
40          * @param decorations The decorations to be added
41          */
42         fun combine(decorations: List<TextDecoration>): TextDecoration {
43             val mask = decorations.fold(0) { acc, decoration -> acc or decoration.mask }
44             return TextDecoration(mask)
45         }
46     }
47 
48     /** Creates a decoration that includes both of the TextDecorations. */
49     @Stable
50     operator fun plus(decoration: TextDecoration): TextDecoration {
51         return TextDecoration(this.mask or decoration.mask)
52     }
53 
54     /** Check whether this [TextDecoration] contains the given decoration. */
55     @Stable
56     operator fun contains(other: TextDecoration): Boolean {
57         return (mask or other.mask) == mask
58     }
59 
60     override fun toString(): String {
61         if (mask == 0) {
62             return "TextDecoration.None"
63         }
64 
65         val values: MutableList<String> = mutableListOf()
66         if ((mask and Underline.mask) != 0) {
67             values.add("Underline")
68         }
69         if ((mask and LineThrough.mask) != 0) {
70             values.add("LineThrough")
71         }
72         if ((values.size == 1)) {
73             return "TextDecoration.${values[0]}"
74         }
75         return "TextDecoration[${values.joinToString(separator = ", ")}]"
76     }
77 }
78