adding some more styling

This commit is contained in:
tibbi 2022-01-17 20:58:14 +01:00
parent 6bd6d128b9
commit 767f65c5e9
6 changed files with 38 additions and 15 deletions

View File

@ -247,6 +247,9 @@ class MyKeyboard {
/** The current pressed state of this key */ /** The current pressed state of this key */
var pressed = false var pressed = false
/** Focused state, used after long pressing a key and swiping to alternative keys */
var focused = false
/** Text to output when pressed. This can be multiple characters, like ".com" */ /** Text to output when pressed. This can be multiple characters, like ".com" */
var text: CharSequence? = null var text: CharSequence? = null
@ -331,6 +334,14 @@ class MyKeyboard {
edgeFlags = parent.rowEdgeFlags edgeFlags = parent.rowEdgeFlags
} }
fun onPressed() {
pressed = true
}
fun onReleased() {
pressed = false
}
fun parseCSV(value: String): ArrayList<Int> { fun parseCSV(value: String): ArrayList<Int> {
var count = 0 var count = 0
var lastIndex = 0 var lastIndex = 0

View File

@ -587,10 +587,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
keyBackground.setBounds(0, 0, key.width, key.height) keyBackground.setBounds(0, 0, key.width, key.height)
} }
keyBackground.state = if (key.pressed) { keyBackground.state = when {
intArrayOf(android.R.attr.state_pressed) key.pressed -> intArrayOf(android.R.attr.state_pressed)
} else { key.focused -> intArrayOf(android.R.attr.state_focused)
intArrayOf() else -> intArrayOf()
} }
canvas.translate((key.x + kbdPaddingLeft).toFloat(), (key.y + kbdPaddingTop).toFloat()) canvas.translate((key.x + kbdPaddingLeft).toFloat(), (key.y + kbdPaddingTop).toFloat())
@ -605,8 +605,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
paint.typeface = Typeface.DEFAULT paint.typeface = Typeface.DEFAULT
} }
// Draw a drop shadow for the text
paint.setShadowLayer(mShadowRadius, 0f, 0f, mShadowColor)
// Draw the text // Draw the text
canvas.drawText( canvas.drawText(
label, ((key.width - padding.left - padding.right) / 2 + padding.left).toFloat(), label, ((key.width - padding.left - padding.right) / 2 + padding.left).toFloat(),
@ -769,6 +767,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
if (oldKeyIndex != mCurrentKeyIndex) { if (oldKeyIndex != mCurrentKeyIndex) {
if (oldKeyIndex != NOT_A_KEY && keys.size > oldKeyIndex) { if (oldKeyIndex != NOT_A_KEY && keys.size > oldKeyIndex) {
val oldKey = keys[oldKeyIndex] val oldKey = keys[oldKeyIndex]
oldKey.onReleased()
invalidateKey(oldKeyIndex) invalidateKey(oldKeyIndex)
val keyCode = oldKey.codes[0] val keyCode = oldKey.codes[0]
sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT, keyCode) sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT, keyCode)
@ -777,6 +776,14 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
if (mCurrentKeyIndex != NOT_A_KEY && keys.size > mCurrentKeyIndex) { if (mCurrentKeyIndex != NOT_A_KEY && keys.size > mCurrentKeyIndex) {
val newKey = keys[mCurrentKeyIndex] val newKey = keys[mCurrentKeyIndex]
val code = newKey.codes.firstOrNull() ?: -100
if (code == MyKeyboard.KEYCODE_SHIFT || code == MyKeyboard.KEYCODE_MODE_CHANGE || code == MyKeyboard.KEYCODE_DELETE ||
code == MyKeyboard.KEYCODE_ENTER || code == MyKeyboard.KEYCODE_SPACE
) {
newKey.onPressed()
}
invalidateKey(mCurrentKeyIndex) invalidateKey(mCurrentKeyIndex)
val keyCode = newKey.codes[0] val keyCode = newKey.codes[0]
sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER, keyCode) sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER, keyCode)
@ -1057,8 +1064,9 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, keysCnt - 1)) selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, keysCnt - 1))
for (i in 0 until keysCnt) { for (i in 0 until keysCnt) {
mMiniKeyboard!!.mKeys[i].pressed = i == selectedKeyIndex mMiniKeyboard!!.mKeys[i].focused = i == selectedKeyIndex
} }
mMiniKeyboardSelectedKeyIndex = selectedKeyIndex mMiniKeyboardSelectedKeyIndex = selectedKeyIndex
mMiniKeyboard!!.invalidateAllKeys() mMiniKeyboard!!.invalidateAllKeys()
@ -1153,7 +1161,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, keysCnt - 1)) selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, keysCnt - 1))
if (selectedKeyIndex != mMiniKeyboardSelectedKeyIndex) { if (selectedKeyIndex != mMiniKeyboardSelectedKeyIndex) {
for (i in 0 until keysCnt) { for (i in 0 until keysCnt) {
mMiniKeyboard!!.mKeys[i].pressed = i == selectedKeyIndex mMiniKeyboard!!.mKeys[i].focused = i == selectedKeyIndex
} }
mMiniKeyboardSelectedKeyIndex = selectedKeyIndex mMiniKeyboardSelectedKeyIndex = selectedKeyIndex
mMiniKeyboard!!.invalidateAllKeys() mMiniKeyboard!!.invalidateAllKeys()
@ -1167,7 +1175,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
} }
} }
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
mMiniKeyboard?.mKeys?.firstOrNull { it.pressed }?.apply { mMiniKeyboard?.mKeys?.firstOrNull { it.focused }?.apply {
onKeyboardActionListener!!.onKey(codes[0], codes.toIntArray()) onKeyboardActionListener!!.onKey(codes[0], codes.toIntArray())
} }
mMiniKeyboardSelectedKeyIndex = -1 mMiniKeyboardSelectedKeyIndex = -1

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- used after long pressing a key to highlight the currently selected key alternative on minikeyboard-->
<item android:drawable="@drawable/minikeyboard_selected_background" android:state_focused="true" />
<!-- used at clicking some keys on the main keyboard -->
<item android:drawable="@color/key_press_color" android:state_pressed="true" />
</selector>

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/minikeyboard_selected_background" android:state_pressed="true" />
</selector>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<color name="key_press_color">#11ffffff</color>
</resources> </resources>

View File

@ -7,7 +7,7 @@
</style> </style>
<style name="Widget.KeyboardView" parent="Widget"> <style name="Widget.KeyboardView" parent="Widget">
<item name="keyBackground">@drawable/minikeyboard_key_selector</item> <item name="keyBackground">@drawable/keyboard_key_selector</item>
<item name="keyTextSize">22sp</item> <item name="keyTextSize">22sp</item>
<item name="keyTextColor">#FFFFFFFF</item> <item name="keyTextColor">#FFFFFFFF</item>
<item name="keyPreviewLayout">@layout/keyboard_key_preview</item> <item name="keyPreviewLayout">@layout/keyboard_key_preview</item>