Debian Codegraph @

src/imports/Components/2.0/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 as Lomiri
19 import Lomiri.Components.Popups
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 ? caret.width : 0) + units.gu(2)
161 onWidthChanged: draggedItem.moveToCaret()
162 height: cursorItem.height + (caret ? caret.height : 0) + 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: function(event) {
193 handler.main.selectWord()
194 event.accepted = true;
195 }
196 Lomiri.Mouse.enabled: enabled
197
198 // Visible touch target area for debugging purposes
199 Rectangle {
200 anchors.fill: parent
201 color: 'red'
202 opacity: 0.1
203 visible: false // draggedItemMouseArea.enabled
204 }
205 }
206
207 // aligns the draggedItem to the caret and resets the dragger
208 function moveToCaret() {
209 if (!caret) {
210 return;
211 }
212 // The style may render handlers either on top or bottom
213 var flip = caret.rotation === 180;
214 draggedItem.x = fakeCursor.x + (flip ? -caret.width : -draggedItem.width + caret.width);
215 draggedItem.y = fakeCursor.y + caret.y + caret.height - threshold;
216 }
217 // positions caret to the dragged position
218 function positionCaret() {
219 if (dragger.dragActive) {
220 var dx = dragger.dragStartX + dragger.dragAmountX + handler.flickable.contentX;
221 var dy = dragger.dragStartY + dragger.dragAmountY + handler.flickable.contentY;
222 dx -= handler.frameDistance.x;
223 dy -= handler.frameDistance.y;
224 handler.positionCaret(positionProperty, dx, dy);
225 }
226 }
227 }
228 MouseArea {
229 id: dragger
230 objectName: cursorItem.positionProperty + "_dragger"
231 cursorShape: Qt.IBeamCursor
232 // fill the entire component area
233 parent: handler.main
234 anchors.fill: parent
235 enabled: draggedItemMouseArea.enabled && draggedItemMouseArea.pressed && caret.visible
236 onEnabledChanged: {
237 if (enabled) {
238 dragAmountX = 0;
239 dragAmountY = 0;
240 firstMouseXChange = true;
241 firstMouseYChange = true;
242 } else {
243 dragActive = false;
244 }
245 }
246
247 property int dragStartX
248 property int dragAmountX
249 property int dragStartY
250 property int dragAmountY
251 property bool dragActive: false
252 property int dragThreshold: units.gu(2)
253 property bool firstMouseXChange: true
254 property bool firstMouseYChange: true
255
256 onMouseXChanged: {
257 if (firstMouseXChange) {
258 dragStartX = mouseX;
259 firstMouseXChange = false;
260 } else {
261 var amount = mouseX - dragStartX;
262 if (Math.abs(amount) >= dragThreshold) {
263 dragActive = true;
264 }
265 if (dragActive) {
266 dragAmountX = amount;
267 draggedItem.positionCaret();
268 }
269 }
270 }
271
272 onMouseYChanged: {
273 if (firstMouseYChange) {
274 dragStartY = mouseY;
275 firstMouseYChange = false;
276 } else {
277 var amount = mouseY - dragStartY;
278 if (Math.abs(amount) >= dragThreshold) {
279 dragActive = true;
280 }
281 if (dragActive) {
282 dragAmountY = amount;
283 draggedItem.positionCaret()
284 }
285 }
286 }
287
288 onDragActiveChanged: {
289 // close contextual menu when dragging and reopen it at the end of the drag
290 if (dragActive) {
291 if (handler.popover != null) {
292 PopupUtils.close(handler.popover);
293 }
294 } else {
295 handler.pressAndHold(-1, false);
296 }
297 }
298 }
299
300 // fake cursor, caret is reparented to it to avoid caret clipping
301 Item {
302 id: fakeCursor
303 objectName: positionProperty + "FakeCursor"
304 parent: QuickUtils.rootItem(handler.main)
305 width: cursorItem.width
306 height: cursorItem.height
307 Component.onCompleted: caret.parent = fakeCursor
308
309 x: mappedCursorPosition("x")
310 y: mappedCursorPosition("y")
311 onXChanged: draggedItem.moveToCaret()
312 onYChanged: draggedItem.moveToCaret()
313
314 // manual clipping: the caret should be visible only while the cursor's
315 // top/bottom falls into the text area
316 visible: {
317 if (!caret || !cursorItem.visible || cursorItem.opacity < 1.0)
318 return false;
319
320 var pos = handler.main.mapFromItem(fakeCursor.parent, fakeCursor.x, fakeCursor.y);
321 var leftTop = Qt.point(pos.x - handler.frameDistance.x, pos.y + handler.frameDistance.y + handler.lineSpacing);
322 var rightBottom = Qt.point(pos.x - handler.frameDistance.x, pos.y + height - handler.frameDistance.y - handler.lineSpacing);
323 return (handler.visibleArea.contains(leftTop) || handler.visibleArea.contains(rightBottom));
324 }
325 }
326 }