Lomiri
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.12
18 import Lomiri.Components 1.3
19 import Lomiri.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  LomiriListView {
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.raisedSecondaryText
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  color: theme.palette.normal.raisedSecondaryText
130  SlotsLayout.position: SlotsLayout.Leading
131  name: "go-previous"
132 
133  MouseArea {
134  anchors.fill: parent
135  onClicked: showLoginList()
136  }
137  }
138  }
139 
140  headerPositioning: ListView.OverlayHeader
141 
142  // The highlighting is all self-managed, so account for that
143  highlightFollowsCurrentItem: false
144  highlight: QtObject {}
145 
146  delegate: ListItem {
147  id: delegate
148  objectName: "sessionDelegate" + index
149 
150  property string initialSession: ""
151 
152  divider.visible: false
153  visible: y > sessionsList.headerItem.y
154  + sessionsList.headerItem.height
155  - sessionsList.anchors.margins
156 
157  MouseArea {
158  anchors.fill: parent
159  onClicked: {
160  sessionsList.currentIndex = index
161  sessionSelected(key)
162  showLoginList()
163  }
164  }
165 
166  Rectangle {
167  id: backgroundHighlight
168 
169  height: sessionsList.currentItem.height
170  width: sessionsList.currentItem.width
171  color: theme.palette.normal.selection
172 
173  visible: initialSession === key && !!key
174  }
175 
176  Rectangle {
177  height: parent.height
178  width: parent.width
179  color: "transparent"
180  border {
181  color: theme.palette.normal.focus
182  width: units.gu(0.2)
183  }
184 
185  visible: index === sessionsList.currentIndex
186  }
187 
188  ListItemLayout {
189  id: layout
190 
191  readonly property color itemColor: theme.palette.normal.raisedSecondaryText
192  SessionIcon {
193  id: sessionIcon
194  source: icon_url
195  SlotsLayout.position: SlotsLayout.Leading
196  color: parent.itemColor
197  }
198 
199  title.text: display
200  title.color: itemColor
201  }
202  }
203  }
204  }
205 }