on edcd710 help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.h
306 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 Q_PROPERTY(bool rootFocus READ rootFocus WRITE setRootFocus NOTIFY rootFocusChanged)
109public:
110 /**
111 * @brief The Roles supported by the model
112 *
113 * WindowRole - A Window.
114 * ApplicationRole - An ApplicationInfoInterface
115 */
116 enum Roles {
117 WindowRole = Qt::UserRole,
118 ApplicationRole = Qt::UserRole + 1,
119 };
121 TopLevelWindowModel();
123 // From QAbstractItemModel
124 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
125 QVariant data(const QModelIndex& index, int role) const override;
126 QHash<int, QByteArray> roleNames() const override {
127 QHash<int, QByteArray> roleNames { {WindowRole, "window"},
128 {ApplicationRole, "application"} };
129 return roleNames;
130 }
132 // Own API
134 unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
135 Window* focusedWindow() const;
137 unity::shell::application::ApplicationManagerInterface *applicationManager() const { return m_applicationManager; }
138 void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
140 unity::shell::application::SurfaceManagerInterface *surfaceManager() const { return m_surfaceManager; }
141 void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
143 int nextId() const { return m_nextId; }
145public:
146 /**
147 * @brief Returns the surface at the given index
148 *
149 * It will be a nullptr if the application is still starting up and thus hasn't yet created
150 * and drawn into a surface.
151 *
152 * Same as windowAt(index).surface()
153 */
154 Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
156 /**
157 * @brief Returns the window at the given index
158 *
159 * Will always be valid
160 */
161 Q_INVOKABLE Window *windowAt(int index) const;
163 /**
164 * @brief Returns the application at the given index
165 */
166 Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
168 /**
169 * @brief Returns the unique id of the element at the given index
170 */
171 Q_INVOKABLE int idAt(int index) const;
173 /**
174 * @brief Returns the index where the row with the given id is located
175 *
176 * Returns -1 if there's no row with the given id.
177 */
178 Q_INVOKABLE int indexForId(int id) const;
180 /**
181 * @brief Raises the row with the given id to the top of the window stack (index == count-1)
182 */
183 Q_INVOKABLE void raiseId(int id);
185 /**
186 * @brief Closes all windows, emits closedAllWindows when done
187 */
188 Q_INVOKABLE void closeAllWindows();
190 /**
191 * @brief Sets pending activation flag
192 */
193 Q_INVOKABLE void pendingActivation();
195 void setRootFocus(bool focus);
196 bool rootFocus();
198Q_SIGNALS:
199 void countChanged();
200 void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
201 void focusedWindowChanged(Window *focusedWindow);
202 void applicationManagerChanged(unity::shell::application::ApplicationManagerInterface*);
203 void surfaceManagerChanged(unity::shell::application::SurfaceManagerInterface*);
205 /**
206 * @brief Emitted when the list changes
207 *
208 * Emitted when model gains an element, loses an element or when elements exchange positions.
209 */
210 void listChanged();
212 void nextIdChanged();
214 void closedAllWindows();
216 void rootFocusChanged();
218private Q_SLOTS:
219 void onSurfaceCreated(unity::shell::application::MirSurfaceInterface *surface);
220 void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
222 void onModificationsStarted();
223 void onModificationsEnded();
225private:
226 void doRaiseId(int id);
227 int generateId();
228 int nextFreeId(int candidateId, const int latestId);
229 int nextId(int id) const;
230 QString toString();
231 int indexOf(unity::shell::application::MirSurfaceInterface *surface);
233 void setInputMethodWindow(Window *window);
234 void setFocusedWindow(Window *window);
235 void removeInputMethodWindow();
236 int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
237 void deleteAt(int index);
238 void removeAt(int index);
240 void addApplication(unity::shell::application::ApplicationInfoInterface *application);
241 void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
243 void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
244 void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
245 unity::shell::application::ApplicationInfoInterface *application);
246 void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
247 unity::shell::application::ApplicationInfoInterface *application);
248 void prependWindow(Window *window, unity::shell::application::ApplicationInfoInterface *application);
250 void connectWindow(Window *window);
251 void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
253 void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
254 void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
256 void move(int from, int to);
258 void activateEmptyWindow(Window *window);
260 void activateTopMostWindowWithoutId(int forbiddenId);
262 Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
264 struct ModelEntry {
265 ModelEntry() {}
266 ModelEntry(Window *window,
267 unity::shell::application::ApplicationInfoInterface *application)
268 : window(window), application(application) {}
269 Window *window{nullptr};
270 unity::shell::application::ApplicationInfoInterface *application{nullptr};
271 bool removeOnceSurfaceDestroyed{false};
272 };
274 QVector<ModelEntry> m_windowModel;
275 Window* m_inputMethodWindow{nullptr};
276 Window* m_focusedWindow{nullptr};
277 Window* m_nullWindow;
278 Window* m_previousWindow{nullptr};
279 bool m_pendingActivation;
281 int m_nextId{1};
282 // Just something big enough that we don't risk running out of unused id numbers.
283 // Not sure if QML int type supports something close to std::numeric_limits<int>::max() and
284 // there's no reason to try out its limits.
285 static const int m_maxId{1000000};
287 unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
288 unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
290 enum ModelState {
291 IdleState,
292 InsertingState,
293 RemovingState,
294 MovingState,
295 ResettingState
296 };
297 ModelState m_modelState{IdleState};
299 // Valid between modificationsStarted and modificationsEnded
300 bool m_focusedWindowChanged{false};
301 Window *m_newlyFocusedWindow{nullptr};
303 bool m_closingAllApps{false};
304};
306#endif // TOPLEVELWINDOWMODEL_H