This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIcon.kt
154 lines (147 loc) · 5.95 KB
/
Icon.kt
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.material3
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.material3.tokens.IconButtonTokens
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.paint
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.toolingGraphicsLayer
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics
/**
* Icon component that draws [imageVector] using [tint], defaulting to [LocalContentColor]. For a
* clickable icon, see [IconButton].
*
* To learn more about icons, see [Material Design icons](https://m3.material.io/styles/icons/overview)
*
* @param imageVector [ImageVector] to draw inside this icon
* @param contentDescription text used by accessibility services to describe what this icon
* represents. This should always be provided unless this icon is used for decorative purposes, and
* does not represent a meaningful action that a user can take. This text should be localized, such
* as by using [androidx.compose.ui.res.stringResource] or similar
* @param modifier the [Modifier] to be applied to this icon
* @param tint tint to be applied to [imageVector]. If [Color.Unspecified] is provided, then no tint
* is applied.
*/
@Composable
fun Icon(
imageVector: ImageVector,
contentDescription: String?,
modifier: Modifier = Modifier,
tint: Color = LocalContentColor.current
) {
Icon(
painter = rememberVectorPainter(imageVector),
contentDescription = contentDescription,
modifier = modifier,
tint = tint
)
}
/**
* Icon component that draws [bitmap] using [tint], defaulting to [LocalContentColor]. For a
* clickable icon, see [IconButton].
*
* To learn more about icons, see [Material Design icons](https://m3.material.io/styles/icons/overview)
*
* @param bitmap [ImageBitmap] to draw inside this icon
* @param contentDescription text used by accessibility services to describe what this icon
* represents. This should always be provided unless this icon is used for decorative purposes, and
* does not represent a meaningful action that a user can take. This text should be localized, such
* as by using [androidx.compose.ui.res.stringResource] or similar
* @param modifier the [Modifier] to be applied to this icon
* @param tint tint to be applied to [bitmap]. If [Color.Unspecified] is provided, then no tint is
* applied.
*/
@Composable
fun Icon(
bitmap: ImageBitmap,
contentDescription: String?,
modifier: Modifier = Modifier,
tint: Color = LocalContentColor.current
) {
val painter = remember(bitmap) { BitmapPainter(bitmap) }
Icon(
painter = painter,
contentDescription = contentDescription,
modifier = modifier,
tint = tint
)
}
/**
* Icon component that draws a [painter] using [tint], defaulting to [LocalContentColor]. For a
* clickable icon, see [IconButton].
*
* To learn more about icons, see [Material Design icons](https://m3.material.io/styles/icons/overview)
*
* @param painter [Painter] to draw inside this icon
* @param contentDescription text used by accessibility services to describe what this icon
* represents. This should always be provided unless this icon is used for decorative purposes, and
* does not represent a meaningful action that a user can take. This text should be localized, such
* as by using [androidx.compose.ui.res.stringResource] or similar
* @param modifier the [Modifier] to be applied to this icon
* @param tint tint to be applied to [painter]. If [Color.Unspecified] is provided, then no tint is
* applied.
*/
@Composable
fun Icon(
painter: Painter,
contentDescription: String?,
modifier: Modifier = Modifier,
tint: Color = LocalContentColor.current
) {
val colorFilter = if (tint == Color.Unspecified) null else ColorFilter.tint(tint)
val semantics =
if (contentDescription != null) {
Modifier.semantics {
this.contentDescription = contentDescription
this.role = Role.Image
}
} else {
Modifier
}
Box(
modifier
.toolingGraphicsLayer()
.defaultSizeFor(painter)
.paint(painter, colorFilter = colorFilter, contentScale = ContentScale.Fit)
.then(semantics)
)
}
private fun Modifier.defaultSizeFor(painter: Painter) =
this.then(
if (painter.intrinsicSize == Size.Unspecified || painter.intrinsicSize.isInfinite()) {
DefaultIconSizeModifier
} else {
Modifier
}
)
private fun Size.isInfinite() = width.isInfinite() && height.isInfinite()
// Default icon size, for icons with no intrinsic size information
private val DefaultIconSizeModifier = Modifier.size(IconButtonTokens.IconSize)