on 80fc88d help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.h
261 lines 9.2 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>
23#include <memory>
25Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)
27class Window;
28class Workspace;
30namespace miral { class Workspace; }
32namespace unity {
33 namespace shell {
34 namespace application {
35 class ApplicationInfoInterface;
36 class ApplicationManagerInterface;
37 class MirSurfaceInterface;
38 class SurfaceManagerInterface;
39 }
40 }
43/**
44 * @brief A model of top-level surfaces
45 *
46 * It's an abstraction of top-level application windows.
47 *
48 * When an entry first appears, it normaly doesn't have a surface yet, meaning that the application is
49 * still starting up. A shell should then display a splash screen or saved screenshot of the application
50 * until its surface comes up.
51 *
52 * As applications can have multiple surfaces and you can also have entries without surfaces at all,
53 * the only way to unambiguously refer to an entry in this model is through its id.
54 */
55class TopLevelWindowModel : public QAbstractListModel
57 Q_OBJECT
59 /**
60 * @brief Number of top-level surfaces in this model
61 *
62 * This is the same as rowCount, added in order to keep compatibility with QML ListModels.
63 */
64 Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
66 /**
67 * @brief The input method surface, if any
68 *
69 * The surface of a onscreen keyboard (akak "virtual keyboard") would be kept here and not in the model itself.
70 */
71 Q_PROPERTY(unity::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)
73 /**
74 * @brief The currently focused window, if any
75 */
76 Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)
78 /**
79 The id to be used on the next entry created
80 Useful for tests
81 */
82 Q_PROPERTY(int nextId READ nextId NOTIFY nextIdChanged)
84public:
85 /**
86 * @brief The Roles supported by the model
87 *
88 * WindowRole - A Window.
89 * ApplicationRole - An ApplicationInfoInterface
90 */
91 enum Roles {
92 WindowRole = Qt::UserRole,
93 ApplicationRole = Qt::UserRole + 1,
94 };
96 TopLevelWindowModel(Workspace* workspace);
97 ~TopLevelWindowModel();
99 // From QAbstractItemModel
100 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
101 QVariant data(const QModelIndex& index, int role) const override;
102 QHash<int, QByteArray> roleNames() const override {
103 QHash<int, QByteArray> roleNames { {WindowRole, "window"},
104 {ApplicationRole, "application"} };
105 return roleNames;
106 }
108 // Own API;
110 unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
111 Window* focusedWindow() const;
113 int nextId() const { return m_nextId; }
115public:
116 /**
117 * @brief Returns the surface at the given index
118 *
119 * It will be a nullptr if the application is still starting up and thus hasn't yet created
120 * and drawn into a surface.
121 *
122 * Same as windowAt(index).surface()
123 */
124 Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
126 /**
127 * @brief Returns the window at the given index
128 *
129 * Will always be valid
130 */
131 Q_INVOKABLE Window *windowAt(int index) const;
133 /**
134 * @brief Returns the application at the given index
135 */
136 Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
138 /**
139 * @brief Returns the unique id of the element at the given index
140 */
141 Q_INVOKABLE int idAt(int index) const;
143 /**
144 * @brief Returns the index where the row with the given id is located
145 *
146 * Returns -1 if there's no row with the given id.
147 */
148 Q_INVOKABLE int indexForId(int id) const;
150 /**
151 * @brief Raises the row with the given id to the top of the window stack (index == count-1)
152 */
153 Q_INVOKABLE void raiseId(int id);
155Q_SIGNALS:
156 void countChanged();
157 void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
158 void focusedWindowChanged(Window *focusedWindow);
160 /**
161 * @brief Emitted when the list changes
162 *
163 * Emitted when model gains an element, loses an element or when elements exchange positions.
164 */
165 void listChanged();
167 void nextIdChanged();
169private Q_SLOTS:
170 void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
171 void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
173 void onSurfaceCreated(unity::shell::application::MirSurfaceInterface *surface);
174 void onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
175 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces);
176 void onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
177 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces);
178 void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
180 void onModificationsStarted();
181 void onModificationsEnded();
183private:
184 void doRaiseId(int id);
185 int generateId();
186 int nextFreeId(int candidateId, const int latestId);
187 int nextId(int id) const;
188 QString toString();
189 int indexOf(unity::shell::application::MirSurfaceInterface *surface);
191 void setInputMethodWindow(Window *window);
192 void setFocusedWindow(Window *window);
193 void removeInputMethodWindow();
194 int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
195 void removeAt(int index);
197 void addApplication(unity::shell::application::ApplicationInfoInterface *application);
198 void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
200 void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
201 void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
202 unity::shell::application::ApplicationInfoInterface *application);
203 void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
204 unity::shell::application::ApplicationInfoInterface *application);
206 void connectWindow(Window *window);
207 void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
209 void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
210 void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
212 void move(int from, int to);
214 void activateEmptyWindow(Window *window);
216 void activateTopMostWindowWithoutId(int forbiddenId);
217 void refreshWindows();
218 void clear();
220 Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
222 struct ModelEntry {
223 ModelEntry() {}
224 ModelEntry(Window *window,
225 unity::shell::application::ApplicationInfoInterface *application)
226 : window(window), application(application) {}
227 Window *window{nullptr};
228 unity::shell::application::ApplicationInfoInterface *application{nullptr};
229 bool removeOnceSurfaceDestroyed{false};
230 };
232 QVector<ModelEntry> m_windowModel;
233 Window* m_inputMethodWindow{nullptr};
234 Window* m_focusedWindow{nullptr};
235 Workspace* m_workspace{nullptr};
236 // track all the surfaces we've been told about.
237 QSet<unity::shell::application::MirSurfaceInterface*> m_allSurfaces;
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_focusedWindowCleared{false};
259};
261#endif // TOPLEVELWINDOWMODEL_H