aboutsummaryrefslogtreecommitdiffstats
path: root/qt-mobile/qml/mobilecomponents/OverlayDrawer.qml
blob: c6e9d4b364c3b868169c4c1b7bf11d5c57de6bed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 *   Copyright 2012 Marco Martin <mart@kde.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Library General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import QtQuick 2.1
import QtGraphicalEffects 1.0
import org.kde.plasma.mobilecomponents 0.2
import "private"

/**Documented API

Imports:
        QtQuick 2.1

Description:
        Overlay Drawers are used to expose additional UI elements needed for
        small secondary tasks for which the main UI elements are not needed.
        For example in Okular Active, an Overlay Drawer is used to display
        thumbnails of all pages within a document along with a search field.
        This is used for the distinct task of navigating to another page.

Properties:
        bool opened:
        If true the drawer is open showing the contents of the "drawer"
		component.

        Item page:
        It's the default property. it's the main content of the drawer page,
		the part that is always shown

        Item contentItem:
        It's the part that can be pulled in and out, will act as a sidebar.
**/
AbstractDrawer {
    id: root
    anchors.fill: parent
    z: 9999

    default property alias page: mainPage.data
    property Item contentItem
    property alias opened: browserFrame.open
    property int edge: Qt.LeftEdge
    property real position: 0

    onContentItemChanged: contentItem.parent = drawerPage
    onPositionChanged: {
        if (!enabled) {
            return;
        }
        if (root.edge == Qt.LeftEdge) {
            browserFrame.x = -browserFrame.width + position * browserFrame.width;
        } else {
            browserFrame.x = root.width - (position * browserFrame.width);
        }
    }
    function open () {
        mouseEventListener.startBrowserFrameX = browserFrame.x;
        browserFrame.state = "Dragging";
        browserFrame.state = "Open";
    }
    function close () {
        mouseEventListener.startBrowserFrameX = browserFrame.x;
        browserFrame.state = "Dragging";
        browserFrame.state = "Closed";
    }

    Item {
        id: mainPage
        anchors.fill: parent
        onChildrenChanged: mainPage.children[0].anchors.fill = mainPage
    }

    Rectangle {
        anchors.fill: parent
        color: "black"
        opacity: 0.6 * (root.edge == Qt.LeftEdge
            ? ((browserFrame.x + browserFrame.width) / browserFrame.width)
            : (1 - browserFrame.x / root.width))
    }

    MouseArea {
        anchors {
            right: root.edge == Qt.LeftEdge ? undefined : parent.right
            left: root.edge == Qt.LeftEdge ? parent.left : undefined
            top: parent.top
            bottom: parent.bottom
        }
        z: 99
        width: Units.smallSpacing
        onPressed: mouseEventListener.managePressed(mouse)
        onPositionChanged: mouseEventListener.positionChanged(mouse)
        onReleased: mouseEventListener.released(mouse)
    }
    MouseArea {
        id: mouseEventListener
        anchors.fill: parent
        drag.filterChildren: true
        property int startBrowserFrameX
        property int startMouseX
        property real oldMouseX
        property bool startDragging: false
        property string startState
        enabled: browserFrame.state != "Closed"

        onPressed: managePressed(mouse)
        function managePressed(mouse) {
            if (drawerPage.children.length == 0) {
                mouse.accepted = false;
                return;
            }

            mouse.accepted = true;
            startBrowserFrameX = browserFrame.x;
            oldMouseX = startMouseX = mouse.x;
            startDragging = false;
            startState = browserFrame.state;
            browserFrame.state = "Dragging";
            browserFrame.x = startBrowserFrameX;
        }

        onPositionChanged: {
            if (drawerPage.children.length == 0) {
                mouse.accepted = false;
                return;
            }

            if (mouse.x < Units.gridUnit ||
                Math.abs(mouse.x - startMouseX) > root.width / 5) {
                startDragging = true;
            }
            if (startDragging) {
                browserFrame.x = root.edge == Qt.LeftEdge
                    ? Math.min(0, browserFrame.x + mouse.x - oldMouseX)
                    : Math.max(root.width - browserFrame.width, browserFrame.x + mouse.x - oldMouseX);
            }
            oldMouseX = mouse.x;
        }

        onReleased: {
            if (drawerPage.children.length == 0) {
                mouse.accepted = false;
                return;
            }

            if (root.edge == Qt.LeftEdge) {
                if (mouse.x < Units.gridUnit) {
                    browserFrame.state = "Closed";
                } else if (browserFrame.x - startBrowserFrameX > browserFrame.width / 3) {
                    browserFrame.state = "Open";
                } else if (startBrowserFrameX - browserFrame.x > browserFrame.width / 3) {
                    browserFrame.state = "Closed";
                } else {
                    browserFrame.state = startState
                }

            } else {
                if (mouse.x > width - Units.gridUnit) {
                    browserFrame.state = "Closed";
                } else if (browserFrame.x - startBrowserFrameX > browserFrame.width / 3) {
                    browserFrame.state = "Closed";
                } else if (startBrowserFrameX - browserFrame.x > browserFrame.width / 3) {
                    browserFrame.state = "Open";
                } else {
                    browserFrame.state = startState;
                }
            }
        }
        onCanceled: {
            if (oldMouseX > width - Units.gridUnit) {
                browserFrame.state = "Closed";
            } else if (Math.abs(browserFrame.x - startBrowserFrameX) > browserFrame.width / 3) {
                browserFrame.state = startState == "Open" ? "Closed" : "Open";
            } else {
                browserFrame.state = "Open";
            }
        }
        onClicked: {
            if (Math.abs(startMouseX - mouse.x) > Units.gridUnit) {
                return;
            }
            if ((root.edge == Qt.LeftEdge && mouse.x > browserFrame.width) ||
                (root.edge != Qt.LeftEdge && mouse.x < browserFrame.x)) {
                browserFrame.state = startState == "Open" ? "Closed" : "Open";
                root.clicked();
            }
        }
        Rectangle {
            id: browserFrame
            z: 100
            color: Theme.viewBackgroundColor
            anchors {
                top: parent.top
                bottom: parent.bottom
            }

            width: {
                if (drawerPage.children.length > 0 && drawerPage.children[0].implicitWidth > 0) {
                    return Math.min( parent.width - Units.gridUnit, drawerPage.children[0].implicitWidth)
                } else {
                    return parent.width - Units.gridUnit * 3
                }
            }

            onXChanged: {
                root.position = root.edge == Qt.LeftEdge ? 1 + browserFrame.x/browserFrame.width : (root.width - browserFrame.x)/browserFrame.width;
            }

            state: "Closed"
            onStateChanged: open = (state != "Closed")
            property bool open: false
            onOpenChanged: {
                if (browserFrame.state == "Dragging" || drawerPage.children.length == 0) {
                    return;
                }

                if (open) {
                    browserFrame.state = "Open";
                } else {
                    browserFrame.state = "Closed";
                }
            }

            LinearGradient {
                width: Units.gridUnit/2
                anchors {
                    right: root.edge == Qt.LeftEdge ? undefined : parent.left
                    left: root.edge == Qt.LeftEdge ? parent.right : undefined
                    top: parent.top
                    bottom: parent.bottom
                }
                opacity: root.position == 0 ? 0 : 1
                start: Qt.point(0, 0)
                end: Qt.point(Units.gridUnit/2, 0)
                gradient: Gradient {
                    GradientStop {
                        position: 0.0
                        color: root.edge == Qt.LeftEdge ? Qt.rgba(0, 0, 0, 0.3) : "transparent"
                    }
                    GradientStop {
                        position: root.edge == Qt.LeftEdge ? 0.3 : 0.7
                        color: Qt.rgba(0, 0, 0, 0.15)
                    }
                    GradientStop {
                        position: 1.0
                        color: root.edge == Qt.LeftEdge ? "transparent" : Qt.rgba(0, 0, 0, 0.3)
                    }
                }
                Behavior on opacity {
                    NumberAnimation {
                        duration: Units.longDuration
                        easing.type: Easing.InOutQuad
                    }
                }
            }


            MouseArea {
                id: drawerPage
                anchors {
                    fill: parent
                    //leftMargin: Units.gridUnit
                }
                clip: true
                onChildrenChanged: drawerPage.children[0].anchors.fill = drawerPage
            }

            states: [
                State {
                    name: "Open"
                    PropertyChanges {
                        target: browserFrame
                        x: root.edge == Qt.LeftEdge ? 0 : root.width - browserFrame.width
                    }
                },
                State {
                    name: "Dragging"
                    //workaround for a quirkiness of the state machine
                    //if no x binding gets defined in this state x will be set to whatever last x it had last time it was in this state
                    PropertyChanges {
                        target: browserFrame
                        x: mouseEventListener.startBrowserFrameX
                    }
                },
                State {
                    name: "Closed"
                    PropertyChanges {
                        target: browserFrame
                        x: root.edge == Qt.LeftEdge ? -browserFrame.width : root.width
                    }
                }
            ]

            transitions: [
                Transition {
                    //Exclude Dragging
                    to: "Open,Closed"
                    NumberAnimation {
                        id: transitionAnim
                        properties: "x"
                        duration: Units.longDuration
                        easing.type: Easing.InOutQuad
                    }
                }
            ]
        }
    }
}