-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
141 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/Icon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package top.yukonga.miuix.kmp.basic | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.size | ||
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 | ||
import androidx.compose.ui.unit.dp | ||
import top.yukonga.miuix.kmp.theme.MiuixTheme | ||
|
||
/** | ||
* A [Icon] component that draws [imageVector] using [tint], with a default value. | ||
* | ||
* @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 = MiuixTheme.colorScheme.onBackground | ||
) { | ||
Icon( | ||
painter = rememberVectorPainter(imageVector), | ||
contentDescription = contentDescription, | ||
modifier = modifier, | ||
tint = tint | ||
) | ||
} | ||
|
||
/** | ||
* A [Icon] component that draws [bitmap] using [tint], with a default value. | ||
* | ||
* @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 = MiuixTheme.colorScheme.onBackground | ||
) { | ||
val painter = remember(bitmap) { BitmapPainter(bitmap) } | ||
Icon( | ||
painter = painter, | ||
contentDescription = contentDescription, | ||
modifier = modifier, | ||
tint = tint | ||
) | ||
} | ||
|
||
/** | ||
* A [Icon] component that draws [painter] using [tint], with a default value. | ||
* | ||
* @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 = MiuixTheme.colorScheme.onBackground | ||
) { | ||
val colorFilter = | ||
remember(tint) { 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(24.dp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters