[android, ui] Fix sticky focus behavior (#3242)

This fixes an issue where game cards can stack focus highlights by touching and sliding in Grid/List views. Running and exiting the game by touch leaves a sticky focus that is not cleared. It is again possible to stack focus highlights that way.

The first commit fixes the bug, the second refactors and simplifies the state management in GradientBorderCardView.

WIP for now, until I thoroughly test it.

Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/3242
Reviewed-by: crueter <crueter@eden-emu.dev>
Reviewed-by: DraVee <dravee@eden-emu.dev>
Co-authored-by: Mike22 <misakupka@gmail.com>
Co-committed-by: Mike22 <misakupka@gmail.com>
This commit is contained in:
Mike22 2025-12-31 21:40:56 +01:00 committed by crueter
parent 14951348bf
commit 7234875a53
No known key found for this signature in database
GPG key ID: 425ACD2D4830EBC6

View file

@ -35,6 +35,14 @@ class GradientBorderCardView @JvmOverloads constructor(
updateThemeState() updateThemeState()
} }
private fun updateBorderState() {
val shouldShow = isPressed || isFocused || isSelected
if (showGradientBorder != shouldShow) {
showGradientBorder = shouldShow
invalidate()
}
}
private fun updateThemeState() { private fun updateThemeState() {
val prefs = PreferenceManager.getDefaultSharedPreferences(context) val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val themeIndex = try { val themeIndex = try {
@ -43,6 +51,7 @@ class GradientBorderCardView @JvmOverloads constructor(
0 // Default to Eden theme if error 0 // Default to Eden theme if error
} }
isEdenTheme = themeIndex == 0 isEdenTheme = themeIndex == 0
invalidate()
} }
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
@ -93,27 +102,22 @@ class GradientBorderCardView @JvmOverloads constructor(
override fun onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?) { override fun onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect) super.onFocusChanged(gainFocus, direction, previouslyFocusedRect)
showGradientBorder = gainFocus updateBorderState()
invalidate()
} }
override fun setSelected(selected: Boolean) { override fun setSelected(selected: Boolean) {
super.setSelected(selected) super.setSelected(selected)
showGradientBorder = selected updateBorderState()
invalidate()
} }
override fun setPressed(pressed: Boolean) { override fun setPressed(pressed: Boolean) {
super.setPressed(pressed) super.setPressed(pressed)
if (pressed) { updateBorderState()
showGradientBorder = true
invalidate()
}
} }
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
if (showGradientBorder && !isPressed) { if (showGradientBorder) {
canvas.drawPath(borderPath, borderPaint) canvas.drawPath(borderPath, borderPaint)
} }
} }
@ -121,6 +125,5 @@ class GradientBorderCardView @JvmOverloads constructor(
override fun onAttachedToWindow() { override fun onAttachedToWindow() {
super.onAttachedToWindow() super.onAttachedToWindow()
updateThemeState() updateThemeState()
requestLayout()
} }
} }