Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/primary_selection.scoop

Selected production source · src/text/editor/primary_selection.scoop

1package scoop.ui.text
3import scoop.graphics.Point
4import scoop.surface.FocusOrigin
5import scoop.window.Utf8Range
7internal fun editorPastePrimary(host: TextEditHost, local: Point?): Boolean {
8 if (
9 host.closed ||
10 !host.options.enabled ||
11 host.options.readOnly ||
12 !host.area.scope.isInputAllowed ||
13 !editorTouchReady(host)
14 )
15 return false
16 val point = local ?: return false
17 val frame = host.accepted ?: return false
18 val state = host.current
19 val revision = state.revision
20 if (frame.display.source.text != state.text)
21 return false
22 val displayed = frame.layout.hitTest(point)
23 val position = frame.display.documentPosition(frame.display.mapping.reverse(displayed))
24 // Reading first preserves the old PRIMARY even when this click selects another position.
25 val text = host.primarySelection.read(host.options.primarySelection) ?: return false
26 val sameFrame =
27 when (host.accepted) {
28 case Some(val current) -> current === frame
29 case None -> false
30 }
31 if (
32 host.closed ||
33 host.current !==
34 state ||
35 state.revision !=
36 revision ||
37 !sameFrame ||
38 !host.options.enabled ||
39 host.options.readOnly ||
40 !host.area.scope.isInputAllowed
41 )
42 return false
43 host.area.requestFocus(FocusOrigin.Pointer)
44 if (
45 host.closed ||
46 host.current !==
47 state ||
48 state.revision !=
49 revision ||
50 !host.options.enabled ||
51 host.options.readOnly ||
52 !host.area.scope.isInputAllowed ||
53 !editorTouchReady(host)
54 )
55 return true
56 editorStopDrag(host, false)
57 host.perform(
58 {
59 editorApplyEdit(
60 host,
61 Utf8Range(position.byteOffset, position.byteOffset),
62 text,
63 TextEditSource.Paste
64 )
65 },
66 endComposition = true
67 )
68 return true