diff --git a/app/src/main/java/dev/patrickgold/florisboard/ime/key/KeyView.kt b/app/src/main/java/dev/patrickgold/florisboard/ime/key/KeyView.kt index 0bc67396..fe2e88e6 100644 --- a/app/src/main/java/dev/patrickgold/florisboard/ime/key/KeyView.kt +++ b/app/src/main/java/dev/patrickgold/florisboard/ime/key/KeyView.kt @@ -16,6 +16,7 @@ import dev.patrickgold.florisboard.R import dev.patrickgold.florisboard.ime.core.FlorisBoard import dev.patrickgold.florisboard.ime.keyboard.KeyboardRowView import dev.patrickgold.florisboard.ime.keyboard.KeyboardView +import dev.patrickgold.florisboard.ime.popup.KeyPopupManager import dev.patrickgold.florisboard.util.* import java.util.* @@ -29,6 +30,7 @@ class KeyView( private var isKeyPressed: Boolean = false private val osHandler = Handler() private var osTimer: Timer? = null + private val popupManager = KeyPopupManager(keyboardView, this) init { super.setOnTouchListener(this) @@ -46,34 +48,40 @@ class KeyView( } override fun onTouch(v: View, event: MotionEvent): Boolean { - if (event.action == MotionEvent.ACTION_DOWN) { - isKeyPressed = true - keyboardView.popupManager.show(this) - if (data.code == KeyCode.DELETE && data.type == KeyType.ENTER_EDITING) { - osTimer = Timer() - osTimer?.scheduleAtFixedRate(object : TimerTask() { - override fun run() { - florisboard.sendKeyPress(data) - if (!isKeyPressed) { - osTimer?.cancel() - osTimer = null + when (event.action) { + MotionEvent.ACTION_DOWN -> { + isKeyPressed = true + popupManager.show() + if (data.code == KeyCode.DELETE && data.type == KeyType.ENTER_EDITING) { + osTimer = Timer() + osTimer?.scheduleAtFixedRate(object : TimerTask() { + override fun run() { + florisboard.sendKeyPress(data) + if (!isKeyPressed) { + osTimer?.cancel() + osTimer = null + } } - } - }, 500, 50) - } - osHandler.postDelayed({ - if (data.popup.isNotEmpty()) { - keyboardView.popupManager.extend(this) + }, 500, 50) } - }, 300) - } - if (event.action == MotionEvent.ACTION_UP) { - isKeyPressed = false - osHandler.removeCallbacksAndMessages(null) - osTimer?.cancel() - osTimer = null - keyboardView.popupManager.hide(this) - florisboard.sendKeyPress(data) + osHandler.postDelayed({ + if (data.popup.isNotEmpty()) { + popupManager.extend() + } + }, 300) + } + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { + isKeyPressed = false + osHandler.removeCallbacksAndMessages(null) + osTimer?.cancel() + osTimer = null + popupManager.hide() + florisboard.sendKeyPress(data) + } + MotionEvent.ACTION_MOVE -> { + // TODO: handle movement + } + else -> return false } return true } diff --git a/app/src/main/java/dev/patrickgold/florisboard/ime/keyboard/KeyboardView.kt b/app/src/main/java/dev/patrickgold/florisboard/ime/keyboard/KeyboardView.kt index fbfde304..8db4f078 100644 --- a/app/src/main/java/dev/patrickgold/florisboard/ime/keyboard/KeyboardView.kt +++ b/app/src/main/java/dev/patrickgold/florisboard/ime/keyboard/KeyboardView.kt @@ -6,7 +6,6 @@ import android.view.ContextThemeWrapper import android.widget.LinearLayout import com.google.android.flexbox.FlexboxLayout import dev.patrickgold.florisboard.R -import dev.patrickgold.florisboard.ime.popup.KeyPopupManager import dev.patrickgold.florisboard.ime.core.FlorisBoard import dev.patrickgold.florisboard.ime.key.KeyView import dev.patrickgold.florisboard.ime.key.KeyboardMode @@ -20,7 +19,6 @@ class KeyboardView( ) { var computedLayout: ComputedLayoutData? = null - val popupManager = KeyPopupManager(this) private fun buildLayout() { destroyLayout() diff --git a/app/src/main/java/dev/patrickgold/florisboard/ime/popup/KeyPopupManager.kt b/app/src/main/java/dev/patrickgold/florisboard/ime/popup/KeyPopupManager.kt index e7564213..05a6ac4e 100644 --- a/app/src/main/java/dev/patrickgold/florisboard/ime/popup/KeyPopupManager.kt +++ b/app/src/main/java/dev/patrickgold/florisboard/ime/popup/KeyPopupManager.kt @@ -20,13 +20,19 @@ import kotlin.math.roundToInt @SuppressLint("RtlHardcoded") -class KeyPopupManager(kbd: KeyboardView) { +class KeyPopupManager( + private val keyboardView: KeyboardView, + private val keyView: KeyView +) { - private val keyPopupWidth = kbd.resources.getDimension(R.dimen.key_popup_width).toInt() - private val keyPopupHeight = kbd.resources.getDimension(R.dimen.key_popup_height).toInt() - private val keyboardView: KeyboardView = kbd - private var windows: HashMap = hashMapOf() - private var windowsExt: HashMap = hashMapOf() + private val keyPopupWidth = keyboardView.resources.getDimension(R.dimen.key_popup_width).toInt() + private val keyPopupHeight = keyboardView.resources.getDimension(R.dimen.key_popup_height).toInt() + private val popupView = View.inflate(keyboardView.context, + R.layout.key_popup, null) as ViewGroup + private val popupViewExt = View.inflate(keyboardView.context, + R.layout.key_popup_extended, null) as ViewGroup + private var window: PopupWindow? = null + private var windowExt: PopupWindow? = null private fun createTextView(keyView: KeyView, k: Int): TextView { val textView = TextView(keyboardView.context) @@ -52,52 +58,14 @@ class KeyPopupManager(kbd: KeyboardView) { return w } - /*fun prepare(keyView: KeyView) { + fun show() { val code = keyView.data.code if (code <= 32) { return } - if (windows.containsKey(code)) { + if (window != null) { return } - val popupView = View.inflate(keyboardView.context, - R.layout.key_popup, null) as ViewGroup - val w = createPopupWindow(popupView, 0, 0) - w.setTouchInterceptor { _, event -> - keyView.dispatchTouchEvent(event) - true - } - w.showAtLocation( - keyboardView, Gravity.LEFT or Gravity.TOP, - (keyboardView.x + (keyView.parent as ViewGroup).x + keyView.x - ((keyPopupWidth - keyView.measuredWidth).toFloat() / 2.0f)).toInt(), - (keyboardView.y + (keyView.parent as ViewGroup).y + keyView.y - (keyPopupHeight - keyView.measuredHeight)).toInt() - ) - windows[code] = w - } - - fun updateLocation(keyView: KeyView) { - val code = keyView.data.code - if (code <= 32) { - return - } - val w = windows[code] ?: return - w.update( - (keyboardView.x + (keyView.parent as ViewGroup).x + keyView.x - ((keyPopupWidth - keyView.measuredWidth).toFloat() / 2.0f)).toInt(), - (keyboardView.y + (keyView.parent as ViewGroup).y + keyView.y - (keyPopupHeight - keyView.measuredHeight)).toInt(), - -1, -1 - ) - }*/ - - fun show(keyView: KeyView) { - val code = keyView.data.code - if (code <= 32) { - return - } - if (windows.containsKey(code)) { - return - } - val popupView = View.inflate(keyboardView.context, - R.layout.key_popup, null) as ViewGroup popupView.findViewById(R.id.key_popup_text).text = keyView.getComputedLetter() popupView.findViewById(R.id.key_popup_threedots).visibility = when { keyView.data.popup.isEmpty() -> View.INVISIBLE @@ -113,19 +81,17 @@ class KeyPopupManager(kbd: KeyboardView) { (keyboardView.x + (keyView.parent as ViewGroup).x + keyView.x - ((keyPopupWidth - keyView.measuredWidth).toFloat() / 2.0f)).toInt(), (keyboardView.y + (keyView.parent as ViewGroup).y + keyView.y - (keyPopupHeight - keyView.measuredHeight)).toInt() ) - windows[code] = w + window = w } - fun extend(keyView: KeyView) { + fun extend() { val code = keyView.data.code if (code <= 32 || keyView.data.popup.isEmpty()) { return } - if (windowsExt.containsKey(code)) { + if (windowExt != null) { return } - val popupViewExt = View.inflate(keyboardView.context, - R.layout.key_popup_extended, null) as ViewGroup // Extended popup layout: // row 1 // row 0 (has always items, takes all if size <= 5, when higher and uneven 1 more than row 1 @@ -145,44 +111,35 @@ class KeyPopupManager(kbd: KeyboardView) { row0.addView(createTextView(keyView, k)) } } - windows[code]?.contentView?.findViewById(R.id.key_popup_threedots)?.visibility = View.INVISIBLE + popupView.findViewById(R.id.key_popup_threedots)?.visibility = View.INVISIBLE val w = createPopupWindow(popupViewExt, row0count * keyPopupWidth, when { row1count > 0 -> keyView.measuredHeight * 2 else -> keyView.measuredHeight }) + val x = when { + (keyView.x < keyboardView.measuredWidth / 2) -> + (keyboardView.x + (keyView.parent as ViewGroup).x + keyView.x - ((keyPopupWidth - keyView.measuredWidth).toFloat() / 2.0f)).toInt() + else -> + (keyboardView.x + (keyView.parent as ViewGroup).x + keyView.x - ((keyPopupWidth - keyView.measuredWidth).toFloat() / 2.0f)).toInt() - row0count * keyPopupWidth + keyView.measuredWidth + } w.showAtLocation( keyboardView, Gravity.LEFT or Gravity.TOP, - (keyboardView.x + (keyView.parent as ViewGroup).x + keyView.x - ((keyPopupWidth - keyView.measuredWidth).toFloat() / 2.0f)).toInt(), - (keyboardView.y + (keyView.parent as ViewGroup).y + keyView.y - (keyPopupHeight - keyView.measuredHeight) - when { + x, (keyboardView.y + (keyView.parent as ViewGroup).y + keyView.y - (keyPopupHeight - keyView.measuredHeight) - when { row1count > 0 -> keyView.measuredHeight else -> 0 }).toInt() ) - windowsExt[code] = w + windowExt = w } - fun hide(keyView: KeyView) { + fun hide() { val code = keyView.data.code - if (code < 32) { + if (code <= 32) { return } - // Hide popup delayed so the popup can show for persons who type fast - /*Handler().postDelayed({*/ - windows[code]?.dismiss() - windows.remove(code) - windowsExt[code]?.dismiss() - windowsExt.remove(code) - /*}, 10)*/ + window?.dismiss() + window = null + windowExt?.dismiss() + windowExt = null } - - /*fun reset() { - for ((code, window) in windows) { - window.dismiss() - windows.remove(code) - } - for ((code, windowExt) in windowsExt) { - windowExt.dismiss() - windowsExt.remove(code) - } - }*/ } diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index f7151b50..aadd278a 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -11,7 +11,7 @@ 35dp 90dp - 26sp + 24sp 192sp