Refactor icons
This commit is contained in:
@@ -1,85 +1,14 @@
|
||||
package de.mm20.launcher2.icons
|
||||
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.graphics.drawable.Drawable
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
import androidx.palette.graphics.Palette
|
||||
import java.lang.ref.WeakReference
|
||||
import android.content.res.Resources
|
||||
|
||||
open class LauncherIcon(
|
||||
foreground: Drawable,
|
||||
background: Drawable? = null,
|
||||
foregroundScale: Float = 1f,
|
||||
backgroundScale: Float = 1f,
|
||||
var autoGenerateBackgroundMode: Int = BACKGROUND_WHITE,
|
||||
val isThemeable: Boolean = false,
|
||||
) {
|
||||
sealed interface LauncherIcon
|
||||
|
||||
var foreground = foreground
|
||||
set(value) {
|
||||
field = value
|
||||
updateBackgroundColor()
|
||||
notifyCallbacks()
|
||||
}
|
||||
data class StaticLauncherIcon(
|
||||
val foregroundLayer: LauncherIconLayer,
|
||||
val backgroundLayer: LauncherIconLayer,
|
||||
): LauncherIcon
|
||||
|
||||
private fun updateBackgroundColor() {
|
||||
if (background == null) {
|
||||
when (autoGenerateBackgroundMode) {
|
||||
BACKGROUND_DYNAMIC -> {
|
||||
val palette = Palette
|
||||
.from(foreground.toBitmap())
|
||||
.generate()
|
||||
this.background = ColorDrawable(palette.getDominantColor(Color.WHITE))
|
||||
badgeColor = palette.getLightVibrantColor(0xFFF0F0F0.toInt())
|
||||
}
|
||||
BACKGROUND_WHITE -> this.background = ColorDrawable(Color.WHITE)
|
||||
else -> this.foregroundScale = 1f
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var background = background
|
||||
set(value) {
|
||||
field = value
|
||||
notifyCallbacks()
|
||||
}
|
||||
|
||||
var foregroundScale = foregroundScale
|
||||
set(value) {
|
||||
field = value
|
||||
notifyCallbacks()
|
||||
}
|
||||
|
||||
var backgroundScale = backgroundScale
|
||||
set(value) {
|
||||
field = value
|
||||
notifyCallbacks()
|
||||
}
|
||||
|
||||
private val callbacks = mutableListOf<WeakReference<(LauncherIcon) -> Unit>>()
|
||||
|
||||
fun registerCallback(callback: (LauncherIcon) -> Unit) {
|
||||
callbacks.add(WeakReference(callback))
|
||||
}
|
||||
|
||||
protected fun notifyCallbacks() {
|
||||
val iterator = callbacks.iterator()
|
||||
while(iterator.hasNext()) {
|
||||
val callback = iterator.next()
|
||||
callback.get()?.invoke(this) ?: iterator.remove()
|
||||
}
|
||||
}
|
||||
|
||||
var badgeColor: Int = 0xFFF0F0F0.toInt()
|
||||
|
||||
init {
|
||||
updateBackgroundColor()
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val BACKGROUND_NONE = 1
|
||||
const val BACKGROUND_DYNAMIC = 0
|
||||
const val BACKGROUND_WHITE = 2
|
||||
}
|
||||
interface DynamicLauncherIcon: LauncherIcon {
|
||||
suspend fun getIcon(time: Long): StaticLauncherIcon
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package de.mm20.launcher2.icons
|
||||
|
||||
import android.graphics.drawable.Drawable
|
||||
|
||||
sealed interface LauncherIconLayer
|
||||
|
||||
data class StaticIconLayer(
|
||||
val icon: Drawable,
|
||||
val scale: Float = 1f,
|
||||
) : LauncherIconLayer
|
||||
|
||||
data class ColorLayer(
|
||||
val color: Int = 0,
|
||||
) : LauncherIconLayer
|
||||
|
||||
data class ClockLayer(
|
||||
val sublayers: List<ClockSublayer>,
|
||||
val scale: Float,
|
||||
) : LauncherIconLayer
|
||||
|
||||
data class ClockSublayer(
|
||||
val drawable: Drawable,
|
||||
val role: ClockSublayerRole
|
||||
)
|
||||
|
||||
enum class ClockSublayerRole {
|
||||
Hour,
|
||||
Minute,
|
||||
Second,
|
||||
Static,
|
||||
}
|
||||
|
||||
data class TintedIconLayer(
|
||||
val icon: Drawable,
|
||||
val scale: Float = 0.5f,
|
||||
val color: Int = 0
|
||||
) : LauncherIconLayer
|
||||
|
||||
data class TintedClockLayer(
|
||||
val sublayers: List<ClockSublayer>,
|
||||
val scale: Float,
|
||||
val color: Int = 0,
|
||||
) : LauncherIconLayer
|
||||
|
||||
data class TextLayer(
|
||||
val text: String,
|
||||
val color: Int = 0,
|
||||
) : LauncherIconLayer
|
||||
|
||||
object TransparentLayer: LauncherIconLayer
|
||||
Reference in New Issue
Block a user