Skip to content

Commit 75b5f80

Browse files
authored
Allow to disable display cutouts padding (#101)
1 parent e464108 commit 75b5f80

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ All notable changes to this project will be documented in this file. Take a look
3232
* The new `Navigator.Listener.onJumpToLocator()` API is called every time the navigator jumps to an explicit location, which might break the linear reading progression.
3333
* For example, it is called when clicking on internal links or programmatically calling `Navigator.go()`, but not when turning pages.
3434
* You can use this callback to implement a navigation history by differentiating between continuous and discontinuous moves.
35+
* You can now disable the display cutouts padding in the EPUB navigator (contributed by [@szymn](https://github.com/readium/kotlin-toolkit/pull/101)).
36+
* This is useful when the navigator is not laid out full screen.
3537
* (*experimental*) A new audiobook navigator based on Jetpack `media2`.
3638
* See the [pull request #80](https://github.com/readium/kotlin-toolkit/pull/80) for the differences with the previous audiobook navigator.
3739
* This navigator is located in its own module `readium-navigator-media2`. You will need to add it to your dependencies to use it.

readium/navigator/src/main/java/org/readium/r2/navigator/R2BasicWebView.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
7272
}
7373

7474
lateinit var listener: Listener
75-
lateinit var navigator: Navigator
7675
internal var preferences: SharedPreferences? = null
7776

7877
var resourceUrl: String? = null

readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFragment.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class EpubNavigatorFragment private constructor(
8282
* Provide one if you want to customize the selection context menu items.
8383
*/
8484
var selectionActionModeCallback: ActionMode.Callback? = null,
85+
86+
/**
87+
* Whether padding accounting for display cutouts should be applied.
88+
*/
89+
val shouldApplyInsetsPadding: Boolean? = true,
8590
)
8691

8792
interface PaginationListener {

readium/navigator/src/main/java/org/readium/r2/navigator/pager/R2EpubPageFragment.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ class R2EpubPageFragment : Fragment() {
6262

6363
private var isLoading: Boolean = false
6464

65+
private val navigator: EpubNavigatorFragment?
66+
get() = parentFragment as? EpubNavigatorFragment
67+
68+
private val shouldApplyInsetsPadding: Boolean
69+
get() = navigator?.config?.shouldApplyInsetsPadding ?: true
70+
6571
@SuppressLint("SetJavaScriptEnabled")
6672
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
67-
val navigatorFragment = parentFragment as EpubNavigatorFragment
6873
_binding = ViewpagerFragmentEpubBinding.inflate(inflater, container, false)
6974
containerView = binding.root
7075
preferences = activity?.getSharedPreferences("org.readium.r2.settings", Context.MODE_PRIVATE)!!
@@ -73,8 +78,9 @@ class R2EpubPageFragment : Fragment() {
7378
this.webView = webView
7479

7580
webView.visibility = View.INVISIBLE
76-
webView.navigator = navigatorFragment
77-
webView.listener = navigatorFragment.webViewListener
81+
navigator?.let {
82+
webView.listener = it.webViewListener
83+
}
7884
webView.preferences = preferences
7985

8086
webView.setScrollMode(preferences.getBoolean(SCROLL_REF, false))
@@ -225,11 +231,13 @@ class R2EpubPageFragment : Fragment() {
225231
}
226232
}
227233

228-
// Update padding when the window insets change, for example when the navigation and status
229-
// bars are toggled.
230-
ViewCompat.setOnApplyWindowInsetsListener(containerView) { _, insets ->
231-
updatePadding()
232-
insets
234+
if (shouldApplyInsetsPadding) {
235+
// Update padding when the window insets change, for example when the navigation and status
236+
// bars are toggled.
237+
ViewCompat.setOnApplyWindowInsetsListener(containerView) { _, insets ->
238+
updatePadding()
239+
insets
240+
}
233241
}
234242
}
235243

@@ -241,6 +249,7 @@ class R2EpubPageFragment : Fragment() {
241249

242250
// Add additional padding to take into account the display cutout, if needed.
243251
if (
252+
shouldApplyInsetsPadding &&
244253
android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P &&
245254
window.attributes.layoutInDisplayCutoutMode != WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER
246255
) {
@@ -267,7 +276,7 @@ class R2EpubPageFragment : Fragment() {
267276
internal val paddingBottom: Int get() = containerView.paddingBottom
268277

269278
private val isCurrentResource: Boolean get() {
270-
val epubNavigator = webView?.navigator as? EpubNavigatorFragment ?: return false
279+
val epubNavigator = navigator ?: return false
271280
val currentFragment = (epubNavigator.resourcePager.adapter as? R2PagerAdapter)?.getCurrentFragment() as? R2EpubPageFragment ?: return false
272281
return tag == currentFragment.tag
273282
}
@@ -283,7 +292,7 @@ class R2EpubPageFragment : Fragment() {
283292
webView.visibility = View.VISIBLE
284293

285294
if (isCurrentResource) {
286-
val epubNavigator = requireNotNull(webView.navigator as? EpubNavigatorFragment)
295+
val epubNavigator = requireNotNull(navigator)
287296
val locator = epubNavigator.pendingLocator
288297
epubNavigator.pendingLocator = null
289298
if (locator != null) {

readium/navigator/src/main/java/org/readium/r2/navigator/pager/R2FXLPageFragment.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import android.webkit.WebResourceResponse
2020
import android.webkit.WebView
2121
import androidx.fragment.app.Fragment
2222
import androidx.webkit.WebViewClientCompat
23-
import org.readium.r2.navigator.Navigator
24-
import org.readium.r2.navigator.R
2523
import org.readium.r2.navigator.R2BasicWebView
2624
import org.readium.r2.navigator.databinding.FragmentFxllayoutDoubleBinding
2725
import org.readium.r2.navigator.databinding.FragmentFxllayoutSingleBinding
@@ -45,6 +43,9 @@ class R2FXLPageFragment : Fragment() {
4543
private var _singleBinding: FragmentFxllayoutSingleBinding? = null
4644
private val singleBinding get() = _singleBinding!!
4745

46+
private val navigator: EpubNavigatorFragment?
47+
get() = parentFragment as? EpubNavigatorFragment
48+
4849
@SuppressLint("SetJavaScriptEnabled")
4950
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
5051

@@ -114,9 +115,9 @@ class R2FXLPageFragment : Fragment() {
114115
@SuppressLint("SetJavaScriptEnabled")
115116
private fun setupWebView(webView: R2BasicWebView, resourceUrl: String?) {
116117
webViews.add(webView)
117-
val navigator = parentFragment as EpubNavigatorFragment
118-
webView.navigator = navigator
119-
webView.listener = navigator.webViewListener
118+
navigator?.let {
119+
webView.listener = it.webViewListener
120+
}
120121

121122
webView.settings.javaScriptEnabled = true
122123
webView.isVerticalScrollBarEnabled = false

0 commit comments

Comments
 (0)