Skip to content

Commit 77c3187

Browse files
committed
Refactor flags
1 parent d615e39 commit 77c3187

File tree

4 files changed

+41
-80
lines changed

4 files changed

+41
-80
lines changed

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import org.json.JSONObject
3535
import org.jsoup.Jsoup
3636
import org.jsoup.safety.Safelist
3737
import org.readium.r2.navigator.extensions.optRectF
38-
import org.readium.r2.navigator.input.InputModifiers
38+
import org.readium.r2.navigator.input.InputModifier
3939
import org.readium.r2.navigator.input.Key
4040
import org.readium.r2.navigator.input.KeyEvent
4141
import org.readium.r2.shared.InternalReadiumApi
@@ -457,7 +457,7 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
457457
else -> return false
458458
},
459459
key = Key(jsonObject.optString("code")),
460-
modifiers = InputModifiers(jsonObject)
460+
modifiers = inputModifiers(jsonObject)
461461
)
462462

463463
return listener?.onKey(event) ?: false
@@ -693,19 +693,18 @@ open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebView(conte
693693
}
694694
}
695695

696-
private operator fun InputModifiers.Companion.invoke(json: JSONObject): InputModifiers {
697-
var modifiers = None
698-
if (json.optBoolean("alt")) {
699-
modifiers += Alt
700-
}
701-
if (json.optBoolean("control")) {
702-
modifiers += Control
703-
}
704-
if (json.optBoolean("shift")) {
705-
modifiers += Shift
706-
}
707-
if (json.optBoolean("meta")) {
708-
modifiers += Meta
696+
private fun inputModifiers(json: JSONObject): Set<InputModifier> =
697+
buildSet {
698+
if (json.optBoolean("alt")) {
699+
add(InputModifier.Alt)
700+
}
701+
if (json.optBoolean("control")) {
702+
add(InputModifier.Control)
703+
}
704+
if (json.optBoolean("shift")) {
705+
add(InputModifier.Shift)
706+
}
707+
if (json.optBoolean("meta")) {
708+
add(InputModifier.Meta)
709+
}
709710
}
710-
return modifiers
711-
}
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
package org.readium.r2.navigator.input
22

33
/**
4-
* Represents a set of modifiers for an input event.
4+
* Represents a key modifier for an input event.
55
*/
6-
@JvmInline
7-
value class InputModifiers(val value: Int) {
8-
9-
companion object {
10-
val None = InputModifiers(0)
11-
val Alt = InputModifiers(1 shl 0)
12-
val Control = InputModifiers(1 shl 1)
13-
val Meta = InputModifiers(1 shl 2)
14-
val Shift = InputModifiers(1 shl 3)
15-
}
16-
17-
fun contains(other: InputModifiers): Boolean =
18-
(value and other.value) == other.value
19-
20-
operator fun plus(other: InputModifiers): InputModifiers =
21-
InputModifiers(value or other.value)
6+
enum class InputModifier {
7+
Alt, Control, Meta, Shift
228
}

readium/navigator/src/main/java/org/readium/r2/navigator/input/KeyEvent.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import android.view.KeyEvent as AndroidKeyEvent
1212
data class KeyEvent(
1313
val type: Type,
1414
val key: Key,
15-
val modifiers: InputModifiers
15+
val modifiers: Set<InputModifier> = emptySet()
1616
) {
1717

1818
enum class Type {
@@ -24,7 +24,7 @@ data class KeyEvent(
2424
return KeyEvent(
2525
type = type,
2626
key = Key(event) ?: return null,
27-
modifiers = InputModifiers(event)
27+
modifiers = inputModifiers(event)
2828
)
2929
}
3030
}
@@ -346,19 +346,18 @@ value class Key(val code: String) {
346346
}
347347
}
348348

349-
private operator fun InputModifiers.Companion.invoke(event: android.view.KeyEvent): InputModifiers {
350-
var modifiers = None
351-
if (event.isAltPressed) {
352-
modifiers += Alt
353-
}
354-
if (event.isCtrlPressed) {
355-
modifiers += Control
356-
}
357-
if (event.isMetaPressed) {
358-
modifiers += Meta
359-
}
360-
if (event.isShiftPressed) {
361-
modifiers += Shift
349+
private fun inputModifiers(event: android.view.KeyEvent): Set<InputModifier> =
350+
buildSet {
351+
if (event.isAltPressed) {
352+
add(InputModifier.Alt)
353+
}
354+
if (event.isCtrlPressed) {
355+
add(InputModifier.Control)
356+
}
357+
if (event.isMetaPressed) {
358+
add(InputModifier.Meta)
359+
}
360+
if (event.isShiftPressed) {
361+
add(InputModifier.Shift)
362+
}
362363
}
363-
return modifiers
364-
}

readium/navigator/src/main/java/org/readium/r2/navigator/util/DirectionalNavigationAdapter.kt

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import org.readium.r2.navigator.VisualNavigator
44
import org.readium.r2.navigator.goLeft
55
import org.readium.r2.navigator.goRight
66
import org.readium.r2.navigator.input.InputListener
7-
import org.readium.r2.navigator.input.InputModifiers
87
import org.readium.r2.navigator.input.Key
98
import org.readium.r2.navigator.input.KeyEvent
109
import org.readium.r2.navigator.input.TapEvent
@@ -34,7 +33,7 @@ import org.readium.r2.shared.ExperimentalReadiumApi
3433
*/
3534
@ExperimentalReadiumApi
3635
class DirectionalNavigationAdapter(
37-
private val tapEdges: TapEdges = TapEdges.Horizontal,
36+
private val tapEdges: Set<TapEdge> = setOf(TapEdge.Horizontal),
3837
private val handleTapsWhileScrolling: Boolean = false,
3938
private val minimumHorizontalEdgeSize: Double = 80.0,
4039
private val horizontalEdgeThresholdPercent: Double? = 0.3,
@@ -46,38 +45,16 @@ class DirectionalNavigationAdapter(
4645
/**
4746
* Indicates which viewport edges trigger page turns on tap.
4847
*/
49-
@JvmInline
50-
value class TapEdges(val value: Int) {
51-
companion object {
52-
/** The user cannot turn pages by tapping on the edges. */
53-
val None = TapEdges(0)
54-
55-
/** The user can turn pages when tapping on the left and right edges. */
56-
val Horizontal = TapEdges(1 shl 0)
57-
58-
/** The user can turn pages when tapping on the top and bottom edges. */
59-
val Vertical = TapEdges(1 shl 1)
60-
61-
/**
62-
* The user can turn pages when tapping on the edges of both the horizontal and vertical
63-
* axes.
64-
*/
65-
val All = TapEdges(Horizontal.value or Vertical.value)
66-
}
67-
68-
fun contains(other: TapEdges): Boolean =
69-
(value and other.value) == other.value
70-
71-
operator fun plus(other: TapEdges): TapEdges =
72-
TapEdges(value or other.value)
48+
enum class TapEdge {
49+
Horizontal, Vertical;
7350
}
7451

7552
override fun onTap(navigator: VisualNavigator, event: TapEvent): Boolean {
7653
if (navigator.presentation.value.scroll && !handleTapsWhileScrolling) {
7754
return false
7855
}
7956

80-
if (tapEdges.contains(TapEdges.Horizontal)) {
57+
if (tapEdges.contains(TapEdge.Horizontal)) {
8158
val width = navigator.publicationView.width.toDouble()
8259

8360
val horizontalEdgeSize = horizontalEdgeThresholdPercent?.let {
@@ -93,7 +70,7 @@ class DirectionalNavigationAdapter(
9370
}
9471
}
9572

96-
if (tapEdges.contains(TapEdges.Vertical)) {
73+
if (tapEdges.contains(TapEdge.Vertical)) {
9774
val height = navigator.publicationView.height.toDouble()
9875

9976
val verticalEdgeSize = verticalEdgeThresholdPercent?.let {
@@ -113,7 +90,7 @@ class DirectionalNavigationAdapter(
11390
}
11491

11592
override fun onKey(navigator: VisualNavigator, event: KeyEvent): Boolean {
116-
if (event.type != KeyEvent.Type.Down || event.modifiers != InputModifiers.None) {
93+
if (event.type != KeyEvent.Type.Down || !event.modifiers.isEmpty()) {
11794
return false
11895
}
11996

0 commit comments

Comments
 (0)