Skip to content

Commit

Permalink
feat:Added Shadow to the keys and reduced border radius
Browse files Browse the repository at this point in the history
  • Loading branch information
angrezichatterbox committed Jul 1, 2024
1 parent 4b546f8 commit 7e5ea76
Showing 1 changed file with 33 additions and 43 deletions.
76 changes: 33 additions & 43 deletions app/src/main/kotlin/org/scribe/views/MyKeyboardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) :
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -505,15 +494,26 @@ 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(),
(key.y + keyMargin - shadowOffset + padding).toFloat(),
(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,
Expand All @@ -533,6 +533,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
else -> intArrayOf()
}



if (key.focused || code == KEYCODE_ENTER) {
keyBackground.applyColorFilter(mPrimaryColor)
} else {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7e5ea76

Please sign in to comment.