From 415fe592243f6b2b691e1a4002f0049cd6c5e305 Mon Sep 17 00:00:00 2001 From: Rosemoe <2073412493@qq.com> Date: Sun, 11 Feb 2024 10:17:54 +0800 Subject: [PATCH] feat: replace span single click with Ctrl+Click and ignore cursor position when the user is using mouse --- .../ext/EditorSpanInteractionHandler.kt | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/editor/src/main/java/io/github/rosemoe/sora/widget/ext/EditorSpanInteractionHandler.kt b/editor/src/main/java/io/github/rosemoe/sora/widget/ext/EditorSpanInteractionHandler.kt index 248b340d6..b6aa42827 100644 --- a/editor/src/main/java/io/github/rosemoe/sora/widget/ext/EditorSpanInteractionHandler.kt +++ b/editor/src/main/java/io/github/rosemoe/sora/widget/ext/EditorSpanInteractionHandler.kt @@ -59,24 +59,29 @@ open class EditorSpanInteractionHandler(val editor: CodeEditor) { init { eventManager.subscribeAlways { event -> - handleInteractionEvent( - event, - SpanInteractionInfo::isClickable, - ::handleSpanClick - ) + if (!event.isFromMouse || (event.isFromMouse && editor.keyMetaStates.isCtrlPressed)) { + handleInteractionEvent( + event, + SpanInteractionInfo::isClickable, + ::handleSpanClick, + !event.isFromMouse + ) + } } eventManager.subscribeAlways { event -> handleInteractionEvent( event, SpanInteractionInfo::isDoubleClickable, - ::handleSpanDoubleClick + ::handleSpanDoubleClick, + !event.isFromMouse ) } eventManager.subscribeAlways { event -> handleInteractionEvent( event, SpanInteractionInfo::isLongClickable, - ::handleSpanLongClick + ::handleSpanLongClick, + !event.isFromMouse ) } } @@ -84,7 +89,8 @@ open class EditorSpanInteractionHandler(val editor: CodeEditor) { private fun handleInteractionEvent( event: EditorMotionEvent, predicate: (interactionInfo: SpanInteractionInfo) -> Boolean, - handler: (Span, SpanInteractionInfo, TextRange) -> Boolean + handler: (Span, SpanInteractionInfo, TextRange) -> Boolean, + checkCursorRange: Boolean = true ) { val regionInfo = editor.resolveTouchRegion(event.causingEvent) val span = event.span @@ -93,7 +99,7 @@ open class EditorSpanInteractionHandler(val editor: CodeEditor) { IntPair.getSecond(regionInfo) == IN_BOUND && span != null && spanRange != null ) { - if (spanRange.isPositionInside(editor.cursor.left())) { + if (!checkCursorRange || spanRange.isPositionInside(editor.cursor.left())) { span.getSpanExt(SpanExtAttrs.EXT_INTERACTION_INFO)?.let { if (predicate(it)) { if (handler(span, it, spanRange)) {