forked from starifly/NekoBoxForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemePickerPreference.kt
More file actions
230 lines (204 loc) · 8.12 KB
/
Copy pathThemePickerPreference.kt
File metadata and controls
230 lines (204 loc) · 8.12 KB
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
package moe.matsuri.nb4a.ui
import android.content.Context
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.GridLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.content.res.ResourcesCompat
import androidx.core.content.res.TypedArrayUtils
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.view.setPadding
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.nekohasekai.sagernet.R
import io.nekohasekai.sagernet.ktx.getColorAttr
import io.nekohasekai.sagernet.utils.Theme
import kotlin.math.roundToInt
/**
* Two-tier theme picker.
*
* The preference opens a modern named list of full-fledged Material 3 themes
* (each row: preview swatch + name). A trailing "Classic colors…" row opens the
* legacy single-accent color grid (the original swatch-grid UX).
*
* Both paths persist the same int [Theme] id via [persistInt] so the rest of the
* app (Theme.getTheme / DataStore.appTheme) is unchanged.
*/
class ThemePickerPreference
@JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyle: Int = TypedArrayUtils.getAttr(
context,
androidx.preference.R.attr.editTextPreferenceStyle,
android.R.attr.editTextPreferenceStyle
)
) : Preference(
context, attrs, defStyle
) {
private var inited = false
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
val widgetFrame = holder.findViewById(android.R.id.widget_frame) as? LinearLayout
?: return
if (!inited) {
inited = true
widgetFrame.addView(
nekoImageView(context.getColorAttr(R.attr.colorPrimary), 48, 0)
)
widgetFrame.visibility = View.VISIBLE
}
}
private fun nekoImageView(color: Int, sizeDp: Int, paddingDp: Int): ImageView {
val factor = context.resources.displayMetrics.density
val size = (sizeDp * factor).roundToInt()
val paddingSize = (paddingDp * factor).roundToInt()
return ImageView(context).apply {
layoutParams = ViewGroup.LayoutParams(size, size)
setPadding(paddingSize)
setImageDrawable(nekoAtColor(resources, color))
}
}
private fun nekoAtColor(res: Resources, color: Int): Drawable {
val neko = ResourcesCompat.getDrawable(
res, R.drawable.ic_baseline_fiber_manual_record_24, null
)!!
DrawableCompat.setTint(neko.mutate(), color)
return neko
}
private fun dp(value: Int): Int =
(value * context.resources.displayMetrics.density).roundToInt()
override fun onClick() {
super.onClick()
showModernDialog()
}
/** Modern named list: one row per [Theme.MODERN_THEMES] entry + a "Classic colors…" row. */
private fun showModernDialog() {
lateinit var dialog: AlertDialog
val container = LinearLayout(context).apply {
orientation = LinearLayout.VERTICAL
setPadding(dp(8), dp(8), dp(8), dp(8))
}
for (info in Theme.MODERN_THEMES) {
container.addView(
buildRow(
name = context.getString(info.nameRes),
swatchColor = ResourcesCompat.getColor(context.resources, info.previewColor, context.theme),
) {
select(info.id)
dialog.dismiss()
}
)
}
// Trailing entry: open the legacy single-accent color grid.
container.addView(
buildRow(
name = context.getString(R.string.theme_classic_colors),
swatchColor = null,
icon = R.drawable.ic_baseline_color_lens_24,
) {
dialog.dismiss()
showClassicGrid()
}
)
dialog = MaterialAlertDialogBuilder(context)
.setTitle(title)
.setView(container)
.setNegativeButton(android.R.string.cancel, null)
.show()
}
private fun buildRow(
name: String,
swatchColor: Int?,
icon: Int? = null,
onClick: () -> Unit,
): View {
return LinearLayout(context).apply {
orientation = LinearLayout.HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
isClickable = true
isFocusable = true
setPadding(dp(16), dp(14), dp(16), dp(14))
// Selectable item background for ripple feedback.
val outValue = android.util.TypedValue()
context.theme.resolveAttribute(
android.R.attr.selectableItemBackground, outValue, true
)
setBackgroundResource(outValue.resourceId)
// Announce the theme name to screen readers when the row is focused.
contentDescription = name
val leading = when {
swatchColor != null -> nekoImageView(swatchColor, 28, 0)
icon != null -> ImageView(context).apply {
layoutParams = ViewGroup.LayoutParams(dp(28), dp(28))
setImageDrawable(
ResourcesCompat.getDrawable(resources, icon, context.theme)?.also {
DrawableCompat.setTint(
it.mutate(), context.getColorAttr(android.R.attr.textColorPrimary)
)
}
)
}
else -> View(context).apply {
layoutParams = ViewGroup.LayoutParams(dp(28), dp(28))
}
}
addView(leading)
addView(TextView(context).apply {
text = name
setPadding(dp(16), 0, 0, 0)
textSize = 16f
setTextColor(context.getColorAttr(android.R.attr.textColorPrimary))
layoutParams = LinearLayout.LayoutParams(
0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f
)
})
setOnClickListener { onClick() }
}
}
/** Legacy single-accent color grid (the original swatch-grid UX). */
private fun showClassicGrid() {
lateinit var dialog: AlertDialog
val grid = GridLayout(context).apply {
columnCount = 4
val colors = context.resources.getIntArray(R.array.material_colors)
for ((index, color) in colors.withIndex()) {
// Swatch index+1 is the Theme id (see Theme.kt). Skip themes that
// are presented in the modern named list instead of the grid.
val themeId = index + 1
if (Theme.MODERN_THEMES.any { it.id == themeId }) continue
addView(nekoImageView(color, 64, 0).apply {
contentDescription =
context.getString(R.string.theme_classic_color_swatch, themeId)
setOnClickListener {
select(themeId)
dialog.dismiss()
}
})
}
}
dialog = MaterialAlertDialogBuilder(context)
.setTitle(R.string.theme_classic_colors)
.setView(LinearLayout(context).apply {
gravity = Gravity.CENTER
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
)
addView(grid)
})
.setNegativeButton(android.R.string.cancel, null)
.show()
}
private fun select(themeId: Int) {
// Match the legacy swatch-grid ordering: persist first, then notify the
// change listener (SettingsPreferenceFragment applies/recreates).
persistInt(themeId)
callChangeListener(themeId)
}
}