scoop:scoop.ui:0.1.0
src/text/menu/selectable.scoop
Selected production source · src/text/menu/selectable.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.surface.*6
import scoop.window.*8
internal fun selectableMenuReady(host: SelectableTextHost): Boolean {9
if (10
host.closed ||11
!host.scope.isMounted ||12
!host.options.enabled ||13
!host.scope.nativeWindow.isFocused ||14
!host.area.scope.activity.hasFocus15
)16
return false17
return when (host.inputFrame()) {18
case Some(_) -> true19
case None -> false20
}21
}23
internal fun selectableContextMenu(24
host: SelectableTextHost,25
pointer: PointerState?,26
local: Point?,27
touch: Boolean = false28
): Boolean {29
if (30
host.closed ||31
!host.scope.isMounted ||32
!host.options.enabled ||33
(!touch && host.options.contextMenu.mode == TextContextMenuMode.Disabled) ||34
host.contextMenu.pending ||35
!host.area.scope.isInputAllowed ||36
host.scope.nativeWindow.state !=37
WindowState.Open ||38
host.scope.nativeWindow.isInputBlocked39
)40
return false41
val frame = host.inputFrame() ?: return false42
val document = host.content43
val bounds = host.area.visibleBounds ?: return false44
if (bounds.width <= 0.0 || bounds.height <= 0.0)45
return false46
when (pointer) {47
case Some(_) -> if (!host.area.requestFocus(FocusOrigin.Pointer))48
return false49
case None -> {}50
}51
if (!host.area.scope.activity.hasFocus || !sameMenuText(document, host.content))52
return false53
val options = host.options.contextMenu54
val popup = options.mode != TextContextMenuMode.Disabled55
if (popup && !host.contextMenu.eligible())56
return false57
val template =58
if (popup)59
Some(60
host.scope.application.textContextMenuTemplate(61
options.mode == TextContextMenuMode.PreferSystem,62
options.labels63
)64
)65
else66
None67
selectableStopDrag(host)68
host.preferredX = None69
if (touch)70
host.touchHandles.show()71
val placement =72
when (pointer) {73
case Some(val value) -> {74
val point = local ?: return false75
val range = host.selection.range76
var inside = false77
for (box in frame.layout.selection(TextRange(range.start, range.end)))78
if (textMenuContains(box.rect, point))79
inside = true80
if (!inside && options.selection != TextContextMenuSelection.Preserve) {81
val position = frame.layout.hitTest(point)82
if (textMenuSelectsWord(options, template, touch)) {83
val word = frame.layout.wordRange(position)84
host.publishSelection(85
EditorSelection(86
TextPosition(word.start),87
TextPosition(word.end, TextAffinity.Upstream)88
),89
cancelContacts = !touch90
)91
} else92
host.publishSelection(93
EditorSelection(position, position),94
cancelContacts = !touch95
)96
}97
PopupPlacement.Pointer(value.logicalPosition)98
}99
case None -> {100
val anchor = selectableMenuAnchor(host, frame, bounds) ?: return false101
PopupPlacement.Anchor(102
LogicalRect(103
LogicalPosition(anchor.x, anchor.y),104
LogicalSize(anchor.width, anchor.height)105
)106
)107
}108
}109
if (host.closed || !host.scope.isMounted || !sameMenuText(document, host.content))110
return false111
return if (popup)112
openSelectableTextMenu(host, template!!, placement)113
else114
true115
}117
private fun sameMenuText(before: StyledText, after: StyledText): Boolean =118
before === after || (before.text == after.text && before.toPlainText() == after.toPlainText())120
internal fun selectableMenuAnchor(121
host: SelectableTextHost,122
frame: SelectableFrame,123
bounds: Rect124
): Rect? {125
val range = host.selection.range126
for (box in frame.layout.selection(TextRange(range.start, range.end))) {127
when (val anchor = textMenuAnchor(host.area, box.rect, bounds)) {128
case Some(val rect) -> return Some(rect)129
case None -> {}130
}131
}132
val caret = frame.layout.caret(host.cursor) ?: return None133
return textMenuAnchor(host.area, caret, bounds)134
}