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