Debian Codegraph @

src/imports/Components/1.2/TextCursor.qml

raw
1 /*
2 * Copyright 2012 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.2 as Lomiri
19 import Lomiri.Components.Popups 1.0
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 style: Theme.createStyleComponent("TextCursorStyle.qml", cursorItem);
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 + units.gu(2)
163 parent: fakeCursor.parent
164 visible: caret.visible
165
166 /*
167 Mouse area to turn on dragging or selection mode when pressed
168 on the handler area. The drag mode is turned off when the drag
169 gets inactive or when the LeftButton is released.
170 */
171 MouseArea {
172 id: draggedItemMouseArea
173 objectName: cursorItem.positionProperty + "_activator"
174 anchors.fill: parent
175 acceptedButtons: Qt.LeftButton
176 preventStealing: true
177 enabled: parent.width && parent.height && parent.visible && !handler.doubleTapInProgress
178 onPressedChanged: {
179 if (!pressed) {
180 // when the dragging ends, reposition the dragger back to caret
181 draggedItem.moveToCaret();
182 }
183 }
184 Lomiri.Mouse.forwardTo: [dragger]
185 Lomiri.Mouse.onClicked: openPopover()
186 Lomiri.Mouse.onPressAndHold: {
187 handler.main.selectWord();
188 handler.pressAndHold(-1, false);
189 }
190 Lomiri.Mouse.onDoubleClicked: handler.main.selectWord()
191 Lomiri.Mouse.enabled: enabled
192
193 // Visible touch target area for debugging purposes
194 Rectangle {
195 anchors.fill: parent
196 color: 'red'
197 opacity: 0.1
198 visible: false // draggedItemMouseArea.enabled
199 }
200 }
201
202 // aligns the draggedItem to the caret and resets the dragger
203 function moveToCaret() {
204 if (!caret) {
205 return;
206 }
207 // The style may render handlers either on top or bottom
208 var flip = caret.rotation == 180;
209 draggedItem.x = fakeCursor.x + (flip ? -units.gu(1) : -draggedItem.width + units.gu(1));
210 draggedItem.y = fakeCursor.y - caret.height - units.gu(0.5);
211 }
212 // positions caret to the dragged position
213 function positionCaret() {
214 if (dragger.dragActive) {
215 var dx = dragger.dragStartX + dragger.dragAmountX + handler.flickable.contentX;
216 var dy = dragger.dragStartY + dragger.dragAmountY + handler.flickable.contentY;
217 dx -= handler.frameDistance.x;
218 dy -= handler.frameDistance.y;
219 handler.positionCaret(positionProperty, dx, dy);
220 }
221 }
222 }
223 MouseArea {
224 id: dragger
225 objectName: cursorItem.positionProperty + "_dragger"
226 // fill the entire component area
227 parent: handler.main
228 anchors.fill: parent
229 enabled: draggedItemMouseArea.enabled && draggedItemMouseArea.pressed && caret.visible
230 onEnabledChanged: {
231 if (enabled) {
232 dragAmountX = 0;
233 dragAmountY = 0;
234 firstMouseXChange = true;
235 firstMouseYChange = true;
236 } else {
237 dragActive = false;
238 }
239 }
240
241 property int dragStartX
242 property int dragAmountX
243 property int dragStartY
244 property int dragAmountY
245 property bool dragActive: false
246 property int dragThreshold: units.gu(2)
247 property bool firstMouseXChange: true
248 property bool firstMouseYChange: true
249
250 onMouseXChanged: {
251 if (firstMouseXChange) {
252 dragStartX = mouseX;
253 firstMouseXChange = false;
254 } else {
255 var amount = mouseX - dragStartX;
256 if (Math.abs(amount) >= dragThreshold) {
257 dragActive = true;
258 }
259 if (dragActive) {
260 dragAmountX = amount;
261 draggedItem.positionCaret();
262 }
263 }
264 }
265
266 onMouseYChanged: {
267 if (firstMouseYChange) {
268 dragStartY = mouseY;
269 firstMouseYChange = false;
270 } else {
271 var amount = mouseY - dragStartY;
272 if (Math.abs(amount) >= dragThreshold) {
273 dragActive = true;
274 }
275 if (dragActive) {
276 dragAmountY = amount;
277 draggedItem.positionCaret()
278 }
279 }
280 }
281
282 onDragActiveChanged: {
283 // close contextual menu when dragging and reopen it at the end of the drag
284 if (dragActive) {
285 if (handler.popover != null) {
286 PopupUtils.close(handler.popover);
287 }
288 } else {
289 handler.pressAndHold(-1, false);
290 }
291 }
292 }
293
294 // fake cursor, caret is reparented to it to avoid caret clipping
295 Item {
296 id: fakeCursor
297 objectName: positionProperty + "FakeCursor"
298 parent: QuickUtils.rootItem(handler.main)
299 width: cursorItem.width
300 height: cursorItem.height
301 Component.onCompleted: caret.parent = fakeCursor
302
303 x: mappedCursorPosition("x")
304 y: mappedCursorPosition("y")
305 onXChanged: draggedItem.moveToCaret()
306 onYChanged: draggedItem.moveToCaret()
307
308 // manual clipping: the caret should be visible only while the cursor's
309 // top/bottom falls into the text area
310 visible: {
311 if (!caret || !cursorItem.visible || cursorItem.opacity < 1.0)
312 return false;
313
314 var pos = handler.main.mapFromItem(fakeCursor.parent, fakeCursor.x, fakeCursor.y);
315 var leftTop = Qt.point(pos.x - handler.frameDistance.x, pos.y + handler.frameDistance.y + handler.lineSpacing);
316 var rightBottom = Qt.point(pos.x - handler.frameDistance.x, pos.y + height - handler.frameDistance.y - handler.lineSpacing);
317 return (handler.visibleArea.contains(leftTop) || handler.visibleArea.contains(rightBottom));
318 }
319 }
320 }