on 329a385 help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.h
287 lines 10 kB history raw
1/*
2 * Copyright (C) 2016-2017 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 "WindowManagerGlobal.h"
25Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)
27class Window;
29namespace unity {
30 namespace shell {
31 namespace application {
32 class ApplicationInfoInterface;
33 class ApplicationManagerInterface;
34 class MirSurfaceInterface;
35 class SurfaceManagerInterface;
36 }
37 }
40/**
41 * @brief A model of top-level surfaces
42 *
43 * It's an abstraction of top-level application windows.
44 *
45 * When an entry first appears, it normaly doesn't have a surface yet, meaning that the application is
46 * still starting up. A shell should then display a splash screen or saved screenshot of the application
47 * until its surface comes up.
48 *
49 * As applications can have multiple surfaces and you can also have entries without surfaces at all,
50 * the only way to unambiguously refer to an entry in this model is through its id.
51 */
52class WINDOWMANAGERQML_EXPORT TopLevelWindowModel : public QAbstractListModel
54 Q_OBJECT
56 /**
57 * @brief Number of top-level surfaces in this model
58 *
59 * This is the same as rowCount, added in order to keep compatibility with QML ListModels.
60 */
61 Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
63 /**
64 * @brief The input method surface, if any
65 *
66 * The surface of a onscreen keyboard (akak "virtual keyboard") would be kept here and not in the model itself.
67 */
68 Q_PROPERTY(unity::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)
70 /**
71 * @brief The currently focused window, if any
72 */
73 Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)
75 Q_PROPERTY(unity::shell::application::SurfaceManagerInterface* surfaceManager
76 READ surfaceManager
77 WRITE setSurfaceManager
78 NOTIFY surfaceManagerChanged)
80 Q_PROPERTY(unity::shell::application::ApplicationManagerInterface* applicationManager
81 READ applicationManager
82 WRITE setApplicationManager
83 NOTIFY applicationManagerChanged)
85 /**
86 The id to be used on the next entry created
87 Useful for tests
88 */
89 Q_PROPERTY(int nextId READ nextId NOTIFY nextIdChanged)
91public:
92 /**
93 * @brief The Roles supported by the model
94 *
95 * WindowRole - A Window.
96 * ApplicationRole - An ApplicationInfoInterface
97 */
98 enum Roles {
99 WindowRole = Qt::UserRole,
100 ApplicationRole = Qt::UserRole + 1,
101 };
103 TopLevelWindowModel();
105 // From QAbstractItemModel
106 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
107 QVariant data(const QModelIndex& index, int role) const override;
108 QHash<int, QByteArray> roleNames() const override {
109 QHash<int, QByteArray> roleNames { {WindowRole, "window"},
110 {ApplicationRole, "application"} };
111 return roleNames;
112 }
114 // Own API
116 unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
117 Window* focusedWindow() const;
119 unity::shell::application::ApplicationManagerInterface *applicationManager() const { return m_applicationManager; }
120 void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
122 unity::shell::application::SurfaceManagerInterface *surfaceManager() const { return m_surfaceManager; }
123 void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
125 int nextId() const { return m_nextId; }
127public:
128 /**
129 * @brief Returns the surface at the given index
130 *
131 * It will be a nullptr if the application is still starting up and thus hasn't yet created
132 * and drawn into a surface.
133 *
134 * Same as windowAt(index).surface()
135 */
136 Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
138 /**
139 * @brief Returns the window at the given index
140 *
141 * Will always be valid
142 */
143 Q_INVOKABLE Window *windowAt(int index) const;
145 /**
146 * @brief Returns the application at the given index
147 */
148 Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
150 /**
151 * @brief Returns the unique id of the element at the given index
152 */
153 Q_INVOKABLE int idAt(int index) const;
155 /**
156 * @brief Returns the index where the row with the given id is located
157 *
158 * Returns -1 if there's no row with the given id.
159 */
160 Q_INVOKABLE int indexForId(int id) const;
162 /**
163 * @brief Raises the row with the given id to the top of the window stack (index == count-1)
164 */
165 Q_INVOKABLE void raiseId(int id);
167 /**
168 * @brief Raises and focuses a window with no surface
169 */
170 Q_INVOKABLE void rootFocus(bool focus);
172 /**
173 * @brief Closes all windows, emits closedAllWindows when done
174 */
175 Q_INVOKABLE void closeAllWindows();
177 /**
178 * @brief Sets pending activation flag
179 */
180 Q_INVOKABLE void pendingActivation();
181Q_SIGNALS:
182 void countChanged();
183 void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
184 void focusedWindowChanged(Window *focusedWindow);
185 void applicationManagerChanged(unity::shell::application::ApplicationManagerInterface*);
186 void surfaceManagerChanged(unity::shell::application::SurfaceManagerInterface*);
188 /**
189 * @brief Emitted when the list changes
190 *
191 * Emitted when model gains an element, loses an element or when elements exchange positions.
192 */
193 void listChanged();
195 void nextIdChanged();
197 void closedAllWindows();
199private Q_SLOTS:
200 void onSurfaceCreated(unity::shell::application::MirSurfaceInterface *surface);
201 void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
203 void onModificationsStarted();
204 void onModificationsEnded();
206private:
207 void doRaiseId(int id);
208 int generateId();
209 int nextFreeId(int candidateId, const int latestId);
210 int nextId(int id) const;
211 QString toString();
212 int indexOf(unity::shell::application::MirSurfaceInterface *surface);
214 void setInputMethodWindow(Window *window);
215 void setFocusedWindow(Window *window);
216 void removeInputMethodWindow();
217 int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
218 void deleteAt(int index);
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);
229 void prependWindow(Window *window, unity::shell::application::ApplicationInfoInterface *application);
231 void connectWindow(Window *window);
232 void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
234 void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
235 void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
237 void move(int from, int to);
239 void activateEmptyWindow(Window *window);
241 void activateTopMostWindowWithoutId(int forbiddenId);
243 Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
245 struct ModelEntry {
246 ModelEntry() {}
247 ModelEntry(Window *window,
248 unity::shell::application::ApplicationInfoInterface *application)
249 : window(window), application(application) {}
250 Window *window{nullptr};
251 unity::shell::application::ApplicationInfoInterface *application{nullptr};
252 bool removeOnceSurfaceDestroyed{false};
253 };
255 QVector<ModelEntry> m_windowModel;
256 Window* m_inputMethodWindow{nullptr};
257 Window* m_focusedWindow{nullptr};
258 Window* m_nullWindow;
259 Window* m_previousWindow{nullptr};
260 bool m_pendingActivation;
262 int m_nextId{1};
263 // Just something big enough that we don't risk running out of unused id numbers.
264 // Not sure if QML int type supports something close to std::numeric_limits<int>::max() and
265 // there's no reason to try out its limits.
266 static const int m_maxId{1000000};
268 unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
269 unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
271 enum ModelState {
272 IdleState,
273 InsertingState,
274 RemovingState,
275 MovingState,
276 ResettingState
277 };
278 ModelState m_modelState{IdleState};
280 // Valid between modificationsStarted and modificationsEnded
281 bool m_focusedWindowChanged{false};
282 Window *m_newlyFocusedWindow{nullptr};
284 bool m_closingAllApps{false};
285};
287#endif // TOPLEVELWINDOWMODEL_H