From f02ba5fae9c348e314d1a505b963bacf4864c6c9 Mon Sep 17 00:00:00 2001 From: Dmitriy Chernysh Date: Thu, 12 Oct 2023 22:50:18 +0300 Subject: [PATCH 1/2] Added "/" to 'phone' keyboard as a new key shown at long pressing zero. And made the popup keyboard work with 5+ symbols. Fix #239 --- .../keyboard/views/MyKeyboardView.kt | 26 +++++++++++++++---- app/src/main/res/xml/keys_phone.xml | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt index bb49abe..e67cf6c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt @@ -1005,6 +1005,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut val popupKeyboardId = popupKey.popupResId if (popupKeyboardId != 0) { 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) { val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater keyboardPopupBinding = KeyboardPopupKeyboardBinding.inflate(inflater).apply { @@ -1013,11 +1018,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut } val keyboard = if (popupKey.popupCharacters != null) { - MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, popupKey.width) + MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, popupKeyWidth) } else { MyKeyboard(context, popupKeyboardId, 0) } - mMiniKeyboard!!.setKeyboard(keyboard) mPopupParent = this mMiniKeyboardContainer!!.measure( @@ -1032,8 +1036,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut mPopupX = popupKey.x mPopupY = popupKey.y - val widthToUse = mMiniKeyboardContainer!!.measuredWidth - (popupKey.popupCharacters!!.length / 2) * popupKey.width - mPopupX = mPopupX + popupKey.width - widthToUse + val widthToUse = mMiniKeyboardContainer!!.measuredWidth - (popupKey.popupCharacters!!.length / 2) * popupKeyWidth + mPopupX = mPopupX + popupKeyWidth - widthToUse mPopupY -= mMiniKeyboardContainer!!.measuredHeight val x = mPopupX + mCoordinates[0] val y = mPopupY + mCoordinates[1] @@ -1048,7 +1052,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut } 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) { 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] */ 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 + } } diff --git a/app/src/main/res/xml/keys_phone.xml b/app/src/main/res/xml/keys_phone.xml index 8fb90d0..63e9218 100644 --- a/app/src/main/res/xml/keys_phone.xml +++ b/app/src/main/res/xml/keys_phone.xml @@ -59,7 +59,7 @@ Date: Fri, 13 Oct 2023 15:36:11 +0300 Subject: [PATCH 2/2] Use brackets for if/else --- .../com/simplemobiletools/keyboard/views/MyKeyboardView.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt index e67cf6c..74bc370 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt @@ -1725,9 +1725,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut private fun MyKeyboard.Key.calcKeyWidth(containerWidth: Int): Int { val popupKeyCount = this.popupCharacters!!.length - return if (popupKeyCount > containerWidth / this.width) + return if (popupKeyCount > containerWidth / this.width) { containerWidth / popupKeyCount - else + } else { this.width + } } }