Debian Codegraph @

src/imports/Components/Popups/1.2/PopupBase.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
19
20 /*!
21 \qmltype PopupBase
22 \inqmlmodule Lomiri.Components.Popups 1.0
23 \ingroup lomiri-popups
24 \brief The base class for all dialogs, sheets and popovers. Do not use directly.
25
26 Examples: See subclasses.
27 */
28 OrientationHelper {
29 id: popupBase
30
31 /*!
32 The property holds the area used to dismiss the popups, the area from where
33 mouse and touch events will be grabbed. By default this area is the Popup
34 itself.
35 */
36 property Item dismissArea: popupBase
37
38 /*!
39 The property specifies whether to forward or not the mouse and touch events
40 happening outside of the popover. By default all events are grabbed.
41 */
42 property bool grabDismissAreaEvents: true
43
44 /*!
45 \internal
46 FIXME: publish this property once agreed
47 */
48 property PropertyAnimation fadingAnimation: PropertyAnimation{duration: 0}
49
50 // without specifying width and height below, some width calculations go wrong in Sheet.
51 // I guess popupBase.width is not correctly set initially
52 width: parent ? parent.width : undefined
53 height: parent ? parent.height : undefined
54
55 // copy value of automaticOrientation from root object (typically a MainView)
56 automaticOrientation: stateWrapper.rootItem && stateWrapper.rootItem.automaticOrientation ?
57 stateWrapper.rootItem.automaticOrientation : false
58 anchorToKeyboard: true
59
60 LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft
61 LayoutMirroring.childrenInherit: true
62
63 /*!
64 Make the popup visible. Reparent to the background area object first if needed.
65 Only use this function if you handle memory management. Otherwise use
66 PopupUtils.open() to do it automatically.
67 */
68 function show() {
69
70 if ((typeof popupBase["reparentToRootItem"]) === "boolean") {
71 // So the property exists. Let's use it then.
72 if (reparentToRootItem) {
73 parent = stateWrapper.rootItem;
74 }
75 } else {
76 // The property does not exist. Default is to reparent
77 parent = stateWrapper.rootItem;
78 }
79
80 if (!dismissArea) {
81 dismissArea = parent
82 }
83
84 // Without setting the parent, mapFromItem() breaks in internalPopupUtils.
85 stateWrapper.state = 'opened';
86 }
87
88 /*!
89 Hide the popup.
90 Only use this function if you handle memory management. Otherwise use
91 PopupUtils.close() to do it automatically.
92 */
93 function hide() {
94 stateWrapper.state = 'closed';
95 }
96
97 /*!
98 \internal
99 When the popup is created by calling PopupUtils.open(),
100 onVisibleChanged is connected to __closeIfHidden().
101 */
102 function __closeIfHidden() {
103 if (!visible) __closePopup();
104 }
105
106 /*!
107 \internal
108 The function closes the popup. This is called when popup's caller is no
109 longer valid.
110 */
111 function __closePopup() {
112 if (popupBase) {
113 popupBase.destroy();
114 }
115 }
116
117 /*!
118 \internal
119 Foreground component excluded from InverseMouseArea
120 */
121 property Item __foreground
122
123 /*!
124 \internal
125 Set to true if the InverseMouseArea should dismiss the area
126 */
127 property bool __closeOnDismissAreaPress: false
128
129 /*!
130 \internal
131 Property driving dimming the popup's background. The default is the same as
132 defined in the style
133 */
134 property alias __dimBackground: background.dim
135
136 /*!
137 \internal
138 Property to control dismissArea event capture.
139 */
140 property alias __eventGrabber: eventGrabber
141
142 // dimmer
143 Rectangle {
144 id: background
145 // styling properties
146 property bool dim: false
147 anchors.fill: parent
148 visible: dim
149 color: popupBase.width > units.gu(60) ? Qt.rgba(0, 0, 0, 0.6) : Qt.rgba(0, 0, 0, 0.9)
150 }
151
152 InverseMouseArea {
153 id: eventGrabber
154 enabled: true
155 acceptedButtons: Qt.LeftButton | Qt.MiddleButton | Qt.RightButton
156 anchors.fill: __foreground
157 sensingArea: dismissArea
158 propagateComposedEvents: !grabDismissAreaEvents
159 onPressed: if (__closeOnDismissAreaPress) popupBase.hide()
160 onWheel: wheel.accepted = true
161 }
162
163 MouseArea {
164 acceptedButtons: Qt.LeftButton | Qt.MiddleButton | Qt.RightButton
165 anchors.fill: __foreground
166 onWheel: wheel.accepted = true
167 }
168
169 // set visible as false by default
170 visible: false
171 opacity: 0.0
172 /*! \internal */
173 onVisibleChanged: stateWrapper.state = (visible) ? 'opened' : 'closed'
174 /*! \internal */
175 onParentChanged: stateWrapper.rootItem = QuickUtils.rootItem(popupBase)
176 Component.onCompleted: {
177 stateWrapper.saveActiveFocus();
178 stateWrapper.rootItem = QuickUtils.rootItem(popupBase);
179 }
180
181 Item {
182 id: stateWrapper
183 property Item rootItem: QuickUtils.rootItem(popupBase)
184
185 property bool windowIsValid: typeof window != "undefined"
186 property Item prevFocus
187
188 function saveActiveFocus() {
189 // 'window' context property is exposed to QML after component completion
190 // before rendering is complete, therefore a simple 'if (window)' check is
191 // not enough.
192 if (windowIsValid) {
193 prevFocus = window.activeFocusItem;
194 windowIsValidChanged.disconnect(saveActiveFocus);
195 } else {
196 // connect the function so we can save the original focus item
197 windowIsValidChanged.connect(saveActiveFocus);
198 }
199 }
200
201 function restoreActiveFocus() {
202 if (prevFocus) {
203 if (prevFocus.hasOwnProperty("requestFocus")) {
204 prevFocus.requestFocus(Qt.OtherFocusReason);
205 } else {
206 prevFocus.forceActiveFocus(Qt.OtherFocusReason);
207 }
208 }
209 }
210
211 states: [
212 State {
213 name: 'closed'
214 extend: ''
215 },
216 State {
217 name: 'opened'
218 }
219 ]
220 transitions: [
221 Transition {
222 from: "*"
223 to: "opened"
224 SequentialAnimation {
225 ScriptAction {
226 script: popupBase.visible = true
227 }
228 NumberAnimation {
229 target: popupBase
230 property: "opacity"
231 from: 0.0
232 to: 1.0
233 duration: fadingAnimation.duration
234 easing: fadingAnimation.easing
235 }
236 }
237 },
238 Transition {
239 from: "opened"
240 to: "closed"
241 SequentialAnimation {
242 NumberAnimation {
243 target: popupBase
244 property: "opacity"
245 from: 1.0
246 to: 0.0
247 duration: fadingAnimation.duration
248 easing: fadingAnimation.easing
249 }
250 ScriptAction {
251 script: {
252 popupBase.visible = false;
253 if (eventGrabber.enabled) {
254 stateWrapper.restoreActiveFocus();
255 }
256 }
257 }
258 }
259 }
260 ]
261 }
262 }