on 96917ee help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.h
262 lines 9.3 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>
23Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)
25class Window;
27namespace unity {
28 namespace shell {
29 namespace application {
30 class ApplicationInfoInterface;
31 class ApplicationManagerInterface;
32 class MirSurfaceInterface;
33 class SurfaceManagerInterface;
34 }
35 }
38/**
39 * @brief A model of top-level surfaces
40 *
41 * It's an abstraction of top-level application windows.
42 *
43 * When an entry first appears, it normaly doesn't have a surface yet, meaning that the application is
44 * still starting up. A shell should then display a splash screen or saved screenshot of the application
45 * until its surface comes up.
46 *
47 * As applications can have multiple surfaces and you can also have entries without surfaces at all,
48 * the only way to unambiguously refer to an entry in this model is through its id.
49 */
50class TopLevelWindowModel : public QAbstractListModel
52 Q_OBJECT
54 /**
55 * @brief Number of top-level surfaces in this model
56 *
57 * This is the same as rowCount, added in order to keep compatibility with QML ListModels.
58 */
59 Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
61 /**
62 * @brief The input method surface, if any
63 *
64 * The surface of a onscreen keyboard (akak "virtual keyboard") would be kept here and not in the model itself.
65 */
66 Q_PROPERTY(unity::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)
68 /**
69 * @brief The currently focused window, if any
70 */
71 Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)
73 Q_PROPERTY(unity::shell::application::SurfaceManagerInterface* surfaceManager
74 READ surfaceManager
75 WRITE setSurfaceManager
76 NOTIFY surfaceManagerChanged)
78 Q_PROPERTY(unity::shell::application::ApplicationManagerInterface* applicationManager
79 READ applicationManager
80 WRITE setApplicationManager
81 NOTIFY applicationManagerChanged)
83 /**
84 The id to be used on the next entry created
85 Useful for tests
86 */
87 Q_PROPERTY(int nextId READ nextId NOTIFY nextIdChanged)
89public:
90 /**
91 * @brief The Roles supported by the model
92 *
93 * WindowRole - A Window.
94 * ApplicationRole - An ApplicationInfoInterface
95 */
96 enum Roles {
97 WindowRole = Qt::UserRole,
98 ApplicationRole = Qt::UserRole + 1,
99 };
101 TopLevelWindowModel();
103 // From QAbstractItemModel
104 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
105 QVariant data(const QModelIndex& index, int role) const override;
106 QHash<int, QByteArray> roleNames() const override {
107 QHash<int, QByteArray> roleNames { {WindowRole, "window"},
108 {ApplicationRole, "application"} };
109 return roleNames;
110 }
112 // Own API
114 unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
115 Window* focusedWindow() const;
117 unity::shell::application::ApplicationManagerInterface *applicationManager() const { return m_applicationManager; }
118 void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
120 unity::shell::application::SurfaceManagerInterface *surfaceManager() const { return m_surfaceManager; }
121 void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
123 int nextId() const { return m_nextId; }
125public:
126 /**
127 * @brief Returns the surface at the given index
128 *
129 * It will be a nullptr if the application is still starting up and thus hasn't yet created
130 * and drawn into a surface.
131 *
132 * Same as windowAt(index).surface()
133 */
134 Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
136 /**
137 * @brief Returns the window at the given index
138 *
139 * Will always be valid
140 */
141 Q_INVOKABLE Window *windowAt(int index) const;
143 /**
144 * @brief Returns the application at the given index
145 */
146 Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
148 /**
149 * @brief Returns the unique id of the element at the given index
150 */
151 Q_INVOKABLE int idAt(int index) const;
153 /**
154 * @brief Returns the index where the row with the given id is located
155 *
156 * Returns -1 if there's no row with the given id.
157 */
158 Q_INVOKABLE int indexForId(int id) const;
160 /**
161 * @brief Raises the row with the given id to the top of the window stack (index == count-1)
162 */
163 Q_INVOKABLE void raiseId(int id);
165Q_SIGNALS:
166 void countChanged();
167 void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
168 void focusedWindowChanged(Window *focusedWindow);
169 void applicationManagerChanged(unity::shell::application::ApplicationManagerInterface*);
170 void surfaceManagerChanged(unity::shell::application::SurfaceManagerInterface*);
172 /**
173 * @brief Emitted when the list changes
174 *
175 * Emitted when model gains an element, loses an element or when elements exchange positions.
176 */
177 void listChanged();
179 void nextIdChanged();
181private Q_SLOTS:
182 void onSurfaceCreated(unity::shell::application::MirSurfaceInterface *surface);
183 void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
185 void onModificationsStarted();
186 void onModificationsEnded();
188private:
189 void doRaiseId(int id);
190 int generateId();
191 int nextFreeId(int candidateId, const int latestId);
192 int nextId(int id) const;
193 QString toString();
194 int indexOf(unity::shell::application::MirSurfaceInterface *surface);
196 void setInputMethodWindow(Window *window);
197 void setFocusedWindow(Window *window);
198 void removeInputMethodWindow();
199 int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
200 void removeAt(int index);
202 void addApplication(unity::shell::application::ApplicationInfoInterface *application);
203 void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
205 void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
206 void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
207 unity::shell::application::ApplicationInfoInterface *application);
208 void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
209 unity::shell::application::ApplicationInfoInterface *application);
211 void connectWindow(Window *window);
212 void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
214 void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
215 void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
217 void move(int from, int to);
219 void activateEmptyWindow(Window *window);
221 void activateTopMostWindowWithoutId(int forbiddenId);
223 Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
225 struct ModelEntry {
226 ModelEntry() {}
227 ModelEntry(Window *window,
228 unity::shell::application::ApplicationInfoInterface *application)
229 : window(window), application(application) {}
230 Window *window{nullptr};
231 unity::shell::application::ApplicationInfoInterface *application{nullptr};
232 bool removeOnceSurfaceDestroyed{false};
233 };
235 QVector<ModelEntry> m_windowModel;
236 Window* m_inputMethodWindow{nullptr};
237 Window* m_focusedWindow{nullptr};
239 int m_nextId{1};
240 // Just something big enough that we don't risk running out of unused id numbers.
241 // Not sure if QML int type supports something close to std::numeric_limits<int>::max() and
242 // there's no reason to try out its limits.
243 static const int m_maxId{1000000};
245 unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
246 unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
248 enum ModelState {
249 IdleState,
250 InsertingState,
251 RemovingState,
252 MovingState,
253 ResettingState
254 };
255 ModelState m_modelState{IdleState};
257 // Valid between modificationsStarted and modificationsEnded
258 bool m_focusedWindowChanged{false};
259 Window *m_newlyFocusedWindow{nullptr};
260};
262#endif // TOPLEVELWINDOWMODEL_H