Skip to content
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
3 changes: 2 additions & 1 deletion readium/navigator/src/main/assets/_scripts/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ window.addEventListener(
"load",
function () {
const observer = new ResizeObserver(() => {
appendVirtualColumnIfNeeded();
onViewportWidthChanged();
snapCurrentOffset();
});
Expand Down Expand Up @@ -71,6 +70,8 @@ function onViewportWidthChanged() {
"--RS__viewportWidth",
"calc(" + width + "px / " + window.devicePixelRatio + ")"
);

appendVirtualColumnIfNeeded();
}

export function getColumnCountPerScreen() {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
fun resourceAtUrl(url: String): Resource? = null
}

lateinit var listener: Listener
var listener: Listener? = null
internal var preferences: SharedPreferences? = null
internal var useLegacySettings: Boolean = false

Expand Down Expand Up @@ -124,7 +124,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
val pageWidth = computeHorizontalScrollExtent()
val contentWidth = computeHorizontalScrollRange()

val isRtl = (listener.readingProgression == ReadingProgression.RTL)
val isRtl = (listener?.readingProgression == ReadingProgression.RTL)

// For RTL, we need to add the equivalent of one page to the x position, otherwise the
// progression will be one page off.
Expand Down Expand Up @@ -162,7 +162,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte

override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
super.onScrollChanged(l, t, oldl, oldt)
listener.onProgressionChanged()
listener?.onProgressionChanged()
}

override fun onAttachedToWindow() {
Expand All @@ -180,6 +180,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
@android.webkit.JavascriptInterface
open fun scrollRight(animated: Boolean = false) {
uiScope.launch {
val listener = listener ?: return@launch
listener.onScroll()

fun goRight() {
Expand All @@ -205,6 +206,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
@android.webkit.JavascriptInterface
open fun scrollLeft(animated: Boolean = false) {
uiScope.launch {
val listener = listener ?: return@launch
listener.onScroll()

fun goLeft() {
Expand Down Expand Up @@ -270,7 +272,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
true
}
else ->
runBlocking(uiScope.coroutineContext) { listener.onTap(event.point) }
runBlocking(uiScope.coroutineContext) { listener?.onTap(event.point) ?: false }
}
}

Expand All @@ -289,7 +291,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
return false
}

return listener.onDecorationActivated(id, group, rect, click.point)
return listener?.onDecorationActivated(id, group, rect, click.point) ?: false
}

/** Produced by gestures.js */
Expand Down Expand Up @@ -336,7 +338,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte

val aside = runBlocking {
tryOrLog {
listener.resourceAtUrl(absoluteUrl)
listener?.resourceAtUrl(absoluteUrl)
?.use { res ->
res.readAsString()
.map { Jsoup.parse(it) }
Expand Down Expand Up @@ -398,23 +400,23 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
val event = DragEvent.fromJSON(eventJson)?.takeIf { it.isValid }
?: return false

return runBlocking(uiScope.coroutineContext) { listener.onDragStart(event) }
return runBlocking(uiScope.coroutineContext) { listener?.onDragStart(event) ?: false }
}

@android.webkit.JavascriptInterface
fun onDragMove(eventJson: String): Boolean {
val event = DragEvent.fromJSON(eventJson)?.takeIf { it.isValid }
?: return false

return runBlocking(uiScope.coroutineContext) { listener.onDragMove(event) }
return runBlocking(uiScope.coroutineContext) { listener?.onDragMove(event) ?: false }
}

@android.webkit.JavascriptInterface
fun onDragEnd(eventJson: String): Boolean {
val event = DragEvent.fromJSON(eventJson)?.takeIf { it.isValid }
?: return false

return runBlocking(uiScope.coroutineContext) { listener.onDragEnd(event) }
return runBlocking(uiScope.coroutineContext) { listener?.onDragEnd(event) ?: false }
}

@android.webkit.JavascriptInterface
Expand Down Expand Up @@ -484,14 +486,14 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
@android.webkit.JavascriptInterface
fun highlightActivated(id: String) {
uiScope.launch {
listener.onHighlightActivated(id)
listener?.onHighlightActivated(id)
}
}

@android.webkit.JavascriptInterface
fun highlightAnnotationMarkActivated(id: String) {
uiScope.launch {
listener.onHighlightAnnotationMarkActivated(id)
listener?.onHighlightAnnotationMarkActivated(id)
}
}

Expand Down Expand Up @@ -587,7 +589,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
internal fun shouldOverrideUrlLoading(request: WebResourceRequest): Boolean {
if (resourceUrl == request.url?.toString()) return false

return listener.shouldOverrideUrlLoading(this, request)
return listener?.shouldOverrideUrlLoading(this, request) ?: false
}

internal fun shouldInterceptRequest(webView: WebView, request: WebResourceRequest): WebResourceResponse? {
Expand All @@ -598,7 +600,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
}
}

return listener.shouldInterceptRequest(webView, request)
return listener?.shouldInterceptRequest(webView, request)
}

// Text selection ActionMode overrides
Expand All @@ -608,7 +610,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
// used by the web view.

override fun startActionMode(callback: ActionMode.Callback?): ActionMode? {
val customCallback = listener.selectionActionModeCallback
val customCallback = listener?.selectionActionModeCallback
?: return super.startActionMode(callback)

val parent = parent ?: return null
Expand All @@ -628,7 +630,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte

@RequiresApi(Build.VERSION_CODES.M)
override fun startActionMode(callback: ActionMode.Callback?, type: Int): ActionMode? {
val customCallback = listener.selectionActionModeCallback
val customCallback = listener?.selectionActionModeCallback
?: return super.startActionMode(callback, type)

val parent = parent ?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
if (mCurItem < numPages - 1) {
mCurItem++
url?.let {
listener.onPageChanged(mCurItem + 1, numPages, it)
listener?.onPageChanged(mCurItem + 1, numPages, it)
}
}
}
Expand All @@ -59,7 +59,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
if (mCurItem > 0) {
mCurItem--
url?.let {
listener.onPageChanged(mCurItem + 1, numPages, it)
listener?.onPageChanged(mCurItem + 1, numPages, it)
}
}
}
Expand Down Expand Up @@ -354,7 +354,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,

if (post) {
url?.let {
listener.onPageChanged(item + 1, numPages, it)
listener?.onPageChanged(item + 1, numPages, it)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,9 @@ class EpubNavigatorFragment internal constructor(
}

private fun notifyCurrentLocation() {
// Make sure viewLifecycleOwner is accessible.
view ?: return

val navigator = this
debounceLocationNotificationJob?.cancel()
debounceLocationNotificationJob = viewLifecycleOwner.lifecycleScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class EpubPreferencesEditor internal constructor(
getEffectiveValue = { state.settings.fontSize },
getIsEffective = { layout == EpubLayout.REFLOWABLE },
updateValue = { value -> updateValues { it.copy(fontSize = value) } },
supportedRange = 0.4..5.0,
supportedRange = 0.1..5.0,
progressionStrategy = DoubleIncrement(0.1),
valueFormatter = percentFormatter(),
)
Expand Down Expand Up @@ -240,7 +240,7 @@ class EpubPreferencesEditor internal constructor(
getEffectiveValue = { state.settings.pageMargins },
getIsEffective = { layout == EpubLayout.REFLOWABLE },
updateValue = { value -> updateValues { it.copy(pageMargins = value) } },
supportedRange = 0.5..4.0,
supportedRange = 0.0..4.0,
progressionStrategy = DoubleIncrement(0.3),
valueFormatter = { it.format(5) },
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ class R2EpubPageFragment : Fragment() {
in topDecile..bottomDecile -> {
if (!endReached) {
endReached = true
webView.listener.onPageEnded(endReached)
webView.listener?.onPageEnded(endReached)
}
}
else -> {
if (endReached) {
endReached = false
webView.listener.onPageEnded(endReached)
webView.listener?.onPageEnded(endReached)
}
}
}
Expand All @@ -211,7 +211,7 @@ class R2EpubPageFragment : Fragment() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)

webView.listener.onResourceLoaded(link, webView, url)
webView.listener?.onResourceLoaded(link, webView, url)

// To make sure the page is properly laid out before jumping to the target locator,
// we execute a dummy JavaScript and wait for the callback result.
Expand Down Expand Up @@ -241,7 +241,7 @@ class R2EpubPageFragment : Fragment() {
// Forward a tap event when the web view is not ready to propagate the taps. This allows
// to toggle a navigation UI while a page is loading, for example.
binding.root.setOnClickListenerWithPoint { _, point ->
webView.listener.onTap(point)
webView.listener?.onTap(point)
}

return containerView
Expand All @@ -260,6 +260,13 @@ class R2EpubPageFragment : Fragment() {
}
}

override fun onDestroyView() {
webView?.listener = null
_binding = null

super.onDestroyView()
}

override fun onDetach() {
super.onDetach()

Expand All @@ -272,11 +279,6 @@ class R2EpubPageFragment : Fragment() {
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private fun setupPadding() {
updatePadding()

Expand Down Expand Up @@ -355,7 +357,7 @@ class R2EpubPageFragment : Fragment() {
}
.also { pendingLocator = null }

webView.listener.onPageLoaded()
webView.listener?.onPageLoaded()
}
}

Expand All @@ -369,7 +371,7 @@ class R2EpubPageFragment : Fragment() {
val webView = requireNotNull(webView)
val epubNavigator = requireNotNull(navigator)
loadLocator(webView, epubNavigator.readingProgression, locator)
webView.listener.onProgressionChanged()
webView.listener?.onProgressionChanged()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class R2FXLPageFragment : Fragment() {
private val secondResourceUrl: String?
get() = requireArguments().getString("secondUrl")

private var webViews = mutableListOf<WebView>()
private var webViews = mutableListOf<R2BasicWebView>()

private var _doubleBinding: FragmentFxllayoutDoubleBinding? = null
private val doubleBinding get() = _doubleBinding!!
Expand Down Expand Up @@ -74,7 +74,7 @@ class R2FXLPageFragment : Fragment() {
r2FXLLayout.addOnDoubleTapListener(R2FXLOnDoubleTapListener(true))
r2FXLLayout.addOnTapListener(object : R2FXLLayout.OnTapListener {
override fun onTap(view: R2FXLLayout, info: R2FXLLayout.TapInfo): Boolean {
return left.listener.onTap(PointF(info.x, info.y))
return left.listener?.onTap(PointF(info.x, info.y)) ?: false
}
})

Expand All @@ -94,7 +94,7 @@ class R2FXLPageFragment : Fragment() {
r2FXLLayout.addOnDoubleTapListener(R2FXLOnDoubleTapListener(true))
r2FXLLayout.addOnTapListener(object : R2FXLLayout.OnTapListener {
override fun onTap(view: R2FXLLayout, info: R2FXLLayout.TapInfo): Boolean {
return webview.listener.onTap(PointF(info.x, info.y))
return webview.listener?.onTap(PointF(info.x, info.y)) ?: false
}
})

Expand All @@ -115,9 +115,13 @@ class R2FXLPageFragment : Fragment() {
}

override fun onDestroyView() {
super.onDestroyView()
for (webView in webViews) {
webView.listener = null
}
_singleBinding = null
_doubleBinding = null

super.onDestroyView()
}

@SuppressLint("SetJavaScriptEnabled")
Expand Down