|
| 1 | +package com.simplemobiletools.filemanager.pro.views |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.util.AttributeSet |
| 5 | +import android.util.TypedValue |
| 6 | +import android.view.MotionEvent |
| 7 | +import androidx.appcompat.widget.AppCompatTextView |
| 8 | +import com.alexvasilkov.gestures.GestureController |
| 9 | +import com.alexvasilkov.gestures.State |
| 10 | +import com.alexvasilkov.gestures.views.interfaces.GestureView |
| 11 | +import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor |
| 12 | +import com.simplemobiletools.filemanager.pro.extensions.config |
| 13 | + |
| 14 | +// taken from |
| 15 | +// https://github.com/alexvasilkov/GestureViews/blob/f0a4c266e31dcad23bd0d9013531bc1c501b9c9f/sample/src/main/java/com/alexvasilkov/gestures/sample/ex/custom/text/GestureTextView.java |
| 16 | +class GestureTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) : AppCompatTextView(context, attrs, defStyle), GestureView { |
| 17 | + private val controller: GestureController = GestureController(this) |
| 18 | + private var origSize = 0f |
| 19 | + private var size = 0f |
| 20 | + |
| 21 | + init { |
| 22 | + controller.settings.setOverzoomFactor(1f).isPanEnabled = false |
| 23 | + controller.settings.initFromAttributes(context, attrs) |
| 24 | + controller.addOnStateChangeListener(object : GestureController.OnStateChangeListener { |
| 25 | + override fun onStateChanged(state: State) { |
| 26 | + applyState(state) |
| 27 | + } |
| 28 | + |
| 29 | + override fun onStateReset(oldState: State, newState: State) { |
| 30 | + applyState(newState) |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + origSize = textSize |
| 35 | + setTextColor(context.config.textColor) |
| 36 | + setLinkTextColor(context.getAdjustedPrimaryColor()) |
| 37 | + } |
| 38 | + |
| 39 | + override fun getController() = controller |
| 40 | + |
| 41 | + override fun onTouchEvent(event: MotionEvent) = controller.onTouch(this, event) |
| 42 | + |
| 43 | + override fun setTextSize(size: Float) { |
| 44 | + super.setTextSize(size) |
| 45 | + origSize = textSize |
| 46 | + applyState(controller.state) |
| 47 | + } |
| 48 | + |
| 49 | + override fun setTextSize(unit: Int, size: Float) { |
| 50 | + super.setTextSize(unit, size) |
| 51 | + origSize = textSize |
| 52 | + applyState(controller.state) |
| 53 | + } |
| 54 | + |
| 55 | + override fun onSizeChanged(width: Int, height: Int, oldWidth: Int, oldHeight: Int) { |
| 56 | + super.onSizeChanged(width, height, oldWidth, oldHeight) |
| 57 | + controller.settings.setViewport(width, height).setImage(width, height) |
| 58 | + controller.updateState() |
| 59 | + } |
| 60 | + |
| 61 | + private fun applyState(state: State) { |
| 62 | + var size = origSize * state.zoom |
| 63 | + val maxSize = origSize * controller.stateController.getMaxZoom(state) |
| 64 | + size = Math.max(origSize, Math.min(size, maxSize)) |
| 65 | + |
| 66 | + size = Math.round(size).toFloat() |
| 67 | + if (!State.equals(this.size, size)) { |
| 68 | + this.size = size |
| 69 | + super.setTextSize(TypedValue.COMPLEX_UNIT_PX, size) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments