|
1 | 1 | package helium314.keyboard.latin.utils |
2 | 2 |
|
| 3 | +import android.app.AlertDialog |
3 | 4 | import android.content.Context |
| 5 | +import android.content.res.ColorStateList |
| 6 | +import android.graphics.Color |
| 7 | +import android.graphics.drawable.GradientDrawable |
| 8 | +import android.graphics.drawable.InsetDrawable |
4 | 9 | import android.view.ContextThemeWrapper |
5 | | -import helium314.keyboard.latin.R |
| 10 | +import android.view.View |
| 11 | +import android.view.ViewGroup |
| 12 | +import android.widget.CheckedTextView |
| 13 | +import android.widget.TextView |
| 14 | +import androidx.core.graphics.ColorUtils |
| 15 | +import helium314.keyboard.latin.common.ColorType |
| 16 | +import helium314.keyboard.latin.settings.Settings |
| 17 | + |
| 18 | +private const val MAX_CORNER_RADIUS_DP = 28 |
| 19 | +private const val WINDOW_INSET_DP = 16 |
| 20 | +private const val MIN_ACCENT_CONTRAST = 4.5 |
6 | 21 |
|
7 | 22 | // todo: ideally the custom InputMethodPicker would be removed / replaced with compose dialog, then this can be removed |
8 | 23 | fun getPlatformDialogThemeContext(context: Context): Context { |
9 | | - // Because {@link AlertDialog.Builder.create()} doesn't honor the specified theme with |
10 | | - // createThemeContextWrapper=false, the result dialog box has unneeded paddings around it. |
11 | | - return ContextThemeWrapper(context, R.style.platformActivityTheme) |
| 24 | + // light or dark like the keyboard, not like the system, so everything the dialog draws itself stays readable |
| 25 | + // An activity theme, because {@link AlertDialog.Builder.create()} doesn't honor a specified dialog theme with |
| 26 | + // createThemeContextWrapper=false, the result dialog box would have unneeded paddings around it. |
| 27 | + val isLight = ColorUtils.calculateLuminance(keyboardDialogBackgroundColor()) > 0.4 |
| 28 | + return ContextThemeWrapper(context, |
| 29 | + if (isLight) android.R.style.Theme_DeviceDefault_Light else android.R.style.Theme_DeviceDefault) |
| 30 | +} |
| 31 | + |
| 32 | +private fun keyboardDialogBackgroundColor() = |
| 33 | + ColorUtils.setAlphaComponent(Settings.getValues().mColors.get(ColorType.MAIN_BACKGROUND), 0xFF) |
| 34 | + |
| 35 | +/** Gives a dialog opened from the keyboard the keyboard's colours and corners. Call after [AlertDialog.show]. */ |
| 36 | +fun AlertDialog.applyKeyboardStyle() { |
| 37 | + val window = window ?: return |
| 38 | + val colors = Settings.getValues().mColors |
| 39 | + val density = context.resources.displayMetrics.density |
| 40 | + val background = keyboardDialogBackgroundColor() |
| 41 | + val textColor = ColorUtils.setAlphaComponent(colors.get(ColorType.KEY_TEXT), 0xFF) |
| 42 | + val accent = ColorUtils.setAlphaComponent(colors.get(ColorType.ACTION_KEY_BACKGROUND), 0xFF) |
| 43 | + val accentIsReadable = ColorUtils.calculateContrast(accent, background) >= MIN_ACCENT_CONTRAST |
| 44 | + val shape = GradientDrawable().apply { |
| 45 | + // the largest key radius would cut into the text |
| 46 | + cornerRadius = minOf(keyboardCornerRadiusPx(context.resources), MAX_CORNER_RADIUS_DP * density) |
| 47 | + setColor(background) |
| 48 | + } |
| 49 | + window.setBackgroundDrawable(InsetDrawable(shape, (WINDOW_INSET_DP * density).toInt())) |
| 50 | + |
| 51 | + val titleId = context.resources.getIdentifier("alertTitle", "id", "android") |
| 52 | + if (titleId != 0) findViewById<TextView>(titleId)?.setTextColor(textColor) |
| 53 | + findViewById<TextView>(android.R.id.message)?.setTextColor(textColor) |
| 54 | + for (which in intArrayOf(AlertDialog.BUTTON_POSITIVE, AlertDialog.BUTTON_NEGATIVE, AlertDialog.BUTTON_NEUTRAL)) { |
| 55 | + getButton(which)?.setTextColor(if (accentIsReadable) accent else textColor) |
| 56 | + } |
| 57 | + |
| 58 | + val list = listView ?: return |
| 59 | + fun styleRow(row: View) { |
| 60 | + if (row !is TextView) return |
| 61 | + row.setTextColor(textColor) |
| 62 | + if (row is CheckedTextView) row.checkMarkTintList = ColorStateList.valueOf(textColor) |
| 63 | + } |
| 64 | + list.setBackgroundColor(Color.TRANSPARENT) |
| 65 | + // rows are created while the list is laid out, which may be after this |
| 66 | + list.setOnHierarchyChangeListener(object : ViewGroup.OnHierarchyChangeListener { |
| 67 | + override fun onChildViewAdded(parent: View?, child: View) = styleRow(child) |
| 68 | + override fun onChildViewRemoved(parent: View?, child: View?) = Unit |
| 69 | + }) |
| 70 | + for (i in 0 until list.childCount) styleRow(list.getChildAt(i)) |
12 | 71 | } |
0 commit comments