Skip to content

Commit 9443a99

Browse files
authored
EPUB Navigator: Fix the rect of the current selection (#63)
1 parent 1176cc9 commit 9443a99

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ All notable changes to this project will be documented in this file. Take a look
3636

3737
* Fixed turning pages of an EPUB reflowable resource with an odd number of columns. A virtual blank trailing column is appended to the resource when displayed as two columns.
3838
* EPUB: Fallback on `reflowable` if the `presentation.layout` hint is missing from a manifest.
39+
* EPUB: Offset of the current selection's `rect` to take into account the vertical padding.
3940
4041
4142
## [2.1.1]

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,33 @@ class EpubNavigatorFragment private constructor(
366366
?.let { tryOrLog { JSONObject(it) } }
367367
?: return null
368368

369+
val rect = json.optRectF("rect")
370+
?.apply { adjustToViewport() }
371+
369372
return Selection(
370373
locator = currentLocator.value.copy(
371374
text = Locator.Text.fromJSON(json.optJSONObject("text"))
372375
),
373-
rect = json.optRectF("rect")
376+
rect = rect
374377
)
375378
}
376379

377380
override fun clearSelection() {
378381
run(viewModel.clearSelection())
379382
}
380383

384+
private fun PointF.adjustToViewport() {
385+
currentFragment?.paddingTop?.let { top ->
386+
y += top
387+
}
388+
}
389+
390+
private fun RectF.adjustToViewport() {
391+
currentFragment?.paddingTop?.let { top ->
392+
this.top += top
393+
}
394+
}
395+
381396
// DecorableNavigator
382397

383398
override fun <T : Decoration.Style> supportsDecorationStyle(style: KClass<T>): Boolean =
@@ -452,14 +467,13 @@ class EpubNavigatorFragment private constructor(
452467
}
453468

454469
override fun onTap(point: PointF): Boolean {
470+
point.adjustToViewport()
455471
return listener?.onTap(point) ?: false
456472
}
457473

458474
override fun onDecorationActivated(id: DecorationId, group: String, rect: RectF, point: PointF): Boolean {
459-
currentFragment?.paddingTop?.let { top ->
460-
rect.top += top
461-
point.y += top
462-
}
475+
rect.adjustToViewport()
476+
point.adjustToViewport()
463477
return viewModel.onDecorationActivated(id, group, rect, point)
464478
}
465479

0 commit comments

Comments
 (0)