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/>.
19 import Lomiri.Components 1.3
20 import Lomiri.Launcher 0.1
22 import "../Components"
23 import Qt.labs.settings 1.0
25 import AccountsService 0.1
26 import 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
36 property alias backgroundSourceSize: background.sourceSize
38 property var fullyOpen: x === 0
39 property var fullyClosed: x === -width
41 signal applicationSelected(string appId)
43 // Request that the Drawer is opened fully, if it was partially closed then
45 signal openRequested()
47 // Request that the Drawer (and maybe its parent) is hidden, normally if
48 // the Drawer has been dragged away.
49 signal hideRequested()
51 property bool allowSlidingAnimation: false
52 property bool draggingHorizontally: false
53 property int dragDistance: 0
55 property var hadFocus: false
56 property var oldSelectionStart: null
57 property var oldSelectionEnd: null
60 onRightMarginChanged: refocusInputAfterUserLetsGo()
63 Behavior on anchors.rightMargin {
64 enabled: allowSlidingAnimation && !draggingHorizontally
67 easing.type: Easing.OutCubic
71 onDraggingHorizontallyChanged: {
72 // See refocusInputAfterUserLetsGo()
73 if (draggingHorizontally) {
74 hadFocus = searchField.focus;
75 oldSelectionStart = searchField.selectionStart;
76 oldSelectionEnd = searchField.selectionEnd;
77 searchField.focus = false;
79 if (x < -units.gu(10)) {
84 refocusInputAfterUserLetsGo();
88 Keys.onEscapePressed: {
92 onDragDistanceChanged: {
93 anchors.rightMargin = Math.max(-drawer.width, anchors.rightMargin + dragDistance);
96 function resetOldFocus() {
98 oldSelectionStart = null;
99 oldSelectionEnd = null;
102 function refocusInputAfterUserLetsGo() {
103 if (!draggingHorizontally) {
104 if (fullyOpen && hadFocus) {
105 searchField.focus = hadFocus;
106 searchField.select(oldSelectionStart, oldSelectionEnd);
107 } else if (fullyOpen || fullyClosed) {
112 searchField.text = "";
113 appList.currentIndex = 0;
114 searchField.focus = false;
115 appList.focus = false;
120 function focusInput() {
121 searchField.selectAll();
122 searchField.focus = true;
125 function unFocusInput() {
126 searchField.focus = false;
130 if (event.text.trim() !== "") {
132 searchField.text = event.text;
138 appList.focus = true;
144 // Catch all presses here in case the navigation lets something through
145 // We never want to end up in the launcher with focus
146 event.accepted = true;
152 acceptedButtons: Qt.AllButtons
153 onWheel: wheel.accepted = true
163 objectName: "drawerBackground"
165 source: root.background
169 anchors.fill: background
175 // Images with fastblur can't use opacity, so we'll put this on top
177 anchors.fill: background
184 objectName: "drawerHandle"
188 bottom: parent.bottom
194 handle.active = true;
198 var diff = oldX - mouseX;
199 root.draggingHorizontally |= diff > units.gu(2);
200 if (!root.draggingHorizontally) {
203 root.dragDistance += diff;
210 root.draggingHorizontally = false;
211 handle.active = false;
212 root.dragDistance = 0;
218 active: parent.pressed
226 AppDrawerProxyModel {
228 source: appDrawerModel
229 filterString: searchField.displayText
230 sortBy: AppDrawerProxyModel.SortByAToZ
237 right: drawerHandle.left
239 bottom: parent.bottom
240 leftMargin: root.panelWidth
244 id: searchFieldContainer
246 anchors { left: parent.left; top: parent.top; right: parent.right; margins: units.gu(1) }
250 objectName: "searchField"
251 inputMethodHints: Qt.ImhNoPredictiveText; //workaround to get the clear button enabled without the need of a space char event or change in focus
256 bottom: parent.bottom
258 placeholderText: i18n.tr("Search…")
261 KeyNavigation.down: appList
264 if (searchField.displayText != "" && appList) {
265 // In case there is no currentItem (it might have been filtered away) lets reset it to the first item
266 if (!appList.currentItem) {
267 appList.currentIndex = 0;
269 root.applicationSelected(appList.getFirstAppId());
277 objectName: "drawerAppList"
281 top: searchFieldContainer.bottom
282 bottom: parent.bottom
284 height: rows * delegateHeight
287 model: sortProxyModel
288 delegateWidth: root.delegateWidth
289 delegateHeight: units.gu(11)
290 delegate: drawerDelegateComponent
291 onDraggingVerticallyChanged: {
292 if (draggingVertically) {
297 refreshing: appDrawerModel.refreshing
299 appDrawerModel.refresh();
305 id: drawerDelegateComponent
308 width: GridView.view.cellWidth
310 objectName: "drawerItem_" + model.appId
312 readonly property bool focused: index === GridView.view.currentIndex && GridView.view.activeFocus
314 onClicked: root.applicationSelected(model.appId)
316 if (model.appId.includes(".")) { // Open OpenStore page if app is a click
317 var splitAppId = model.appId.split("_");
318 Qt.openUrlExternally("https://open-store.io/app/" + model.appId.replace("_" + splitAppId[splitAppId.length-1],"") + "/");
321 z: loader.active ? 1 : 0
325 anchors.horizontalCenter: parent.horizontalCenter
326 height: childrenRect.height
332 height: 7.5 / 8 * width
333 anchors.horizontalCenter: parent.horizontalCenter
335 borderSource: 'undefined'
339 sourceSize.width: appIcon.width
342 sourceFillMode: LomiriShape.PreserveAspectCrop
345 styleName: "FocusShape"
348 visible: drawerDelegate.focused
349 radius: units.gu(2.55)
358 anchors.horizontalCenter: parent.horizontalCenter
359 horizontalAlignment: Text.AlignHCenter
361 wrapMode: Text.WordWrap
363 elide: Text.ElideRight
370 aux = label.width / 2 - item.width / 2;
371 var containerXMap = mapToItem(contentContainer, aux, 0).x
372 if (containerXMap < 0) {
373 aux = aux - containerXMap;
376 if (containerXMap + item.width > contentContainer.width) {
377 aux = aux - (containerXMap + item.width - contentContainer.width);
383 active: label.truncated && (drawerDelegate.hovered || drawerDelegate.focused)
384 sourceComponent: Rectangle {
385 color: LomiriColors.jet
386 width: fullLabel.contentWidth + units.gu(1)
387 height: fullLabel.height + units.gu(1)
391 width: Math.min(root.delegateWidth * 2, implicitWidth)
393 horizontalAlignment: Text.AlignHCenter
395 elide: Text.ElideRight
396 anchors.centerIn: parent