-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7262 from thundernest/image_with_baseline
Add `ImageWithBaseline`
- Loading branch information
Showing
4 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...ose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/baseline/BaselineModifier.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,31 @@ | ||
package app.k9mail.core.ui.compose.common.baseline | ||
|
||
import androidx.compose.foundation.layout.RowScope | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.layout.FirstBaseline | ||
import androidx.compose.ui.layout.LastBaseline | ||
import androidx.compose.ui.layout.layout | ||
import androidx.compose.ui.unit.Dp | ||
|
||
/** | ||
* Adds a baseline to a Composable that typically doesn't have one. | ||
* | ||
* This can be used to align e.g. an icon to the baseline of some text next to it. See e.g. [RowScope.alignByBaseline]. | ||
* | ||
* @param baseline The number of device-independent pixels (dp) the baseline is from the top of the Composable. | ||
*/ | ||
fun Modifier.withBaseline(baseline: Dp) = layout { measurable, constraints -> | ||
val placeable = measurable.measure(constraints) | ||
val baselineInPx = baseline.roundToPx() | ||
|
||
layout( | ||
width = placeable.width, | ||
height = placeable.height, | ||
alignmentLines = mapOf( | ||
FirstBaseline to baselineInPx, | ||
LastBaseline to baselineInPx, | ||
), | ||
) { | ||
placeable.placeRelative(x = 0, y = 0) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...mpose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/image/ImageWithBaseline.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,11 @@ | ||
package app.k9mail.core.ui.compose.common.image | ||
|
||
import androidx.compose.runtime.Immutable | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import androidx.compose.ui.unit.Dp | ||
|
||
@Immutable | ||
data class ImageWithBaseline( | ||
val image: ImageVector, | ||
val baseline: Dp, | ||
) |
16 changes: 16 additions & 0 deletions
16
core/ui/compose/theme/src/main/java/app/k9mail/core/ui/compose/theme/IconsWithBaseline.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,16 @@ | ||
package app.k9mail.core.ui.compose.theme | ||
|
||
import androidx.compose.material.icons.filled.Warning | ||
import androidx.compose.ui.unit.dp | ||
import app.k9mail.core.ui.compose.common.image.ImageWithBaseline | ||
import androidx.compose.material.icons.Icons as MaterialIcons | ||
|
||
// We're using "by lazy" so not all icons are loaded into memory as soon as a nested object is accessed. But once a | ||
// property is accessed we want to retain the `ImageWithBaseline` instance. | ||
object IconsWithBaseline { | ||
object Filled { | ||
val warning: ImageWithBaseline by lazy { | ||
ImageWithBaseline(image = MaterialIcons.Filled.Warning, baseline = 21.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