Skip to content

Commit d3bf5f0

Browse files
committed
Implement key borders
1 parent 836b9a6 commit d3bf5f0

File tree

5 files changed

+156
-54
lines changed

5 files changed

+156
-54
lines changed

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

Lines changed: 109 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import android.content.Context
99
import android.content.Intent
1010
import android.graphics.*
1111
import android.graphics.Paint.Align
12-
import android.graphics.drawable.ColorDrawable
13-
import android.graphics.drawable.Drawable
14-
import android.graphics.drawable.LayerDrawable
15-
import android.graphics.drawable.RippleDrawable
12+
import android.graphics.drawable.*
1613
import android.os.Handler
1714
import android.os.Looper
1815
import android.os.Message
@@ -280,23 +277,9 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
280277
mPrimaryColor = context.getProperPrimaryColor()
281278
val strokeColor = context.getStrokeColor()
282279

283-
val toolbarColor = if (context.config.isUsingSystemTheme) {
284-
resources.getColor(R.color.you_keyboard_toolbar_color, context.theme)
285-
} else {
286-
mBackgroundColor.darkenColor()
287-
}
288-
289-
val darkerColor = if (context.config.isUsingSystemTheme) {
290-
resources.getColor(R.color.you_keyboard_background_color, context.theme)
291-
} else {
292-
mBackgroundColor.darkenColor(2)
293-
}
294-
295-
val miniKeyboardBackgroundColor = if (context.config.isUsingSystemTheme) {
296-
resources.getColor(R.color.you_keyboard_toolbar_color, context.theme)
297-
} else {
298-
mBackgroundColor.darkenColor(4)
299-
}
280+
val toolbarColor = getToolbarColor()
281+
val darkerColor = getKeyboardBackgroundColor()
282+
val miniKeyboardBackgroundColor = getToolbarColor(4)
300283

301284
if (changedView == mini_keyboard_view) {
302285
val previewBackground = background as LayerDrawable
@@ -563,36 +546,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
563546
for (i in 0 until keyCount) {
564547
val key = keys[i]
565548
val code = key.code
566-
var keyBackground = mKeyBackground
567-
if (code == KEYCODE_SPACE) {
568-
keyBackground = if (context.config.isUsingSystemTheme) {
569-
resources.getDrawable(R.drawable.keyboard_space_background_material, context.theme)
570-
} else {
571-
resources.getDrawable(R.drawable.keyboard_space_background, context.theme)
572-
}
573-
} else if (code == KEYCODE_ENTER) {
574-
keyBackground = resources.getDrawable(R.drawable.keyboard_enter_background, context.theme)
575-
}
549+
setupKeyBackground(key, code, canvas)
576550

577551
// Switch the character to uppercase if shift is pressed
578552
val label = adjustCase(key.label)?.toString()
579-
val bounds = keyBackground!!.bounds
580-
if (key.width != bounds.right || key.height != bounds.bottom) {
581-
keyBackground.setBounds(0, 0, key.width, key.height)
582-
}
583-
584-
keyBackground.state = when {
585-
key.pressed -> intArrayOf(android.R.attr.state_pressed)
586-
key.focused -> intArrayOf(android.R.attr.state_focused)
587-
else -> intArrayOf()
588-
}
589-
590-
if (key.focused || code == KEYCODE_ENTER) {
591-
keyBackground.applyColorFilter(mPrimaryColor)
592-
}
593-
594-
canvas.translate(key.x.toFloat(), key.y.toFloat())
595-
keyBackground.draw(canvas)
596553
if (label?.isNotEmpty() == true) {
597554
// For characters, use large font. For labels like "Done", use small font.
598555
if (label.length > 1) {
@@ -656,6 +613,57 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
656613
mDirtyRect.setEmpty()
657614
}
658615

616+
private fun setupKeyBackground(key: MyKeyboard.Key, keyCode: Int, canvas: Canvas) {
617+
val showKeyBorders = context.config.showKeyBorders
618+
val drawableId = when (keyCode) {
619+
KEYCODE_SPACE -> if (context.config.isUsingSystemTheme) {
620+
if (showKeyBorders) {
621+
R.drawable.keyboard_space_background_material_outlined
622+
} else {
623+
R.drawable.keyboard_space_background_material
624+
}
625+
} else {
626+
if (showKeyBorders) {
627+
R.drawable.keyboard_key_selector_outlined
628+
} else {
629+
R.drawable.keyboard_space_background
630+
}
631+
}
632+
KEYCODE_ENTER -> if (showKeyBorders) {
633+
R.drawable.keyboard_enter_background_outlined
634+
} else {
635+
R.drawable.keyboard_enter_background
636+
}
637+
else -> if (showKeyBorders) {
638+
R.drawable.keyboard_key_selector_outlined
639+
} else {
640+
R.drawable.keyboard_key_selector
641+
}
642+
}
643+
val keyBackground = resources.getDrawable(drawableId, context.theme)
644+
645+
val bounds = keyBackground!!.bounds
646+
if (key.width != bounds.right || key.height != bounds.bottom) {
647+
keyBackground.setBounds(0, 0, key.width, key.height)
648+
}
649+
650+
keyBackground.state = when {
651+
key.pressed -> intArrayOf(android.R.attr.state_pressed)
652+
key.focused -> intArrayOf(android.R.attr.state_focused)
653+
else -> intArrayOf()
654+
}
655+
656+
if (key.focused || keyCode == KEYCODE_ENTER) {
657+
keyBackground.applyColorFilter(mPrimaryColor)
658+
} else if (showKeyBorders && drawableId == R.drawable.keyboard_key_selector_outlined) {
659+
val keyColor = getKeyColor(key.pressed)
660+
keyBackground.applyColorFilter(keyColor)
661+
}
662+
663+
canvas.translate(key.x.toFloat(), key.y.toFloat())
664+
keyBackground.draw(canvas)
665+
}
666+
659667
private fun handleClipboard() {
660668
if (mToolbarHolder != null && mPopupParent.id != R.id.mini_keyboard_view) {
661669
val clipboardContent = context.getCurrentClip()
@@ -767,7 +775,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
767775
val newKey = keys[mCurrentKeyIndex]
768776

769777
val code = newKey.code
770-
if (code == KEYCODE_SHIFT || code == KEYCODE_MODE_CHANGE || code == KEYCODE_DELETE || code == KEYCODE_ENTER || code == KEYCODE_SPACE) {
778+
if (context.config.showKeyBorders || (code == KEYCODE_SHIFT || code == KEYCODE_MODE_CHANGE || code == KEYCODE_DELETE || code == KEYCODE_ENTER || code == KEYCODE_SPACE)) {
771779
newKey.pressed = true
772780
}
773781

@@ -819,11 +827,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
819827
}
820828
}
821829

822-
val previewBackgroundColor = if (context.config.isUsingSystemTheme) {
823-
resources.getColor(R.color.you_keyboard_toolbar_color, context.theme)
824-
} else {
825-
mBackgroundColor.darkenColor(4)
826-
}
830+
val previewBackgroundColor = getToolbarColor(4)
827831

828832
val previewBackground = mPreviewText!!.background as LayerDrawable
829833
previewBackground.findDrawableByLayerId(R.id.button_background_shape).applyColorFilter(previewBackgroundColor)
@@ -1529,4 +1533,55 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
15291533
invalidateAllKeys()
15301534
}
15311535
}
1536+
1537+
private fun maybeDarkenColor(color: Int, factor: Int): Int {
1538+
// use darker background color when key borders are enabled
1539+
if (context.config.showKeyBorders && !context.isUsingSystemDarkTheme()) {
1540+
val darkerColor = color.darkenColor(factor)
1541+
return if (darkerColor == Color.WHITE) {
1542+
resources.getColor(R.color.md_grey_200, context.theme).darkenColor(4)
1543+
} else {
1544+
darkerColor
1545+
}
1546+
}
1547+
return color
1548+
}
1549+
1550+
private fun getToolbarColor(factor: Int = 8): Int {
1551+
val color = if (context.config.isUsingSystemTheme) {
1552+
resources.getColor(R.color.you_keyboard_toolbar_color, context.theme)
1553+
} else {
1554+
mBackgroundColor.darkenColor(factor)
1555+
}
1556+
return maybeDarkenColor(color, 2)
1557+
}
1558+
1559+
private fun getKeyboardBackgroundColor(): Int {
1560+
val color = if (context.config.isUsingSystemTheme) {
1561+
resources.getColor(R.color.you_keyboard_background_color, context.theme)
1562+
} else {
1563+
mBackgroundColor.darkenColor(2)
1564+
}
1565+
return maybeDarkenColor(color, 6)
1566+
}
1567+
1568+
private fun getKeyColor(pressed: Boolean): Int {
1569+
val backgroundColor = getKeyboardBackgroundColor()
1570+
val lighterColor = backgroundColor.lightenColor()
1571+
val keyColor = if (context.config.isUsingSystemTheme) {
1572+
lighterColor
1573+
} else {
1574+
if (backgroundColor == Color.BLACK) {
1575+
backgroundColor.getContrastColor().adjustAlpha(0.1f)
1576+
} else {
1577+
lighterColor
1578+
}
1579+
}
1580+
1581+
return if (pressed) {
1582+
keyColor.adjustAlpha(0.2f)
1583+
} else {
1584+
keyColor
1585+
}
1586+
}
15321587
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:id="@+id/key_normal" android:bottom="@dimen/small_margin" android:left="@dimen/tiny_margin" android:right="@dimen/tiny_margin" android:top="@dimen/small_margin">
4+
<shape android:shape="rectangle">
5+
<solid android:color="@color/white"/>
6+
<corners android:radius="@dimen/medium_margin"/>
7+
</shape>
8+
</item>
9+
</layer-list>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:id="@+id/button_background_shape" android:bottom="@dimen/small_margin" android:left="@dimen/tiny_margin" android:right="@dimen/tiny_margin" android:top="@dimen/small_margin">
4+
<shape android:shape="rectangle">
5+
<solid android:color="@color/color_primary"/>
6+
<corners android:radius="@dimen/medium_margin"/>
7+
</shape>
8+
</item>
9+
</layer-list>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<!--used after long pressing a key to highlight the currently selected key alternative on minikeyboard-->
4+
<item android:drawable="@drawable/minikeyboard_selected_background" android:state_focused="true"/>
5+
<item android:drawable="@drawable/key_background_outlined"/>
6+
</selector>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:state_pressed="true">
4+
<layer-list>
5+
<item android:id="@+id/space_pressed" android:bottom="@dimen/small_margin" android:left="@dimen/tiny_margin" android:right="@dimen/tiny_margin" android:state_pressed="true" android:top="@dimen/small_margin">
6+
<shape android:shape="rectangle">
7+
<solid android:color="@android:color/system_accent2_600"/>
8+
<corners android:radius="@dimen/medium_margin"/>
9+
</shape>
10+
</item>
11+
</layer-list>
12+
</item>
13+
<item>
14+
<layer-list>
15+
<item android:id="@+id/space_normal" android:bottom="@dimen/small_margin" android:left="@dimen/tiny_margin" android:right="@dimen/tiny_margin" android:state_pressed="true" android:top="@dimen/small_margin">
16+
<shape android:shape="rectangle">
17+
<solid android:color="@android:color/system_accent2_700"/>
18+
<corners android:radius="@dimen/medium_margin"/>
19+
</shape>
20+
</item>
21+
</layer-list>
22+
</item>
23+
</selector>

0 commit comments

Comments
 (0)