on c2ffdb9 help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.h
310 lines 11 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)
92 /**
93 * @brief Sets whether a user Window or "nothing" should be focused
94 *
95 * This implementation of TLWM must have something focused. However, the
96 * user may wish to have nothing in some cases - for example, when they
97 * minimize all their windows on the desktop or unfocus the app they're
98 * using by clicking the background or indicators.
99 *
100 * Unsetting rootFocus effectively focuses "nothing" by setting up a Window
101 * that has no displayable Surfaces and bringing it into focus.
102 *
103 * Setting rootFocus attempts to focus the Window which was focused last -
104 * unless another app is attempting to gain focus (as determined by
105 * pendingActivation) and that's why we got rootFocus.
106 *
107 * If the previously-focused Window was closed before rootFocus was set,
108 * the next available window will be focused.
109 */
110 Q_PROPERTY(bool rootFocus READ rootFocus WRITE setRootFocus NOTIFY rootFocusChanged)
112public:
113 /**
114 * @brief The Roles supported by the model
115 *
116 * WindowRole - A Window.
117 * ApplicationRole - An ApplicationInfoInterface
118 */
119 enum Roles {
120 WindowRole = Qt::UserRole,
121 ApplicationRole = Qt::UserRole + 1,
122 };
124 TopLevelWindowModel();
126 // From QAbstractItemModel
127 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
128 QVariant data(const QModelIndex& index, int role) const override;
129 QHash<int, QByteArray> roleNames() const override {
130 QHash<int, QByteArray> roleNames { {WindowRole, "window"},
131 {ApplicationRole, "application"} };
132 return roleNames;
133 }
135 // Own API
137 unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
138 Window* focusedWindow() const;
140 unity::shell::application::ApplicationManagerInterface *applicationManager() const { return m_applicationManager; }
141 void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
143 unity::shell::application::SurfaceManagerInterface *surfaceManager() const { return m_surfaceManager; }
144 void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
146 int nextId() const { return m_nextId; }
148public:
149 /**
150 * @brief Returns the surface at the given index
151 *
152 * It will be a nullptr if the application is still starting up and thus hasn't yet created
153 * and drawn into a surface.
154 *
155 * Same as windowAt(index).surface()
156 */
157 Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
159 /**
160 * @brief Returns the window at the given index
161 *
162 * Will always be valid
163 */
164 Q_INVOKABLE Window *windowAt(int index) const;
166 /**
167 * @brief Returns the application at the given index
168 */
169 Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
171 /**
172 * @brief Returns the unique id of the element at the given index
173 */
174 Q_INVOKABLE int idAt(int index) const;
176 /**
177 * @brief Returns the index where the row with the given id is located
178 *
179 * Returns -1 if there's no row with the given id.
180 */
181 Q_INVOKABLE int indexForId(int id) const;
183 /**
184 * @brief Raises the row with the given id to the top of the window stack (index == count-1)
185 */
186 Q_INVOKABLE void raiseId(int id);
188 /**
189 * @brief Closes all windows, emits closedAllWindows when done
190 */
191 Q_INVOKABLE void closeAllWindows();
193 /**
194 * @brief Sets pending activation flag
195 */
196 Q_INVOKABLE void pendingActivation();
198 void setRootFocus(bool focus);
199 bool rootFocus();
201Q_SIGNALS:
202 void countChanged();
203 void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
204 void focusedWindowChanged(Window *focusedWindow);
205 void applicationManagerChanged(unity::shell::application::ApplicationManagerInterface*);
206 void surfaceManagerChanged(unity::shell::application::SurfaceManagerInterface*);
208 /**
209 * @brief Emitted when the list changes
210 *
211 * Emitted when model gains an element, loses an element or when elements exchange positions.
212 */
213 void listChanged();
215 void nextIdChanged();
217 void closedAllWindows();
219 void rootFocusChanged();
221private Q_SLOTS:
222 void onSurfaceCreated(unity::shell::application::MirSurfaceInterface *surface);
223 void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
225 void onModificationsStarted();
226 void onModificationsEnded();
228private:
229 void doRaiseId(int id);
230 int generateId();
231 int nextFreeId(int candidateId, const int latestId);
232 int nextId(int id) const;
233 QString toString();
234 int indexOf(unity::shell::application::MirSurfaceInterface *surface);
236 void setInputMethodWindow(Window *window);
237 void setFocusedWindow(Window *window);
238 void removeInputMethodWindow();
239 int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
240 void deleteAt(int index);
241 void removeAt(int index);
243 void addApplication(unity::shell::application::ApplicationInfoInterface *application);
244 void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
246 void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
247 void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
248 unity::shell::application::ApplicationInfoInterface *application);
249 void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
250 unity::shell::application::ApplicationInfoInterface *application);
251 void prependWindow(Window *window, unity::shell::application::ApplicationInfoInterface *application);
253 void connectWindow(Window *window);
254 void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
256 void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
257 void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
259 void move(int from, int to);
261 void activateEmptyWindow(Window *window);
263 void activateTopMostWindowWithoutId(int forbiddenId);
265 Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
267 struct ModelEntry {
268 ModelEntry() {}
269 ModelEntry(Window *window,
270 unity::shell::application::ApplicationInfoInterface *application)
271 : window(window), application(application) {}
272 Window *window{nullptr};
273 unity::shell::application::ApplicationInfoInterface *application{nullptr};
274 bool removeOnceSurfaceDestroyed{false};
275 };
277 QVector<ModelEntry> m_windowModel;
278 Window* m_inputMethodWindow{nullptr};
279 Window* m_focusedWindow{nullptr};
280 Window* m_nullWindow;
281 Window* m_previousWindow{nullptr};
282 bool m_pendingActivation;
284 int m_nextId{1};
285 // Just something big enough that we don't risk running out of unused id numbers.
286 // Not sure if QML int type supports something close to std::numeric_limits<int>::max() and
287 // there's no reason to try out its limits.
288 static const int m_maxId{1000000};
290 unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
291 unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
292 bool m_surfaceManagerBusy;
294 enum ModelState {
295 IdleState,
296 InsertingState,
297 RemovingState,
298 MovingState,
299 ResettingState
300 };
301 ModelState m_modelState{IdleState};
303 // Valid between modificationsStarted and modificationsEnded
304 bool m_focusedWindowChanged{false};
305 Window *m_newlyFocusedWindow{nullptr};
307 bool m_closingAllApps{false};
308};
310#endif // TOPLEVELWINDOWMODEL_H