|
| 1 | +package app.grapheneos.pdfviewer.fragment |
| 2 | + |
| 3 | +import android.app.Dialog |
| 4 | +import android.content.DialogInterface |
| 5 | +import android.os.Bundle |
| 6 | +import android.view.Gravity |
| 7 | +import android.widget.FrameLayout |
| 8 | +import android.widget.LinearLayout |
| 9 | +import android.widget.SeekBar |
| 10 | +import android.widget.TextView |
| 11 | +import androidx.core.view.marginTop |
| 12 | +import androidx.fragment.app.DialogFragment |
| 13 | +import app.grapheneos.pdfviewer.PdfViewer |
| 14 | +import com.google.android.material.dialog.MaterialAlertDialogBuilder |
| 15 | +import kotlin.math.ln |
| 16 | +import kotlin.math.pow |
| 17 | + |
| 18 | +class SetZoomFragment( |
| 19 | + private var mCurrentViewerZoomRatio: Double, |
| 20 | + private var mMinZoomRatio: Double, |
| 21 | + private var mMaxZoomRatio: Double, |
| 22 | +) : DialogFragment() { |
| 23 | + |
| 24 | + companion object { |
| 25 | + const val TAG = "SetZoomFragment" |
| 26 | + private const val STATE_SEEKBAR_CUR = "seekbar_cur" |
| 27 | + private const val STATE_SEEKBAR_MIN = "seekbar_min" |
| 28 | + private const val STATE_SEEKBAR_MAX = "seekbar_max" |
| 29 | + private const val STATE_VIEWER_CUR = "viewer_cur" |
| 30 | + private const val STATE_VIEWER_MIN = "viewer_min" |
| 31 | + private const val STATE_VIEWER_MAX = "viewer_max" |
| 32 | + private const val STATE_ZOOM_FOCUSX = "viewer_zoom_focusx" |
| 33 | + private const val STATE_ZOOM_FOCUSY = "viewer_zoom_focusy" |
| 34 | + private const val SEEKBAR_RESOLUTION = 1024 |
| 35 | + } |
| 36 | + |
| 37 | + private val mSeekBar: SeekBar by lazy { SeekBar(requireActivity()) } |
| 38 | + private val mZoomLevelText: TextView by lazy { TextView(requireActivity()) } |
| 39 | + |
| 40 | + private var mZoomFocusX: Float = 0.0f |
| 41 | + public fun setZoomFocusX(value: Float) {mZoomFocusX = value} |
| 42 | + private var mZoomFocusY: Float = 0.0f |
| 43 | + public fun setZoomFocusY(value: Float) {mZoomFocusY = value} |
| 44 | + |
| 45 | + private fun progressToZoom(progress: Int): Double { |
| 46 | + val progressClip = progress.coerceAtLeast(0).coerceAtMost(SEEKBAR_RESOLUTION); |
| 47 | + return mMinZoomRatio * (mMaxZoomRatio / mMinZoomRatio).pow(progressClip.toDouble() / SEEKBAR_RESOLUTION) |
| 48 | + } |
| 49 | + |
| 50 | + private fun zoomToProgress(zoom: Double): Int { |
| 51 | + val zoomClip = zoom.coerceAtLeast(mMinZoomRatio).coerceAtMost(mMaxZoomRatio); |
| 52 | + return (SEEKBAR_RESOLUTION * ln(zoomClip / mMinZoomRatio) / ln(mMaxZoomRatio / mMinZoomRatio)).toInt() |
| 53 | + } |
| 54 | + |
| 55 | + fun refreshZoomText(progress: Int) { |
| 56 | + mZoomLevelText.text = "${(progressToZoom(progress) * 100).toInt()}%" |
| 57 | + } |
| 58 | + |
| 59 | + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { |
| 60 | + |
| 61 | + val viewerActivity: PdfViewer = (requireActivity() as PdfViewer) |
| 62 | + |
| 63 | + if (savedInstanceState != null) { |
| 64 | + val progress = savedInstanceState.getInt(STATE_SEEKBAR_CUR) |
| 65 | + mSeekBar.setMin(savedInstanceState.getInt(STATE_SEEKBAR_MIN)) |
| 66 | + mSeekBar.setMax(savedInstanceState.getInt(STATE_SEEKBAR_MAX)) |
| 67 | + mSeekBar.progress = progress |
| 68 | + refreshZoomText(progress) |
| 69 | + mCurrentViewerZoomRatio = savedInstanceState.getDouble(STATE_VIEWER_CUR) |
| 70 | + mMinZoomRatio = savedInstanceState.getDouble(STATE_VIEWER_MIN) |
| 71 | + mMaxZoomRatio = savedInstanceState.getDouble(STATE_VIEWER_MAX) |
| 72 | + mZoomFocusX = savedInstanceState.getFloat(STATE_ZOOM_FOCUSX) |
| 73 | + mZoomFocusY = savedInstanceState.getFloat(STATE_ZOOM_FOCUSY) |
| 74 | + } else { |
| 75 | + mSeekBar.setMin(0) |
| 76 | + mSeekBar.setMax(SEEKBAR_RESOLUTION) |
| 77 | + val progress = zoomToProgress(mCurrentViewerZoomRatio) |
| 78 | + mSeekBar.setProgress(progress) |
| 79 | + refreshZoomText(progress) |
| 80 | + } |
| 81 | + mSeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { |
| 82 | + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { |
| 83 | + refreshZoomText(progress) |
| 84 | + } |
| 85 | + |
| 86 | + override fun onStartTrackingTouch(seekBar: SeekBar?) {} |
| 87 | + override fun onStopTrackingTouch(seekBar: SeekBar?) {} |
| 88 | + }) |
| 89 | + val layout = LinearLayout(requireActivity()) |
| 90 | + layout.orientation = LinearLayout.VERTICAL |
| 91 | + layout.gravity = Gravity.CENTER |
| 92 | + val textParams = LinearLayout.LayoutParams( |
| 93 | + LinearLayout.LayoutParams.WRAP_CONTENT, |
| 94 | + LinearLayout.LayoutParams.WRAP_CONTENT, |
| 95 | + ) |
| 96 | + textParams.setMargins(0, 24, 0, 0) // Margin above the text |
| 97 | + layout.addView(mZoomLevelText, textParams) |
| 98 | + layout.addView( |
| 99 | + mSeekBar, LinearLayout.LayoutParams( |
| 100 | + LinearLayout.LayoutParams.MATCH_PARENT, |
| 101 | + LinearLayout.LayoutParams.WRAP_CONTENT, |
| 102 | + ) |
| 103 | + ) |
| 104 | + return MaterialAlertDialogBuilder(requireActivity()) |
| 105 | + .setView(layout) |
| 106 | + .setPositiveButton(android.R.string.ok) { _: DialogInterface?, _: Int -> |
| 107 | + mSeekBar.clearFocus() |
| 108 | + val zoom = progressToZoom(mSeekBar.progress) |
| 109 | + viewerActivity.onZoomPage((zoom / mCurrentViewerZoomRatio).toFloat(), mZoomFocusX, mZoomFocusY, true) |
| 110 | + } |
| 111 | + .setNegativeButton(android.R.string.cancel, null) |
| 112 | + .create() |
| 113 | + } |
| 114 | + |
| 115 | + override fun onSaveInstanceState(outState: Bundle) { |
| 116 | + outState.putInt(STATE_SEEKBAR_CUR, mSeekBar.progress) |
| 117 | + outState.putInt(STATE_SEEKBAR_MIN, mSeekBar.min) |
| 118 | + outState.putInt(STATE_SEEKBAR_MAX, mSeekBar.max) |
| 119 | + outState.putDouble(STATE_VIEWER_CUR, mCurrentViewerZoomRatio) |
| 120 | + outState.putDouble(STATE_VIEWER_MIN, mMinZoomRatio) |
| 121 | + outState.putDouble(STATE_VIEWER_MAX, mMaxZoomRatio) |
| 122 | + outState.putFloat(STATE_ZOOM_FOCUSX, mZoomFocusX) |
| 123 | + outState.putFloat(STATE_ZOOM_FOCUSY, mZoomFocusY) |
| 124 | + } |
| 125 | +} |
0 commit comments