Skip to content

Commit cd41efc

Browse files
committed
feat: Icon shape pref move to settings module
Signed-off-by: Hu Shenghao <dede.hu@qq.com>
1 parent 84fe775 commit cd41efc

File tree

6 files changed

+107
-103
lines changed

6 files changed

+107
-103
lines changed

app/src/main/java/com/dede/android_eggs/views/main/compose/EasterEggLogo.kt

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import androidx.compose.foundation.layout.size
77
import androidx.compose.foundation.lazy.grid.GridCells
88
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
99
import androidx.compose.foundation.lazy.grid.items
10-
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
1110
import androidx.compose.runtime.Composable
1211
import androidx.compose.runtime.DisposableEffect
1312
import androidx.compose.runtime.getValue
@@ -31,8 +30,7 @@ import com.dede.android_eggs.util.Receiver
3130
import com.dede.android_eggs.views.main.util.EasterEggHelp
3231
import com.dede.android_eggs.views.main.util.EasterEggLogoSensorMatrixConvert
3332
import com.dede.android_eggs.views.settings.compose.prefs.IconShapePrefUtil
34-
import com.dede.android_eggs.views.settings.compose.prefs.getIconShapeRoundedPolygon
35-
import com.dede.android_eggs.views.settings.compose.prefs.toShapePlus
33+
import com.dede.android_eggs.views.settings.compose.prefs.IconShapePrefUtil.toShapePlus
3634
import com.dede.basic.isAdaptiveIconDrawable
3735
import com.dede.basic.provider.EasterEgg
3836

@@ -48,7 +46,6 @@ fun PreviewEasterEggLogo() {
4846
}
4947
}
5048

51-
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
5249
@Composable
5350
fun EasterEggLogo(
5451
modifier: Modifier = Modifier,
@@ -60,10 +57,10 @@ fun EasterEggLogo(
6057
val isAdaptiveIcon = remember(res) { context.isAdaptiveIconDrawable(res) }
6158
if (isAdaptiveIcon) {
6259
var clipRoundedPolygon by remember {
63-
mutableStateOf(getIconShapeRoundedPolygon(context))
60+
mutableStateOf(IconShapePrefUtil.getIconShapeRoundedPolygon(context))
6461
}
6562
LocalEvent.Receiver(IconShapePrefUtil.ACTION_CHANGED) {
66-
clipRoundedPolygon = getIconShapeRoundedPolygon(context)
63+
clipRoundedPolygon = IconShapePrefUtil.getIconShapeRoundedPolygon(context)
6764
}
6865
var foregroundMatrix by remember { mutableStateOf(Matrix()) }
6966
var size by remember { mutableStateOf(IntSize.Zero) }
@@ -74,16 +71,16 @@ fun EasterEggLogo(
7471
val floats = FloatArray(9)
7572
val bounds = Rect(0, 0, size.width, size.height)
7673
val listener = object : EasterEggLogoSensorMatrixConvert.Listener(bounds) {
77-
override fun onUpdateMatrix(matrix: android.graphics.Matrix) {
78-
matrix.getValues(floats)
79-
foregroundMatrix = Matrix().apply {
80-
resetToPivotedTransform(
81-
translationX = floats[android.graphics.Matrix.MTRANS_X],
82-
translationY = floats[android.graphics.Matrix.MTRANS_Y]
83-
)
84-
}
74+
override fun onUpdateMatrix(matrix: android.graphics.Matrix) {
75+
matrix.getValues(floats)
76+
foregroundMatrix = Matrix().apply {
77+
resetToPivotedTransform(
78+
translationX = floats[android.graphics.Matrix.MTRANS_X],
79+
translationY = floats[android.graphics.Matrix.MTRANS_Y]
80+
)
8581
}
8682
}
83+
}
8784
sensorGroup.register(listener)
8885

8986
onDispose {

app/src/main/java/com/dede/android_eggs/views/settings/compose/prefs/IconShapePref.kt

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
package com.dede.android_eggs.views.settings.compose.prefs
44

5-
import android.content.Context
65
import androidx.compose.foundation.Image
76
import androidx.compose.foundation.background
87
import androidx.compose.foundation.layout.Box
@@ -27,25 +26,20 @@ import androidx.compose.ui.draw.clip
2726
import androidx.compose.ui.graphics.Shape
2827
import androidx.compose.ui.layout.ContentScale
2928
import androidx.compose.ui.layout.Layout
30-
import androidx.compose.ui.platform.LocalContext
3129
import androidx.compose.ui.res.painterResource
3230
import androidx.compose.ui.res.stringResource
3331
import androidx.compose.ui.tooling.preview.Preview
3432
import androidx.compose.ui.unit.Constraints
3533
import androidx.compose.ui.unit.dp
3634
import androidx.core.os.bundleOf
37-
import androidx.graphics.shapes.CornerRounding
38-
import androidx.graphics.shapes.RoundedPolygon
39-
import androidx.graphics.shapes.rectangle
40-
import androidx.graphics.shapes.star
4135
import com.dede.android_eggs.R
42-
import com.dede.android_eggs.alterable_adaptive_icon.PathShape
4336
import com.dede.android_eggs.ui.composes.icons.rounded.Shapes
4437
import com.dede.android_eggs.util.LocalEvent
4538
import com.dede.android_eggs.views.settings.compose.basic.ExpandOptionsPref
4639
import com.dede.android_eggs.views.settings.compose.basic.SettingPrefUtil
4740
import com.dede.android_eggs.views.settings.compose.basic.rememberPrefIntState
48-
import sv.lib.squircleshape.SquircleShape
41+
import com.dede.android_eggs.views.settings.compose.prefs.IconShapePrefUtil.polygonItems
42+
import com.dede.android_eggs.views.settings.compose.prefs.IconShapePrefUtil.toShapePlusNullable
4943
import com.dede.android_eggs.resources.R as StringsR
5044

5145
private const val SPAN_COUNT = 5
@@ -80,77 +74,6 @@ fun IconShapePref() {
8074
}
8175
}
8276

83-
fun getIconShapeRoundedPolygon(context: Context): RoundedPolygon? {
84-
val index = IconShapePrefUtil.getIconShapeIndexOf(context)
85-
return polygonItems.getOrNull(index)
86-
}
87-
88-
@Composable
89-
fun getIconShapePref(): Shape {
90-
val roundedPolygon = getIconShapeRoundedPolygon(LocalContext.current)
91-
return roundedPolygon.toShapePlus()
92-
}
93-
94-
@Composable
95-
fun RoundedPolygon?.toShapePlus(): Shape {
96-
val shape = this.toShapePlusNullable()
97-
if (shape == null) {
98-
val path = IconShapePrefUtil.getSystemIconMaskPath(LocalContext.current)
99-
if (path != null) {
100-
return PathShape(path)
101-
}
102-
return defaultSquare.toShape()
103-
}
104-
return shape
105-
}
106-
107-
@Composable
108-
private fun RoundedPolygon?.toShapePlusNullable(): Shape? {
109-
if (this == null) return null
110-
if (this == _fakeSquircle) return SquircleShape()
111-
return this.toShape()
112-
}
113-
114-
private val defaultSquare = MaterialShapes.Square
115-
116-
@Suppress("ObjectPropertyName")
117-
private val _fakeSquircle = RoundedPolygon.rectangle()
118-
119-
private val polygonItems: Array<RoundedPolygon?> = arrayOf(
120-
null,
121-
defaultSquare,
122-
// Squircle
123-
_fakeSquircle,
124-
MaterialShapes.Circle,
125-
// CornerSE
126-
RoundedPolygon(
127-
vertices = floatArrayOf(1f, 1f, -1f, 1f, -1f, -1f, 1f, -1f),
128-
perVertexRounding = listOf(
129-
CornerRounding(0.4f),
130-
CornerRounding(1f),
131-
CornerRounding(1f),
132-
CornerRounding(1f),
133-
),
134-
).normalized(),
135-
136-
MaterialShapes.Cookie4Sided,
137-
// Scallop
138-
RoundedPolygon.star(
139-
numVerticesPerRadius = 13,
140-
innerRadius = .9f,
141-
rounding = CornerRounding(.2f),
142-
innerRounding = CornerRounding(.3f)
143-
).normalized(),
144-
MaterialShapes.Clover8Leaf,
145-
MaterialShapes.Pill,
146-
RoundedPolygon.star(
147-
numVerticesPerRadius = 10,
148-
innerRadius = .6f,
149-
rounding = CornerRounding(.3f),
150-
innerRounding = CornerRounding(.3f)
151-
).normalized(),
152-
)
153-
15477
@Composable
15578
private fun IconShapeGridLayout(spanCount: Int = SPAN_COUNT, content: @Composable () -> Unit) {
15679
Layout(

app/src/main/java/com/dede/android_eggs/views/timeline/TimelineListDialog.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import androidx.lifecycle.ViewModel
5454
import com.dede.android_eggs.R
5555
import com.dede.android_eggs.views.main.compose.EasterEggLogo
5656
import com.dede.android_eggs.views.main.util.AndroidLogoMatcher
57-
import com.dede.android_eggs.views.settings.compose.prefs.getIconShapePref
57+
import com.dede.android_eggs.views.settings.compose.prefs.IconShapePrefUtil
5858
import com.dede.android_eggs.views.timeline.TimelineEventHelp.eventAnnotatedString
5959
import com.dede.android_eggs.views.timeline.TimelineEventHelp.isNewGroup
6060
import com.dede.android_eggs.views.timeline.TimelineEventHelp.localMonth
@@ -220,7 +220,7 @@ private fun TimelineItem(
220220
val isAdaptiveIcon = context.isAdaptiveIconDrawable(logoRes)
221221
if (!isAdaptiveIcon) {
222222
imageModifier = Modifier
223-
.background(colorScheme.secondaryContainer, getIconShapePref())
223+
.background(colorScheme.secondaryContainer, IconShapePrefUtil.getIconShapePref())
224224
.then(imageModifier)
225225
.padding(6.dp)
226226
}

core/settings/build.gradle.kts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id("easter.eggs.library")
2+
id("easter.eggs.compose.library")
33
}
44

55
android {
@@ -9,4 +9,13 @@ android {
99
dependencies {
1010
implementation(libs.androidx.core)
1111
implementation(libs.androidx.appcompat)
12+
13+
implementation(libs.androidx.compose.runtime)
14+
implementation(libs.androidx.compose.ui)
15+
implementation(libs.google.accompanist.drawablepainter)
16+
implementation(libs.androidx.compose.ui.tooling.preview)
17+
implementation(libs.androidx.compose.material3)
18+
debugImplementation(libs.androidx.compose.ui.tooling)
19+
20+
implementation(libs.squircle.shape)
1221
}

core/settings/src/main/java/com/dede/android_eggs/views/settings/compose/prefs/IconShapePrefUtil.kt

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1+
@file:OptIn(ExperimentalMaterial3ExpressiveApi::class)
2+
13
package com.dede.android_eggs.views.settings.compose.prefs
24

35
import android.annotation.SuppressLint
46
import android.content.Context
57
import android.content.res.Resources
6-
import android.graphics.Path
78
import android.os.Build
89
import android.text.TextUtils
910
import androidx.annotation.RequiresApi
10-
import com.dede.android_eggs.util.PathInflater
11+
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
12+
import androidx.compose.material3.MaterialShapes
13+
import androidx.compose.material3.toShape
14+
import androidx.compose.runtime.Composable
15+
import androidx.compose.ui.graphics.Shape
16+
import androidx.compose.ui.platform.LocalContext
17+
import androidx.graphics.shapes.CornerRounding
18+
import androidx.graphics.shapes.RoundedPolygon
19+
import androidx.graphics.shapes.rectangle
20+
import androidx.graphics.shapes.star
1121
import com.dede.android_eggs.views.settings.compose.basic.SettingPrefUtil
22+
import com.dede.android_eggs.views.settings.compose.utils.PathShape
23+
import sv.lib.squircleshape.SquircleShape
1224

1325
object IconShapePrefUtil {
1426

1527
const val KEY_ICON_SHAPE = "pref_key_override_icon_shape"
1628

1729
const val ACTION_CHANGED = "com.dede.easter_eggs.IconShapeChanged"
1830

19-
fun getSystemIconMaskPath(context: Context): Path? {
31+
private fun getSystemIconMaskShape(context: Context): Shape? {
2032
var pathStr: String? = null
2133
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
2234
val resId = getConfigResId(context.resources)
@@ -30,7 +42,7 @@ object IconShapePrefUtil {
3042
if (pathStr == null || TextUtils.isEmpty(pathStr)) {
3143
return null
3244
}
33-
return PathInflater.inflate(pathStr)
45+
return PathShape(pathStr)
3446
}
3547

3648
@SuppressLint("DiscouragedApi")
@@ -39,8 +51,71 @@ object IconShapePrefUtil {
3951
return resources.getIdentifier("config_icon_mask", "string", "android")
4052
}
4153

42-
fun getIconShapeIndexOf(context: Context): Int {
43-
return SettingPrefUtil.getValue(context, KEY_ICON_SHAPE, 0)
54+
fun getIconShapeRoundedPolygon(context: Context): RoundedPolygon? {
55+
val index = SettingPrefUtil.getValue(context, KEY_ICON_SHAPE, 0)
56+
return polygonItems.getOrNull(index)
57+
}
58+
59+
@Composable
60+
fun getIconShapePref(): Shape {
61+
val roundedPolygon = getIconShapeRoundedPolygon(LocalContext.current)
62+
return roundedPolygon.toShapePlus()
4463
}
4564

65+
@Composable
66+
fun RoundedPolygon?.toShapePlus(): Shape {
67+
val shape = this.toShapePlusNullable()
68+
if (shape != null) {
69+
return shape
70+
}
71+
return getSystemIconMaskShape(LocalContext.current) ?: defaultSquare.toShape()
72+
}
73+
74+
75+
@Composable
76+
fun RoundedPolygon?.toShapePlusNullable(): Shape? {
77+
if (this == null) return null
78+
if (this == fakeSquircle) return SquircleShape()
79+
return this.toShape()
80+
}
81+
82+
private val defaultSquare = MaterialShapes.Square
83+
84+
private val fakeSquircle = RoundedPolygon.rectangle()
85+
86+
val polygonItems: Array<RoundedPolygon?> = arrayOf(
87+
null,
88+
defaultSquare,
89+
// Squircle
90+
fakeSquircle,
91+
MaterialShapes.Circle,
92+
// CornerSE
93+
RoundedPolygon(
94+
vertices = floatArrayOf(1f, 1f, -1f, 1f, -1f, -1f, 1f, -1f),
95+
perVertexRounding = listOf(
96+
CornerRounding(0.4f),
97+
CornerRounding(1f),
98+
CornerRounding(1f),
99+
CornerRounding(1f),
100+
),
101+
).normalized(),
102+
103+
MaterialShapes.Cookie4Sided,
104+
// Scallop
105+
RoundedPolygon.star(
106+
numVerticesPerRadius = 13,
107+
innerRadius = .9f,
108+
rounding = CornerRounding(.2f),
109+
innerRounding = CornerRounding(.3f)
110+
).normalized(),
111+
MaterialShapes.Clover8Leaf,
112+
MaterialShapes.Pill,
113+
RoundedPolygon.star(
114+
numVerticesPerRadius = 10,
115+
innerRadius = .6f,
116+
rounding = CornerRounding(.3f),
117+
innerRounding = CornerRounding(.3f)
118+
).normalized(),
119+
)
120+
46121
}

core/alterable-adaptive-icon/src/main/java/com/dede/android_eggs/alterable_adaptive_icon/PathShape.kt renamed to core/settings/src/main/java/com/dede/android_eggs/views/settings/compose/utils/PathShape.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dede.android_eggs.alterable_adaptive_icon
1+
package com.dede.android_eggs.views.settings.compose.utils
22

33
import androidx.compose.ui.geometry.Size
44
import androidx.compose.ui.graphics.Matrix

0 commit comments

Comments
 (0)