Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix many more toolbar issues #3005

Merged
merged 1 commit into from
Dec 7, 2024
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
11 changes: 11 additions & 0 deletions app/src/main/java/com/quran/labs/androidquran/ui/PagerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.core.view.marginEnd
import androidx.core.view.updatePadding
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -483,6 +484,16 @@ class PagerActivity : AppCompatActivity(), AudioBarListener, OnBookmarkTagsUpdat
makeText(this@PagerActivity, charSequence!!, Toast.LENGTH_SHORT).show()
}

ViewCompat.setOnApplyWindowInsetsListener(ayahToolBar) { view, windowInsets ->
val insets = windowInsets.getInsets(
WindowInsetsCompat.Type.statusBars() or
WindowInsetsCompat.Type.displayCutout() or
WindowInsetsCompat.Type.navigationBars()
)
ayahToolBar.insets = insets
windowInsets
}

val nonRestoringViewPager = findViewById<NonRestoringViewPager>(R.id.quran_pager)
nonRestoringViewPager.setIsDualPagesInLandscape(
QuranUtils.isDualPagesInLandscape(this, quranScreenInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.widget.ImageButton
import android.widget.LinearLayout
import androidx.appcompat.widget.PopupMenu
import androidx.core.content.ContextCompat
import androidx.core.graphics.Insets
import com.quran.data.model.selection.SelectionIndicator
import com.quran.labs.androidquran.common.toolbar.R
import com.quran.page.common.toolbar.dao.SelectedAyahPlacementType
Expand Down Expand Up @@ -48,6 +49,9 @@ class AyahToolBar @JvmOverloads constructor(
var longPressLambda: ((CharSequence) -> Unit) = {}
var isRecitationEnabled = false
var lastMeasuredWidth = 0
var lastSelectionShouldPadForCutout = false

var insets: Insets = Insets.NONE

@Inject
lateinit var ayahToolBarPresenter: AyahToolBarPresenter
Expand Down Expand Up @@ -115,10 +119,12 @@ class AyahToolBar @JvmOverloads constructor(
}

// handle first layout of toolbar
val parentWidth = (parent as View).width
if (lastMeasuredWidth != totalWidth && lastMeasuredWidth == 0) {
// whenever we're RTL, we need to adjust the translationX
if (layoutDirection == LAYOUT_DIRECTION_RTL) {
translationX = translationX - totalWidth
val insetToAdd = if (lastSelectionShouldPadForCutout) insets.left + insets.right else 0
translationX = translationX - (parentWidth - measuredWidth) + insetToAdd
}
lastMeasuredWidth = totalWidth
}
Expand Down Expand Up @@ -227,17 +233,38 @@ class AyahToolBar @JvmOverloads constructor(
// hack to help fix RTL when measuredWidth is not yet set. if this is set,
// we adjust the translationX _after_ onLayout
lastMeasuredWidth = measuredWidth
val leftInset = if (position is SelectionIndicator.SelectedPointPosition) {
lastSelectionShouldPadForCutout = true
insets.left + insets.right
} else {
lastSelectionShouldPadForCutout = false
0
}

val actualX = if (layoutDirection == LAYOUT_DIRECTION_RTL) {
// in RTL, x=0 is on the very right of the screen. translationX is still to the
// right, however (i.e. translationX of 100 is 100 off the screen to the right).
// consequently, we need to subtract the width of the view to get the actual x,
// which is some negative value between -measuredWidth and 0 to properly render
// when RTL.
x - measuredWidth
if (measuredWidth > 0) {
// in RTL, x=0 means the end of the toolbar is touching the very right of the screen, with
// the toolbar itself appearing before the very right of the screen. translationX is still
// to the right, however (i.e. translationX of 100 is 100 off the screen to the right).
// consequently, we need to subtract the width of the view to get the actual x,
// which is some negative value between -measuredWidth and 0 to properly render
// when RTL.
x - (parentView.width - measuredWidth) + leftInset
} else {
// if measuredWidth is not set, we do this step in onLayout instead
x
}
} else {
x
x + leftInset
}
setPosition(actualX, y)

val actualY = if (position is SelectionIndicator.SelectedPointPosition) {
y + insets.top
} else {
y
}

setPosition(actualX, actualY)
if (needsLayout) {
requestLayout()
}
Expand Down
Loading