-
Notifications
You must be signed in to change notification settings - Fork 18
/
EasyLauncherConfig.kt
261 lines (229 loc) · 8.58 KB
/
EasyLauncherConfig.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.project.starter.easylauncher.plugin
import com.project.starter.easylauncher.filter.ChromeLikeFilter
import com.project.starter.easylauncher.filter.ColorRibbonFilter
import com.project.starter.easylauncher.filter.EasyLauncherFilter
import com.project.starter.easylauncher.filter.OverlayFilter
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.Nested
import java.awt.Color
import java.io.File
import java.io.Serializable
import javax.inject.Inject
import kotlin.math.roundToInt
@Suppress("TooManyFunctions", "MagicNumber")
open class EasyLauncherConfig @Inject constructor(
val name: String,
objectFactory: ObjectFactory,
) : Serializable {
val enabled: Property<Boolean> = objectFactory.property(Boolean::class.java).apply {
set(true)
}
@Nested
internal val filters: SetProperty<EasyLauncherFilter> = objectFactory.setProperty(EasyLauncherFilter::class.java).apply {
set(emptyList())
}
val iconNames: ListProperty<String> = objectFactory.listProperty(String::class.java).apply {
set(emptyList())
}
fun enable(enabled: Boolean) {
this.enabled.value(enabled)
}
fun setFilters(filters: Iterable<EasyLauncherFilter>) {
this.filters.addAll(filters)
}
fun setFilters(filter: EasyLauncherFilter) {
this.filters.value(this.filters.get() + filter)
}
fun filters(vararg filters: EasyLauncherFilter) {
this.filters.value(this.filters.get() + filters)
}
fun setIconNames(names: Iterable<String>) {
iconNames.set(names)
}
@JvmOverloads
@Deprecated("use customRibbon method instead")
fun customColorRibbonFilter(
name: String? = null,
ribbonColor: String?,
labelColor: String = "#FFFFFF",
position: String = "topleft",
textSizeRatio: Float? = null,
): ColorRibbonFilter {
return customRibbon(
label = name,
ribbonColor = ribbonColor,
labelColor = labelColor,
gravity = ColorRibbonFilter.Gravity.valueOf(position.uppercase()),
textSizeRatio = textSizeRatio,
)
}
fun customRibbon(properties: Map<String, Any>): ColorRibbonFilter {
val label = properties["label"]?.toString()
val ribbonColor = properties["ribbonColor"]?.toString()
val labelColor = properties["labelColor"]?.toString()
val position = properties["position"]?.toString()
?.uppercase()
?.let(ColorRibbonFilter.Gravity::valueOf)
val textSizeRatio = properties["textSizeRatio"]?.toString()?.toFloatOrNull()
val drawingOptions = (properties["drawingOptions"] as? Iterable<*>).toDrawingOptions()
val fontName: String?
val font: File?
when (val fontRaw = properties["font"]) {
is File -> {
fontName = null
font = fontRaw
}
is String -> {
fontName = fontRaw
font = null
}
else -> {
fontName = properties["fontName"]?.toString()
font = null
}
}
return customRibbon(
label = label,
ribbonColor = ribbonColor,
labelColor = labelColor,
gravity = position,
textSizeRatio = textSizeRatio,
fontName = fontName,
font = font,
drawingOptions = drawingOptions,
)
}
fun customRibbon(
label: String? = null,
ribbonColor: String? = null,
labelColor: String? = null,
gravity: ColorRibbonFilter.Gravity? = null,
textSizeRatio: Float? = null,
fontName: String? = null,
font: File? = null,
drawingOptions: Set<ColorRibbonFilter.DrawingOption> = emptySet(),
) = ColorRibbonFilter(
label = label ?: name,
ribbonColor = ribbonColor?.toColor(),
labelColor = labelColor?.toColor(),
gravity = gravity,
textSizeRatio = textSizeRatio,
fontName = fontName,
fontResource = font,
drawingOptions = drawingOptions,
)
@JvmOverloads
fun grayRibbonFilter(label: String? = null) =
ColorRibbonFilter(label ?: this.name, Color(0x60, 0x60, 0x60, 0x99))
@JvmOverloads
fun greenRibbonFilter(label: String? = null) =
ColorRibbonFilter(label ?: this.name, Color(0, 0x72, 0, 0x99))
@JvmOverloads
fun orangeRibbonFilter(label: String? = null) =
ColorRibbonFilter(label ?: this.name, Color(0xff, 0x76, 0, 0x99))
@JvmOverloads
fun yellowRibbonFilter(label: String? = null) =
ColorRibbonFilter(label ?: this.name, Color(0xff, 251, 0, 0x99))
@JvmOverloads
fun redRibbonFilter(label: String? = null) =
ColorRibbonFilter(label ?: this.name, Color(0xff, 0, 0, 0x99))
@JvmOverloads
fun blueRibbonFilter(label: String? = null) =
ColorRibbonFilter(label ?: this.name, Color(0, 0, 255, 0x99))
fun overlayFilter(fgFile: File) =
OverlayFilter(fgFile)
@JvmOverloads
fun chromeLike(
label: String? = null,
ribbonColor: String? = null,
labelColor: String? = null,
fontName: String? = null,
font: File? = null,
labelPadding: Int? = null,
overlayHeight: Float? = null,
textSizeRatio: Float? = null,
gravity: ChromeLikeFilter.Gravity? = null,
) = ChromeLikeFilter(
label ?: this.name,
ribbonColor = ribbonColor?.toColor(),
labelColor = labelColor?.toColor(),
labelPadding = labelPadding,
gravity = gravity,
fontName = fontName,
fontResource = font,
overlayHeight = overlayHeight,
textSizeRatio = textSizeRatio,
)
fun chromeLike(properties: Map<String, Any>): ChromeLikeFilter {
val gravity = properties["gravity"]?.toString()
?.uppercase()
?.let { ChromeLikeFilter.Gravity.valueOf(it) }
val ribbonText = properties["label"]?.toString()
val background = properties["ribbonColor"]?.toString()
val labelColor = properties["labelColor"]?.toString()
val labelPadding = properties["labelPadding"]?.toString()?.toDoubleOrNull()?.roundToInt()
val overlayHeight = properties["overlayHeight"]?.toString()?.toFloatOrNull()
val textSizeRatio = properties["textSizeRatio"]?.toString()?.toFloatOrNull()
val fontName: String?
val font: File?
when (val fontRaw = properties["font"]) {
is File -> {
fontName = null
font = fontRaw
}
is String -> {
fontName = fontRaw
font = null
}
else -> {
fontName = properties["fontName"]?.toString()
font = null
}
}
return chromeLike(
gravity = gravity,
label = ribbonText,
ribbonColor = background,
labelColor = labelColor,
labelPadding = labelPadding,
fontName = fontName,
font = font,
overlayHeight = overlayHeight,
textSizeRatio = textSizeRatio,
)
}
private fun String.toColor(): Color {
val value = java.lang.Long.decode(this)
return when (length) {
"#AARRGGBB".length -> {
val alpha = (value shr 24 and 0xFF).toInt()
val red = (value shr 16 and 0xFF).toInt()
val green = (value shr 8 and 0xFF).toInt()
val blue = (value and 0xFF).toInt()
Color(red, green, blue, alpha)
}
"#RRGGBB".length -> {
val red = (value shr 16 and 0xFF).toInt()
val green = (value shr 8 and 0xFF).toInt()
val blue = (value and 0xFF).toInt()
Color(red, green, blue)
}
else -> Color.decode(this)
}
}
companion object {
private const val serialVersionUID = 1L
}
}
private fun Iterable<*>?.toDrawingOptions() =
this?.map { it.toString() }.orEmpty()
.map { rawOption ->
val option = ColorRibbonFilter.DrawingOption.values().firstOrNull { rawOption.matchesEnum(it) }
checkNotNull(option) { "Unknown option: $rawOption. Use one of ${ColorRibbonFilter.DrawingOption.values().map { it.name }}" }
}
.toSet()
private fun <T : Enum<T>> String.matchesEnum(option: T) =
replace("_", "").equals(option.name.replace("_", ""), ignoreCase = true)