@@ -23,7 +23,9 @@ import kotlinx.coroutines.flow.onEach
2323import kotlinx.parcelize.Parcelize
2424import org.readium.r2.lcp.lcpLicense
2525import org.readium.r2.navigator.*
26+ import org.readium.r2.navigator.util.BaseActionModeCallback
2627import org.readium.r2.shared.publication.Locator
28+ import org.readium.r2.shared.publication.services.isProtected
2729import org.readium.r2.testapp.R
2830import org.readium.r2.testapp.databinding.FragmentReaderBinding
2931import 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 {
0 commit comments