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.permission.ui.wear.elements
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
33 /**
34 * This component is an alternative to [Icon], providing the following:
35 * - a convenient way of setting the icon to be mirrored in RTL mode;
36 */
37 @Composable
Iconnull38 public fun Icon(
39 imageVector: ImageVector,
40 contentDescription: String?,
41 modifier: Modifier = Modifier,
42 tint: Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current),
43 rtlMode: IconRtlMode = IconRtlMode.Default
44 ) {
45 val shouldMirror =
46 rtlMode == IconRtlMode.Mirrored && LocalLayoutDirection.current == LayoutDirection.Rtl
47 Icon(
48 modifier = modifier.scale(scaleX = if (shouldMirror) -1f else 1f, scaleY = 1f),
49 imageVector = imageVector,
50 contentDescription = contentDescription,
51 tint = tint
52 )
53 }
54
55 /**
56 * This component is an alternative to [Icon], providing the following:
57 * - a convenient way of setting the icon to be mirrored in RTL mode;
58 */
59 @Composable
Iconnull60 public fun Icon(
61 @DrawableRes id: Int,
62 contentDescription: String?,
63 modifier: Modifier = Modifier,
64 tint: Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current),
65 rtlMode: IconRtlMode = IconRtlMode.Default
66 ) {
67 val shouldMirror =
68 rtlMode == IconRtlMode.Mirrored && LocalLayoutDirection.current == LayoutDirection.Rtl
69
70 Icon(
71 painter = painterResource(id = id),
72 contentDescription = contentDescription,
73 modifier = modifier.scale(scaleX = if (shouldMirror) -1f else 1f, scaleY = 1f),
74 tint = tint
75 )
76 }
77
78 /**
79 * This component is an alternative to [Icon], providing the following:
80 * - a convenient way of providing an icon of various types
81 * - a convenient way of setting the icon to be mirrored in RTL mode;
82 */
83 @Composable
Iconnull84 fun Icon(
85 icon: Any,
86 contentDescription: String?,
87 modifier: Modifier = Modifier,
88 tint: Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current),
89 rtlMode: IconRtlMode = IconRtlMode.Default
90 ) {
91 val shouldMirror =
92 rtlMode == IconRtlMode.Mirrored && LocalLayoutDirection.current == LayoutDirection.Rtl
93
94 val iconModifier = modifier.scale(scaleX = if (shouldMirror) -1f else 1f, scaleY = 1f)
95 when (icon) {
96 is ImageVector -> {
97 Icon(
98 imageVector = icon,
99 modifier = iconModifier,
100 contentDescription = contentDescription,
101 tint = tint
102 )
103 }
104 is Int -> {
105 Icon(
106 painter = painterResource(id = icon),
107 contentDescription = contentDescription,
108 modifier = iconModifier,
109 tint = tint
110 )
111 }
112 is Drawable -> {
113 Icon(
114 painter = rememberDrawablePainter(icon),
115 contentDescription = contentDescription,
116 modifier = iconModifier,
117 tint = tint
118 )
119 }
120 else -> throw IllegalArgumentException("Type not supported.")
121 }
122 }
123
124 public enum class IconRtlMode {
125 Default,
126 Mirrored
127 }
128