mirror of
				https://github.com/SimpleMobileTools/Simple-Keyboard.git
				synced 2025-06-05 21:49:26 +02:00 
			
		
		
		
	fix key swipe selection if there are more than 5 alternatives
This commit is contained in:
		| @@ -19,10 +19,7 @@ import android.widget.PopupWindow | |||||||
| import android.widget.TextView | import android.widget.TextView | ||||||
| import com.simplemobiletools.commons.extensions.adjustAlpha | import com.simplemobiletools.commons.extensions.adjustAlpha | ||||||
| import com.simplemobiletools.keyboard.R | import com.simplemobiletools.keyboard.R | ||||||
| import com.simplemobiletools.keyboard.helpers.MyKeyboard | import com.simplemobiletools.keyboard.helpers.* | ||||||
| import com.simplemobiletools.keyboard.helpers.SHIFT_OFF |  | ||||||
| import com.simplemobiletools.keyboard.helpers.SHIFT_ON_ONE_CHAR |  | ||||||
| import com.simplemobiletools.keyboard.helpers.SHIFT_ON_PERMANENT |  | ||||||
| import java.util.* | import java.util.* | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -1014,7 +1011,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut | |||||||
|  |  | ||||||
|                 //mInputView.setSuggest(mSuggest); |                 //mInputView.setSuggest(mSuggest); | ||||||
|                 val keyboard = if (popupKey.popupCharacters != null) { |                 val keyboard = if (popupKey.popupCharacters != null) { | ||||||
|                     MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, -1, paddingLeft + paddingRight) |                     MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, paddingLeft + paddingRight) | ||||||
|                 } else { |                 } else { | ||||||
|                     MyKeyboard(context, popupKeyboardId) |                     MyKeyboard(context, popupKeyboardId) | ||||||
|                 } |                 } | ||||||
| @@ -1048,9 +1045,15 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut | |||||||
|             } else { |             } else { | ||||||
|                 measuredWidth - mMiniKeyboard!!.measuredWidth |                 measuredWidth - mMiniKeyboard!!.measuredWidth | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |             val keysCnt = mMiniKeyboard!!.mKeys.size | ||||||
|             var selectedKeyIndex = Math.floor((me.x - miniKeyboardX) / popupKey.width.toDouble()).toInt() |             var selectedKeyIndex = Math.floor((me.x - miniKeyboardX) / popupKey.width.toDouble()).toInt() | ||||||
|             selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, mMiniKeyboard!!.mKeys.size - 1)) |             if (keysCnt > MAX_KEYS_PER_MINI_ROW) { | ||||||
|             for (i in 0 until mMiniKeyboard!!.mKeys.size) { |                 selectedKeyIndex += MAX_KEYS_PER_MINI_ROW | ||||||
|  |             } | ||||||
|  |             selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, keysCnt - 1)) | ||||||
|  |  | ||||||
|  |             for (i in 0 until keysCnt) { | ||||||
|                 mMiniKeyboard!!.mKeys[i].pressed = i == selectedKeyIndex |                 mMiniKeyboard!!.mKeys[i].pressed = i == selectedKeyIndex | ||||||
|             } |             } | ||||||
|             mMiniKeyboardSelectedKeyIndex = selectedKeyIndex |             mMiniKeyboardSelectedKeyIndex = selectedKeyIndex | ||||||
| @@ -1125,11 +1128,28 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut | |||||||
|                     if (mMiniKeyboard != null) { |                     if (mMiniKeyboard != null) { | ||||||
|                         val coords = intArrayOf(0, 0) |                         val coords = intArrayOf(0, 0) | ||||||
|                         mMiniKeyboard!!.getLocationOnScreen(coords) |                         mMiniKeyboard!!.getLocationOnScreen(coords) | ||||||
|                         val widthPerKey = mMiniKeyboard!!.width / mMiniKeyboard!!.mKeys.size |                         val keysCnt = mMiniKeyboard!!.mKeys.size | ||||||
|  |                         val lastRowKeyCount = if (keysCnt > MAX_KEYS_PER_MINI_ROW) { | ||||||
|  |                             Math.max(keysCnt % MAX_KEYS_PER_MINI_ROW, 1) | ||||||
|  |                         } else { | ||||||
|  |                             keysCnt | ||||||
|  |                         } | ||||||
|  |  | ||||||
|  |                         val widthPerKey = if (keysCnt > MAX_KEYS_PER_MINI_ROW) { | ||||||
|  |                             mMiniKeyboard!!.width / MAX_KEYS_PER_MINI_ROW | ||||||
|  |                         } else { | ||||||
|  |                             mMiniKeyboard!!.width / lastRowKeyCount | ||||||
|  |                         } | ||||||
|  |  | ||||||
|                         var selectedKeyIndex = Math.floor((me.x - coords[0]) / widthPerKey.toDouble()).toInt() |                         var selectedKeyIndex = Math.floor((me.x - coords[0]) / widthPerKey.toDouble()).toInt() | ||||||
|                         selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, mMiniKeyboard!!.mKeys.size - 1)) |                         if (keysCnt > MAX_KEYS_PER_MINI_ROW) { | ||||||
|  |                             selectedKeyIndex = Math.max(0, selectedKeyIndex) | ||||||
|  |                             selectedKeyIndex += MAX_KEYS_PER_MINI_ROW | ||||||
|  |                         } | ||||||
|  |  | ||||||
|  |                         selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, keysCnt - 1)) | ||||||
|                         if (selectedKeyIndex != mMiniKeyboardSelectedKeyIndex) { |                         if (selectedKeyIndex != mMiniKeyboardSelectedKeyIndex) { | ||||||
|                             for (i in 0 until mMiniKeyboard!!.mKeys.size) { |                             for (i in 0 until keysCnt) { | ||||||
|                                 mMiniKeyboard!!.mKeys[i].pressed = i == selectedKeyIndex |                                 mMiniKeyboard!!.mKeys[i].pressed = i == selectedKeyIndex | ||||||
|                             } |                             } | ||||||
|                             mMiniKeyboardSelectedKeyIndex = selectedKeyIndex |                             mMiniKeyboardSelectedKeyIndex = selectedKeyIndex | ||||||
| @@ -1146,14 +1166,9 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut | |||||||
|                 } |                 } | ||||||
|                 MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { |                 MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { | ||||||
|                     if (mMiniKeyboard != null) { |                     if (mMiniKeyboard != null) { | ||||||
|                         val coords = intArrayOf(0, 0) |                         mMiniKeyboard!!.mKeys.firstOrNull { it.pressed }?.apply { | ||||||
|                         mMiniKeyboard!!.getLocationOnScreen(coords) |                             onKeyboardActionListener!!.onKey(codes[0], codes.toIntArray()) | ||||||
|                         val keys = mMiniKeyboard!!.mKeys |                         } | ||||||
|                         val widthPerKey = mMiniKeyboard!!.width / keys.size |  | ||||||
|                         var selectedKeyIndex = Math.floor((me.x - coords[0]) / widthPerKey.toDouble()).toInt() |  | ||||||
|                         selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, keys.size - 1)) |  | ||||||
|                         val selectedKeyCodes = keys[selectedKeyIndex].codes |  | ||||||
|                         onKeyboardActionListener!!.onKey(selectedKeyCodes[0], selectedKeyCodes.toIntArray()) |  | ||||||
|                     } |                     } | ||||||
|                     mMiniKeyboardSelectedKeyIndex = -1 |                     mMiniKeyboardSelectedKeyIndex = -1 | ||||||
|                     dismissPopupKeyboard() |                     dismissPopupKeyboard() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user