Lomiri
Drawer.qml
1/*
2 * Copyright (C) 2016 Canonical Ltd.
3 * Copyright (C) 2020-2021 UBports Foundation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18import QtQuick 2.12
19import Lomiri.Components 1.3
20import Lomiri.Launcher 0.1
21import Utils 0.1
22import "../Components"
23import Qt.labs.settings 1.0
24import GSettings 1.0
25import AccountsService 0.1
26import QtGraphicalEffects 1.0
27
28FocusScope {
29 id: root
30
31 property int panelWidth: 0
32 readonly property bool moving: (appList && appList.moving) ? true : false
33 readonly property Item searchTextField: searchField
34 readonly property real delegateWidth: units.gu(10)
35 property url background
36 visible: x > -width
37 property var fullyOpen: x === 0
38 property var fullyClosed: x === -width
39
40 signal applicationSelected(string appId)
41
42 // Request that the Drawer is opened fully, if it was partially closed then
43 // brought back
44 signal openRequested()
45
46 // Request that the Drawer (and maybe its parent) is hidden, normally if
47 // the Drawer has been dragged away.
48 signal hideRequested()
49
50 property bool allowSlidingAnimation: false
51 property bool draggingHorizontally: false
52 property int dragDistance: 0
53
54 property var hadFocus: false
55 property var oldSelectionStart: null
56 property var oldSelectionEnd: null
57
58 anchors {
59 onRightMarginChanged: refocusInputAfterUserLetsGo()
60 }
61
62 Behavior on anchors.rightMargin {
63 enabled: allowSlidingAnimation && !draggingHorizontally
64 NumberAnimation {
65 duration: 300
66 easing.type: Easing.OutCubic
67 }
68 }
69
70 onDraggingHorizontallyChanged: {
71 // See refocusInputAfterUserLetsGo()
72 if (draggingHorizontally) {
73 hadFocus = searchField.focus;
74 oldSelectionStart = searchField.selectionStart;
75 oldSelectionEnd = searchField.selectionEnd;
76 searchField.focus = false;
77 } else {
78 if (x < -units.gu(10)) {
79 hideRequested();
80 } else {
81 openRequested();
82 }
83 refocusInputAfterUserLetsGo();
84 }
85 }
86
87 Keys.onEscapePressed: {
88 root.hideRequested()
89 }
90
91 onDragDistanceChanged: {
92 anchors.rightMargin = Math.max(-drawer.width, anchors.rightMargin + dragDistance);
93 }
94
95 function resetOldFocus() {
96 hadFocus = false;
97 oldSelectionStart = null;
98 oldSelectionEnd = null;
99 }
100
101 function refocusInputAfterUserLetsGo() {
102 if (!draggingHorizontally) {
103 if (fullyOpen && hadFocus) {
104 searchField.focus = hadFocus;
105 searchField.select(oldSelectionStart, oldSelectionEnd);
106 } else if (fullyOpen || fullyClosed) {
107 resetOldFocus();
108 }
109
110 if (fullyClosed) {
111 searchField.text = "";
112 appList.currentIndex = 0;
113 searchField.focus = false;
114 appList.focus = false;
115 }
116 }
117 }
118
119 function focusInput() {
120 searchField.selectAll();
121 searchField.focus = true;
122 }
123
124 function unFocusInput() {
125 searchField.focus = false;
126 }
127
128 Keys.onPressed: {
129 if (event.text.trim() !== "") {
130 focusInput();
131 searchField.text = event.text;
132 }
133 switch (event.key) {
134 case Qt.Key_Right:
135 case Qt.Key_Left:
136 case Qt.Key_Down:
137 appList.focus = true;
138 break;
139 case Qt.Key_Up:
140 focusInput();
141 break;
142 }
143 // Catch all presses here in case the navigation lets something through
144 // We never want to end up in the launcher with focus
145 event.accepted = true;
146 }
147
148 MouseArea {
149 anchors.fill: parent
150 hoverEnabled: true
151 acceptedButtons: Qt.AllButtons
152 onWheel: wheel.accepted = true
153 }
154
155 Rectangle {
156 anchors.fill: parent
157 color: "#BF000000"
158
159 MouseArea {
160 id: drawerHandle
161 objectName: "drawerHandle"
162 anchors {
163 right: parent.right
164 top: parent.top
165 bottom: parent.bottom
166 }
167 width: units.gu(2)
168 property int oldX: 0
169
170 onPressed: {
171 handle.active = true;
172 oldX = mouseX;
173 }
174 onMouseXChanged: {
175 var diff = oldX - mouseX;
176 root.draggingHorizontally |= diff > units.gu(2);
177 if (!root.draggingHorizontally) {
178 return;
179 }
180 root.dragDistance += diff;
181 oldX = mouseX
182 }
183 onReleased: reset()
184 onCanceled: reset()
185
186 function reset() {
187 root.draggingHorizontally = false;
188 handle.active = false;
189 root.dragDistance = 0;
190 }
191
192 Handle {
193 id: handle
194 anchors.fill: parent
195 active: parent.pressed
196 }
197 }
198
199 AppDrawerModel {
200 id: appDrawerModel
201 }
202
203 AppDrawerProxyModel {
204 id: sortProxyModel
205 source: appDrawerModel
206 filterString: searchField.displayText
207 sortBy: AppDrawerProxyModel.SortByAToZ
208 }
209
210 Item {
211 id: contentContainer
212 anchors {
213 left: parent.left
214 right: drawerHandle.left
215 top: parent.top
216 bottom: parent.bottom
217 leftMargin: root.panelWidth
218 }
219
220 Item {
221 id: searchFieldContainer
222 height: units.gu(4)
223 anchors { left: parent.left; top: parent.top; right: parent.right; margins: units.gu(1) }
224
225 TextField {
226 id: searchField
227 objectName: "searchField"
228 inputMethodHints: Qt.ImhNoPredictiveText; //workaround to get the clear button enabled without the need of a space char event or change in focus
229 anchors {
230 left: parent.left
231 top: parent.top
232 right: parent.right
233 bottom: parent.bottom
234 }
235 placeholderText: i18n.tr("Search…")
236 z: 100
237
238 KeyNavigation.down: appList
239
240 onAccepted: {
241 if (searchField.displayText != "" && appList) {
242 // In case there is no currentItem (it might have been filtered away) lets reset it to the first item
243 if (!appList.currentItem) {
244 appList.currentIndex = 0;
245 }
246 root.applicationSelected(appList.getFirstAppId());
247 }
248 }
249 }
250 }
251
252 DrawerGridView {
253 id: appList
254 objectName: "drawerAppList"
255 anchors {
256 left: parent.left
257 right: parent.right
258 top: searchFieldContainer.bottom
259 bottom: parent.bottom
260 }
261 height: rows * delegateHeight
262 clip: true
263
264 model: sortProxyModel
265 delegateWidth: root.delegateWidth
266 delegateHeight: units.gu(11)
267 delegate: drawerDelegateComponent
268 onDraggingVerticallyChanged: {
269 if (draggingVertically) {
270 unFocusInput();
271 }
272 }
273
274 refreshing: appDrawerModel.refreshing
275 onRefresh: {
276 appDrawerModel.refresh();
277 }
278 }
279 }
280
281 Component {
282 id: drawerDelegateComponent
283 AbstractButton {
284 id: drawerDelegate
285 width: GridView.view.cellWidth
286 height: units.gu(11)
287 objectName: "drawerItem_" + model.appId
288
289 readonly property bool focused: index === GridView.view.currentIndex && GridView.view.activeFocus
290
291 onClicked: root.applicationSelected(model.appId)
292 onPressAndHold: {
293 if (model.appId.includes(".")) { // Open OpenStore page if app is a click
294 var splitAppId = model.appId.split("_");
295 Qt.openUrlExternally("https://open-store.io/app/" + model.appId.replace("_" + splitAppId[splitAppId.length-1],"") + "/");
296 }
297 }
298 z: loader.active ? 1 : 0
299
300 Column {
301 width: units.gu(9)
302 anchors.horizontalCenter: parent.horizontalCenter
303 height: childrenRect.height
304 spacing: units.gu(1)
305
306 LomiriShape {
307 id: appIcon
308 width: units.gu(6)
309 height: 7.5 / 8 * width
310 anchors.horizontalCenter: parent.horizontalCenter
311 radius: "medium"
312 borderSource: 'undefined'
313 source: Image {
314 id: sourceImage
315 asynchronous: true
316 sourceSize.width: appIcon.width
317 source: model.icon
318 }
319 sourceFillMode: LomiriShape.PreserveAspectCrop
320
321 StyledItem {
322 styleName: "FocusShape"
323 anchors.fill: parent
324 StyleHints {
325 visible: drawerDelegate.focused
326 radius: units.gu(2.55)
327 }
328 }
329 }
330
331 Label {
332 id: label
333 text: model.name
334 width: parent.width
335 anchors.horizontalCenter: parent.horizontalCenter
336 horizontalAlignment: Text.AlignHCenter
337 fontSize: "small"
338 wrapMode: Text.WordWrap
339 maximumLineCount: 2
340 elide: Text.ElideRight
341
342 Loader {
343 id: loader
344 x: {
345 var aux = 0;
346 if (item) {
347 aux = label.width / 2 - item.width / 2;
348 var containerXMap = mapToItem(contentContainer, aux, 0).x
349 if (containerXMap < 0) {
350 aux = aux - containerXMap;
351 containerXMap = 0;
352 }
353 if (containerXMap + item.width > contentContainer.width) {
354 aux = aux - (containerXMap + item.width - contentContainer.width);
355 }
356 }
357 return aux;
358 }
359 y: -units.gu(0.5)
360 active: label.truncated && (drawerDelegate.hovered || drawerDelegate.focused)
361 sourceComponent: Rectangle {
362 color: LomiriColors.jet
363 width: fullLabel.contentWidth + units.gu(1)
364 height: fullLabel.height + units.gu(1)
365 radius: units.dp(4)
366 Label {
367 id: fullLabel
368 width: Math.min(root.delegateWidth * 2, implicitWidth)
369 wrapMode: Text.Wrap
370 horizontalAlignment: Text.AlignHCenter
371 maximumLineCount: 3
372 elide: Text.ElideRight
373 anchors.centerIn: parent
374 text: model.name
375 fontSize: "small"
376 }
377 }
378 }
379 }
380 }
381 }
382 }
383 }
384}