Scoop API

scoop:scoop.ui:0.1.0

src/text/menu/request.scoop

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

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.*
6import scoop.ui.*
7import scoop.window.*
9internal fun editorContextMenu(
10 host: TextEditHost,
11 pointer: PointerState?,
12 local: Point?,
13 touch: Boolean = false
14): 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.isInputBlocked
25 )
26 return false
27 val frame = host.accepted ?: return false
28 val document = host.current
29 if (frame.display.source.text != host.current.text)
30 return false
31 val bounds = host.area.visibleBounds ?: return false
32 if (bounds.width <= 0.0 || bounds.height <= 0.0)
33 return false
34 when (pointer) {
35 case Some(_) -> if (!host.area.requestFocus(FocusOrigin.Pointer))
36 return false
37 case None -> {}
38 }
39 if (
40 !host.area.isFocusActive ||
41 host.current !==
42 document ||
43 frame.display.source.text !=
44 host.current.text
45 )
46 return false
47 val options = host.options.contextMenu
48 val popup = options.mode != TextContextMenuMode.Disabled
49 if (popup && !host.contextMenu.eligible())
50 return false
51 val template =
52 if (popup)
53 Some(
54 host.scope.application.textContextMenuTemplate(
55 options.mode == TextContextMenuMode.PreferSystem,
56 options.labels
57 )
58 )
59 else
60 None
61 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 false
68 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 = false
72 for (box in selected)
73 if (textMenuContains(box.rect, point))
74 inside = true
75 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 = !touch
96 )
97 }
98 PopupPlacement.Pointer(value.logicalPosition)
99 }
100 case None -> {
101 val rect = editorMenuAnchor(host, frame, bounds) ?: return false
102 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 false
112 return if (popup)
113 openEditorTextMenu(host, template!!, placement)
114 else
115 true
118internal 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 None
133internal 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.height
149internal 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 )
164internal fun textMenuSelectsWord(
165 options: TextContextMenuOptions,
166 template: TextContextMenuTemplate?,
167 touch: Boolean
168): Boolean =
169 options.selection ==
170 TextContextMenuSelection.SelectWord ||
171 (
172 options.selection ==
173 TextContextMenuSelection.Platform &&
174 (touch || (template?.selectWordOutsideSelection ?: false))
175 )
177internal fun textMenuAnchor(area: InputArea, local: Rect, bounds: Rect): Rect? {
178 val rect = area.toWindow(local) ?: return None
179 val left =
180 if (rect.x > bounds.x)
181 rect.x
182 else
183 bounds.x
184 val top =
185 if (rect.y > bounds.y)
186 rect.y
187 else
188 bounds.y
189 val right =
190 if (rect.x + rect.width < bounds.x + bounds.width)
191 rect.x + rect.width
192 else
193 bounds.x + bounds.width
194 val bottom =
195 if (rect.y + rect.height < bounds.y + bounds.height)
196 rect.y + rect.height
197 else
198 bounds.y + bounds.height
199 return if (right > left && bottom > top)
200 Some(Rect(left, top, right - left, bottom - top))
201 else
202 None