Debian Codegraph @

src/imports/Components/1.3/TextCursor.qml

raw
1 /*
2 * Copyright 2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 1.3 as Lomiri
19 import Lomiri.Components.Popups 1.3
20
21 Lomiri.StyledItem {
22 id: cursorItem
23
24 height: cursorRectangle.height
25
26 /*
27 Property holding the text input's cursor position property. Can be one of
28 the following ones: cursorPosition, selectionStart and selectionEnd.
29 */
30 property string positionProperty: "cursorPosition"
31
32 /*
33 Input handler instance
34 */
35 property InputHandler handler
36
37 /*
38 Cursor delegate used. This is the visual component from the main
39 */
40 readonly property Component cursorDelegate: handler.main.cursorDelegate ?
41 handler.main.cursorDelegate :
42 __styleInstance.cursorDelegate
43
44 styleName: "TextCursorStyle"
45
46 objectName: "textCursor"
47 //Caret instance from the style.
48 property Item caret: __styleInstance.caret
49 property int absX: {
50 return fakeCursor.parent.mapFromItem(handler.main, cursorItem.x, cursorItem.y).x
51 }
52 property int absY: {
53 // Take parent flickable movement into account
54 var flickable = handler.main;
55 do {
56 flickable = flickable.parent;
57 } while (flickable && !flickable.contentY && flickable != fakeCursor.parent);
58 return fakeCursor.parent.mapFromItem(handler.main, cursorItem.x, cursorItem.y).y
59 }
60
61 // Returns "x" or "y" relative to the item handlers are a child of
62 function mappedCursorPosition(coordinate) {
63 var cpos = cursorItem["abs" + coordinate.toUpperCase()];
64 cpos += handler.frameDistance[coordinate];
65 cpos += handler.input[coordinate];
66 cpos -= handler.flickable["content" + coordinate.toUpperCase()];
67 return cpos;
68 }
69 /*
70 The function opens the text input popover setting the text cursor as caller.
71 */
72 Connections {
73 target: handler
74 function onPressAndHold(pos, fromTouch) {
75 // open context menu only for cursorPosition or selectionStart as to
76 // ensure that only one popover gets opened
77 if (positionProperty !== "selectionEnd") {
78 openPopover(fromTouch);
79 }
80 }
81 function onTextModified() { typing = true }
82 function onTap(pos) {
83 typing = false
84 if (handler.popover != null) {
85 PopupUtils.close(handler.popover);
86 }
87 }
88 }
89
90 Binding {
91 target: handler
92 property: "cursorPositionCursor"
93 value: cursorItem
94 }
95
96 function openPopover() {
97 if (!visible
98 || opacity === 0.0
99 || dragger.dragActive) {
100 return;
101 }
102
103 if (handler.popover != null) {
104 PopupUtils.close(handler.popover);
105 }
106
107 var component = handler.main.popover;
108 if (component === undefined)
109 component = Qt.resolvedUrl("TextInputPopover.qml");
110
111 // if the cursor is out of the visible viewport, anchor the
112 // contextual menu to the input field
113 var anchor = caret.visible ? draggedItem : handler.main
114 var popup = PopupUtils.open(component, anchor, {
115 "target": handler.main,
116 });
117 // hide the arrow
118 popup.__foreground.direction = "none";
119 cursorItem.Component.onDestruction.connect(popup.__closePopup);
120 contextMenuVisible = true;
121 popup.onVisibleChanged.connect(contextMenuHidden.bind(undefined, popup));
122 // do not grab focus!
123 popup.__foreground.activeFocusOnPress = false;
124 handler.popover = popup;
125 }
126
127 visible: handler.main.cursorVisible &&
128 !(positionProperty === "cursorPosition" && handler.main.selectedText !== "")
129
130 // cursor visual loader
131 Loader {
132 id: cursorLoader
133 sourceComponent: cursorDelegate
134 height: parent.height
135 }
136
137 /*
138 * Caret dragging handling. We need a separate item which is dragged along the
139 * component's area, which can move freely and not attached to the caret itself.
140 * This area will then be used to update the caret position.
141 */
142 Binding {
143 target: caret
144 when: caret
145 property: "visible"
146 value: QuickUtils.touchScreenAvailable
147 && (contextMenuVisible || !typing)
148 && handler.main.text !== ""
149 }
150 property bool typing: false
151 property bool contextMenuVisible: false
152 property bool readOnly: handler.main.readOnly
153 function contextMenuHidden(p) {
154 contextMenuVisible = false
155 }
156
157 //dragged item
158 property Item draggedItem: Item {
159 objectName: cursorItem.positionProperty + "_draggeditem"
160 width: caret.width + units.gu(2)
161 onWidthChanged: draggedItem.moveToCaret()
162 height: cursorItem.height + caret.height + threshold
163 property real threshold: units.gu(4)
164 parent: fakeCursor.parent
165 visible: caret.visible
166
167 /*
168 Mouse area to turn on dragging or selection mode when pressed
169 on the handler area. The drag mode is turned off when the drag
170 gets inactive or when the LeftButton is released.
171 */
172 MouseArea {
173 id: draggedItemMouseArea
174 objectName: cursorItem.positionProperty + "_activator"
175 anchors.fill: parent
176 acceptedButtons: Qt.LeftButton
177 preventStealing: true
178 cursorShape: Qt.IBeamCursor
179 enabled: parent.width && parent.height && parent.visible && !handler.doubleTapInProgress
180 onPressedChanged: {
181 if (!pressed) {
182 // when the dragging ends, reposition the dragger back to caret
183 draggedItem.moveToCaret();
184 }
185 }
186 Lomiri.Mouse.forwardTo: [dragger]
187 Lomiri.Mouse.onClicked: openPopover()
188 Lomiri.Mouse.onPressAndHold: {
189 handler.main.selectWord();
190 handler.pressAndHold(-1, false);
191 }
192 Lomiri.Mouse.onDoubleClicked: handler.main.selectWord()
193 Lomiri.Mouse.enabled: enabled
194
195 // Visible touch target area for debugging purposes
196 Rectangle {
197 anchors.fill: parent
198 color: 'red'
199 opacity: 0.1
200 visible: false // draggedItemMouseArea.enabled
201 }
202 }
203
204 // aligns the draggedItem to the caret and resets the dragger
205 function moveToCaret() {
206 if (!caret) {
207 return;
208 }
209 // The style may render handlers either on top or bottom
210 var flip = caret.rotation == 180;
211 draggedItem.x = fakeCursor.x + (flip ? -caret.width : -draggedItem.width + caret.width);
212 draggedItem.y = fakeCursor.y + caret.y + caret.height - threshold;
213 }
214 // positions caret to the dragged position
215 function positionCaret() {
216 if (dragger.dragActive) {
217 var dx = dragger.dragStartX + dragger.dragAmountX + handler.flickable.contentX;
218 var dy = dragger.dragStartY + dragger.dragAmountY + handler.flickable.contentY;
219 dx -= handler.frameDistance.x;
220 dy -= handler.frameDistance.y;
221 handler.positionCaret(positionProperty, dx, dy);
222 }
223 }
224 }
225 MouseArea {
226 id: dragger
227 objectName: cursorItem.positionProperty + "_dragger"
228 cursorShape: Qt.IBeamCursor
229 // fill the entire component area
230 parent: handler.main
231 anchors.fill: parent
232 enabled: draggedItemMouseArea.enabled && draggedItemMouseArea.pressed && caret.visible
233 onEnabledChanged: {
234 if (enabled) {
235 dragAmountX = 0;
236 dragAmountY = 0;
237 firstMouseXChange = true;
238 firstMouseYChange = true;
239 } else {
240 dragActive = false;
241 }
242 }
243
244 property int dragStartX
245 property int dragAmountX
246 property int dragStartY
247 property int dragAmountY
248 property bool dragActive: false
249 property int dragThreshold: units.gu(2)
250 property bool firstMouseXChange: true
251 property bool firstMouseYChange: true
252
253 onMouseXChanged: {
254 if (firstMouseXChange) {
255 dragStartX = mouseX;
256 firstMouseXChange = false;
257 } else {
258 var amount = mouseX - dragStartX;
259 if (Math.abs(amount) >= dragThreshold) {
260 dragActive = true;
261 }
262 if (dragActive) {
263 dragAmountX = amount;
264 draggedItem.positionCaret();
265 }
266 }
267 }
268
269 onMouseYChanged: {
270 if (firstMouseYChange) {
271 dragStartY = mouseY;
272 firstMouseYChange = false;
273 } else {
274 var amount = mouseY - dragStartY;
275 if (Math.abs(amount) >= dragThreshold) {
276 dragActive = true;
277 }
278 if (dragActive) {
279 dragAmountY = amount;
280 draggedItem.positionCaret()
281 }
282 }
283 }
284
285 onDragActiveChanged: {
286 // close contextual menu when dragging and reopen it at the end of the drag
287 if (dragActive) {
288 if (handler.popover != null) {
289 PopupUtils.close(handler.popover);
290 }
291 } else {
292 handler.pressAndHold(-1, false);
293 }
294 }
295 }
296
297 // fake cursor, caret is reparented to it to avoid caret clipping
298 Item {
299 id: fakeCursor
300 objectName: positionProperty + "FakeCursor"
301 parent: QuickUtils.rootItem(handler.main)
302 width: cursorItem.width
303 height: cursorItem.height
304 Component.onCompleted: caret.parent = fakeCursor
305
306 x: mappedCursorPosition("x")
307 y: mappedCursorPosition("y")
308 onXChanged: draggedItem.moveToCaret()
309 onYChanged: draggedItem.moveToCaret()
310
311 // manual clipping: the caret should be visible only while the cursor's
312 // top/bottom falls into the text area
313 visible: {
314 if (!caret || !cursorItem.visible || cursorItem.opacity < 1.0)
315 return false;
316
317 var pos = handler.main.mapFromItem(fakeCursor.parent, fakeCursor.x, fakeCursor.y);
318 var leftTop = Qt.point(pos.x - handler.frameDistance.x, pos.y + handler.frameDistance.y + handler.lineSpacing);
319 var rightBottom = Qt.point(pos.x - handler.frameDistance.x, pos.y + height - handler.frameDistance.y - handler.lineSpacing);
320 return (handler.visibleArea.contains(leftTop) || handler.visibleArea.contains(rightBottom));
321 }
322 }
323 }