Skip to content

Commit d797ba3

Browse files
feat: added secondary icon attribute to key.
1 parent 35b4c65 commit d797ba3

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/MyKeyboard.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class MyKeyboard {
138138
/** Icon to display instead of a label. Icon takes precedence over a label */
139139
var icon: Drawable? = null
140140

141+
/** Icon to display top left of an icon.*/
142+
var secondaryIcon: Drawable? = null
143+
141144
/** Width of the key, not including the gap */
142145
var width: Int
143146

@@ -204,6 +207,9 @@ class MyKeyboard {
204207
icon = a.getDrawable(R.styleable.MyKeyboard_Key_keyIcon)
205208
icon?.setBounds(0, 0, icon!!.intrinsicWidth, icon!!.intrinsicHeight)
206209

210+
secondaryIcon = a.getDrawable(R.styleable.MyKeyboard_Key_secondaryKeyIcon)
211+
secondaryIcon?.setBounds(0, 0, secondaryIcon!!.intrinsicWidth, secondaryIcon!!.intrinsicHeight)
212+
207213
label = a.getText(R.styleable.MyKeyboard_Key_keyLabel) ?: ""
208214
topSmallNumber = a.getString(R.styleable.MyKeyboard_Key_topSmallNumber) ?: ""
209215

@@ -230,10 +236,12 @@ class MyKeyboard {
230236
fun isInside(x: Int, y: Int): Boolean {
231237
val leftEdge = edgeFlags and EDGE_LEFT > 0
232238
val rightEdge = edgeFlags and EDGE_RIGHT > 0
233-
return ((x >= this.x || leftEdge && x <= this.x + width)
234-
&& (x < this.x + width || rightEdge && x >= this.x)
235-
&& (y >= this.y && y <= this.y + height)
236-
&& (y < this.y + height && y >= this.y))
239+
return (
240+
(x >= this.x || leftEdge && x <= this.x + width) &&
241+
(x < this.x + width || rightEdge && x >= this.x) &&
242+
(y >= this.y && y <= this.y + height) &&
243+
(y < this.y + height && y >= this.y)
244+
)
237245
}
238246
}
239247

@@ -249,7 +257,7 @@ class MyKeyboard {
249257
mDefaultHorizontalGap = 0
250258
mDefaultWidth = mDisplayWidth / 10
251259
mDefaultHeight = mDefaultWidth
252-
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier);
260+
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier)
253261
mKeys = ArrayList()
254262
mEnterKeyType = enterKeyType
255263
loadKeyboard(context, context.resources.getXml(xmlLayoutResId))
@@ -273,7 +281,7 @@ class MyKeyboard {
273281
row.defaultHeight = mDefaultHeight
274282
row.defaultWidth = keyWidth
275283
row.defaultHorizontalGap = mDefaultHorizontalGap
276-
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier);
284+
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier)
277285

278286
characters.forEachIndexed { index, character ->
279287
val key = Key(row)
@@ -386,7 +394,7 @@ class MyKeyboard {
386394
}
387395

388396
private fun getKeyboardHeightMultiplier(multiplierType: Int): Float {
389-
return when(multiplierType) {
397+
return when (multiplierType) {
390398
KEYBOARD_HEIGHT_MULTIPLIER_SMALL -> 1.0F
391399
KEYBOARD_HEIGHT_MULTIPLIER_MEDIUM -> 1.2F
392400
KEYBOARD_HEIGHT_MULTIPLIER_LARGE -> 1.4F

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,13 +612,33 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
612612
} else if (code == KEYCODE_DELETE || code == KEYCODE_SHIFT || code == KEYCODE_EMOJI) {
613613
key.icon!!.applyColorFilter(mTextColor)
614614
}
615+
val keyIcon = key.icon!!
616+
val secondaryIcon = key.secondaryIcon
615617

616-
val drawableX = (key.width - key.icon!!.intrinsicWidth) / 2
617-
val drawableY = (key.height - key.icon!!.intrinsicHeight) / 2
618-
canvas.translate(drawableX.toFloat(), drawableY.toFloat())
619-
key.icon!!.setBounds(0, 0, key.icon!!.intrinsicWidth, key.icon!!.intrinsicHeight)
620-
key.icon!!.draw(canvas)
621-
canvas.translate(-drawableX.toFloat(), -drawableY.toFloat())
618+
if (secondaryIcon != null) {
619+
val keyIconWidth = keyIcon.intrinsicWidth * 1
620+
val keyIconHeight = keyIcon.intrinsicWidth * 1
621+
val secondaryIconWidth = (secondaryIcon.intrinsicWidth * 0.6).toInt()
622+
val secondaryIconHeight = (secondaryIcon.intrinsicHeight * 0.6).toInt()
623+
624+
secondaryIcon.setBounds(key.width - secondaryIconWidth, 0, key.width, secondaryIconHeight)
625+
secondaryIcon.draw(canvas)
626+
627+
val drawableX = (key.width - keyIconWidth) / 2
628+
val drawableY = (key.height - keyIconHeight) / 2
629+
canvas.translate(drawableX.toFloat(), drawableY.toFloat() / 1.5f)
630+
631+
keyIcon.setBounds(0, 0, keyIconWidth, keyIconHeight)
632+
keyIcon.draw(canvas)
633+
canvas.translate(-drawableX.toFloat(), -drawableY.toFloat())
634+
} else {
635+
val drawableX = (key.width - keyIcon.intrinsicWidth) / 2
636+
val drawableY = (key.height - keyIcon.intrinsicHeight) / 2
637+
canvas.translate(drawableX.toFloat(), drawableY.toFloat())
638+
keyIcon.setBounds(0, 0, keyIcon.intrinsicWidth, keyIcon.intrinsicHeight)
639+
keyIcon.draw(canvas)
640+
canvas.translate(-drawableX.toFloat(), -drawableY.toFloat())
641+
}
622642
}
623643
canvas.translate(-key.x.toFloat(), -key.y.toFloat())
624644
}

app/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
<attr name="keyLabel" format="string" />
3434
<!-- The icon to display on the key instead of the label. -->
3535
<attr name="keyIcon" format="reference" />
36+
<!-- The icon to display on the top left of a key with icon. -->
37+
<attr name="secondaryKeyIcon" format="reference" />
3638
<!-- Top small number shown above letters of the first row. -->
3739
<attr name="topSmallNumber" format="string" />
3840
</declare-styleable>

0 commit comments

Comments
 (0)