From 7e5ea76a93b6222385426db19f35aaed99dd3609 Mon Sep 17 00:00:00 2001 From: angrezichatterbox Date: Mon, 1 Jul 2024 14:30:40 +0530 Subject: [PATCH 1/2] feat:Added Shadow to the keys and reduced border radius --- .../kotlin/org/scribe/views/MyKeyboardView.kt | 76 ++++++++----------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/app/src/main/kotlin/org/scribe/views/MyKeyboardView.kt b/app/src/main/kotlin/org/scribe/views/MyKeyboardView.kt index f64debd8..e3904df9 100644 --- a/app/src/main/kotlin/org/scribe/views/MyKeyboardView.kt +++ b/app/src/main/kotlin/org/scribe/views/MyKeyboardView.kt @@ -29,7 +29,6 @@ import org.scribe.helpers.MyKeyboard.Companion.KEYCODE_MODE_CHANGE import org.scribe.helpers.MyKeyboard.Companion.KEYCODE_SHIFT import org.scribe.helpers.MyKeyboard.Companion.KEYCODE_SPACE import java.util.* -import org.scribe.services.SimpleKeyboardIME.* @SuppressLint("UseCompatLoadingForDrawables") class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int = 0) : @@ -68,10 +67,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut * @param text the string to be displayed. */ fun onText(text: String) - - fun hasTextBeforeCursor(): Boolean - - fun commitPeriodAfterSpace() } private var mKeyboard: MyKeyboard? = null @@ -161,9 +156,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut private var mHandler: Handler? = null - - private var lastSpaceBarTapTime = 0L - companion object { private const val NOT_A_KEY = -1 private val LONG_PRESSABLE_STATE_SET = intArrayOf(R.attr.state_long_pressable) @@ -175,8 +167,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut private const val REPEAT_INTERVAL = 50 // ~20 keys per second private const val REPEAT_START_DELAY = 400 private val LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout() - private const val DOUBLE_TAP_DELAY = 300L - } init { @@ -187,7 +177,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut try { for (i in 0 until indexCnt) { - val attr = attributes.getIndex(i) when (attr) { R.styleable.MyKeyboardView_keyTextSize -> mKeyTextSize = attributes.getDimensionPixelSize(attr, 18) @@ -231,7 +220,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut mTopSmallNumberMarginHeight = resources.getDimension(R.dimen.top_small_number_margin_height) } - @SuppressLint("HandlerLeak") override fun onAttachedToWindow() { super.onAttachedToWindow() @@ -344,10 +332,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut } } - - - - /** * Sets the state of the shift key of the keyboard, if any. * @param shifted whether or not to enable the state of the shift key @@ -433,11 +417,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut private fun onBufferDraw() { val keyMargin = 8 val shadowOffset = 3 - val shadowPaint = Paint().apply { - style = Paint.Style.STROKE - strokeWidth = 2f - color = Color.BLACK - } if (mBuffer == null || mKeyboardChanged) { if (mBuffer == null || mKeyboardChanged && (mBuffer!!.width != width || mBuffer!!.height != height)) { // Make sure our bitmap is at least 1x1 @@ -467,10 +446,18 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut typeface = Typeface.DEFAULT } - val borderPaint = Paint().apply { - style = Paint.Style.STROKE - strokeWidth = 0.5f - color = Color.BLACK + + + + val keyBackgroundPaint = Paint().apply { + color = Color.WHITE + style = Paint.Style.FILL + } + + val shadowPaint = Paint().apply { + color = Color.GRAY + alpha = 100 + style = Paint.Style.FILL } canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) @@ -480,6 +467,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut val key = keys[i] val code = key.code var keyBackground = mKeyBackground + + // if (code == KEYCODE_ENTER) { // keyBackground = resources.getDrawable(R.drawable.keyboard_enter_background, context.theme) // } else if (code == KEYCODE_SPACE) { @@ -505,7 +494,15 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut val padding = 5 - val rectRadius = 25f + val rectRadius = 15f + val shadowOffsetY = 9f + + val shadowRect = RectF( + (key.x + keyMargin + padding).toFloat(), + (key.y + keyMargin + padding + shadowOffsetY).toFloat(), + (key.x + key.width - keyMargin - padding).toFloat(), + (key.y + key.height - keyMargin - padding + shadowOffsetY).toFloat() + ) val keyRect = RectF( (key.x + keyMargin - shadowOffset + padding).toFloat(), @@ -513,7 +510,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut (key.x + key.width - keyMargin + shadowOffset - padding).toFloat(), (key.y + key.height - keyMargin + shadowOffset - padding).toFloat() ) - canvas.drawRoundRect(keyRect, rectRadius, rectRadius, shadowPaint) + canvas.drawRoundRect(shadowRect, rectRadius, rectRadius, shadowPaint) + + canvas.drawRoundRect(keyRect, rectRadius, rectRadius, keyBackgroundPaint) + keyBackground!!.setBounds( keyMargin, keyMargin, @@ -533,6 +533,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut else -> intArrayOf() } + + if (key.focused || code == KEYCODE_ENTER) { keyBackground.applyColorFilter(mPrimaryColor) } else { @@ -541,6 +543,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut canvas.translate(key.x.toFloat(), key.y.toFloat()) keyBackground.draw(canvas) + + paint.color = Color.BLACK if (label?.isNotEmpty() == true) { // For characters, use large font. For labels like "Done", use small font. if (label.length > 1) { @@ -858,14 +862,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut override fun onText(text: String) { mOnKeyboardActionListener!!.onText(text) } - - override fun hasTextBeforeCursor(): Boolean { - return mOnKeyboardActionListener!!.hasTextBeforeCursor() - } - - override fun commitPeriodAfterSpace() { - mOnKeyboardActionListener!!.commitPeriodAfterSpace() - } } val keyboard = if (popupKey.popupCharacters != null) { @@ -1178,15 +1174,9 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut } if (mKeys.getOrNull(mCurrentKey)?.code == KEYCODE_SPACE && !mIsLongPressingSpace) { - val currentTime = System.currentTimeMillis() - if (currentTime - lastSpaceBarTapTime < DOUBLE_TAP_DELAY + 200 && context.config.periodOnDoubleTap && mOnKeyboardActionListener!!.hasTextBeforeCursor() ) { - mOnKeyboardActionListener!!.commitPeriodAfterSpace() - } else { - detectAndSendKey(mCurrentKey, touchX, touchY, eventTime) - } - lastSpaceBarTapTime = currentTime - + detectAndSendKey(mCurrentKey, touchX, touchY, eventTime) } + invalidateKey(keyIndex) mRepeatKeyIndex = NOT_A_KEY mOnKeyboardActionListener!!.onActionUp() From 88fa9ed8a5a4b879d45281e9ea39c0e2c797004f Mon Sep 17 00:00:00 2001 From: angrezichatterbox Date: Tue, 2 Jul 2024 10:14:27 +0530 Subject: [PATCH 2/2] fix:Implemented the function present in SimpleKeyboardIME.kt --- .../kotlin/org/scribe/views/MyKeyboardView.kt | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/org/scribe/views/MyKeyboardView.kt b/app/src/main/kotlin/org/scribe/views/MyKeyboardView.kt index e3904df9..f0989be7 100644 --- a/app/src/main/kotlin/org/scribe/views/MyKeyboardView.kt +++ b/app/src/main/kotlin/org/scribe/views/MyKeyboardView.kt @@ -29,6 +29,7 @@ import org.scribe.helpers.MyKeyboard.Companion.KEYCODE_MODE_CHANGE import org.scribe.helpers.MyKeyboard.Companion.KEYCODE_SHIFT import org.scribe.helpers.MyKeyboard.Companion.KEYCODE_SPACE import java.util.* +import org.scribe.services.SimpleKeyboardIME.* @SuppressLint("UseCompatLoadingForDrawables") class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int = 0) : @@ -67,6 +68,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut * @param text the string to be displayed. */ fun onText(text: String) + + fun hasTextBeforeCursor(): Boolean + + fun commitPeriodAfterSpace() } private var mKeyboard: MyKeyboard? = null @@ -156,6 +161,9 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut private var mHandler: Handler? = null + + private var lastSpaceBarTapTime = 0L + companion object { private const val NOT_A_KEY = -1 private val LONG_PRESSABLE_STATE_SET = intArrayOf(R.attr.state_long_pressable) @@ -167,6 +175,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut private const val REPEAT_INTERVAL = 50 // ~20 keys per second private const val REPEAT_START_DELAY = 400 private val LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout() + private const val DOUBLE_TAP_DELAY = 300L + } init { @@ -177,6 +187,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut try { for (i in 0 until indexCnt) { + val attr = attributes.getIndex(i) when (attr) { R.styleable.MyKeyboardView_keyTextSize -> mKeyTextSize = attributes.getDimensionPixelSize(attr, 18) @@ -220,6 +231,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut mTopSmallNumberMarginHeight = resources.getDimension(R.dimen.top_small_number_margin_height) } + @SuppressLint("HandlerLeak") override fun onAttachedToWindow() { super.onAttachedToWindow() @@ -332,6 +344,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut } } + + + + /** * Sets the state of the shift key of the keyboard, if any. * @param shifted whether or not to enable the state of the shift key @@ -467,8 +483,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut val key = keys[i] val code = key.code var keyBackground = mKeyBackground - - // if (code == KEYCODE_ENTER) { // keyBackground = resources.getDrawable(R.drawable.keyboard_enter_background, context.theme) // } else if (code == KEYCODE_SPACE) { @@ -533,8 +547,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut else -> intArrayOf() } - - if (key.focused || code == KEYCODE_ENTER) { keyBackground.applyColorFilter(mPrimaryColor) } else { @@ -543,8 +555,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut canvas.translate(key.x.toFloat(), key.y.toFloat()) keyBackground.draw(canvas) - - paint.color = Color.BLACK if (label?.isNotEmpty() == true) { // For characters, use large font. For labels like "Done", use small font. if (label.length > 1) { @@ -862,6 +872,14 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut override fun onText(text: String) { mOnKeyboardActionListener!!.onText(text) } + + override fun hasTextBeforeCursor(): Boolean { + return mOnKeyboardActionListener!!.hasTextBeforeCursor() + } + + override fun commitPeriodAfterSpace() { + mOnKeyboardActionListener!!.commitPeriodAfterSpace() + } } val keyboard = if (popupKey.popupCharacters != null) { @@ -1174,9 +1192,15 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut } if (mKeys.getOrNull(mCurrentKey)?.code == KEYCODE_SPACE && !mIsLongPressingSpace) { - detectAndSendKey(mCurrentKey, touchX, touchY, eventTime) - } + val currentTime = System.currentTimeMillis() + if (currentTime - lastSpaceBarTapTime < DOUBLE_TAP_DELAY + 200 && context.config.periodOnDoubleTap && mOnKeyboardActionListener!!.hasTextBeforeCursor() ) { + mOnKeyboardActionListener!!.commitPeriodAfterSpace() + } else { + detectAndSendKey(mCurrentKey, touchX, touchY, eventTime) + } + lastSpaceBarTapTime = currentTime + } invalidateKey(keyIndex) mRepeatKeyIndex = NOT_A_KEY mOnKeyboardActionListener!!.onActionUp()