Unity 8
SessionsList.qml
1 /*
2  * Copyright (C) 2015 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 Ubuntu.Components.ListItems 1.3
20 import "." 0.1
21 import "../Components"
22 
23 Item {
24  id: root
25  objectName: "sessionsList"
26 
27  signal sessionSelected(string sessionKey)
28  signal showLoginList()
29 
30  // Sets the position of the background highlight
31  function updateHighlight(session) {
32  sessionsList.currentIndex = getIndexOfSession(session);
33  sessionsList.currentItem.initialSession = session;
34  }
35 
36  function getIndexOfSession(session) {
37  for (var i = 0; i < sessionsList.model.count; i++) {
38  var key = sessionsList.model.get(i).key;
39  if (key === session) {
40  return i;
41  }
42  }
43 
44  return 0; // Just choose the first session
45  }
46 
47  function currentKey() {
48  var session = LightDMService.sessions.data(
49  sessionsList.currentIndex, LightDMService.sessionRoles.KeyRole)
50  return session;
51  }
52 
53  Keys.onEnterPressed: {
54  sessionSelected(currentKey());
55  showLoginList();
56  event.accepted = true;
57  }
58 
59  Keys.onEscapePressed: {
60  showLoginList();
61  event.accepted = true;
62  }
63 
64  Keys.onReturnPressed: {
65  sessionSelected(currentKey());
66  showLoginList();
67  event.accepted = true;
68  }
69 
70  Keys.onDownPressed: {
71  if (sessionsList.currentIndex < sessionsList.model.count - 1)
72  sessionsList.currentIndex++;
73  event.accepted = true;
74  }
75 
76  Keys.onUpPressed: {
77  if (sessionsList.currentIndex > 0)
78  sessionsList.currentIndex--;
79  event.accepted = true;
80  }
81 
82  LoginAreaContainer {
83  readonly property real margins: sessionsList.anchors.margins
84  readonly property real preferredHeight: {
85  if (sessionsList.currentItem) {
86  return (sessionsList.currentItem.height *
87  (1 + sessionsList.model.count)) + 2 * margins
88  } else {
89  return sessionsList.headerItem.height + 2 * margins
90  }
91  }
92 
93  height: preferredHeight < parent.height ? preferredHeight : parent.height - units.gu(4)
94  width: parent.width
95 
96  anchors {
97  left: parent.left
98  right: parent.right
99  verticalCenter: parent.verticalCenter
100  }
101 
102  UbuntuListView {
103  id: sessionsList
104 
105  anchors {
106  top: parent.top
107  left: parent.left
108  right: parent.right
109  margins: units.gu(2)
110  }
111 
112  clip: true
113  height: parent.height - units.gu(2.5)
114  boundsBehavior: Flickable.StopAtBounds
115 
116  model: LightDMService.sessions
117  header: ListItemLayout {
118  id: header
119 
120  padding.leading: 0 // handled by parent's margins
121 
122  title.color: theme.palette.normal.raisedText
123  title.font.pixelSize: units.gu(2.1)
124  title.text: i18n.tr("Select desktop environment")
125 
126  Icon {
127  id: icon
128  width: units.gu(3)
129  SlotsLayout.position: SlotsLayout.Leading
130  name: "go-previous"
131 
132  MouseArea {
133  anchors.fill: parent
134  onClicked: showLoginList()
135  }
136  }
137  }
138 
139  headerPositioning: ListView.OverlayHeader
140 
141  // The highlighting is all self-managed, so account for that
142  highlightFollowsCurrentItem: false
143  highlight: QtObject {}
144 
145  delegate: ListItem {
146  id: delegate
147  objectName: "sessionDelegate" + index
148 
149  property string initialSession: ""
150 
151  divider.visible: false
152  visible: y > sessionsList.headerItem.y
153  + sessionsList.headerItem.height
154  - sessionsList.anchors.margins
155 
156  MouseArea {
157  anchors.fill: parent
158  onClicked: {
159  sessionsList.currentIndex = index
160  sessionSelected(key)
161  showLoginList()
162  }
163  }
164 
165  Rectangle {
166  id: backgroundHighlight
167 
168  height: sessionsList.currentItem.height
169  width: sessionsList.currentItem.width
170  color: theme.palette.normal.selection
171 
172  visible: initialSession === key && !!key
173  }
174 
175  Rectangle {
176  height: parent.height
177  width: parent.width
178  color: "transparent"
179  border {
180  color: theme.palette.normal.focus
181  width: units.gu(0.2)
182  }
183 
184  visible: index === sessionsList.currentIndex
185  }
186 
187  ListItemLayout {
188  id: layout
189 
190  readonly property color itemColor: theme.palette.normal.raisedText
191  SessionIcon {
192  id: sessionIcon
193  source: icon_url
194  SlotsLayout.position: SlotsLayout.Leading
195  color: parent.itemColor
196  }
197 
198  title.text: display
199  title.color: itemColor
200  }
201  }
202  }
203  }
204 }