improving some widgetview gesture related handling
This commit is contained in:
parent
6d6bc948f2
commit
ae66ce83f2
|
@ -19,3 +19,4 @@ const val ITEM_TYPE_WIDGET = 1
|
|||
const val ITEM_TYPE_SHORTCUT = 2
|
||||
|
||||
const val WIDGET_HOST_ID = 12345
|
||||
const val MAX_ALLOWED_MOVE_PX = 10
|
||||
|
|
|
@ -166,6 +166,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
fun widgetLongPressed(item: HomeScreenGridItem) {
|
||||
resizedWidget = item
|
||||
redrawGrid()
|
||||
|
@ -178,7 +179,15 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||
val frameRect = Rect(viewX, viewY, viewX + widgetView.width, viewY + widgetView.height)
|
||||
resize_frame.updateFrameCoords(frameRect)
|
||||
resize_frame.beVisible()
|
||||
resize_frame.onClickListener = {
|
||||
hideResizeLines()
|
||||
}
|
||||
|
||||
widgetView.ignoreTouches = true
|
||||
widgetView.setOnTouchListener { v, event ->
|
||||
resize_frame.onTouchEvent(event)
|
||||
return@setOnTouchListener true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -188,7 +197,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||
}
|
||||
|
||||
resize_frame.beGone()
|
||||
widgetViews.firstOrNull { it.tag == resizedWidget!!.widgetId }?.ignoreTouches = false
|
||||
widgetViews.firstOrNull { it.tag == resizedWidget!!.widgetId }?.apply {
|
||||
ignoreTouches = false
|
||||
setOnTouchListener(null)
|
||||
}
|
||||
resizedWidget = null
|
||||
}
|
||||
|
||||
|
|
|
@ -7,12 +7,14 @@ import android.os.Handler
|
|||
import android.view.MotionEvent
|
||||
import android.view.ViewConfiguration
|
||||
import com.simplemobiletools.commons.extensions.performHapticFeedback
|
||||
import com.simplemobiletools.launcher.helpers.MAX_ALLOWED_MOVE_PX
|
||||
|
||||
class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
|
||||
private var MAX_ALLOWED_MOVE = 10
|
||||
private var MAX_CLICK_DURATION = 150
|
||||
private var longPressHandler = Handler()
|
||||
private var actionDownCoords = PointF()
|
||||
private var currentCoords = PointF()
|
||||
private var actionDownMS = 0L
|
||||
var hasLongPressed = false
|
||||
var ignoreTouches = false
|
||||
var longPressListener: ((x: Float, y: Float) -> Unit)? = null
|
||||
|
@ -44,6 +46,7 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
|
|||
actionDownCoords.y = event.rawY
|
||||
currentCoords.x = event.rawX
|
||||
currentCoords.y = event.rawY
|
||||
actionDownMS = System.currentTimeMillis()
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
currentCoords.x = event.rawX
|
||||
|
@ -58,7 +61,7 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
|
|||
}
|
||||
|
||||
private val longPressRunnable = Runnable {
|
||||
if (Math.abs(actionDownCoords.x - currentCoords.x) < MAX_ALLOWED_MOVE && Math.abs(actionDownCoords.y - currentCoords.y) < MAX_ALLOWED_MOVE) {
|
||||
if (Math.abs(actionDownCoords.x - currentCoords.x) < MAX_ALLOWED_MOVE_PX && Math.abs(actionDownCoords.y - currentCoords.y) < MAX_ALLOWED_MOVE_PX) {
|
||||
longPressHandler.removeCallbacksAndMessages(null)
|
||||
hasLongPressed = true
|
||||
longPressListener?.invoke(actionDownCoords.x, actionDownCoords.y)
|
||||
|
|
|
@ -2,20 +2,23 @@ package com.simplemobiletools.launcher.views
|
|||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Rect
|
||||
import android.graphics.*
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.util.AttributeSet
|
||||
import android.view.MotionEvent
|
||||
import android.widget.RelativeLayout
|
||||
import com.simplemobiletools.launcher.R
|
||||
import com.simplemobiletools.launcher.helpers.MAX_ALLOWED_MOVE_PX
|
||||
|
||||
@SuppressLint("ViewConstructor")
|
||||
class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: Int) : RelativeLayout(context, attrs, defStyle) {
|
||||
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
|
||||
|
||||
private var resizeWidgetLinePaint: Paint
|
||||
private var actionDownCoords = PointF()
|
||||
private var actionDownMS = 0L
|
||||
private var MAX_CLICK_DURATION = 150
|
||||
var onClickListener: (() -> Unit)? = null
|
||||
|
||||
init {
|
||||
background = ColorDrawable(Color.TRANSPARENT)
|
||||
|
@ -35,6 +38,30 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
|
|||
requestLayout()
|
||||
}
|
||||
|
||||
override fun onTouchEvent(event: MotionEvent?): Boolean {
|
||||
if (event == null) {
|
||||
return false
|
||||
}
|
||||
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
actionDownCoords.x = event.rawX
|
||||
actionDownCoords.y = event.rawY
|
||||
actionDownMS = System.currentTimeMillis()
|
||||
}
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
||||
if (System.currentTimeMillis() - actionDownMS < MAX_CLICK_DURATION &&
|
||||
Math.abs(actionDownCoords.x - event.rawX) < MAX_ALLOWED_MOVE_PX &&
|
||||
Math.abs(actionDownCoords.y - event.rawY) < MAX_ALLOWED_MOVE_PX
|
||||
) {
|
||||
onClickListener?.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return super.onTouchEvent(event)
|
||||
}
|
||||
|
||||
override fun onDraw(canvas: Canvas) {
|
||||
super.onDraw(canvas)
|
||||
if (x != 0f || y != 0f) {
|
||||
|
|
Loading…
Reference in New Issue