scoop:scoop.ui:0.1.0
src/text/menu/request.scoop
Selected production source · src/text/menu/request.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.surface.*6
import scoop.ui.*7
import scoop.window.*9
internal fun editorContextMenu(10
host: TextEditHost,11
pointer: PointerState?,12
local: Point?,13
touch: Boolean = false14
): Boolean {15
if (16
host.closed ||17
!host.scope.isMounted ||18
!host.options.enabled ||19
(!touch && host.options.contextMenu.mode == TextContextMenuMode.Disabled) ||20
host.contextMenu.pending ||21
!host.area.scope.isInputAllowed ||22
host.window.state !=23
WindowState.Open ||24
host.window.isInputBlocked25
)26
return false27
val frame = host.accepted ?: return false28
val document = host.current29
if (frame.display.source.text != host.current.text)30
return false31
val bounds = host.area.visibleBounds ?: return false32
if (bounds.width <= 0.0 || bounds.height <= 0.0)33
return false34
when (pointer) {35
case Some(_) -> if (!host.area.requestFocus(FocusOrigin.Pointer))36
return false37
case None -> {}38
}39
if (40
!host.area.isFocusActive ||41
host.current !==42
document ||43
frame.display.source.text !=44
host.current.text45
)46
return false47
val options = host.options.contextMenu48
val popup = options.mode != TextContextMenuMode.Disabled49
if (popup && !host.contextMenu.eligible())50
return false51
val template =52
if (popup)53
Some(54
host.scope.application.textContextMenuTemplate(55
options.mode == TextContextMenuMode.PreferSystem,56
options.labels57
)58
)59
else60
None61
editorStopDrag(host, false)62
if (touch)63
host.touchHandles.show()64
val placement =65
when (pointer) {66
case Some(val value) -> {67
val point = local ?: return false68
val input = EditorInputLayout(frame.layout, frame.display, frame.caretStyle)69
val selected =70
frame.layout.selection(frame.display.displayRange(host.current.selection.range))71
var inside = false72
for (box in selected)73
if (textMenuContains(box.rect, point))74
inside = true75
if (!inside && options.selection != TextContextMenuSelection.Preserve) {76
val displayed = frame.layout.hitTest(point)77
val selection =78
if (textMenuSelectsWord(options, template, touch)) {79
val range =80
editorProjectedRange(input, frame.layout.wordRange(displayed))81
EditorSelection(82
TextPosition(range.start),83
TextPosition(range.end, TextAffinity.Upstream)84
)85
} else {86
val position =87
frame.display.documentPosition(88
frame.display.mapping.reverse(displayed)89
)90
EditorSelection(position, position)91
}92
host.perform(93
{ editorSetSelection(host, selection) },94
endComposition = true,95
cancelContacts = !touch96
)97
}98
PopupPlacement.Pointer(value.logicalPosition)99
}100
case None -> {101
val rect = editorMenuAnchor(host, frame, bounds) ?: return false102
PopupPlacement.Anchor(103
LogicalRect(104
LogicalPosition(rect.x, rect.y),105
LogicalSize(rect.width, rect.height)106
)107
)108
}109
}110
if (host.closed || !host.scope.isMounted || host.current !== document)111
return false112
return if (popup)113
openEditorTextMenu(host, template!!, placement)114
else115
true116
}118
internal fun editorMenuAnchor(host: TextEditHost, frame: EditorFrame, bounds: Rect): Rect? {119
when (val caret = host.area.visibleCaretBounds) {120
case Some(val rect) -> if (rect.height > 0.0)121
return Some(rect)122
case None -> {}123
}124
for (box in frame.layout.selection(frame.display.displayRange(host.current.selection.range))) {125
when (val anchor = textMenuAnchor(host.area, box.rect, bounds)) {126
case Some(val rect) -> return Some(rect)127
case None -> {}128
}129
}130
return None131
}133
internal fun textMenuContains(rect: Rect, point: Point): Boolean =134
rect.width >135
0.0 &&136
rect.height >137
0.0 &&138
point.x >=139
rect.x &&140
point.y >=141
rect.y &&142
point.x <143
rect.x +144
rect.width &&145
point.y <146
rect.y +147
rect.height149
internal fun textMenuKey(key: KeyEvent): Boolean =150
key.state ==151
ElementState.Pressed &&152
!key.synthetic &&153
!key.isRepeat &&154
!key.modifiers.control &&155
!key.modifiers.alt &&156
!key.modifiers.superKey &&157
key.modifiers.altGraph !=158
Some(true) &&159
(160
(key.logicalKey == LogicalKey.Named(NamedKey.ContextMenu) && !key.modifiers.shift) ||161
(key.logicalKey == LogicalKey.Named(NamedKey.F10) && key.modifiers.shift)162
)164
internal fun textMenuSelectsWord(165
options: TextContextMenuOptions,166
template: TextContextMenuTemplate?,167
touch: Boolean168
): Boolean =169
options.selection ==170
TextContextMenuSelection.SelectWord ||171
(172
options.selection ==173
TextContextMenuSelection.Platform &&174
(touch || (template?.selectWordOutsideSelection ?: false))175
)177
internal fun textMenuAnchor(area: InputArea, local: Rect, bounds: Rect): Rect? {178
val rect = area.toWindow(local) ?: return None179
val left =180
if (rect.x > bounds.x)181
rect.x182
else183
bounds.x184
val top =185
if (rect.y > bounds.y)186
rect.y187
else188
bounds.y189
val right =190
if (rect.x + rect.width < bounds.x + bounds.width)191
rect.x + rect.width192
else193
bounds.x + bounds.width194
val bottom =195
if (rect.y + rect.height < bounds.y + bounds.height)196
rect.y + rect.height197
else198
bounds.y + bounds.height199
return if (right > left && bottom > top)200
Some(Rect(left, top, right - left, bottom - top))201
else202
None203
}