1 /*
2 * Copyright (C) 2023 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 com.android.permissioncontroller.wear.permission.components.material2
18
19 import android.graphics.drawable.Drawable
20 import androidx.annotation.DrawableRes
21 import androidx.compose.runtime.Composable
22 import androidx.compose.ui.Modifier
23 import androidx.compose.ui.draw.scale
24 import androidx.compose.ui.graphics.Color
25 import androidx.compose.ui.graphics.vector.ImageVector
26 import androidx.compose.ui.platform.LocalLayoutDirection
27 import androidx.compose.ui.res.painterResource
28 import androidx.compose.ui.unit.LayoutDirection
29 import androidx.wear.compose.material.Icon
30 import androidx.wear.compose.material.LocalContentAlpha
31 import androidx.wear.compose.material.LocalContentColor
32 import com.android.permissioncontroller.wear.permission.components.rememberDrawablePainter
33
34 /**
35 * This component is an alternative to [Icon], providing the following:
36 * - a convenient way of setting the icon to be mirrored in RTL mode;
37 */
38 @Composable
Iconnull39 fun Icon(
40 imageVector: ImageVector,
41 contentDescription: String?,
42 modifier: Modifier = Modifier,
43 tint: Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current),
44 rtlMode: IconRtlMode = IconRtlMode.Default,
45 ) {
46 val shouldMirror =
47 rtlMode == IconRtlMode.Mirrored && LocalLayoutDirection.current == LayoutDirection.Rtl
48 Icon(
49 modifier = modifier.scale(scaleX = if (shouldMirror) -1f else 1f, scaleY = 1f),
50 imageVector = imageVector,
51 contentDescription = contentDescription,
52 tint = tint,
53 )
54 }
55
56 /**
57 * This component is an alternative to [Icon], providing the following:
58 * - a convenient way of setting the icon to be mirrored in RTL mode;
59 */
60 @Composable
Iconnull61 fun Icon(
62 @DrawableRes id: Int,
63 contentDescription: String?,
64 modifier: Modifier = Modifier,
65 tint: Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current),
66 rtlMode: IconRtlMode = IconRtlMode.Default,
67 ) {
68 val shouldMirror =
69 rtlMode == IconRtlMode.Mirrored && LocalLayoutDirection.current == LayoutDirection.Rtl
70
71 Icon(
72 painter = painterResource(id = id),
73 contentDescription = contentDescription,
74 modifier = modifier.scale(scaleX = if (shouldMirror) -1f else 1f, scaleY = 1f),
75 tint = tint,
76 )
77 }
78
79 /**
80 * This component is an alternative to [Icon], providing the following:
81 * - a convenient way of providing an icon of various types
82 * - a convenient way of setting the icon to be mirrored in RTL mode;
83 */
84 @Composable
Iconnull85 fun Icon(
86 icon: Any,
87 contentDescription: String?,
88 modifier: Modifier = Modifier,
89 tint: Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current),
90 rtlMode: IconRtlMode = IconRtlMode.Default,
91 ) {
92 val shouldMirror =
93 rtlMode == IconRtlMode.Mirrored && LocalLayoutDirection.current == LayoutDirection.Rtl
94
95 val iconModifier = modifier.scale(scaleX = if (shouldMirror) -1f else 1f, scaleY = 1f)
96 when (icon) {
97 is ImageVector -> {
98 Icon(
99 imageVector = icon,
100 modifier = iconModifier,
101 contentDescription = contentDescription,
102 tint = tint,
103 )
104 }
105 is Int -> {
106 Icon(
107 painter = painterResource(id = icon),
108 contentDescription = contentDescription,
109 modifier = iconModifier,
110 tint = tint,
111 )
112 }
113 is Drawable -> {
114 Icon(
115 painter = rememberDrawablePainter(icon),
116 contentDescription = contentDescription,
117 modifier = iconModifier,
118 tint = tint,
119 )
120 }
121 else -> throw IllegalArgumentException("Type not supported.")
122 }
123 }
124
125 enum class IconRtlMode {
126 Default,
127 Mirrored,
128 }
129