Skip to content

Commit 8cdfa11

Browse files
authored
Merge pull request #258 from dmitriy-chernysh/add-slash-to-numbers#239
Add "/" to 'phone' keyboard as a new key shown at long pressing zero
2 parents f048ab5 + c32d85a commit 8cdfa11

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
10051005
val popupKeyboardId = popupKey.popupResId
10061006
if (popupKeyboardId != 0) {
10071007
mMiniKeyboardContainer = mMiniKeyboardCache[popupKey]
1008+
1009+
// For 'number' and 'phone' keyboards the count of popup keys might be bigger than count of keys in the main keyboard.
1010+
// And therefore the width of the key might be smaller than width declared in MyKeyboard.Key.width for the main keyboard.
1011+
val popupKeyWidth = popupKey.calcKeyWidth(containerWidth = mMiniKeyboardContainer?.measuredWidth ?: width)
1012+
10081013
if (mMiniKeyboardContainer == null) {
10091014
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
10101015
keyboardPopupBinding = KeyboardPopupKeyboardBinding.inflate(inflater).apply {
@@ -1013,11 +1018,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
10131018
}
10141019

10151020
val keyboard = if (popupKey.popupCharacters != null) {
1016-
MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, popupKey.width)
1021+
MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, popupKeyWidth)
10171022
} else {
10181023
MyKeyboard(context, popupKeyboardId, 0)
10191024
}
1020-
10211025
mMiniKeyboard!!.setKeyboard(keyboard)
10221026
mPopupParent = this
10231027
mMiniKeyboardContainer!!.measure(
@@ -1032,8 +1036,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
10321036
mPopupX = popupKey.x
10331037
mPopupY = popupKey.y
10341038

1035-
val widthToUse = mMiniKeyboardContainer!!.measuredWidth - (popupKey.popupCharacters!!.length / 2) * popupKey.width
1036-
mPopupX = mPopupX + popupKey.width - widthToUse
1039+
val widthToUse = mMiniKeyboardContainer!!.measuredWidth - (popupKey.popupCharacters!!.length / 2) * popupKeyWidth
1040+
mPopupX = mPopupX + popupKeyWidth - widthToUse
10371041
mPopupY -= mMiniKeyboardContainer!!.measuredHeight
10381042
val x = mPopupX + mCoordinates[0]
10391043
val y = mPopupY + mCoordinates[1]
@@ -1048,7 +1052,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
10481052
}
10491053

10501054
val keysCnt = mMiniKeyboard!!.mKeys.size
1051-
var selectedKeyIndex = Math.floor((me.x - miniKeyboardX) / popupKey.width.toDouble()).toInt()
1055+
var selectedKeyIndex = Math.floor((me.x - miniKeyboardX) / popupKeyWidth.toDouble()).toInt()
10521056
if (keysCnt > MAX_KEYS_PER_MINI_ROW) {
10531057
selectedKeyIndex += MAX_KEYS_PER_MINI_ROW
10541058
}
@@ -1714,4 +1718,17 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
17141718
* Returns true if there are [InlineContentView]s in [autofill_suggestions_holder]
17151719
*/
17161720
private fun hasInlineViews() = (keyboardViewBinding?.autofillSuggestionsHolder?.childCount ?: 0) > 0
1721+
1722+
/**
1723+
* Returns: Popup Key width depends on popup keys count
1724+
*/
1725+
private fun MyKeyboard.Key.calcKeyWidth(containerWidth: Int): Int {
1726+
val popupKeyCount = this.popupCharacters!!.length
1727+
1728+
return if (popupKeyCount > containerWidth / this.width) {
1729+
containerWidth / popupKeyCount
1730+
} else {
1731+
this.width
1732+
}
1733+
}
17171734
}

app/src/main/res/xml/keys_phone.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<Key
6060
app:keyLabel="0"
6161
app:keyWidth="25%p"
62-
app:popupCharacters="+-,."
62+
app:popupCharacters="-+,./"
6363
app:popupKeyboard="@xml/keyboard_popup_template"
6464
app:topSmallNumber="+" />
6565
<Key

0 commit comments

Comments
 (0)