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
214 right: drawerHandle.left
216 bottom: parent.bottom
217 leftMargin: root.panelWidth
221 id: searchFieldContainer
223 anchors { left: parent.left; top: parent.top; right: parent.right; margins: units.gu(1) }
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
233 bottom: parent.bottom
235 placeholderText: i18n.tr("Search…")
238 KeyNavigation.down: appList
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;
246 root.applicationSelected(appList.getFirstAppId());
254 objectName: "drawerAppList"
258 top: searchFieldContainer.bottom
259 bottom: parent.bottom
261 height: rows * delegateHeight
264 model: sortProxyModel
265 delegateWidth: root.delegateWidth
266 delegateHeight: units.gu(11)
267 delegate: drawerDelegateComponent
268 onDraggingVerticallyChanged: {
269 if (draggingVertically) {
274 refreshing: appDrawerModel.refreshing
276 appDrawerModel.refresh();
282 id: drawerDelegateComponent
285 width: GridView.view.cellWidth
287 objectName: "drawerItem_" + model.appId
289 readonly property bool focused: index === GridView.view.currentIndex && GridView.view.activeFocus
291 onClicked: root.applicationSelected(model.appId)
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],"") + "/");
298 z: loader.active ? 1 : 0
302 anchors.horizontalCenter: parent.horizontalCenter
303 height: childrenRect.height
309 height: 7.5 / 8 * width
310 anchors.horizontalCenter: parent.horizontalCenter
312 borderSource: 'undefined'
316 sourceSize.width: appIcon.width
319 sourceFillMode: LomiriShape.PreserveAspectCrop
322 styleName: "FocusShape"
325 visible: drawerDelegate.focused
326 radius: units.gu(2.55)
335 anchors.horizontalCenter: parent.horizontalCenter
336 horizontalAlignment: Text.AlignHCenter
338 wrapMode: Text.WordWrap
340 elide: Text.ElideRight
347 aux = label.width / 2 - item.width / 2;
348 var containerXMap = mapToItem(contentContainer, aux, 0).x
349 if (containerXMap < 0) {
350 aux = aux - containerXMap;
353 if (containerXMap + item.width > contentContainer.width) {
354 aux = aux - (containerXMap + item.width - contentContainer.width);
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)
368 width: Math.min(root.delegateWidth * 2, implicitWidth)
370 horizontalAlignment: Text.AlignHCenter
372 elide: Text.ElideRight
373 anchors.centerIn: parent