|
| 1 | +package org.cryptomator.presentation.ui.layout |
| 2 | + |
| 3 | +import android.view.MotionEvent |
| 4 | +import android.view.View |
| 5 | +import android.view.ViewGroup |
| 6 | +import android.view.ViewTreeObserver |
| 7 | +import android.widget.ScrollView |
| 8 | + |
| 9 | +/** |
| 10 | + * Wires [thumb] as a draggable fast-scroll handle over this [ScrollView] (whose scrolling content is [content]). |
| 11 | + * Tapping anywhere on [track] jumps the thumb to that position. |
| 12 | + * Returns a cleanup callback to be invoked from the host's `onDestroyView`. |
| 13 | + */ |
| 14 | +fun ScrollView.attachFastScrollThumb(thumb: View, track: View, content: View): () -> Unit { |
| 15 | + val scroll = this |
| 16 | + |
| 17 | + fun scrollableHeight(): Int = |
| 18 | + (content.height + scroll.paddingTop + scroll.paddingBottom - scroll.height).coerceAtLeast(0) |
| 19 | + |
| 20 | + fun trackHeight(): Int { |
| 21 | + val bottomMargin = (thumb.layoutParams as? ViewGroup.MarginLayoutParams)?.bottomMargin ?: 0 |
| 22 | + return (scroll.height - thumb.height - bottomMargin).coerceAtLeast(0) |
| 23 | + } |
| 24 | + |
| 25 | + fun syncThumb() { |
| 26 | + val total = scrollableHeight() |
| 27 | + if (total == 0) { |
| 28 | + thumb.visibility = View.GONE |
| 29 | + track.visibility = View.GONE |
| 30 | + return |
| 31 | + } |
| 32 | + thumb.visibility = View.VISIBLE |
| 33 | + track.visibility = View.VISIBLE |
| 34 | + thumb.translationY = scroll.scrollY.toFloat() / total * trackHeight() |
| 35 | + } |
| 36 | + |
| 37 | + fun jumpToTrackY(yOnTrack: Float) { |
| 38 | + val trackPx = trackHeight().toFloat() |
| 39 | + if (trackPx == 0f) return |
| 40 | + val clamped = yOnTrack.coerceIn(0f, trackPx) |
| 41 | + scroll.scrollTo(0, (clamped / trackPx * scrollableHeight()).toInt()) |
| 42 | + } |
| 43 | + |
| 44 | + val scrollListener = ViewTreeObserver.OnScrollChangedListener { syncThumb() } |
| 45 | + scroll.viewTreeObserver.addOnScrollChangedListener(scrollListener) |
| 46 | + |
| 47 | + val layoutListener = View.OnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> syncThumb() } |
| 48 | + scroll.addOnLayoutChangeListener(layoutListener) |
| 49 | + content.addOnLayoutChangeListener(layoutListener) |
| 50 | + |
| 51 | + var thumbDragOffsetY = 0f |
| 52 | + var thumbDragMoved = false |
| 53 | + thumb.isClickable = true |
| 54 | + thumb.setOnTouchListener { _, event -> |
| 55 | + when (event.actionMasked) { |
| 56 | + MotionEvent.ACTION_DOWN -> { |
| 57 | + thumbDragOffsetY = event.rawY - thumb.translationY |
| 58 | + thumbDragMoved = false |
| 59 | + thumb.isPressed = true |
| 60 | + true |
| 61 | + } |
| 62 | + MotionEvent.ACTION_MOVE -> { |
| 63 | + thumbDragMoved = true |
| 64 | + jumpToTrackY(event.rawY - thumbDragOffsetY) |
| 65 | + true |
| 66 | + } |
| 67 | + MotionEvent.ACTION_UP -> { |
| 68 | + thumb.isPressed = false |
| 69 | + if (!thumbDragMoved) thumb.performClick() |
| 70 | + true |
| 71 | + } |
| 72 | + MotionEvent.ACTION_CANCEL -> { |
| 73 | + thumb.isPressed = false |
| 74 | + true |
| 75 | + } |
| 76 | + else -> false |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + track.isClickable = true |
| 81 | + track.setOnTouchListener { _, event -> |
| 82 | + when (event.actionMasked) { |
| 83 | + MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { |
| 84 | + jumpToTrackY(event.y - thumb.height / 2f) |
| 85 | + true |
| 86 | + } |
| 87 | + MotionEvent.ACTION_UP -> { |
| 88 | + track.performClick() |
| 89 | + true |
| 90 | + } |
| 91 | + else -> false |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return { |
| 96 | + scroll.viewTreeObserver.removeOnScrollChangedListener(scrollListener) |
| 97 | + scroll.removeOnLayoutChangeListener(layoutListener) |
| 98 | + content.removeOnLayoutChangeListener(layoutListener) |
| 99 | + thumb.setOnTouchListener(null) |
| 100 | + track.setOnTouchListener(null) |
| 101 | + } |
| 102 | +} |
0 commit comments