Unity 8
Spread.qml
1 /*
2  * Copyright (C) 2016 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.4
18 import Ubuntu.Components 1.3
19 import "MathUtils.js" as MathUtils
20 
21 Item {
22  id: root
23 
24  // Information about the environment
25  property int highlightedIndex: -1
26  property var model: null
27  property int leftMargin: 0
28  property var spreadFlickable
29 
30  // some config options
31  property real contentMargin: 0.16 * root.height
32  property real contentTopMargin: contentMargin
33  property real contentBottomMargin: 0.35 * contentMargin
34  property real windowTitleTopMargin: 3/4 * (contentTopMargin - windowTitle.height)
35  property int stackItemCount: 3
36  property real leftRotationAngle: 22
37  property real rightRotationAngle: 32
38  property real leftStackScale: .82
39  property real rightStackScale: 1
40  property real rightEdgeBreakPoint: Math.min(units.gu(40) / root.width, .35)
41 
42  signal leaveSpread()
43 
44  // Calculated stuff
45  readonly property int totalItemCount: model.count
46  readonly property real leftStackXPos: 0.03 * root.width + leftMargin
47  readonly property real rightStackXPos: root.width - 1.5 * leftStackXPos + leftMargin
48 
49  readonly property real stackHeight: spreadItemHeight - appInfoHeight
50  readonly property real stackWidth: Math.min(leftStackXPos/3, units.gu(1.5))
51 
52  readonly property real spreadWidth: rightStackXPos - leftStackXPos
53  readonly property real spreadHeight: root.height
54  readonly property real spreadItemHeight: spreadHeight - contentTopMargin - contentBottomMargin
55  readonly property real spreadItemWidth: stackHeight
56 
57  readonly property real dynamicLeftRotationAngle: leftRotationAngle * rotationAngleFactor
58  readonly property real dynamicRightRotationAngle: rightRotationAngle * rotationAngleFactor
59 
60  readonly property real appInfoHeight: {
61  var screenHeightReferencePoint = 40 // ref screen height in gu
62  var valueAtReferencePoint = 0.17 // of screen height at the reference point
63  var appInfoHeightValueChange = -0.0014 // units / gu
64  var minAppInfoHeight = 0.08
65  var maxAppInfoHeight = 0.2
66  var screenHeightInGU = root.height / units.gu(1) // screenHeight in gu
67 
68  return MathUtils.clamp(valueAtReferencePoint + appInfoHeightValueChange * (screenHeightInGU - screenHeightReferencePoint), minAppInfoHeight, maxAppInfoHeight) * root.height
69  }
70 
71  property real rotationAngleFactor: {
72  var spreadHeightReferencePoint = 28 // reference spread height in gu
73  var valueAtReferencePoint = 1.3
74  var rotationAngleValueChange = -0.008 // units / gu
75  var minRotationAngleFactor = 0.6
76  var maxRotationAngleFactor = 1.5
77  var spreadHeightInGU = spreadHeight / units.gu(1)
78 
79  return MathUtils.clamp(valueAtReferencePoint + rotationAngleValueChange * (spreadHeightInGU - spreadHeightReferencePoint), minRotationAngleFactor, maxRotationAngleFactor)
80  }
81  readonly property real itemOverlap: {
82  var spreadAspectRatioReferencePoint = 1.0 // ref screen height in gu
83  var valueAtReferencePoint = 0.74 // of screen height at the reference point
84  var itemOverlapValueChange = -0.068
85  var minOverlap = 0.55
86  var maxOverlap = 0.82
87  var spreadAspectRatio = spreadWidth / stackHeight // spread stack aspect ratio (app info not included)
88 
89  return MathUtils.clamp(valueAtReferencePoint + itemOverlapValueChange * (spreadAspectRatio - spreadAspectRatioReferencePoint), minOverlap, maxOverlap)
90  }
91 
92  readonly property real visibleItemCount: (spreadWidth / spreadItemWidth) / (1 - itemOverlap)
93 
94  readonly property real spreadTotalWidth: Math.max(2,totalItemCount) * spreadWidth / visibleItemCount
95 
96  readonly property real centeringOffset: Math.max(spreadWidth - spreadTotalWidth ,0) / (2 * spreadWidth)
97 
98 
99  readonly property var curve: BezierCurve {
100  controlPoint2: {'x': 0.19, 'y': 0.00}
101  controlPoint3: {'x': 0.91, 'y': 1.00}
102  }
103 
104  Label {
105  id: windowTitle
106 
107  width: Math.min(implicitWidth, 0.5*root.width)
108  elide: Qt.ElideMiddle
109  anchors.horizontalCenter: parent.horizontalCenter
110  y: windowTitleTopMargin
111  readonly property var highlightedSurface: root.model ? root.model.surfaceAt(root.highlightedIndex) : null
112  readonly property var highlightedApp: root.model ? root.model.applicationAt(root.highlightedIndex) : null
113  text: root.highlightedIndex >= 0 && highlightedSurface && highlightedSurface.name != "" ? highlightedSurface.name :
114  highlightedApp ? highlightedApp.name : ""
115  fontSize: root.height < units.gu(85) ? 'medium' : 'large'
116  color: "white"
117  opacity: root.highlightedIndex >= 0 ? 1 : 0
118  Behavior on opacity { UbuntuNumberAnimation { } }
119  }
120 
121  Keys.onPressed: {
122  switch (event.key) {
123  case Qt.Key_Left:
124  case Qt.Key_Backtab:
125  selectPrevious(event.isAutoRepeat)
126  event.accepted = true;
127  break;
128  case Qt.Key_Right:
129  case Qt.Key_Tab:
130  selectNext(event.isAutoRepeat)
131  event.accepted = true;
132  break;
133  case Qt.Key_Escape:
134  highlightedIndex = -1
135  // Falling through intentionally
136  case Qt.Key_Enter:
137  case Qt.Key_Return:
138  case Qt.Key_Space:
139  root.leaveSpread();
140  event.accepted = true;
141  }
142  }
143 
144 
145  function selectNext(isAutoRepeat) {
146  if (isAutoRepeat && highlightedIndex >= totalItemCount -1) {
147  return; // AutoRepeat is not allowed to wrap around
148  }
149 
150  highlightedIndex = (highlightedIndex + 1) % totalItemCount;
151  spreadFlickable.snap(highlightedIndex)
152  }
153 
154  function selectPrevious(isAutoRepeat) {
155  if (isAutoRepeat && highlightedIndex == 0) {
156  return; // AutoRepeat is not allowed to wrap around
157  }
158 
159  highlightedIndex = highlightedIndex - 1 >= 0 ? highlightedIndex - 1 : totalItemCount - 1;
160  spreadFlickable.snap(highlightedIndex)
161  }
162 }