Scoop API

scoop:scoop.ui:0.1.0

src/text/menu/selectable.scoop

Selected production source · src/text/menu/selectable.scoop

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.*
6import scoop.window.*
8internal 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.hasFocus
15 )
16 return false
17 return when (host.inputFrame()) {
18 case Some(_) -> true
19 case None -> false
20 }
23internal fun selectableContextMenu(
24 host: SelectableTextHost,
25 pointer: PointerState?,
26 local: Point?,
27 touch: Boolean = false
28): 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.isInputBlocked
39 )
40 return false
41 val frame = host.inputFrame() ?: return false
42 val document = host.content
43 val bounds = host.area.visibleBounds ?: return false
44 if (bounds.width <= 0.0 || bounds.height <= 0.0)
45 return false
46 when (pointer) {
47 case Some(_) -> if (!host.area.requestFocus(FocusOrigin.Pointer))
48 return false
49 case None -> {}
50 }
51 if (!host.area.scope.activity.hasFocus || !sameMenuText(document, host.content))
52 return false
53 val options = host.options.contextMenu
54 val popup = options.mode != TextContextMenuMode.Disabled
55 if (popup && !host.contextMenu.eligible())
56 return false
57 val template =
58 if (popup)
59 Some(
60 host.scope.application.textContextMenuTemplate(
61 options.mode == TextContextMenuMode.PreferSystem,
62 options.labels
63 )
64 )
65 else
66 None
67 selectableStopDrag(host)
68 host.preferredX = None
69 if (touch)
70 host.touchHandles.show()
71 val placement =
72 when (pointer) {
73 case Some(val value) -> {
74 val point = local ?: return false
75 val range = host.selection.range
76 var inside = false
77 for (box in frame.layout.selection(TextRange(range.start, range.end)))
78 if (textMenuContains(box.rect, point))
79 inside = true
80 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 = !touch
90 )
91 } else
92 host.publishSelection(
93 EditorSelection(position, position),
94 cancelContacts = !touch
95 )
96 }
97 PopupPlacement.Pointer(value.logicalPosition)
98 }
99 case None -> {
100 val anchor = selectableMenuAnchor(host, frame, bounds) ?: return false
101 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 false
111 return if (popup)
112 openSelectableTextMenu(host, template!!, placement)
113 else
114 true
117private fun sameMenuText(before: StyledText, after: StyledText): Boolean =
118 before === after || (before.text == after.text && before.toPlainText() == after.toPlainText())
120internal fun selectableMenuAnchor(
121 host: SelectableTextHost,
122 frame: SelectableFrame,
123 bounds: Rect
124): Rect? {
125 val range = host.selection.range
126 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 None
133 return textMenuAnchor(host.area, caret, bounds)