on bcbc40d help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.h
286 lines 10 kB history raw
1/*
2 * Copyright (C) 2016 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#ifndef TOPLEVELWINDOWMODEL_H
18#define TOPLEVELWINDOWMODEL_H
20#include <QAbstractListModel>
21#include <QLoggingCategory>
23#include <memory>
25Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)
27class Window;
28class Workspace;
30namespace miral { class Workspace; }
32namespace unity {
33 namespace shell {
34 namespace application {
35 class ApplicationInfoInterface;
36 class ApplicationManagerInterface;
37 class MirSurfaceInterface;
38 class SurfaceManagerInterface;
39 }
40 }
43/**
44 * @brief A model of top-level surfaces
45 *
46 * It's an abstraction of top-level application windows.
47 *
48 * When an entry first appears, it normaly doesn't have a surface yet, meaning that the application is
49 * still starting up. A shell should then display a splash screen or saved screenshot of the application
50 * until its surface comes up.
51 *
52 * As applications can have multiple surfaces and you can also have entries without surfaces at all,
53 * the only way to unambiguously refer to an entry in this model is through its id.
54 */
55class TopLevelWindowModel : public QAbstractListModel
57 Q_OBJECT
59 /**
60 * @brief Number of top-level surfaces in this model
61 *
62 * This is the same as rowCount, added in order to keep compatibility with QML ListModels.
63 */
64 Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
66 /**
67 * @brief The input method surface, if any
68 *
69 * The surface of a onscreen keyboard (akak "virtual keyboard") would be kept here and not in the model itself.
70 */
71 Q_PROPERTY(unity::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)
73 /**
74 * @brief The currently focused window, if any
75 */
76 Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)
78 Q_PROPERTY(Workspace* workspace
79 READ workspace
80 WRITE setWorkspace
81 NOTIFY workspaceChanged)
83 Q_PROPERTY(unity::shell::application::SurfaceManagerInterface* surfaceManager
84 READ surfaceManager
85 WRITE setSurfaceManager
86 NOTIFY surfaceManagerChanged)
88 Q_PROPERTY(unity::shell::application::ApplicationManagerInterface* applicationManager
89 READ applicationManager
90 WRITE setApplicationManager
91 NOTIFY applicationManagerChanged)
93 /**
94 The id to be used on the next entry created
95 Useful for tests
96 */
97 Q_PROPERTY(int nextId READ nextId NOTIFY nextIdChanged)
99public:
100 /**
101 * @brief The Roles supported by the model
102 *
103 * WindowRole - A Window.
104 * ApplicationRole - An ApplicationInfoInterface
105 */
106 enum Roles {
107 WindowRole = Qt::UserRole,
108 ApplicationRole = Qt::UserRole + 1,
109 };
111 TopLevelWindowModel();
113 // From QAbstractItemModel
114 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
115 QVariant data(const QModelIndex& index, int role) const override;
116 QHash<int, QByteArray> roleNames() const override {
117 QHash<int, QByteArray> roleNames { {WindowRole, "window"},
118 {ApplicationRole, "application"} };
119 return roleNames;
120 }
122 // Own API
124 Workspace* workspace() const { return m_workspace; }
125 void setWorkspace(Workspace* workspace);
127 unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
128 Window* focusedWindow() const;
130 unity::shell::application::ApplicationManagerInterface *applicationManager() const { return m_applicationManager; }
131 void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
133 unity::shell::application::SurfaceManagerInterface *surfaceManager() const { return m_surfaceManager; }
134 void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
136 int nextId() const { return m_nextId; }
138public:
139 /**
140 * @brief Returns the surface at the given index
141 *
142 * It will be a nullptr if the application is still starting up and thus hasn't yet created
143 * and drawn into a surface.
144 *
145 * Same as windowAt(index).surface()
146 */
147 Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
149 /**
150 * @brief Returns the window at the given index
151 *
152 * Will always be valid
153 */
154 Q_INVOKABLE Window *windowAt(int index) const;
156 /**
157 * @brief Returns the application at the given index
158 */
159 Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
161 /**
162 * @brief Returns the unique id of the element at the given index
163 */
164 Q_INVOKABLE int idAt(int index) const;
166 /**
167 * @brief Returns the index where the row with the given id is located
168 *
169 * Returns -1 if there's no row with the given id.
170 */
171 Q_INVOKABLE int indexForId(int id) const;
173 /**
174 * @brief Raises the row with the given id to the top of the window stack (index == count-1)
175 */
176 Q_INVOKABLE void raiseId(int id);
178Q_SIGNALS:
179 void countChanged();
180 void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
181 void focusedWindowChanged(Window *focusedWindow);
182 void applicationManagerChanged(unity::shell::application::ApplicationManagerInterface*);
183 void surfaceManagerChanged(unity::shell::application::SurfaceManagerInterface*);
185 /**
186 * @brief Emitted when the list changes
187 *
188 * Emitted when model gains an element, loses an element or when elements exchange positions.
189 */
190 void listChanged();
192 void nextIdChanged();
194 void workspaceChanged(Workspace*);
196private Q_SLOTS:
197 void onSurfaceCreated(unity::shell::application::MirSurfaceInterface *surface);
198 void onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
199 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces);
200 void onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
201 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces);
202 void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
204 void onModificationsStarted();
205 void onModificationsEnded();
207private:
208 void doRaiseId(int id);
209 int generateId();
210 int nextFreeId(int candidateId, const int latestId);
211 int nextId(int id) const;
212 QString toString();
213 int indexOf(unity::shell::application::MirSurfaceInterface *surface);
215 void setInputMethodWindow(Window *window);
216 void setFocusedWindow(Window *window);
217 void removeInputMethodWindow();
218 int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
219 void removeAt(int index);
221 void addApplication(unity::shell::application::ApplicationInfoInterface *application);
222 void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
224 void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
225 void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
226 unity::shell::application::ApplicationInfoInterface *application);
227 void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
228 unity::shell::application::ApplicationInfoInterface *application);
230 void connectWindow(Window *window);
231 void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
233 void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
234 void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
236 void move(int from, int to);
238 void activateEmptyWindow(Window *window);
240 void activateTopMostWindowWithoutId(int forbiddenId);
241 void refreshWindows();
242 void clear();
244 Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
246 struct ModelEntry {
247 ModelEntry() {}
248 ModelEntry(Window *window,
249 unity::shell::application::ApplicationInfoInterface *application)
250 : window(window), application(application) {}
251 Window *window{nullptr};
252 unity::shell::application::ApplicationInfoInterface *application{nullptr};
253 bool removeOnceSurfaceDestroyed{false};
254 };
256 QVector<ModelEntry> m_windowModel;
257 Window* m_inputMethodWindow{nullptr};
258 Window* m_focusedWindow{nullptr};
259 Workspace* m_workspace{nullptr};
260 // track all the surfaces we've been told about.
261 QSet<unity::shell::application::MirSurfaceInterface*> m_allSurfaces;
263 int m_nextId{1};
264 // Just something big enough that we don't risk running out of unused id numbers.
265 // Not sure if QML int type supports something close to std::numeric_limits<int>::max() and
266 // there's no reason to try out its limits.
267 static const int m_maxId{1000000};
269 unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
270 unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
272 enum ModelState {
273 IdleState,
274 InsertingState,
275 RemovingState,
276 MovingState,
277 ResettingState
278 };
279 ModelState m_modelState{IdleState};
281 // Valid between modificationsStarted and modificationsEnded
282 bool m_focusedWindowChanged{false};
283 Window *m_newlyFocusedWindow{nullptr};
284};
286#endif // TOPLEVELWINDOWMODEL_H