Added "/" to 'phone' keyboard as a new key shown at long pressing zero. And made the popup keyboard work with 5+ symbols. Fix #239

This commit is contained in:
Dmitriy Chernysh 2023-10-12 22:50:18 +03:00
parent fe1c2ba4ff
commit f02ba5fae9
2 changed files with 22 additions and 6 deletions

View File

@ -1005,6 +1005,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
val popupKeyboardId = popupKey.popupResId val popupKeyboardId = popupKey.popupResId
if (popupKeyboardId != 0) { if (popupKeyboardId != 0) {
mMiniKeyboardContainer = mMiniKeyboardCache[popupKey] mMiniKeyboardContainer = mMiniKeyboardCache[popupKey]
// For 'number' and 'phone' keyboards the count of popup keys might be bigger than count of keys in the main keyboard.
// And therefore the width of the key might be smaller than width declared in MyKeyboard.Key.width for the main keyboard.
val popupKeyWidth = popupKey.calcKeyWidth(containerWidth = mMiniKeyboardContainer?.measuredWidth ?: width)
if (mMiniKeyboardContainer == null) { if (mMiniKeyboardContainer == null) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
keyboardPopupBinding = KeyboardPopupKeyboardBinding.inflate(inflater).apply { keyboardPopupBinding = KeyboardPopupKeyboardBinding.inflate(inflater).apply {
@ -1013,11 +1018,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
} }
val keyboard = if (popupKey.popupCharacters != null) { val keyboard = if (popupKey.popupCharacters != null) {
MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, popupKey.width) MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, popupKeyWidth)
} else { } else {
MyKeyboard(context, popupKeyboardId, 0) MyKeyboard(context, popupKeyboardId, 0)
} }
mMiniKeyboard!!.setKeyboard(keyboard) mMiniKeyboard!!.setKeyboard(keyboard)
mPopupParent = this mPopupParent = this
mMiniKeyboardContainer!!.measure( mMiniKeyboardContainer!!.measure(
@ -1032,8 +1036,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
mPopupX = popupKey.x mPopupX = popupKey.x
mPopupY = popupKey.y mPopupY = popupKey.y
val widthToUse = mMiniKeyboardContainer!!.measuredWidth - (popupKey.popupCharacters!!.length / 2) * popupKey.width val widthToUse = mMiniKeyboardContainer!!.measuredWidth - (popupKey.popupCharacters!!.length / 2) * popupKeyWidth
mPopupX = mPopupX + popupKey.width - widthToUse mPopupX = mPopupX + popupKeyWidth - widthToUse
mPopupY -= mMiniKeyboardContainer!!.measuredHeight mPopupY -= mMiniKeyboardContainer!!.measuredHeight
val x = mPopupX + mCoordinates[0] val x = mPopupX + mCoordinates[0]
val y = mPopupY + mCoordinates[1] val y = mPopupY + mCoordinates[1]
@ -1048,7 +1052,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
} }
val keysCnt = mMiniKeyboard!!.mKeys.size val keysCnt = mMiniKeyboard!!.mKeys.size
var selectedKeyIndex = Math.floor((me.x - miniKeyboardX) / popupKey.width.toDouble()).toInt() var selectedKeyIndex = Math.floor((me.x - miniKeyboardX) / popupKeyWidth.toDouble()).toInt()
if (keysCnt > MAX_KEYS_PER_MINI_ROW) { if (keysCnt > MAX_KEYS_PER_MINI_ROW) {
selectedKeyIndex += MAX_KEYS_PER_MINI_ROW selectedKeyIndex += MAX_KEYS_PER_MINI_ROW
} }
@ -1714,4 +1718,16 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
* Returns true if there are [InlineContentView]s in [autofill_suggestions_holder] * Returns true if there are [InlineContentView]s in [autofill_suggestions_holder]
*/ */
private fun hasInlineViews() = (keyboardViewBinding?.autofillSuggestionsHolder?.childCount ?: 0) > 0 private fun hasInlineViews() = (keyboardViewBinding?.autofillSuggestionsHolder?.childCount ?: 0) > 0
/**
* Returns: Popup Key width depends on popup keys count
*/
private fun MyKeyboard.Key.calcKeyWidth(containerWidth: Int): Int {
val popupKeyCount = this.popupCharacters!!.length
return if (popupKeyCount > containerWidth / this.width)
containerWidth / popupKeyCount
else
this.width
}
} }

View File

@ -59,7 +59,7 @@
<Key <Key
app:keyLabel="0" app:keyLabel="0"
app:keyWidth="25%p" app:keyWidth="25%p"
app:popupCharacters="+-,." app:popupCharacters="-+,./"
app:popupKeyboard="@xml/keyboard_popup_template" app:popupKeyboard="@xml/keyboard_popup_template"
app:topSmallNumber="+" /> app:topSmallNumber="+" />
<Key <Key