Skip to content

Commit 8ccb5d3

Browse files
authored
Merge pull request #1096 from wordpress-mobile/improve-checkboxes
Intercept touch event to task list checkboxes to handle it before the cursor is moved
2 parents f749d3b + 8a6aa05 commit 8ccb5d3

File tree

7 files changed

+52
-10
lines changed

7 files changed

+52
-10
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,41 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
718718
return containingBoxBounds
719719
}
720720

721+
private fun getTaskListHandler(): TaskListClickHandler? {
722+
return EnhancedMovementMethod.taskListClickHandler
723+
}
724+
721725
override fun onTouchEvent(event: MotionEvent): Boolean {
726+
var x = event.x.toInt()
727+
var y = event.y.toInt()
728+
729+
x -= totalPaddingLeft
730+
y -= totalPaddingTop
731+
732+
x += scrollX
733+
y += scrollY
734+
735+
// We check whether the user tap on a checkbox of a task list. The aztec text field should be enabled and we
736+
// check whether the tap event was on the leading margin which is where the checkbox is located plus a padding
737+
// space used to increase the target size for the tap event.
738+
if (isEnabled &&
739+
x + totalPaddingStart <= (blockFormatter.listStyleLeadingMargin() + AztecTaskListSpan.PADDING_SPACE)) {
740+
val line = layout.getLineForVertical(y)
741+
val off = layout.getOffsetForHorizontal(line, x.toFloat())
742+
// If the tap event was on the leading margin, we double check whether we are tapping on a task list item.
743+
// If that is true, then we return false because we don't want to propagate the tap event to stop moving
744+
// the cursor to the item tapped.
745+
if (getTaskListHandler()?.handleTaskListClick(
746+
text,
747+
off,
748+
x,
749+
totalPaddingStart
750+
) == true) {
751+
refreshText(stealEditorFocus = false)
752+
return false
753+
}
754+
}
755+
722756
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
723757
&& event.action == MotionEvent.ACTION_DOWN) {
724758
// we'll use these values in OnLongClickListener

aztec/src/main/kotlin/org/wordpress/aztec/TaskListClickHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.wordpress.aztec.spans.AztecTaskListSpan
88
class TaskListClickHandler(val listStyle: BlockFormatter.ListStyle) {
99
fun handleTaskListClick(text: Spannable, off: Int, x: Int, startMargin: Int): Boolean {
1010
// We want to make sure that text click will not trigger the checked change
11-
if (x + startMargin > listStyle.leadingMargin()) return false
11+
if (x + startMargin > (listStyle.leadingMargin() + AztecTaskListSpan.PADDING_SPACE)) return false
1212
val clickedList = text.getSpans(off, off, AztecTaskListSpan::class.java).firstOrNull()
1313
val clickedLines = text.getSpans(off, off, AztecListItemSpan::class.java)
1414
val clickedLine = clickedLines.find {

aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ class BlockFormatter(editor: AztecText,
8080
indentFormatter.outdent()
8181
}
8282

83+
fun listStyleLeadingMargin(): Int {
84+
return listStyle.leadingMargin()
85+
}
86+
8387
fun isIndentAvailable(): Boolean {
8488
if (listFormatter.isIndentAvailable()) return true
8589
return indentFormatter.isIndentAvailable()

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecTaskListSpan.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,9 @@ open class AztecTaskListSpan(
144144
}
145145

146146
override val textFormat: ITextFormat = AztecTextFormat.FORMAT_TASK_LIST
147+
148+
companion object {
149+
// Extra padding added to the target tap area for checkboxes.
150+
const val PADDING_SPACE = 15
151+
}
147152
}

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderAdapter.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,5 @@ interface ComposePlaceholderAdapter : PlaceholderManager.PlaceholderAdapter {
1010
* @param attrs aztec attributes of the view
1111
*/
1212
@Composable
13-
fun Placeholder(
14-
placeholderUuid: String,
15-
attrs: AztecAttributes,
16-
)
13+
fun Placeholder(placeholderUuid: String, attrs: AztecAttributes)
1714
}

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderManager.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("ktlint")
2+
13
package org.wordpress.aztec.placeholders
24

35
import android.graphics.Rect
@@ -101,7 +103,7 @@ class ComposePlaceholderManager(
101103
.zIndex(9f)
102104
.padding(
103105
top = with(density) { composeView.topMargin.toDp() },
104-
start = with(density) { composeView.leftMargin.toDp() },
106+
start = with(density) { composeView.leftMargin.toDp() }
105107
)
106108
.width(
107109
with(density) { composeView.width.toDp() }
@@ -113,7 +115,7 @@ class ComposePlaceholderManager(
113115
key(composeView.uuid, composeView.width, composeView.height) {
114116
adapters[composeView.adapterKey]?.Placeholder(
115117
composeView.uuid,
116-
composeView.attrs,
118+
composeView.attrs
117119
)
118120
}
119121
}

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class ViewPlaceholderManager(
9696
* @param type placeholder type
9797
* @param attributes other attributes passed to the view. For example a `src` for an image.
9898
*/
99-
override suspend fun insertItem(type: String, vararg attributes: Pair<String, String>) {
99+
suspend override fun insertItem(type: String, vararg attributes: Pair<String, String>) {
100100
val adapter = adapters[type]
101101
?: throw IllegalArgumentException("Adapter for inserted type not found. Register it with `registerAdapter` method")
102102
val attrs = getAttributesForMedia(type, attributes)
@@ -113,7 +113,7 @@ class ViewPlaceholderManager(
113113
* @param shouldMergeItem this method should return true when the previous type is compatible and should be updated
114114
* @param updateItem function to update current parameters with new params
115115
*/
116-
override suspend fun insertOrUpdateItem(
116+
suspend override fun insertOrUpdateItem(
117117
type: String,
118118
shouldMergeItem: (currentItemType: String) -> Boolean,
119119
updateItem: (
@@ -186,7 +186,7 @@ class ViewPlaceholderManager(
186186
* @param shouldUpdateItem This function should return true if the span can be updated, false if it should be removed
187187
* @param updateItem Function that updates the selected item
188188
*/
189-
override suspend fun removeOrUpdate(uuid: String, shouldUpdateItem: (Attributes) -> Boolean, updateItem: (currentAttributes: Map<String, String>) -> Map<String, String>): Boolean {
189+
suspend override fun removeOrUpdate(uuid: String, shouldUpdateItem: (Attributes) -> Boolean, updateItem: (currentAttributes: Map<String, String>) -> Map<String, String>): Boolean {
190190
val currentItem = aztecText.editableText.getSpans(0, aztecText.length(), AztecPlaceholderSpan::class.java).find {
191191
it.attributes.getValue(UUID_ATTRIBUTE) == uuid
192192
} ?: return false

0 commit comments

Comments
 (0)