2 * Copyright (C) 2016 Canonical, Ltd.
3 * Copyright (C) 2020 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 Ubuntu.Components 1.3
20 import Unity.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
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: {
60 if (fullyOpen && hadFocus) {
61 // See onDraggingHorizontallyChanged below
62 searchField.focus = hadFocus;
63 searchField.select(oldSelectionStart, oldSelectionEnd);
64 } else if (fullyClosed || fullyOpen) {
65 searchField.text = "";
71 Behavior on anchors.rightMargin {
72 enabled: allowSlidingAnimation && !draggingHorizontally
75 easing.type: Easing.OutCubic
79 onDraggingHorizontallyChanged: {
80 if (draggingHorizontally) {
81 // Remove (and put back using anchors.onRightMarginChanged) the
82 // focus for the searchfield in order to hide the copy/paste
83 // popover when we move the drawer
84 hadFocus = searchField.focus;
85 oldSelectionStart = searchField.selectionStart;
86 oldSelectionEnd = searchField.selectionEnd;
87 searchField.focus = false;
89 if (x < -units.gu(10)) {
97 Keys.onEscapePressed: {
101 onDragDistanceChanged: {
102 anchors.rightMargin = Math.max(-drawer.width, anchors.rightMargin + dragDistance);
105 function resetOldFocus() {
107 oldSelectionStart = null;
108 oldSelectionEnd = null;
109 appList.currentIndex = 0;
110 searchField.focus = false;
111 appList.focus = false;
114 function focusInput() {
115 searchField.selectAll();
116 searchField.focus = true;
119 function unFocusInput() {
120 searchField.focus = false;
124 if (event.text.trim() !== "") {
126 searchField.text = event.text;
132 appList.focus = true;
138 // Catch all presses here in case the navigation lets something through
139 // We never want to end up in the launcher with focus
140 event.accepted = true;
146 acceptedButtons: Qt.AllButtons
147 onWheel: wheel.accepted = true
158 source: root.background
162 anchors.fill: background
168 // Images with fastblur can't use opacity, so we'll put this on top
170 anchors.fill: background
177 objectName: "drawerHandle"
181 bottom: parent.bottom
187 handle.active = true;
191 var diff = oldX - mouseX;
192 root.draggingHorizontally |= diff > units.gu(2);
193 if (!root.draggingHorizontally) {
196 root.dragDistance += diff;
203 root.draggingHorizontally = false;
204 handle.active = false;
210 active: parent.pressed
218 AppDrawerProxyModel {
220 source: appDrawerModel
221 filterString: searchField.displayText
222 sortBy: AppDrawerProxyModel.SortByAToZ
229 right: drawerHandle.left
231 bottom: parent.bottom
232 leftMargin: root.panelWidth
236 id: searchFieldContainer
238 anchors { left: parent.left; top: parent.top; right: parent.right; margins: units.gu(1) }
242 objectName: "searchField"
243 inputMethodHints: Qt.ImhNoPredictiveText; //workaround to get the clear button enabled without the need of a space char event or change in focus
248 bottom: parent.bottom
250 placeholderText: i18n.tr("Search…")
253 KeyNavigation.down: appList
256 if (searchField.displayText != "" && appList) {
257 // In case there is no currentItem (it might have been filtered away) lets reset it to the first item
258 if (!appList.currentItem) {
259 appList.currentIndex = 0;
261 root.applicationSelected(appList.getFirstAppId());
269 objectName: "drawerAppList"
273 top: searchFieldContainer.bottom
274 bottom: parent.bottom
276 height: rows * delegateHeight
279 model: sortProxyModel
280 delegateWidth: root.delegateWidth
281 delegateHeight: units.gu(11)
282 delegate: drawerDelegateComponent
283 onDraggingVerticallyChanged: {
284 if (draggingVertically) {
289 refreshing: appDrawerModel.refreshing
291 appDrawerModel.refresh();
297 id: drawerDelegateComponent
300 width: GridView.view.cellWidth
302 objectName: "drawerItem_" + model.appId
304 readonly property bool focused: index === GridView.view.currentIndex && GridView.view.activeFocus
306 onClicked: root.applicationSelected(model.appId)
308 if (model.appId.includes(".")) { // Open OpenStore page if app is a click
309 var splitAppId = model.appId.split("_");
310 Qt.openUrlExternally("https://open-store.io/app/" + model.appId.replace("_" + splitAppId[splitAppId.length-1],"") + "/");
313 z: loader.active ? 1 : 0
317 anchors.horizontalCenter: parent.horizontalCenter
318 height: childrenRect.height
324 height: 7.5 / 8 * width
325 anchors.horizontalCenter: parent.horizontalCenter
327 borderSource: 'undefined'
331 sourceSize.width: appIcon.width
334 sourceFillMode: UbuntuShape.PreserveAspectCrop
337 styleName: "FocusShape"
340 visible: drawerDelegate.focused
341 radius: units.gu(2.55)
350 anchors.horizontalCenter: parent.horizontalCenter
351 horizontalAlignment: Text.AlignHCenter
353 wrapMode: Text.WordWrap
355 elide: Text.ElideRight
362 aux = label.width / 2 - item.width / 2;
363 var containerXMap = mapToItem(contentContainer, aux, 0).x
364 if (containerXMap < 0) {
365 aux = aux - containerXMap;
368 if (containerXMap + item.width > contentContainer.width) {
369 aux = aux - (containerXMap + item.width - contentContainer.width);
375 active: label.truncated && (drawerDelegate.hovered || drawerDelegate.focused)
376 sourceComponent: Rectangle {
377 color: UbuntuColors.jet
378 width: fullLabel.contentWidth + units.gu(1)
379 height: fullLabel.height + units.gu(1)
383 width: Math.min(root.delegateWidth * 2, implicitWidth)
385 horizontalAlignment: Text.AlignHCenter
387 elide: Text.ElideRight
388 anchors.centerIn: parent