Debian Codegraph @

src/imports/Components/2.0/Panel.qml

raw
1 /*
2 * Copyright (C) 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 Toolkit
19
20 /*!
21 \qmltype Panel
22 \inqmlmodule Lomiri.Components
23 \ingroup lomiri
24 \brief A panel that can be swiped in and out from an edge of the window by the user.
25 For most applications, it is highly recommended to use the \l MainView instead which includes
26 a toolbar at its bottom that can be swiped in or out.
27
28 Unless your application has very specific needs for a Panel, use a \l MainView with the
29 default toolbar.
30
31 When using a Panel, do not theme it directly, but theme its contents, because
32 the Panel itself should not have visible elements, since it is
33 in the view (to detect mouse events) even when its contents should be invisible.
34
35 Set the anchors and/or width/height of the Panel to specify the area that the Panel covers when
36 opened.
37
38 A black panel that can be swiped in from the lower-right of the window can be created like this:
39 \qml
40 import QtQuick 2.4
41 import Lomiri.Components
42
43 Item {
44 width: units.gu(80)
45 height: units.gu(80)
46
47 Panel {
48 id: panel
49 anchors {
50 right: parent.right
51 bottom: parent.bottom
52 }
53 width: parent.width / 2
54 height: units.gu(8)
55
56 Rectangle {
57 anchors.fill: parent
58 color: theme.palette.normal.overlay
59
60 Button {
61 anchors.centerIn: parent
62 text: "Click"
63 }
64 }
65 }
66 }
67 \endqml
68
69 A panel that looks like the standard (bottom-aligned) toolbar, but with custom contents, can be created like this:
70 \qml
71 import QtQuick 2.4
72 import Lomiri.Components
73
74 Item {
75 width: units.gu(80)
76 height: units.gu(80)
77
78 Panel {
79 id: panel
80 anchors {
81 left: parent.left
82 right: parent.right
83 bottom: parent.bottom
84 }
85 height: units.gu(8)
86
87 Item {
88 anchors.fill: parent
89
90 // two properties used by the toolbar delegate:
91 property bool opened: panel.opened
92 property bool animating: panel.animating
93
94 Button {
95 anchors.centerIn: parent
96 text: "Click"
97 }
98 }
99 }
100 }
101 \endqml
102
103 Any Items can be placed inside the Panel, but MouseAreas can block mouse events from reaching
104 the panel and thus obstruct the swiping behavior for hiding the panel. As a result, the user cannot
105 start swiping on the buttons in the examples above in order to hide the panel. To remedy this, clicked()
106 signals are forwarded from the panel by calling the child's trigger() function. Example:
107 \qml
108 import QtQuick 2.4
109 import Lomiri.Components
110
111 Rectangle {
112 color: theme.palette.normal.background
113 width: units.gu(40)
114 height: units.gu(40)
115
116 Panel {
117 id: panel
118 anchors {
119 left: parent.left
120 right: parent.right
121 bottom: parent.bottom
122 }
123 height: units.gu(8)
124
125 Rectangle {
126 color: theme.palette.normal.overlay
127 anchors.fill: parent
128 Rectangle {
129 width: units.gu(8)
130 height: units.gu(4)
131 anchors.centerIn: parent
132 color: "red"
133 function trigger() {
134 print("The red rectangle was clicked");
135 }
136 }
137 }
138 }
139 Component.onCompleted: panel.open();
140 }
141 \endqml
142 Like this, the red rectangle accepts click events, but the user can still swipe down on top
143 of the rectangle in order to hide the panel.
144 */
145 Item {
146 id: panel
147
148 /*!
149 Default property, holds the content which will be swiped in and out.
150 \qmlproperty list<Object> contents
151 */
152 default property alias contents: bar.data
153
154 /*!
155 The property defines the alignment of the panel.
156 The implementation supports the following values:
157 \list
158 \li Qt.AlignBottom to swipe in the panel from the bottom (default)
159 \li Qt.AlignTop to swipe in the panel from the top
160 \li Qt.AlignLeft to swipe in the panel from the left
161 \li Qt.AlignRight to swipe in the panel from the right
162 \li Qt.AlignLeading left when layout mirrorring is disabled, right otherwise
163 \li Qt.AlignTrailing right when layout mirroring is disabled, left otherwise
164 \endlist
165 The default value is \b Qt.AlignBottom, and it is not recommended to change the
166 default value because the left, right and top edge are already used for system
167 functions, while the bottom edge is reserved for app-specific functionality
168 such as a default toolbar. The use of Qt.AlignLeading and Qt.AlignTrailing is
169 preferred over Qt.AlignLeft and Qt.AlignRight in order to more easily support
170 right-to-left user interfaces that use LayoutMirroring.
171 */
172 property int align: Qt.AlignBottom
173
174 /*!
175 When opened, the panel is visible, otherwise it is hidden.
176 Use edge swipes to open/close the panel.
177 The opened property is not updated until the swipe gesture is completed.
178 */
179 // opened is true if state is spread, or if state is moving/hint and the previous state was spread.
180 property bool opened: (panel.state === "spread") ||
181 (panel.state === "moving" && internal.previousState === "spread")
182 /*! \internal */
183 // FIXME: When opened is made read-only, onOpenedChanged can be removed entirely.
184 onOpenedChanged: {
185 if (internal.openedChangedWarning) {
186 console.log("DEPRECATED use of Panel.opened property. This property will be made read-only,
187 please use the opened property of the Page tools or use Panel.open() and Panel.close().");
188
189 if (opened) {
190 panel.open();
191 } else {
192 panel.close();
193 }
194
195 // re-establish the previous binding for opened.
196 panel.opened = Qt.binding(function() {
197 return (panel.state === "spread") ||
198 (panel.state === "moving" && internal.previousState === "spread");
199 })
200 }
201
202 internal.openedChangedWarning = true;
203 }
204
205 /*!
206 Open the panel
207 */
208 function open() {
209 // FIXME: When opened is made readonly, openedChangedWarning must be removed
210 internal.openedChangedWarning = false;
211 panel.state = "spread";
212 hideTimer.conditionalRestart();
213 }
214
215 /*!
216 Close the panel
217 */
218 function close() {
219 // FIXME: When opened is made readonly, openedChangedWarning must be removed.
220 internal.openedChangedWarning = false;
221 panel.state = "";
222 hideTimer.stop();
223 }
224
225 /*!
226 The time in milliseconds before the panel automatically hides after inactivity
227 when it is not locked. Interacting with the panel resets the timer.
228 Note that adding contents to the panel that accepts mouse events will prevent
229 the panel from detecting interaction and the timer will not be reset.
230 Setting a negative value will disable automatic hiding.
231 Default value: -1 (automatic hiding is disabled).
232 \qmlproperty int hideTimeout
233 */
234 property alias hideTimeout: hideTimer.interval
235
236 /*!
237 Disable edge swipe to open/close the panel. False by default.
238 */
239 property bool locked: false
240 /*! \internal */
241 onLockedChanged: {
242 if (state === "hint" || state === "moving") {
243 draggingArea.finishMoving();
244 }
245 if (!hideTimer.conditionalRestart()) {
246 hideTimer.stop();
247 }
248 }
249
250 Timer {
251 id: hideTimer
252 interval: -1
253 running: panel.opened && !panel.locked && interval >= 0
254
255 function conditionalRestart() {
256 if (hideTimer.interval >= 0) {
257 if (!panel.locked && panel.opened) {
258 hideTimer.restart();
259 return true;
260 }
261 }
262 return false;
263 }
264 onIntervalChanged: {
265 if (!conditionalRestart()) {
266 hideTimer.stop();
267 }
268 }
269 onTriggered: {
270 if (!panel.locked) {
271 panel.close();
272 }
273 }
274 }
275 // disable the timer when the application is not active to avoid closing
276 // the panel immediately after the application becomes active again
277 Connections {
278 target: Qt.application
279 function onActiveChanged() {
280 if (Qt.application.active) {
281 hideTimer.conditionalRestart();
282 } else {
283 hideTimer.stop();
284 }
285 }
286 }
287
288 /*!
289 How much of the panel to show when the user touches the panel's edge.
290 This gives the user a hint that there is a panel hiding at that edge and
291 invites him/her to swipe to show the panel completely. Default value: units.gu(2).
292 */
293 property real hintSize: units.gu(2)
294
295 /*!
296 The size (height for top or bottom-aligned panels, width for left or right-aligned
297 panels) of the mouse area used to detect edge swipes to open the panel, when
298 the panel is not opened. Default value: units.gu(2).
299 */
300 property real triggerSize: units.gu(2)
301
302 /*!
303 \qmlproperty real position
304 The current position of the edge of the panel. The value is 0 when the panel is
305 opened, and has its maximum value when the panel is closed. The maximum value is the
306 width of the Panel for a left or right-aligned panel, and the height of the panel for
307 top or bottom-aligned panels. When the user drags the Panel from the edge to open it,
308 the position will change from the maximum size (closed) to 0 (fully expanded).
309 */
310 readonly property alias position: bar.position
311
312 states: [
313 State {
314 name: "hint"
315 PropertyChanges {
316 target: bar
317 position: bar.size - panel.hintSize
318 }
319 },
320 State {
321 name: "moving"
322 PropertyChanges {
323 target: bar
324 position: Toolkit.MathUtils.clamp(draggingArea.mousePosition - internal.movingDelta, 0, bar.size)
325 }
326 },
327 State {
328 name: "spread"
329 PropertyChanges {
330 target: bar
331 position: 0
332 }
333 },
334 State {
335 name: ""
336 PropertyChanges {
337 target: bar
338 position: bar.size
339 explicit: true
340 }
341 }
342 ]
343
344 /*!
345 Animate transitions between the different panel states.
346 */
347 property bool animate: true
348
349 /*!
350 The toolbar is currently not in a stable hidden or visible state.
351 */
352 readonly property bool animating: draggingArea.pressed || transitionToAll.running
353 || transitionToHint.running
354 || transitionToSpread.running
355
356 transitions: [
357 Transition {
358 id: transitionToAll
359 to: ""
360 LomiriNumberAnimation {
361 target: bar
362 properties: "position"
363 duration: internal.transitionDuration
364 }
365 },
366 Transition {
367 id: transitionToHint
368 to: "hint"
369 LomiriNumberAnimation {
370 target: bar
371 properties: "position"
372 duration: internal.transitionDuration
373 }
374 },
375 Transition {
376 id: transitionToSpread
377 to: "spread"
378 LomiriNumberAnimation {
379 target: bar
380 properties: "position"
381 duration: internal.transitionDuration
382 }
383 },
384 Transition {
385 id: transitionToMoving
386 to: "moving"
387 LomiriNumberAnimation {
388 target: bar
389 properties: "position"
390 duration: panel.animate ? Toolkit.LomiriAnimation.SnapDuration : 0
391 }
392 }
393 ]
394
395 QtObject {
396 id: internal
397
398 // FIXME: Remove when opened property is made readonly
399 property bool openedChangedWarning: true
400
401 /*!
402 The duration in milliseconds of sliding in or out transitions when opening, closing, and showing the hint.
403 Default value: 250
404 */
405 property real transitionDuration: panel.animate ? Toolkit.LomiriAnimation.FastDuration : 0
406
407 property string previousState: ""
408 property int movingDelta
409
410 // Convert from Qt.AlignLeading to Qt.AlignTrailing to Qt.AlignLeft and Qt.AlignRight
411 property int align: {
412 if (panel.align === Qt.AlignLeading) {
413 if (panel.LayoutMirroring.enabled) {
414 return Qt.AlignRight;
415 } else {
416 return Qt.AlignLeft;
417 }
418 } else if (panel.align === Qt.AlignTrailing) {
419 if (panel.LayoutMirroring.enabled) {
420 return Qt.AlignLeft;
421 } else {
422 return Qt.AlignRight;
423 }
424 } else {
425 return panel.align;
426 }
427 }
428
429 readonly property int orientation: (panel.align === Qt.AlignTop || panel.align === Qt.AlignBottom)
430 ? Qt.Horizontal : Qt.Vertical
431 }
432
433 /*! \internal */
434 onStateChanged: {
435 if (state === "hint") {
436 internal.movingDelta = panel.hintSize + draggingArea.initialPosition - bar.size;
437 } else if (state === "moving" && internal.previousState === "spread") {
438 internal.movingDelta = draggingArea.initialPosition;
439 }
440 }
441
442 /*!
443 \internal
444 \deprecated
445 Enable the InverseMouseArea that closes the panel when the user clicks outside of the panel.
446 This functionality moved to the MainView to deal with Toolbar hiding, but this InverseMouseArea
447 is still in the Panel for backwards compatibility in apps that use it directly.
448 Default value is false.
449
450 FIXME: Remove __closeOnContentsClicks and the IMA below when all apps use Toolbar instead of Panel.
451 */
452 property bool __closeOnContentsClicks: false
453 Toolkit.InverseMouseArea {
454 anchors.fill: draggingArea
455 onPressed: function(mouse) {
456 mouse.accepted = false;
457 // the mouse click may cause an update
458 // of locked by the clicked Item behind
459 if (!panel.locked) panel.close();
460 }
461 propagateComposedEvents: true
462 visible: panel.__closeOnContentsClicks && panel.locked === false && panel.state === "spread"
463 }
464
465 /*!
466 \internal
467 \deprecated
468 Enable automatic reveal of panel on mouse hover over hint area, and hide when leaving
469 the panel area. This is disabled by default, because Panel may be used to implement
470 bottom edge behaviors that are completely different from the toolbar, but the property
471 is enabled in Toolbar to make more usable on desktop.
472 */
473 property bool __openOnHover: false
474
475 /*!
476 The user presses on the opened toolbar, or when the toolbar is closed but
477 not locked, the user presses in the toolbar trigger area.
478 \qmlproperty bool pressed
479 */
480 // This is a simple alias to draggingArea.pressed, but the documentation is accurate
481 // because of the visible definition of draggingArea. Pressed is false when draggingArea
482 // is not visible.
483 property alias pressed: draggingArea.pressed
484
485 DraggingArea {
486 id: draggingArea
487 orientation: internal.orientation === Qt.Horizontal ? Qt.Vertical : Qt.Horizontal
488 zeroVelocityCounts: true
489 // For top/left aligned panels, the DraggingArea is always at (0, 0).
490 // For bottom/right aligned panels, the DraggingArea is on the opposite side (parent size - own size).
491 x: internal.align === Qt.AlignRight ? parent.width - width : 0
492 y: internal.align === Qt.AlignBottom ? parent.height - height : 0
493 height: internal.orientation === Qt.Horizontal ? panel.opened ? bar.size + units.gu(1) : panel.triggerSize : parent.height
494 width: internal.orientation === Qt.Vertical ? panel.opened ? bar.size + units.gu(1) : panel.triggerSize : parent.width
495 visible: !panel.locked || panel.opened
496 hoverEnabled: panel.__openOnHover
497
498 property int mousePosition: getMousePosition()
499 function getMousePosition() {
500 switch (internal.align) {
501 case Qt.AlignLeft:
502 return -mouseX;
503 case Qt.AlignRight:
504 return mouseX;
505 case Qt.AlignBottom:
506 return mouseY;
507 case Qt.AlignTop:
508 return -mouseY;
509 }
510 }
511
512 // set in onPressed, reset when entering "moving" state
513 property Item pressedItem: null
514
515 // find the first child with a triggered property:
516 function getTriggerableItem(mouse) {
517 var item = bar; // contains the children
518 while (item && !item.hasOwnProperty("trigger")) {
519 var coords = mapToItem(item, mouse.x, mouse.y);
520 // FIXME: When using a ListView the highlight may be
521 // returned instead of the Item that you are looking for
522 item = item.childAt(coords.x, coords.y);
523 }
524 return item; // will be null if no item has trigger() function.
525 }
526
527 // forward trigger() events to any child Item with trigger() function.
528 // This must also happen when the panel is locked, so the DraggingArea is
529 // never disabled, and other signal handlers will return when panel.locked is true.
530 onClicked: function(mouse) {
531 if (pressedItem && pressedItem === getTriggerableItem(mouse)) {
532 // Click event positioned at the Item where the user first pressed
533 pressedItem.trigger();
534 }
535 }
536
537 property int initialPosition
538 onPressed: function(mouse) {
539 hideTimer.stop();
540 pressedItem = getTriggerableItem(mouse);
541 if (panel.locked) return;
542 initialPosition = getMousePosition();
543 if (panel.state === "") panel.state = "hint";
544 }
545
546 /*!
547 The minimum amount of movement while pressed before switching to "moving" state.
548 This threshold is needed to avoid detecting unintentional small movements while
549 "clicking" as a drag.
550 */
551 property real dragThreshold: units.gu(1)
552
553 onPositionChanged: {
554 if (!pressed) return;
555 if (panel.locked) return;
556 if (panel.state === "hint" && mousePosition < initialPosition - dragThreshold) {
557 internal.previousState = "hint";
558 panel.state = "moving";
559 pressedItem = null;
560 } else if (panel.state === "spread" && mousePosition > initialPosition + dragThreshold) {
561 internal.previousState = "spread";
562 panel.state = "moving";
563 pressedItem = null;
564 }
565 }
566
567 onReleased: {
568 if (panel.state === "moving" || panel.state === "hint") {
569 finishMoving();
570 } else {
571 hideTimer.conditionalRestart();
572 }
573 }
574 // Mouse cursor moving out of the window while pressed on desktop
575 onCanceled: {
576 if (panel.state === "moving" || panel.state === "hint") {
577 finishMoving();
578 } else {
579 hideTimer.conditionalRestart();
580 }
581 }
582
583 onEntered: {
584 if (panel.__openOnHover) {
585 panel.open();
586 }
587 hideTimer.stop();
588 }
589
590 onExited: {
591 // panel.__openOnHover
592 // Ensure the panel is not still opening. The draggingArea will
593 // change after the panel finishes the opening animation.
594 if (!animating) {
595 hideTimer.conditionalRestart();
596 }
597 }
598
599 // FIXME: Make all parameters below themable and resolution-independent.
600 // The value of 44 was copied from the Launcher.
601 function finishMoving() {
602 if (draggingArea.dragVelocity < -44) {
603 if (internal.align === Qt.AlignBottom || internal.align === Qt.AlignRight) {
604 panel.open();
605 } else {
606 panel.close();
607 }
608 } else if (draggingArea.dragVelocity > 44) {
609 if (internal.align === Qt.AlignBottom || internal.align === Qt.AlignRight) {
610 panel.close();
611 } else {
612 panel.open();
613 }
614 } else {
615 if (bar.position < bar.size / 2) {
616 panel.open();
617 } else {
618 panel.close();
619 }
620 }
621 }
622 }
623
624 Item {
625 id: bar
626 objectName: "bar_item"
627 height: parent.height
628 width: parent.width
629
630 property real size: internal.orientation === Qt.Horizontal ? height : width
631 //position will always be in the range 0..size, where position==0 means spread, position==size means hidden.
632 property real position: panel.opened ? 0 : size
633 onSizeChanged: position = panel.opened ? 0 : size
634
635 y: internal.align === Qt.AlignTop ? -position : internal.align === Qt.AlignBottom ? position : 0
636 x: internal.align === Qt.AlignLeft ? -position : internal.align === Qt.AlignRight ? position : 0
637 }
638 }