Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 42 additions & 62 deletions common/src/main/java/com/itsaky/androidide/utils/ViewExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,67 +48,45 @@ fun View.applyLongPressRecursively(listener: (View) -> Boolean) {

@SuppressLint("ClickableViewAccessibility")
fun View.setupGestureHandling(
onLongPress: (View) -> Unit,
onDrag: (View) -> Unit,
onLongPress: (View) -> Unit,
onDrag: (View) -> Unit
) {
val handler = Handler(Looper.getMainLooper())
var isDragging = false
var longPressFired = false
val touchSlop = ViewConfiguration.get(this.context).scaledTouchSlop
var downX = 0f
var downY = 0f

val longPressRunnable =
Runnable {
if (!isDragging) {
longPressFired = true
this.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
onLongPress(this)
}
}

this.setOnTouchListener { view, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
isDragging = false
longPressFired = false
downX = event.x
downY = event.y

view.parent.requestDisallowInterceptTouchEvent(true)

handler.postDelayed(longPressRunnable, ViewConfiguration.getLongPressTimeout().toLong())
true
}

MotionEvent.ACTION_MOVE -> {
if (!isDragging && (abs(event.x - downX) > touchSlop || abs(event.y - downY) > touchSlop)) {
isDragging = true
handler.removeCallbacks(longPressRunnable)
onDrag(view)
}
isDragging
}

MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
view.parent.requestDisallowInterceptTouchEvent(false)

val wasDragging = isDragging
val wasLongPressed = longPressFired

if (!wasDragging && !wasLongPressed) {
view.performClick()
}

handler.removeCallbacks(longPressRunnable)
isDragging = false
longPressFired = false
true
}

else -> false
}
}
val handler = Handler(Looper.getMainLooper())
var isTooltipStarted = false
var startTime = 0L

setOnTouchListener { view, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
isTooltipStarted = false
startTime = System.currentTimeMillis()

// Trigger long press after 800ms
handler.postDelayed({
if (!isTooltipStarted) {
isTooltipStarted = true
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
onLongPress(view)
}
}, LONG_PRESS_TIMEOUT_MS)
}

MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
handler.removeCallbacksAndMessages(null)

if (!isTooltipStarted) {
val holdDuration = System.currentTimeMillis() - startTime
if (holdDuration >= HOLD_DURATION_MS) {
// Medium hold for drag (600-800ms)
onDrag(view)
} else {
view.performClick()
}
}
}
}
true
}
}

/**
Expand Down Expand Up @@ -142,7 +120,6 @@ fun View.handleLongClicksAndDrag(
var longPressFired = false

val handler = Handler(Looper.getMainLooper())
val longPressTimeout = 800L

val longPressRunnable =
Runnable {
Expand All @@ -166,7 +143,7 @@ fun View.handleLongClicksAndDrag(
viewInitialX = view.x
viewInitialY = view.y

handler.postDelayed(longPressRunnable, longPressTimeout)
handler.postDelayed(longPressRunnable, LONG_PRESS_TIMEOUT_MS)
true
}

Expand Down Expand Up @@ -219,3 +196,6 @@ fun View.handleLongClicksAndDrag(
}
}
}

const val HOLD_DURATION_MS = 600L
const val LONG_PRESS_TIMEOUT_MS = 800L