on 5778a8e help sign in
unity8/plugins/WindowManager/TopLevelSurfaceList.h
223 lines 7.7 kB history raw
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 */
17#ifndef TOPLEVELSURFACELIST_H
18#define TOPLEVELSURFACELIST_H
20#include <QAbstractListModel>
21#include <QList>
22#include <QLoggingCategory>
24Q_DECLARE_LOGGING_CATEGORY(UNITY_TOPSURFACELIST)
26namespace unity {
27 namespace shell {
28 namespace application {
29 class ApplicationInfoInterface;
30 class MirSurfaceInterface;
31 }
32 }
35/**
36 * @brief A model of top-level surfaces
37 *
38 * It's an abstraction of top-level application windows.
39 *
40 * When an entry first appears, it normaly doesn't have a surface yet, meaning that the application is
41 * still starting up. A shell should then display a splash screen or saved screenshot of the application
42 * until its surface comes up.
43 *
44 * As applications can have multiple surfaces and you can also have entries without surfaces at all,
45 * the only way to unambiguously refer to an entry in this model is through its id.
46 */
47class TopLevelSurfaceList : public QAbstractListModel
50 Q_OBJECT
52 /**
53 * @brief A list model of applications.
54 *
55 * It's expected to have a role called "application" which returns a ApplicationInfoInterface
56 */
57 Q_PROPERTY(QAbstractListModel* applicationsModel READ applicationsModel
58 WRITE setApplicationsModel
59 NOTIFY applicationsModelChanged)
61 /**
62 * @brief Number of top-level surfaces in this model
63 *
64 * This is the same as rowCount, added in order to keep compatibility with QML ListModels.
65 */
66 Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
68 /**
69 The id to be used on the next entry created
70 Useful for tests
71 */
72 Q_PROPERTY(int nextId READ nextId NOTIFY nextIdChanged)
73public:
75 /**
76 * @brief The Roles supported by the model
77 *
78 * SurfaceRole - A MirSurfaceInterface. It will be null if the application is still starting up
79 * ApplicationRole - An ApplicationInfoInterface
80 * IdRole - A unique identifier for this entry. Useful to unambiguosly track elements as they move around in the list
81 */
82 enum Roles {
83 SurfaceRole = Qt::UserRole,
84 ApplicationRole = Qt::UserRole + 1,
85 IdRole = Qt::UserRole + 2,
86 };
88 explicit TopLevelSurfaceList(QObject *parent = nullptr);
89 virtual ~TopLevelSurfaceList();
91 // QAbstractItemModel methods
92 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
93 QVariant data(const QModelIndex& index, int role) const override;
94 QHash<int, QByteArray> roleNames() const override {
95 QHash<int, QByteArray> roleNames { {SurfaceRole, "surface"},
96 {ApplicationRole, "application"},
97 {IdRole, "id"} };
98 return roleNames;
99 }
101 int nextId() const { return m_nextId; }
103 QAbstractListModel *applicationsModel() const;
104 void setApplicationsModel(QAbstractListModel*);
106public Q_SLOTS:
107 /**
108 * @brief Returns the surface at the given index
109 *
110 * It will be a nullptr if the application is still starting up and thus hasn't yet created
111 * and drawn into a surface.
112 */
113 unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
115 /**
116 * @brief Returns the application at the given index
117 */
118 unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
120 /**
121 * @brief Returns the unique id of the element at the given index
122 */
123 int idAt(int index) const;
125 /**
126 * @brief Returns the index where the row with the given id is located
127 *
128 * Returns -1 if there's no row with the given id.
129 */
130 int indexForId(int id) const;
132 /**
133 * @brief Raises the row with the given id to index 0
134 */
135 void raiseId(int id);
137 void doRaiseId(int id);
139Q_SIGNALS:
140 void countChanged();
142 /**
143 * @brief Emitted when the list changes
144 *
145 * Emitted when model gains an element, loses an element or when elements exchange positions.
146 */
147 void listChanged();
149 void nextIdChanged();
151 void applicationsModelChanged();
153private:
154 void addApplication(unity::shell::application::ApplicationInfoInterface *application);
155 void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
157 int indexOf(unity::shell::application::MirSurfaceInterface *surface);
158 void raise(unity::shell::application::MirSurfaceInterface *surface);
159 void move(int from, int to);
160 void appendSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
161 unity::shell::application::ApplicationInfoInterface *application);
162 void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
163 int generateId();
164 int nextFreeId(int candidateId);
165 QString toString();
166 void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
167 void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
168 void removeAt(int index);
169 void findApplicationRole();
171 unity::shell::application::ApplicationInfoInterface *getApplicationFromModelAt(int index);
173 /*
174 Placeholder for a future surface from a starting or running application.
175 Enables shell to give immediate feedback to the user by showing, eg,
176 a splash screen.
178 It's a model row containing a null surface and the given application.
179 */
180 void appendPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
182 /*
183 Adds a model row with the given surface and application
185 Alternatively, if a placeholder exists for the given application it's
186 filled with the given surface instead.
187 */
188 void appendSurface(unity::shell::application::MirSurfaceInterface *surface,
189 unity::shell::application::ApplicationInfoInterface *application);
191 struct ModelEntry {
192 ModelEntry(unity::shell::application::MirSurfaceInterface *surface, unity::shell::application::ApplicationInfoInterface *application, int id)
193 : surface(surface), application(application), id(id) {}
194 unity::shell::application::MirSurfaceInterface *surface;
195 unity::shell::application::ApplicationInfoInterface *application;
196 int id;
197 bool removeOnceSurfaceDestroyed{false};
198 };
200 QList<ModelEntry> m_surfaceList;
201 int m_nextId{1};
202 static const int m_maxId{1000000};
204 // applications that are being monitored
205 QList<unity::shell::application::ApplicationInfoInterface *> m_applications;
207 QAbstractListModel* m_applicationsModel{nullptr};
208 int m_applicationRole{-1};
210 enum ModelState {
211 IdleState,
212 InsertingState,
213 RemovingState,
214 MovingState,
215 ResettingState
216 };
217 ModelState m_modelState{IdleState};
218};
220Q_DECLARE_METATYPE(TopLevelSurfaceList*)
221Q_DECLARE_METATYPE(QAbstractListModel*)
223#endif // TOPLEVELSURFACELIST_H