Skip to content
Merged
Show file tree
Hide file tree
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
53 changes: 47 additions & 6 deletions app/src/main/java/com/itsaky/androidide/ui/SwipeRevealLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.google.android.material.shape.MaterialShapeDrawable
import com.itsaky.androidide.R
import kotlin.math.max
import kotlin.math.min
import androidx.core.content.withStyledAttributes

/**
* A layout which can be dragged vertically to reveal a hidden content.
Expand Down Expand Up @@ -67,14 +68,22 @@ open class SwipeRevealLayout @JvmOverloads constructor(
private var rightDragProgress = 0f
private var isVerticalDragEnabled = true

private var isDownInDragHandle = false
/**
* Whether the most recent touch-down landed within the configured drag handle. The vertical
* drag-to-reveal gesture is only captured when this is `true`, so that scroll gestures starting
* in the middle of the overlapping content (e.g. the editor) are not stolen.
*/
private val dragHandleLocation = IntArray(2)

init {
leftDragHelper = ViewDragHelper.create(this, 1f, LeftDragCallback())
rightDragHelper = ViewDragHelper.create(this, 1f, RightDragCallback())
}

private val dragHelperCallback = object : ViewDragHelper.Callback() {
override fun tryCaptureView(child: View, pointerId: Int): Boolean {
return isVerticalDragEnabled && child === overlappingContent
return isVerticalDragEnabled && isDownInDragHandle && child === overlappingContent
}

override fun onViewPositionChanged(changedView: View, left: Int, top: Int, dx: Int, dy: Int) {
Expand Down Expand Up @@ -193,11 +202,15 @@ open class SwipeRevealLayout @JvmOverloads constructor(

init {
if (attrs != null) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwipeRevealLayout,
defStyleAttr, defStyleRes)
dragHandleViewId = typedArray.getResourceId(R.styleable.SwipeRevealLayout_dragHandle,
dragHandleViewId)
typedArray.recycle()
context.withStyledAttributes(
attrs, R.styleable.SwipeRevealLayout,
defStyleAttr, defStyleRes
) {
dragHandleViewId = getResourceId(
R.styleable.SwipeRevealLayout_dragHandle,
dragHandleViewId
)
}
}
}

Expand Down Expand Up @@ -237,6 +250,9 @@ open class SwipeRevealLayout @JvmOverloads constructor(
dragHelper.cancel()
return false
}
if (action == MotionEvent.ACTION_DOWN) {
isDownInDragHandle = isTouchInDragHandle(ev)
}
val isLeft = leftDragHelper.shouldInterceptTouchEvent(ev)
val isRight = rightDragHelper.shouldInterceptTouchEvent(ev)
val isVertical = dragHelper.shouldInterceptTouchEvent(ev)
Expand All @@ -251,6 +267,31 @@ open class SwipeRevealLayout @JvmOverloads constructor(
return true
}

/**
* Returns whether the given touch event falls within the bounds of the configured drag handle
* (see [dragHandleViewId] / the `app:dragHandle` attribute). When no drag handle is configured,
* the whole overlapping content acts as the handle (legacy behavior).
*/
private fun isTouchInDragHandle(ev: MotionEvent): Boolean {
if (dragHandleViewId == 0) {
return true
}

val handle = findViewById<View>(dragHandleViewId)
if (handle == null || handle.visibility != VISIBLE) {
return false
}

handle.getLocationOnScreen(dragHandleLocation)
val left = dragHandleLocation[0]
val top = dragHandleLocation[1]
val right = left + handle.width
val bottom = top + handle.height
val x = ev.rawX
val y = ev.rawY
return x >= left && x <= right && y >= top && y <= bottom
}

override fun computeScroll() {
if (leftDragHelper.continueSettling(true) or rightDragHelper.continueSettling(true) or dragHelper.continueSettling(true)) {
postInvalidateOnAnimation()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
android:id="@+id/swipe_reveal"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:dragHandle="@id/project_actions_toolbar">
app:dragHandle="@id/editor_appBarLayout">

<include
android:id="@+id/mem_usage_view"
Expand Down
Loading