0
0
mirror of https://github.com/florisboard/florisboard.git synced 2024-09-19 19:42:20 +02:00

Implement popup and update Ui redraw logic

This commit is contained in:
Patrick Goldinger 2020-04-11 18:07:57 +02:00
parent cc2828b600
commit d2fc8c5cf1
7 changed files with 79 additions and 36 deletions

View File

@ -10,6 +10,7 @@ import android.util.TypedValue
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat.getDrawable
import com.google.android.flexbox.FlexboxLayout
@ -29,9 +30,9 @@ class CustomKey : androidx.appcompat.widget.AppCompatButton, View.OnTouchListene
private val osHandler = Handler()
private var osTimer: Timer? = null
var cmd: Int?
set(v) { updateUI(); field = v }
set(v) { field = v; updateUI() }
var code: Int?
set(v) { updateUI(); field = v }
set(v) { field = v; updateUI() }
var keyboard: CustomKeyboard? = null
var isRepeatable: Boolean = false
var popupCodes = listOf<Int>()
@ -56,21 +57,7 @@ class CustomKey : androidx.appcompat.widget.AppCompatButton, View.OnTouchListene
}
private fun updateUI() {
val label = (code?.toChar() ?: "").toString()
super.setText(when {
keyboard?.caps ?: false -> label.toUpperCase(Locale.getDefault())
else -> label
})
val drawable: Drawable? = when (cmd) {
KeyCodes.ENTER -> getDrawable(context, R.drawable.ic_arrow_forward)
KeyCodes.DELETE -> getDrawable(context, R.drawable.ic_backspace)
KeyCodes.LANGUAGE_SWITCH -> getDrawable(context, R.drawable.ic_language)
KeyCodes.KEYBOARD_CYCLE -> getDrawable(context, R.drawable.ic_language)
KeyCodes.SHIFT -> getDrawable(context, R.drawable.ic_capslock)
else -> null
}
drawable?.setBounds(0, 0, resources.getDimension(R.dimen.key_width).toInt(), resources.getDimension(R.dimen.key_width).toInt())
super.setCompoundDrawables(null, drawable, null, null)
super.setText(getLabel())
when (cmd) {
KeyCodes.DELETE -> (layoutParams as FlexboxLayout.LayoutParams).flexGrow = 1.0f
KeyCodes.ENTER -> (layoutParams as FlexboxLayout.LayoutParams).flexGrow = 1.0f
@ -81,6 +68,20 @@ class CustomKey : androidx.appcompat.widget.AppCompatButton, View.OnTouchListene
super.setText("?123")
}
}
val drawable: Drawable? = when (cmd) {
KeyCodes.ENTER -> getDrawable(context, R.drawable.ic_arrow_forward)
KeyCodes.DELETE -> getDrawable(context, R.drawable.ic_backspace)
KeyCodes.LANGUAGE_SWITCH -> getDrawable(context, R.drawable.ic_language)
KeyCodes.SHIFT -> getDrawable(context, R.drawable.ic_capslock)
else -> null
}
drawable?.setBounds(0, 0, resources.getDimension(R.dimen.key_width).toInt(), resources.getDimension(R.dimen.key_width).toInt())
if (drawable != null) {
overlay.clear()
overlay.add(drawable)
}
requestLayout()
invalidate()
}
/**
@ -95,7 +96,19 @@ class CustomKey : androidx.appcompat.widget.AppCompatButton, View.OnTouchListene
}
private fun showPopup() {
val popupView = View.inflate(context, R.layout.key_popup, null)
if (code == 32 || code == null) {
return
}
val keyPopupWidth = resources.getDimension(R.dimen.key_popup_width).toInt()
val keyPopupHeight = resources.getDimension(R.dimen.key_popup_height).toInt()
val keyWidth = resources.getDimension((R.dimen.key_width)).toInt()
val keyHeight = resources.getDimension((R.dimen.key_height)).toInt()
val keyboardRowMarginV = resources.getDimension((R.dimen.keyboard_row_marginV)).toInt()
val popupView = View.inflate(context, R.layout.key_popup, null) as LinearLayout
popupView.measure(MeasureSpec.makeMeasureSpec(keyPopupWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(keyPopupHeight, MeasureSpec.EXACTLY))
popupView.layout(0, 0, keyPopupWidth, keyPopupHeight)
popupView.x = x - ((keyPopupWidth - keyWidth).toFloat() / 2.0f)
popupView.y = (parent as CustomKeyboardRow).y - (keyPopupHeight - keyHeight - keyboardRowMarginV)
popupView.findViewById<TextView>(R.id.key_popup_text).text = getLabel()
popupView.findViewById<ImageView>(R.id.key_popup_threedots).visibility = when {
popupCodes.isEmpty() -> View.INVISIBLE
@ -104,10 +117,13 @@ class CustomKey : androidx.appcompat.widget.AppCompatButton, View.OnTouchListene
keyboard?.overlay?.add(popupView)
}
private fun hidePopup() {
if (code == 32 || code == null) {
return
}
keyboard?.overlay?.clear()
}
fun getColorFromAttr(
private fun getColorFromAttr(
attrColor: Int,
typedValue: TypedValue = TypedValue(),
resolveRefs: Boolean = true
@ -120,7 +136,7 @@ class CustomKey : androidx.appcompat.widget.AppCompatButton, View.OnTouchListene
override fun onTouch(v: View, event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN) {
isKeyPressed = true
//showPopup()
showPopup()
if (cmd != null && isRepeatable) {
osTimer = Timer()
osTimer?.scheduleAtFixedRate(object : TimerTask() {
@ -131,7 +147,7 @@ class CustomKey : androidx.appcompat.widget.AppCompatButton, View.OnTouchListene
osTimer = null
}
}
}, 500, 100)
}, 500, 50)
}
osHandler.postDelayed({
//
@ -142,14 +158,21 @@ class CustomKey : androidx.appcompat.widget.AppCompatButton, View.OnTouchListene
osHandler.removeCallbacksAndMessages(null)
osTimer?.cancel()
osTimer = null
//hidePopup()
hidePopup()
keyboard?.onKeyClicked(code ?: cmd ?: 0)
}
return true;
return true
}
override fun onDraw(canvas: Canvas?) {
updateUI()
super.onDraw(canvas)
val keyMarginH = resources.getDimension((R.dimen.key_marginH)).toInt()
val keyboardWidth = (parent as FlexboxLayout).measuredWidth
val keyboardRowMarginH = resources.getDimension((R.dimen.keyboard_row_marginH)).toInt()
val keyWidthPlusMargin = (keyboardWidth - 2 * keyboardRowMarginH) / 10
val keyWidth = keyWidthPlusMargin - 2 * keyMarginH
layoutParams = layoutParams.apply {
width = keyWidth
}
}
}

View File

@ -68,11 +68,11 @@ class FlorisBoard : InputMethodService() {
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
rowViewLP.setMargins(
0, resources.getDimension(R.dimen.keyboard_row_marginV).toInt(),
0, resources.getDimension(R.dimen.keyboard_row_marginV).toInt()
)
rowView.layoutParams = rowViewLP
rowView.setPadding(
resources.getDimension(R.dimen.keyboard_row_marginH).toInt(), resources.getDimension(R.dimen.keyboard_row_marginV).toInt(),
resources.getDimension(R.dimen.keyboard_row_marginH).toInt(), resources.getDimension(R.dimen.keyboard_row_marginV).toInt()
)
for (key in row) {
val keyView = CustomKey(context)
val keyViewLP = FlexboxLayout.LayoutParams(

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#25000000" />
<corners android:radius="@dimen/key_borderRadius" />
</shape>
</item>
<item android:drawable="@drawable/shape_rect_rounded"/>
</layer-list>

View File

@ -4,15 +4,17 @@
android:layout_width="@dimen/key_popup_width"
android:layout_height="@dimen/key_popup_height"
android:orientation="vertical"
android:background="@drawable/shape_rect_rounded"
android:backgroundTint="?key_popup_bgColor">
android:background="@drawable/key_popup_bgshape"
android:backgroundTint="?attr/key_popup_bgColor"
android:backgroundTintMode="multiply">
<TextView
android:id="@+id/key_popup_text"
android:layout_width="match_parent"
android:layout_height="@dimen/key_height"
android:gravity="center"
android:textColor="?attr/key_popup_fgColor" />
android:textColor="?attr/key_popup_fgColor"
android:textSize="@dimen/key_popup_textSize"/>
<ImageView
android:id="@+id/key_popup_threedots"

View File

@ -1,12 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="key_width">30dp</dimen>
<dimen name="key_width">33dp</dimen>
<dimen name="key_height">38dp</dimen>
<dimen name="key_marginH">4dp</dimen>
<dimen name="key_marginH">2dp</dimen>
<dimen name="key_borderRadius">6dp</dimen>
<dimen name="key_textSize">18sp</dimen>
<dimen name="key_popup_width">34dp</dimen>
<dimen name="key_popup_width">35dp</dimen>
<dimen name="key_popup_height">76dp</dimen>
<dimen name="key_popup_textSize">28sp</dimen>
<dimen name="keyboard_row_marginV">4dp</dimen>
<dimen name="keyboard_row_marginH">2dp</dimen>
<dimen name="keyboard_row_marginV">5dp</dimen>
</resources>

View File

@ -9,6 +9,7 @@
<item name="android:textAllCaps">false</item>
<item name="android:background">@drawable/shape_rect_rounded</item>
<item name="android:backgroundTint">?attr/key_bgColor</item>
<item name="android:textSize">@dimen/key_textSize</item>
</style>
<style name="CustomKeyboardStyle">
@ -19,6 +20,7 @@
<style name="CustomKeyboardStyleRow">
<item name="android:layout_marginTop">4dp</item>
<item name="android:layout_marginBottom">4dp</item>
<item name="flexDirection">row</item>
<item name="justifyContent">center</item>
</style>

View File

@ -15,7 +15,8 @@
<item name="key_bgColorPressed">#f0f0f0</item>
<item name="key_fgColor">#000</item>
<item name="key_popup_bgColor">#fff</item>
<item name="key_popup_bgColor">#efefef</item>
<item name="key_popup_fgColor">#000</item>
<item name="keyboard_bgColor">#dfdfdf</item>
</style>