mirror of
https://github.com/SimpleMobileTools/Simple-Keyboard.git
synced 2025-06-05 21:49:26 +02:00
cleanup code
- remove duplicate blank lines - move emoji key code check to the onLongPress method
This commit is contained in:
@ -52,7 +52,6 @@ fun Context.getKeyboardDialogBuilder() = if (baseConfig.isUsingSystemTheme) {
|
|||||||
AlertDialog.Builder(this, R.style.MyKeyboard_Alert)
|
AlertDialog.Builder(this, R.style.MyKeyboard_Alert)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun Context.setupKeyboardDialogStuff(
|
fun Context.setupKeyboardDialogStuff(
|
||||||
windowToken: IBinder,
|
windowToken: IBinder,
|
||||||
view: View,
|
view: View,
|
||||||
@ -62,8 +61,6 @@ fun Context.setupKeyboardDialogStuff(
|
|||||||
cancelOnTouchOutside: Boolean = true,
|
cancelOnTouchOutside: Boolean = true,
|
||||||
callback: ((alertDialog: AlertDialog) -> Unit)? = null
|
callback: ((alertDialog: AlertDialog) -> Unit)? = null
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
|
||||||
val textColor = getProperTextColor()
|
val textColor = getProperTextColor()
|
||||||
val backgroundColor = getProperBackgroundColor()
|
val backgroundColor = getProperBackgroundColor()
|
||||||
val primaryColor = getProperPrimaryColor()
|
val primaryColor = getProperPrimaryColor()
|
||||||
|
@ -938,15 +938,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
|
|||||||
}
|
}
|
||||||
|
|
||||||
val popupKey = mKeys[mCurrentKey]
|
val popupKey = mKeys[mCurrentKey]
|
||||||
val result = if (popupKey.code == KEYCODE_EMOJI) {
|
val result = onLongPress(popupKey, me)
|
||||||
ChangeLanguagePopup(this, onSelect = {
|
|
||||||
mOnKeyboardActionListener?.reloadKeyboard()
|
|
||||||
})
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
onLongPress(popupKey, me)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
mAbortKey = true
|
mAbortKey = true
|
||||||
showPreview(NOT_A_KEY)
|
showPreview(NOT_A_KEY)
|
||||||
@ -963,104 +955,111 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
|
|||||||
* handle the call.
|
* handle the call.
|
||||||
*/
|
*/
|
||||||
private fun onLongPress(popupKey: MyKeyboard.Key, me: MotionEvent): Boolean {
|
private fun onLongPress(popupKey: MyKeyboard.Key, me: MotionEvent): Boolean {
|
||||||
val popupKeyboardId = popupKey.popupResId
|
if (popupKey.code == KEYCODE_EMOJI) {
|
||||||
if (popupKeyboardId != 0) {
|
ChangeLanguagePopup(this, onSelect = {
|
||||||
mMiniKeyboardContainer = mMiniKeyboardCache[popupKey]
|
mOnKeyboardActionListener?.reloadKeyboard()
|
||||||
if (mMiniKeyboardContainer == null) {
|
})
|
||||||
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
|
||||||
mMiniKeyboardContainer = inflater.inflate(mPopupLayout, null)
|
|
||||||
mMiniKeyboard = mMiniKeyboardContainer!!.findViewById<View>(R.id.mini_keyboard_view) as MyKeyboardView
|
|
||||||
|
|
||||||
mMiniKeyboard!!.mOnKeyboardActionListener = object : OnKeyboardActionListener {
|
|
||||||
override fun onKey(code: Int) {
|
|
||||||
mOnKeyboardActionListener!!.onKey(code)
|
|
||||||
dismissPopupKeyboard()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onPress(primaryCode: Int) {
|
|
||||||
mOnKeyboardActionListener!!.onPress(primaryCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onActionUp() {
|
|
||||||
mOnKeyboardActionListener!!.onActionUp()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun moveCursorLeft() {
|
|
||||||
mOnKeyboardActionListener!!.moveCursorLeft()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun moveCursorRight() {
|
|
||||||
mOnKeyboardActionListener!!.moveCursorRight()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onText(text: String) {
|
|
||||||
mOnKeyboardActionListener!!.onText(text)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun reloadKeyboard() {
|
|
||||||
mOnKeyboardActionListener!!.reloadKeyboard()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val keyboard = if (popupKey.popupCharacters != null) {
|
|
||||||
MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, popupKey.width)
|
|
||||||
} else {
|
|
||||||
MyKeyboard(context, popupKeyboardId, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
mMiniKeyboard!!.setKeyboard(keyboard)
|
|
||||||
mPopupParent = this
|
|
||||||
mMiniKeyboardContainer!!.measure(
|
|
||||||
MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),
|
|
||||||
MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST)
|
|
||||||
)
|
|
||||||
mMiniKeyboardCache[popupKey] = mMiniKeyboardContainer
|
|
||||||
} else {
|
|
||||||
mMiniKeyboard = mMiniKeyboardContainer!!.findViewById<View>(R.id.mini_keyboard_view) as MyKeyboardView
|
|
||||||
}
|
|
||||||
|
|
||||||
getLocationInWindow(mCoordinates)
|
|
||||||
mPopupX = popupKey.x
|
|
||||||
mPopupY = popupKey.y
|
|
||||||
|
|
||||||
val widthToUse = mMiniKeyboardContainer!!.measuredWidth - (popupKey.popupCharacters!!.length / 2) * popupKey.width
|
|
||||||
mPopupX = mPopupX + popupKey.width - widthToUse
|
|
||||||
mPopupY -= mMiniKeyboardContainer!!.measuredHeight
|
|
||||||
val x = mPopupX + mCoordinates[0]
|
|
||||||
val y = mPopupY + mCoordinates[1]
|
|
||||||
val xOffset = Math.max(0, x)
|
|
||||||
mMiniKeyboard!!.setPopupOffset(xOffset, y)
|
|
||||||
|
|
||||||
// make sure we highlight the proper key right after long pressing it, before any ACTION_MOVE event occurs
|
|
||||||
val miniKeyboardX = if (xOffset + mMiniKeyboard!!.measuredWidth <= measuredWidth) {
|
|
||||||
xOffset
|
|
||||||
} else {
|
|
||||||
measuredWidth - mMiniKeyboard!!.measuredWidth
|
|
||||||
}
|
|
||||||
|
|
||||||
val keysCnt = mMiniKeyboard!!.mKeys.size
|
|
||||||
var selectedKeyIndex = Math.floor((me.x - miniKeyboardX) / popupKey.width.toDouble()).toInt()
|
|
||||||
if (keysCnt > MAX_KEYS_PER_MINI_ROW) {
|
|
||||||
selectedKeyIndex += MAX_KEYS_PER_MINI_ROW
|
|
||||||
}
|
|
||||||
selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, keysCnt - 1))
|
|
||||||
|
|
||||||
for (i in 0 until keysCnt) {
|
|
||||||
mMiniKeyboard!!.mKeys[i].focused = i == selectedKeyIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
mMiniKeyboardSelectedKeyIndex = selectedKeyIndex
|
|
||||||
mMiniKeyboard!!.invalidateAllKeys()
|
|
||||||
|
|
||||||
val miniShiftStatus = if (isShifted()) SHIFT_ON_PERMANENT else SHIFT_OFF
|
|
||||||
mMiniKeyboard!!.setShifted(miniShiftStatus)
|
|
||||||
mPopupKeyboard.contentView = mMiniKeyboardContainer
|
|
||||||
mPopupKeyboard.width = mMiniKeyboardContainer!!.measuredWidth
|
|
||||||
mPopupKeyboard.height = mMiniKeyboardContainer!!.measuredHeight
|
|
||||||
mPopupKeyboard.showAtLocation(this, Gravity.NO_GRAVITY, x, y)
|
|
||||||
mMiniKeyboardOnScreen = true
|
|
||||||
invalidateAllKeys()
|
|
||||||
return true
|
return true
|
||||||
|
} else {
|
||||||
|
val popupKeyboardId = popupKey.popupResId
|
||||||
|
if (popupKeyboardId != 0) {
|
||||||
|
mMiniKeyboardContainer = mMiniKeyboardCache[popupKey]
|
||||||
|
if (mMiniKeyboardContainer == null) {
|
||||||
|
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
||||||
|
mMiniKeyboardContainer = inflater.inflate(mPopupLayout, null)
|
||||||
|
mMiniKeyboard = mMiniKeyboardContainer!!.findViewById<View>(R.id.mini_keyboard_view) as MyKeyboardView
|
||||||
|
|
||||||
|
mMiniKeyboard!!.mOnKeyboardActionListener = object : OnKeyboardActionListener {
|
||||||
|
override fun onKey(code: Int) {
|
||||||
|
mOnKeyboardActionListener!!.onKey(code)
|
||||||
|
dismissPopupKeyboard()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPress(primaryCode: Int) {
|
||||||
|
mOnKeyboardActionListener!!.onPress(primaryCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActionUp() {
|
||||||
|
mOnKeyboardActionListener!!.onActionUp()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun moveCursorLeft() {
|
||||||
|
mOnKeyboardActionListener!!.moveCursorLeft()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun moveCursorRight() {
|
||||||
|
mOnKeyboardActionListener!!.moveCursorRight()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onText(text: String) {
|
||||||
|
mOnKeyboardActionListener!!.onText(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun reloadKeyboard() {
|
||||||
|
mOnKeyboardActionListener!!.reloadKeyboard()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val keyboard = if (popupKey.popupCharacters != null) {
|
||||||
|
MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, popupKey.width)
|
||||||
|
} else {
|
||||||
|
MyKeyboard(context, popupKeyboardId, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
mMiniKeyboard!!.setKeyboard(keyboard)
|
||||||
|
mPopupParent = this
|
||||||
|
mMiniKeyboardContainer!!.measure(
|
||||||
|
MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),
|
||||||
|
MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST)
|
||||||
|
)
|
||||||
|
mMiniKeyboardCache[popupKey] = mMiniKeyboardContainer
|
||||||
|
} else {
|
||||||
|
mMiniKeyboard = mMiniKeyboardContainer!!.findViewById<View>(R.id.mini_keyboard_view) as MyKeyboardView
|
||||||
|
}
|
||||||
|
|
||||||
|
getLocationInWindow(mCoordinates)
|
||||||
|
mPopupX = popupKey.x
|
||||||
|
mPopupY = popupKey.y
|
||||||
|
|
||||||
|
val widthToUse = mMiniKeyboardContainer!!.measuredWidth - (popupKey.popupCharacters!!.length / 2) * popupKey.width
|
||||||
|
mPopupX = mPopupX + popupKey.width - widthToUse
|
||||||
|
mPopupY -= mMiniKeyboardContainer!!.measuredHeight
|
||||||
|
val x = mPopupX + mCoordinates[0]
|
||||||
|
val y = mPopupY + mCoordinates[1]
|
||||||
|
val xOffset = Math.max(0, x)
|
||||||
|
mMiniKeyboard!!.setPopupOffset(xOffset, y)
|
||||||
|
|
||||||
|
// make sure we highlight the proper key right after long pressing it, before any ACTION_MOVE event occurs
|
||||||
|
val miniKeyboardX = if (xOffset + mMiniKeyboard!!.measuredWidth <= measuredWidth) {
|
||||||
|
xOffset
|
||||||
|
} else {
|
||||||
|
measuredWidth - mMiniKeyboard!!.measuredWidth
|
||||||
|
}
|
||||||
|
|
||||||
|
val keysCnt = mMiniKeyboard!!.mKeys.size
|
||||||
|
var selectedKeyIndex = Math.floor((me.x - miniKeyboardX) / popupKey.width.toDouble()).toInt()
|
||||||
|
if (keysCnt > MAX_KEYS_PER_MINI_ROW) {
|
||||||
|
selectedKeyIndex += MAX_KEYS_PER_MINI_ROW
|
||||||
|
}
|
||||||
|
selectedKeyIndex = Math.max(0, Math.min(selectedKeyIndex, keysCnt - 1))
|
||||||
|
|
||||||
|
for (i in 0 until keysCnt) {
|
||||||
|
mMiniKeyboard!!.mKeys[i].focused = i == selectedKeyIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
mMiniKeyboardSelectedKeyIndex = selectedKeyIndex
|
||||||
|
mMiniKeyboard!!.invalidateAllKeys()
|
||||||
|
|
||||||
|
val miniShiftStatus = if (isShifted()) SHIFT_ON_PERMANENT else SHIFT_OFF
|
||||||
|
mMiniKeyboard!!.setShifted(miniShiftStatus)
|
||||||
|
mPopupKeyboard.contentView = mMiniKeyboardContainer
|
||||||
|
mPopupKeyboard.width = mMiniKeyboardContainer!!.measuredWidth
|
||||||
|
mPopupKeyboard.height = mMiniKeyboardContainer!!.measuredHeight
|
||||||
|
mPopupKeyboard.showAtLocation(this, Gravity.NO_GRAVITY, x, y)
|
||||||
|
mMiniKeyboardOnScreen = true
|
||||||
|
invalidateAllKeys()
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user