show a clipboard bar above the keyboard, if content is available

This commit is contained in:
tibbi 2022-01-23 11:16:59 +01:00
parent 5e7edbe102
commit 7c2d370459
5 changed files with 109 additions and 14 deletions

View File

@ -15,6 +15,7 @@ import com.simplemobiletools.keyboard.helpers.SHIFT_OFF
import com.simplemobiletools.keyboard.helpers.SHIFT_ON_ONE_CHAR
import com.simplemobiletools.keyboard.helpers.SHIFT_ON_PERMANENT
import com.simplemobiletools.keyboard.views.MyKeyboardView
import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.*
// based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/
class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionListener {
@ -37,10 +38,12 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
}
override fun onCreateInputView(): View {
keyboardView = layoutInflater.inflate(R.layout.keyboard_view_keyboard, null) as MyKeyboardView
val keyboardHolder = layoutInflater.inflate(R.layout.keyboard_view_keyboard, null)
keyboardView = keyboardHolder.keyboard_view as MyKeyboardView
keyboardView!!.setKeyboard(keyboard!!)
keyboardView!!.setClipboardHolder(keyboardHolder.clipboard_holder)
keyboardView!!.mOnKeyboardActionListener = this
return keyboardView!!
return keyboardHolder!!
}
override fun onPress(primaryCode: Int) {

View File

@ -1,10 +1,14 @@
package com.simplemobiletools.keyboard.views
import android.annotation.SuppressLint
import android.content.ClipboardManager
import android.content.Context
import android.graphics.*
import android.graphics.Paint.Align
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.graphics.drawable.RippleDrawable
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
@ -24,6 +28,7 @@ import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_MODE_
import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SHIFT
import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SPACE
import kotlinx.android.synthetic.main.keyboard_popup_keyboard.view.*
import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.*
import java.util.*
@SuppressLint("UseCompatLoadingForDrawables")
@ -125,6 +130,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private var mKeyBackground: Drawable? = null
private val mDistances = IntArray(MAX_NEARBY_KEYS)
private var mClipboardHolder: View? = null
// For multi-tap
private var mLastTapTime = 0L
@ -160,14 +167,13 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private const val DEBOUNCE_TIME = 70
private const val REPEAT_INTERVAL = 50 // ~20 keys per second
private const val REPEAT_START_DELAY = 400
private const val GENERIC_KEY = -100
private val LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout()
private const val MAX_NEARBY_KEYS = 12
}
init {
val attributes = context.obtainStyledAttributes(attrs, R.styleable.MyKeyboardView, 0, defStyleRes)
val inflate = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val keyTextSize = 0
val indexCnt = attributes.indexCount
@ -193,7 +199,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
mPrimaryColor = context.getAdjustedPrimaryColor()
mPreviewPopup = PopupWindow(context)
mPreviewText = inflate.inflate(resources.getLayout(R.layout.keyboard_key_preview), null) as TextView
mPreviewText = inflater.inflate(resources.getLayout(R.layout.keyboard_key_preview), null) as TextView
mPreviewTextSizeLarge = context.resources.getDimension(R.dimen.preview_text_size).toInt()
mPreviewPopup.contentView = mPreviewText
mPreviewPopup.setBackgroundDrawable(null)
@ -275,6 +281,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
mAbortKey = true // Until the next ACTION_DOWN
}
/** Sets the top row above the keyboard containing the clipboard value **/
fun setClipboardHolder(clipboardHolder: View) {
mClipboardHolder = clipboardHolder
}
/**
* 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
@ -388,6 +399,33 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
}
canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR)
if (context.config.showClipboard && mClipboardHolder != null && mPopupParent.id != R.id.mini_keyboard_view) {
val clipboardContent = (context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).primaryClip?.getItemAt(0)?.text
if (clipboardContent?.trim()?.isNotEmpty() == true) {
val rippleBg = resources.getDrawable(R.drawable.clipboard_background, context.theme) as RippleDrawable
val layerDrawable = rippleBg.findDrawableByLayerId(R.id.clipboard_background_holder) as LayerDrawable
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_shape).applyColorFilter(mBackgroundColor)
mClipboardHolder?.apply {
background = ColorDrawable(mBackgroundColor.darkenColor())
beVisible()
clipboard_value.apply {
text = clipboardContent
background = rippleBg
setTextColor(mTextColor)
setOnClickListener {
}
}
}
} else {
mClipboardHolder?.beGone()
}
} else {
mClipboardHolder?.beGone()
}
val keyCount = keys.size
for (i in 0 until keyCount) {
val key = keys[i]
@ -974,8 +1012,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
return true
}
// Needs to be called after the gesture detector gets a turn, as it may have
// displayed the mini keyboard
// Needs to be called after the gesture detector gets a turn, as it may have displayed the mini keyboard
if (mMiniKeyboardOnScreen && action != MotionEvent.ACTION_CANCEL) {
return true
}
@ -1056,7 +1093,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
}
val diff = mLastX - mLastSpaceMoveX
if (diff < -mSpaceMoveThreshold) {
for (i in diff / mSpaceMoveThreshold until 0) {
mOnKeyboardActionListener?.moveCursorLeft()

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#22ffffff">
<item android:id="@+id/clipboard_background_holder">
<layer-list>
<item android:id="@+id/clipboard_background_shape">
<shape android:shape="rectangle">
<corners android:radius="@dimen/medium_margin" />
<solid android:color="@color/md_grey_800" />
</shape>
</item>
<item android:id="@+id/clipboard_background_stroke">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/md_grey_600" />
<corners android:radius="@dimen/medium_margin" />
</shape>
</item>
</layer-list>
</item>
</ripple>

View File

@ -1,7 +1,40 @@
<com.simplemobiletools.keyboard.views.MyKeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard_view"
style="@style/MyKeyboardView"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/keyboard_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:background="@color/theme_dark_background_color" />
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/clipboard_holder"
android:layout_width="match_parent"
android:layout_height="@dimen/clipboard_height"
android:layout_above="@+id/keyboard_view"
android:gravity="center"
android:paddingStart="@dimen/big_margin"
android:paddingEnd="@dimen/big_margin">
<TextView
android:id="@+id/clipboard_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/clipboard_background"
android:ellipsize="end"
android:lines="1"
android:paddingStart="@dimen/activity_margin"
android:paddingTop="@dimen/smaller_margin"
android:paddingEnd="@dimen/activity_margin"
android:paddingBottom="@dimen/smaller_margin"
android:textSize="@dimen/label_text_size"
tools:text="Clipboard content" />
</RelativeLayout>
<com.simplemobiletools.keyboard.views.MyKeyboardView
android:id="@+id/keyboard_view"
style="@style/MyKeyboardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:background="@color/theme_dark_background_color" />
</RelativeLayout>

View File

@ -2,6 +2,7 @@
<dimen name="popup_max_move_distance">60dp</dimen>
<dimen name="top_small_number_margin_width">12dp</dimen>
<dimen name="top_small_number_margin_height">18dp</dimen>
<dimen name="clipboard_height">50dp</dimen>
<dimen name="key_height">60dp</dimen>
<dimen name="vertical_correction">-10dp</dimen>