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
39 signal applicationSelected(string appId)
41 // Request that the Drawer is opened fully, if it was partially closed then
43 signal openRequested()
45 // Request that the Drawer (and maybe its parent) is hidden, normally if
46 // the Drawer has been dragged away.
47 signal hideRequested()
49 property bool allowSlidingAnimation: false
50 property bool draggingHorizontally: false
51 property int dragDistance: 0
53 property var hadFocus: false
54 property var oldSelectionStart: null
55 property var oldSelectionEnd: null
58 onRightMarginChanged: refocusInputAfterUserLetsGo()
61 Behavior on anchors.rightMargin {
62 enabled: allowSlidingAnimation && !draggingHorizontally
65 easing.type: Easing.OutCubic
69 onDraggingHorizontallyChanged: {
70 // See refocusInputAfterUserLetsGo()
71 if (draggingHorizontally) {
72 hadFocus = searchField.focus;
73 oldSelectionStart = searchField.selectionStart;
74 oldSelectionEnd = searchField.selectionEnd;
75 searchField.focus = false;
77 if (x < -units.gu(10)) {
82 refocusInputAfterUserLetsGo();
86 Keys.onEscapePressed: {
90 onDragDistanceChanged: {
91 anchors.rightMargin = Math.max(-drawer.width, anchors.rightMargin + dragDistance);
94 function resetOldFocus() {
96 oldSelectionStart = null;
97 oldSelectionEnd = null;
100 function refocusInputAfterUserLetsGo() {
101 if (!draggingHorizontally) {
102 if (fullyOpen && hadFocus) {
103 searchField.focus = hadFocus;
104 searchField.select(oldSelectionStart, oldSelectionEnd);
105 } else if (fullyOpen || fullyClosed) {
110 searchField.text = "";
111 appList.currentIndex = 0;
112 searchField.focus = false;
113 appList.focus = false;
118 function focusInput() {
119 searchField.selectAll();
120 searchField.focus = true;
123 function unFocusInput() {
124 searchField.focus = false;
128 if (event.text.trim() !== "") {
130 searchField.text = event.text;
136 appList.focus = true;
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;
150 acceptedButtons: Qt.AllButtons
151 onWheel: wheel.accepted = true
156 color: theme.palette.normal.background.hslLightness >= 0.5 ? "#CAFEFEFE" : "#BF000000"
160 objectName: "drawerHandle"
164 bottom: parent.bottom
170 handle.active = true;
174 var diff = oldX - mouseX;
175 root.draggingHorizontally |= diff > units.gu(2);
176 if (!root.draggingHorizontally) {
179 root.dragDistance += diff;
186 root.draggingHorizontally = false;
187 handle.active = false;
188 root.dragDistance = 0;
194 active: parent.pressed
202 AppDrawerProxyModel {
204 source: appDrawerModel
205 filterString: searchField.displayText
206 sortBy: AppDrawerProxyModel.SortByAToZ
211 function onLanguageChanged() { appDrawerModel.refresh() }
218 right: drawerHandle.left
220 bottom: parent.bottom
221 leftMargin: root.panelWidth
225 id: searchFieldContainer
227 anchors { left: parent.left; top: parent.top; right: parent.right; margins: units.gu(1) }
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
237 bottom: parent.bottom
239 placeholderText: i18n.tr("Search…")
242 KeyNavigation.down: appList
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;
250 root.applicationSelected(appList.getFirstAppId());
258 objectName: "drawerAppList"
262 top: searchFieldContainer.bottom
263 bottom: parent.bottom
265 height: rows * delegateHeight
268 model: sortProxyModel
269 delegateWidth: root.delegateWidth
270 delegateHeight: units.gu(11)
271 delegate: drawerDelegateComponent
272 onDraggingVerticallyChanged: {
273 if (draggingVertically) {
278 refreshing: appDrawerModel.refreshing
280 appDrawerModel.refresh();
286 id: drawerDelegateComponent
289 width: GridView.view.cellWidth
291 objectName: "drawerItem_" + model.appId
293 readonly property bool focused: index === GridView.view.currentIndex && GridView.view.activeFocus
295 onClicked: root.applicationSelected(model.appId)
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],"") + "/");
302 z: loader.active ? 1 : 0
306 anchors.horizontalCenter: parent.horizontalCenter
307 height: childrenRect.height
313 height: 7.5 / 8 * width
314 anchors.horizontalCenter: parent.horizontalCenter
316 borderSource: 'undefined'
320 sourceSize.width: appIcon.width
323 sourceFillMode: LomiriShape.PreserveAspectCrop
326 styleName: "FocusShape"
329 visible: drawerDelegate.focused
330 radius: units.gu(2.55)
339 anchors.horizontalCenter: parent.horizontalCenter
340 horizontalAlignment: Text.AlignHCenter
342 wrapMode: Text.WordWrap
344 elide: Text.ElideRight
351 aux = label.width / 2 - item.width / 2;
352 var containerXMap = mapToItem(contentContainer, aux, 0).x
353 if (containerXMap < 0) {
354 aux = aux - containerXMap;
357 if (containerXMap + item.width > contentContainer.width) {
358 aux = aux - (containerXMap + item.width - contentContainer.width);
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)
372 width: Math.min(root.delegateWidth * 2, implicitWidth)
374 horizontalAlignment: Text.AlignHCenter
376 elide: Text.ElideRight
377 anchors.centerIn: parent