2 * Copyright (C) 2016 Canonical Ltd.
3 * Copyright (C) 2020-2021 UBports Foundation
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.
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.
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/>.
19import Lomiri.Components 1.3
20import Lomiri.Launcher 0.1
23import Qt.labs.settings 1.0
25import AccountsService 0.1
26import QtGraphicalEffects 1.0
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
37 property var fullyOpen: x === 0
38 property var fullyClosed: x === -width
40 signal applicationSelected(string appId)
42 // Request that the Drawer is opened fully, if it was partially closed then
44 signal openRequested()
46 // Request that the Drawer (and maybe its parent) is hidden, normally if
47 // the Drawer has been dragged away.
48 signal hideRequested()
50 property bool allowSlidingAnimation: false
51 property bool draggingHorizontally: false
52 property int dragDistance: 0
54 property var hadFocus: false
55 property var oldSelectionStart: null
56 property var oldSelectionEnd: null
59 onRightMarginChanged: refocusInputAfterUserLetsGo()
62 Behavior on anchors.rightMargin {
63 enabled: allowSlidingAnimation && !draggingHorizontally
66 easing.type: Easing.OutCubic
70 onDraggingHorizontallyChanged: {
71 // See refocusInputAfterUserLetsGo()
72 if (draggingHorizontally) {
73 hadFocus = searchField.focus;
74 oldSelectionStart = searchField.selectionStart;
75 oldSelectionEnd = searchField.selectionEnd;
76 searchField.focus = false;
78 if (x < -units.gu(10)) {
83 refocusInputAfterUserLetsGo();
87 Keys.onEscapePressed: {
91 onDragDistanceChanged: {
92 anchors.rightMargin = Math.max(-drawer.width, anchors.rightMargin + dragDistance);
95 function resetOldFocus() {
97 oldSelectionStart = null;
98 oldSelectionEnd = null;
101 function refocusInputAfterUserLetsGo() {
102 if (!draggingHorizontally) {
103 if (fullyOpen && hadFocus) {
104 searchField.focus = hadFocus;
105 searchField.select(oldSelectionStart, oldSelectionEnd);
106 } else if (fullyOpen || fullyClosed) {
111 searchField.text = "";
112 appList.currentIndex = 0;
113 searchField.focus = false;
114 appList.focus = false;
119 function focusInput() {
120 searchField.selectAll();
121 searchField.focus = true;
124 function unFocusInput() {
125 searchField.focus = false;
129 if (event.text.trim() !== "") {
131 searchField.text = event.text;
137 appList.focus = true;
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;
151 acceptedButtons: Qt.AllButtons
152 onWheel: wheel.accepted = true
161 objectName: "drawerHandle"
165 bottom: parent.bottom
171 handle.active = true;
175 var diff = oldX - mouseX;
176 root.draggingHorizontally |= diff > units.gu(2);
177 if (!root.draggingHorizontally) {
180 root.dragDistance += diff;
187 root.draggingHorizontally = false;
188 handle.active = false;
189 root.dragDistance = 0;
195 active: parent.pressed
203 AppDrawerProxyModel {
205 source: appDrawerModel
206 filterString: searchField.displayText
207 sortBy: AppDrawerProxyModel.SortByAToZ
212 onLanguageChanged: appDrawerModel.refresh()
219 right: drawerHandle.left
221 bottom: parent.bottom
222 leftMargin: root.panelWidth
226 id: searchFieldContainer
228 anchors { left: parent.left; top: parent.top; right: parent.right; margins: units.gu(1) }
232 objectName: "searchField"
233 inputMethodHints: Qt.ImhNoPredictiveText; //workaround to get the clear button enabled without the need of a space char event or change in focus
238 bottom: parent.bottom
240 placeholderText: i18n.tr("Search…")
243 KeyNavigation.down: appList
246 if (searchField.displayText != "" && appList) {
247 // In case there is no currentItem (it might have been filtered away) lets reset it to the first item
248 if (!appList.currentItem) {
249 appList.currentIndex = 0;
251 root.applicationSelected(appList.getFirstAppId());
259 objectName: "drawerAppList"
263 top: searchFieldContainer.bottom
264 bottom: parent.bottom
266 height: rows * delegateHeight
269 model: sortProxyModel
270 delegateWidth: root.delegateWidth
271 delegateHeight: units.gu(11)
272 delegate: drawerDelegateComponent
273 onDraggingVerticallyChanged: {
274 if (draggingVertically) {
279 refreshing: appDrawerModel.refreshing
281 appDrawerModel.refresh();
287 id: drawerDelegateComponent
290 width: GridView.view.cellWidth
292 objectName: "drawerItem_" + model.appId
294 readonly property bool focused: index === GridView.view.currentIndex && GridView.view.activeFocus
296 onClicked: root.applicationSelected(model.appId)
298 if (model.appId.includes(".")) { // Open OpenStore page if app is a click
299 var splitAppId = model.appId.split("_");
300 Qt.openUrlExternally("https://open-store.io/app/" + model.appId.replace("_" + splitAppId[splitAppId.length-1],"") + "/");
303 z: loader.active ? 1 : 0
307 anchors.horizontalCenter: parent.horizontalCenter
308 height: childrenRect.height
314 height: 7.5 / 8 * width
315 anchors.horizontalCenter: parent.horizontalCenter
317 borderSource: 'undefined'
321 sourceSize.width: appIcon.width
324 sourceFillMode: LomiriShape.PreserveAspectCrop
327 styleName: "FocusShape"
330 visible: drawerDelegate.focused
331 radius: units.gu(2.55)
340 anchors.horizontalCenter: parent.horizontalCenter
341 horizontalAlignment: Text.AlignHCenter
343 wrapMode: Text.WordWrap
345 elide: Text.ElideRight
352 aux = label.width / 2 - item.width / 2;
353 var containerXMap = mapToItem(contentContainer, aux, 0).x
354 if (containerXMap < 0) {
355 aux = aux - containerXMap;
358 if (containerXMap + item.width > contentContainer.width) {
359 aux = aux - (containerXMap + item.width - contentContainer.width);
365 active: label.truncated && (drawerDelegate.hovered || drawerDelegate.focused)
366 sourceComponent: Rectangle {
367 color: LomiriColors.jet
368 width: fullLabel.contentWidth + units.gu(1)
369 height: fullLabel.height + units.gu(1)
373 width: Math.min(root.delegateWidth * 2, implicitWidth)
375 horizontalAlignment: Text.AlignHCenter
377 elide: Text.ElideRight
378 anchors.centerIn: parent