recognize dragging of sides at widget resizing

This commit is contained in:
tibbi 2022-10-02 23:10:29 +02:00
parent 30c5dec1be
commit b9f96e0703
2 changed files with 23 additions and 6 deletions

View File

@ -20,3 +20,4 @@ const val ITEM_TYPE_SHORTCUT = 2
const val WIDGET_HOST_ID = 12345 const val WIDGET_HOST_ID = 12345
const val MAX_ALLOWED_MOVE_PX = 10 const val MAX_ALLOWED_MOVE_PX = 10
const val MAX_CLICK_DURATION = 150

View File

@ -8,7 +8,7 @@ import android.util.AttributeSet
import android.view.MotionEvent import android.view.MotionEvent
import android.widget.RelativeLayout import android.widget.RelativeLayout
import com.simplemobiletools.launcher.R import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.helpers.MAX_ALLOWED_MOVE_PX import com.simplemobiletools.launcher.helpers.MAX_CLICK_DURATION
@SuppressLint("ViewConstructor") @SuppressLint("ViewConstructor")
class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: Int) : RelativeLayout(context, attrs, defStyle) { class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: Int) : RelativeLayout(context, attrs, defStyle) {
@ -18,10 +18,17 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
private var resizeWidgetLineDotPaint: Paint private var resizeWidgetLineDotPaint: Paint
private var actionDownCoords = PointF() private var actionDownCoords = PointF()
private var actionDownMS = 0L private var actionDownMS = 0L
private var MAX_CLICK_DURATION = 150
private val lineDotRadius = context.resources.getDimension(R.dimen.resize_frame_dot_radius) private val lineDotRadius = context.resources.getDimension(R.dimen.resize_frame_dot_radius)
private val MAX_TOUCH_LINE_DISTANCE = lineDotRadius * 5 // how close we have to be to the widgets side to drag it
var onClickListener: (() -> Unit)? = null var onClickListener: (() -> Unit)? = null
private val DRAGGING_NONE = 0
private val DRAGGING_LEFT = 1
private val DRAGGING_TOP = 2
private val DRAGGING_RIGHT = 3
private val DRAGGING_BOTTOM = 4
private var dragDirection = DRAGGING_NONE
init { init {
background = ColorDrawable(Color.TRANSPARENT) background = ColorDrawable(Color.TRANSPARENT)
@ -54,13 +61,22 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
actionDownCoords.x = event.rawX actionDownCoords.x = event.rawX
actionDownCoords.y = event.rawY actionDownCoords.y = event.rawY
actionDownMS = System.currentTimeMillis() actionDownMS = System.currentTimeMillis()
if (event.x < MAX_TOUCH_LINE_DISTANCE) {
dragDirection = DRAGGING_LEFT
} else if (event.y < MAX_TOUCH_LINE_DISTANCE) {
dragDirection = DRAGGING_TOP
} else if (event.x > width - MAX_TOUCH_LINE_DISTANCE) {
dragDirection = DRAGGING_RIGHT
} else if (event.y > height - MAX_TOUCH_LINE_DISTANCE) {
dragDirection = DRAGGING_BOTTOM
}
} }
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
if (System.currentTimeMillis() - actionDownMS < MAX_CLICK_DURATION && if (dragDirection == DRAGGING_NONE) {
Math.abs(actionDownCoords.x - event.rawX) < MAX_ALLOWED_MOVE_PX &&
Math.abs(actionDownCoords.y - event.rawY) < MAX_ALLOWED_MOVE_PX
) {
onClickListener?.invoke() onClickListener?.invoke()
} else if (System.currentTimeMillis() - actionDownMS < MAX_CLICK_DURATION) {
onClickListener?.invoke()
dragDirection = DRAGGING_NONE
} }
} }
} }