Skip to content

Commit f271c78

Browse files
authored
Reimplement the EPUB selection menu with selectionActionModeCallback (#455)
1 parent 95a4e95 commit f271c78

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

test-app/r2-testapp/src/main/java/org/readium/r2/testapp/reader/BaseReaderFragment.kt

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import kotlinx.coroutines.flow.onEach
2323
import kotlinx.parcelize.Parcelize
2424
import org.readium.r2.lcp.lcpLicense
2525
import org.readium.r2.navigator.*
26+
import org.readium.r2.navigator.util.BaseActionModeCallback
2627
import org.readium.r2.shared.publication.Locator
28+
import org.readium.r2.shared.publication.services.isProtected
2729
import org.readium.r2.testapp.R
2830
import org.readium.r2.testapp.databinding.FragmentReaderBinding
2931
import org.readium.r2.testapp.domain.model.Highlight
@@ -173,35 +175,37 @@ abstract class BaseReaderFragment : Fragment() {
173175
R.id.purple to Color.rgb(182, 153, 255),
174176
)
175177

176-
fun onActionModeStarted(mode: ActionMode?, menuInflater: MenuInflater) {
177-
val viewScope = viewLifecycleOwner.lifecycleScope
178-
val navigator = (navigator as? SelectableNavigator) ?: return
179-
180-
this.mode = mode?.apply {
181-
// Remove the default text selection menu items and set our own menu.
182-
menu.clear()
183-
menuInflater.inflate(R.menu.menu_action_mode, menu)
184-
185-
fun showHighlightPopupWithStyle(style: Highlight.Style) = viewScope.launchWhenResumed {
186-
// Get the rect of the current selection to know where to position the highlight
187-
// popup.
188-
navigator.currentSelection()?.rect?.let { selectionRect ->
189-
showHighlightPopup(selectionRect, style)
190-
}
191-
}
178+
val customSelectionActionModeCallback: ActionMode.Callback by lazy { SelectionActionModeCallback() }
192179

193-
menu.findItem(R.id.highlight).setOnMenuItemClickListener {
194-
showHighlightPopupWithStyle(Highlight.Style.HIGHLIGHT)
195-
true
180+
private inner class SelectionActionModeCallback : BaseActionModeCallback() {
181+
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
182+
mode.menuInflater.inflate(R.menu.menu_action_mode, menu)
183+
if (navigator is DecorableNavigator) {
184+
menu.findItem(R.id.highlight).isVisible = true
185+
menu.findItem(R.id.underline).isVisible = true
186+
menu.findItem(R.id.note).isVisible = true
196187
}
197-
menu.findItem(R.id.underline).setOnMenuItemClickListener {
198-
showHighlightPopupWithStyle(Highlight.Style.UNDERLINE)
199-
true
200-
}
201-
menu.findItem(R.id.note).setOnMenuItemClickListener {
202-
showAnnotationPopup()
203-
true
188+
return true
189+
}
190+
191+
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
192+
when (item.itemId) {
193+
R.id.highlight -> showHighlightPopupWithStyle(Highlight.Style.HIGHLIGHT)
194+
R.id.underline -> showHighlightPopupWithStyle(Highlight.Style.UNDERLINE)
195+
R.id.note -> showAnnotationPopup()
196+
else -> return false
204197
}
198+
199+
mode.finish()
200+
return true
201+
}
202+
}
203+
204+
private fun showHighlightPopupWithStyle(style: Highlight.Style) = viewLifecycleOwner.lifecycleScope.launchWhenResumed {
205+
// Get the rect of the current selection to know where to position the highlight
206+
// popup.
207+
(navigator as? SelectableNavigator)?.currentSelection()?.rect?.let { selectionRect ->
208+
showHighlightPopup(selectionRect, style)
205209
}
206210
}
207211

@@ -319,7 +323,9 @@ abstract class BaseReaderFragment : Fragment() {
319323
} else {
320324
val tint = highlightTints.values.random()
321325
findViewById<View>(R.id.sidemark).setBackgroundColor(tint)
322-
val selection = (navigator as? SelectableNavigator)?.currentSelection() ?: return@launchWhenResumed
326+
val navigator = navigator as? SelectableNavigator ?: return@launchWhenResumed
327+
val selection = navigator.currentSelection() ?: return@launchWhenResumed
328+
navigator.clearSelection()
323329
findViewById<TextView>(R.id.select_text).text = selection.locator.text.highlight
324330

325331
findViewById<TextView>(R.id.positive).setOnClickListener {

test-app/r2-testapp/src/main/java/org/readium/r2/testapp/reader/EpubReaderFragment.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class EpubReaderFragment : VisualReaderFragment(), EpubNavigatorFragment.Listene
9191
config = EpubNavigatorFragment.Configuration().apply {
9292
// Register the HTML template for our custom [DecorationStyleAnnotationMark].
9393
decorationTemplates[DecorationStyleAnnotationMark::class] = annotationMarkTemplate(activity)
94+
selectionActionModeCallback = customSelectionActionModeCallback
9495
}
9596
)
9697

test-app/r2-testapp/src/main/java/org/readium/r2/testapp/reader/ReaderActivity.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,6 @@ open class ReaderActivity : AppCompatActivity() {
121121
}
122122
}
123123

124-
override fun onActionModeStarted(mode: ActionMode?) {
125-
super.onActionModeStarted(mode)
126-
readerFragment.onActionModeStarted(mode, menuInflater)
127-
}
128-
129124
override fun getDefaultViewModelProviderFactory(): ViewModelProvider.Factory {
130125
return modelFactory
131126
}

test-app/r2-testapp/src/main/res/menu/menu_action_mode.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
<item
66
android:id="@+id/highlight"
77
android:title="@string/action_mode_menu_highlight"
8+
android:visible="false"
89
app:showAsAction="ifRoom">
910
</item>
1011

1112
<item
1213
android:id="@+id/underline"
1314
android:title="@string/action_mode_menu_underline"
15+
android:visible="false"
1416
app:showAsAction="ifRoom">
1517
</item>
1618

1719
<item
1820
android:id="@+id/note"
1921
android:title="@string/action_mode_menu_note"
22+
android:visible="false"
2023
app:showAsAction="ifRoom">
2124
</item>
2225

0 commit comments

Comments
 (0)