on f85133b help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.h
326 lines 12 kB history raw
1/*
2 * Copyright (C) 2016-2017 Canonical, Ltd.
3 * Copyright 2019 UBports Foundation
4 *
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 3, as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
11 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18#ifndef TOPLEVELWINDOWMODEL_H
19#define TOPLEVELWINDOWMODEL_H
21#include <QAbstractListModel>
22#include <QAtomicInteger>
23#include <QLoggingCategory>
25#include "WindowManagerGlobal.h"
27Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)
29class Window;
31namespace unity {
32 namespace shell {
33 namespace application {
34 class ApplicationInfoInterface;
35 class ApplicationManagerInterface;
36 class MirSurfaceInterface;
37 class SurfaceManagerInterface;
38 }
39 }
42/**
43 * @brief A model of top-level surfaces
44 *
45 * It's an abstraction of top-level application windows.
46 *
47 * When an entry first appears, it normaly doesn't have a surface yet, meaning that the application is
48 * still starting up. A shell should then display a splash screen or saved screenshot of the application
49 * until its surface comes up.
50 *
51 * As applications can have multiple surfaces and you can also have entries without surfaces at all,
52 * the only way to unambiguously refer to an entry in this model is through its id.
53 */
54class WINDOWMANAGERQML_EXPORT TopLevelWindowModel : public QAbstractListModel
56 Q_OBJECT
58 /**
59 * @brief Number of top-level surfaces in this model
60 *
61 * This is the same as rowCount, added in order to keep compatibility with QML ListModels.
62 */
63 Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
65 /**
66 * @brief The input method surface, if any
67 *
68 * The surface of a onscreen keyboard (akak "virtual keyboard") would be kept here and not in the model itself.
69 */
70 Q_PROPERTY(unity::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)
72 /**
73 * @brief The currently focused window, if any
74 */
75 Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)
77 Q_PROPERTY(unity::shell::application::SurfaceManagerInterface* surfaceManager
78 READ surfaceManager
79 WRITE setSurfaceManager
80 NOTIFY surfaceManagerChanged)
82 Q_PROPERTY(unity::shell::application::ApplicationManagerInterface* applicationManager
83 READ applicationManager
84 WRITE setApplicationManager
85 NOTIFY applicationManagerChanged)
87 /**
88 The id to be used on the next entry created
89 Useful for tests
90 */
91 Q_PROPERTY(int nextId READ nextId)
93 /**
94 The maximum window ID that may be issued by the model.
95 Will only leave the model in a consistent state if changed before
96 any windows are added.
97 Don't change this unless you are the tests or want to break everything.
98 */
99 Q_PROPERTY(int maxWindowId READ getMaxWindowId WRITE setMaxWindowId)
102 /**
103 * @brief Sets whether a user Window or "nothing" should be focused
104 *
105 * This implementation of TLWM must have something focused. However, the
106 * user may wish to have nothing in some cases - for example, when they
107 * minimize all their windows on the desktop or unfocus the app they're
108 * using by clicking the background or indicators.
109 *
110 * Unsetting rootFocus effectively focuses "nothing" by setting up a Window
111 * that has no displayable Surfaces and bringing it into focus.
112 *
113 * Setting rootFocus attempts to focus the Window which was focused last -
114 * unless another app is attempting to gain focus (as determined by
115 * pendingActivation) and that's why we got rootFocus.
116 *
117 * If the previously-focused Window was closed before rootFocus was set,
118 * the next available window will be focused.
119 */
120 Q_PROPERTY(bool rootFocus READ rootFocus WRITE setRootFocus NOTIFY rootFocusChanged)
122public:
123 /**
124 * @brief The Roles supported by the model
125 *
126 * WindowRole - A Window.
127 * ApplicationRole - An ApplicationInfoInterface
128 */
129 enum Roles {
130 WindowRole = Qt::UserRole,
131 ApplicationRole = Qt::UserRole + 1,
132 };
134 TopLevelWindowModel();
136 // From QAbstractItemModel
137 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
138 QVariant data(const QModelIndex& index, int role) const override;
139 QHash<int, QByteArray> roleNames() const override {
140 QHash<int, QByteArray> roleNames { {WindowRole, "window"},
141 {ApplicationRole, "application"} };
142 return roleNames;
143 }
145 // Own API
147 unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
148 Window* focusedWindow() const;
150 unity::shell::application::ApplicationManagerInterface *applicationManager() const { return m_applicationManager; }
151 void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
153 unity::shell::application::SurfaceManagerInterface *surfaceManager() const { return m_surfaceManager; }
154 void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
156 int nextId() const { return m_nextId.load(); }
158public:
159 /**
160 * @brief Returns the surface at the given index
161 *
162 * It will be a nullptr if the application is still starting up and thus hasn't yet created
163 * and drawn into a surface.
164 *
165 * Same as windowAt(index).surface()
166 */
167 Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
169 /**
170 * @brief Returns the window at the given index
171 *
172 * Will always be valid
173 */
174 Q_INVOKABLE Window *windowAt(int index) const;
176 /**
177 * @brief Returns the application at the given index
178 */
179 Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
181 /**
182 * @brief Returns the unique id of the element at the given index
183 */
184 Q_INVOKABLE int idAt(int index) const;
186 /**
187 * @brief Returns the index where the row with the given id is located
188 *
189 * Returns -1 if there's no row with the given id.
190 */
191 Q_INVOKABLE int indexForId(int id) const;
193 /**
194 * @brief Raises the row with the given id to the top of the window stack (index == count-1)
195 */
196 Q_INVOKABLE void raiseId(int id);
198 /**
199 * @brief Closes all windows, emits closedAllWindows when done
200 */
201 Q_INVOKABLE void closeAllWindows();
203 /**
204 * @brief Sets pending activation flag
205 */
206 Q_INVOKABLE void pendingActivation();
208 void setRootFocus(bool focus);
209 bool rootFocus();
211 void setMaxWindowId(int newMaxWindowId);
212 int getMaxWindowId();
214Q_SIGNALS:
215 void countChanged();
216 void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
217 void focusedWindowChanged(Window *focusedWindow);
218 void applicationManagerChanged(unity::shell::application::ApplicationManagerInterface*);
219 void surfaceManagerChanged(unity::shell::application::SurfaceManagerInterface*);
221 /**
222 * @brief Emitted when the list changes
223 *
224 * Emitted when model gains an element, loses an element or when elements exchange positions.
225 */
226 void listChanged();
228 void closedAllWindows();
230 void rootFocusChanged();
232 void maxWindowIdChanged();
234private Q_SLOTS:
235 void onSurfaceCreated(unity::shell::application::MirSurfaceInterface *surface);
236 void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
238 void onModificationsStarted();
239 void onModificationsEnded();
241private:
242 void doRaiseId(int id);
243 int generateId();
244 int nextFreeId(int candidateId, const int latestId);
245 int nextId(int id) const;
246 QString toString();
247 int indexOf(unity::shell::application::MirSurfaceInterface *surface);
249 void setInputMethodWindow(Window *window);
250 void setFocusedWindow(Window *window);
251 void removeInputMethodWindow();
252 int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
253 void deleteAt(int index);
254 void removeAt(int index);
256 void addApplication(unity::shell::application::ApplicationInfoInterface *application);
257 void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
259 void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
260 void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
261 unity::shell::application::ApplicationInfoInterface *application);
262 void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
263 unity::shell::application::ApplicationInfoInterface *application);
264 void prependWindow(Window *window, unity::shell::application::ApplicationInfoInterface *application);
266 void connectWindow(Window *window);
267 void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
269 void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
270 void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
272 void move(int from, int to);
274 void activateEmptyWindow(Window *window);
276 void activateTopMostWindowWithoutId(int forbiddenId);
278 Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
279 Window *createWindowWithId(unity::shell::application::MirSurfaceInterface *surface, int id);
280 Window *createNullWindow();
282 struct ModelEntry {
283 ModelEntry() {}
284 ModelEntry(Window *window,
285 unity::shell::application::ApplicationInfoInterface *application)
286 : window(window), application(application) {}
287 Window *window{nullptr};
288 unity::shell::application::ApplicationInfoInterface *application{nullptr};
289 bool removeOnceSurfaceDestroyed{false};
290 };
292 QVector<ModelEntry> m_windowModel;
293 Window* m_inputMethodWindow{nullptr};
294 Window* m_focusedWindow{nullptr};
295 Window* m_nullWindow;
296 Window* m_previousWindow{nullptr};
297 bool m_pendingActivation;
299 QAtomicInteger<int> m_nextId{1};
301 // Just something big enough that we don't risk running out of unused id numbers.
302 // Not sure if QML int type supports something close to std::numeric_limits<int>::max() and
303 // there's no reason to try out its limits.
304 int m_maxWindowId;
306 unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
307 unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
308 bool m_surfaceManagerBusy;
310 enum ModelState {
311 IdleState,
312 InsertingState,
313 RemovingState,
314 MovingState,
315 ResettingState
316 };
317 ModelState m_modelState{IdleState};
319 // Valid between modificationsStarted and modificationsEnded
320 bool m_focusedWindowChanged{false};
321 Window *m_newlyFocusedWindow{nullptr};
323 bool m_closingAllApps{false};
324};
326#endif // TOPLEVELWINDOWMODEL_H