Skip to content

Commit

Permalink
feat: replace span single click with Ctrl+Click and ignore cursor pos…
Browse files Browse the repository at this point in the history
…ition when the user is using mouse
  • Loading branch information
Rosemoe committed Feb 11, 2024
1 parent 55bb553 commit 415fe59
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,38 @@ open class EditorSpanInteractionHandler(val editor: CodeEditor) {

init {
eventManager.subscribeAlways<ClickEvent> { event ->
handleInteractionEvent(
event,
SpanInteractionInfo::isClickable,
::handleSpanClick
)
if (!event.isFromMouse || (event.isFromMouse && editor.keyMetaStates.isCtrlPressed)) {
handleInteractionEvent(
event,
SpanInteractionInfo::isClickable,
::handleSpanClick,
!event.isFromMouse
)
}
}
eventManager.subscribeAlways<DoubleClickEvent> { event ->
handleInteractionEvent(
event,
SpanInteractionInfo::isDoubleClickable,
::handleSpanDoubleClick
::handleSpanDoubleClick,
!event.isFromMouse
)
}
eventManager.subscribeAlways<LongPressEvent> { event ->
handleInteractionEvent(
event,
SpanInteractionInfo::isLongClickable,
::handleSpanLongClick
::handleSpanLongClick,
!event.isFromMouse
)
}
}

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
Expand All @@ -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<SpanInteractionInfo>(SpanExtAttrs.EXT_INTERACTION_INFO)?.let {
if (predicate(it)) {
if (handler(span, it, spanRange)) {
Expand Down

0 comments on commit 415fe59

Please sign in to comment.