on 1897f10 help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.h
264 lines 9.3 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 <memory>
25#include "WindowManagerGlobal.h"
27Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)
29class Window;
30class Workspace;
32namespace miral { class Workspace; }
34namespace unity {
35 namespace shell {
36 namespace application {
37 class ApplicationInfoInterface;
38 class ApplicationManagerInterface;
39 class MirSurfaceInterface;
40 class SurfaceManagerInterface;
41 }
42 }
45/**
46 * @brief A model of top-level surfaces
47 *
48 * It's an abstraction of top-level application windows.
49 *
50 * When an entry first appears, it normaly doesn't have a surface yet, meaning that the application is
51 * still starting up. A shell should then display a splash screen or saved screenshot of the application
52 * until its surface comes up.
53 *
54 * As applications can have multiple surfaces and you can also have entries without surfaces at all,
55 * the only way to unambiguously refer to an entry in this model is through its id.
56 */
57class WINDOWMANAGERQML_EXPORT TopLevelWindowModel : public QAbstractListModel
59 Q_OBJECT
61 /**
62 * @brief Number of top-level surfaces in this model
63 *
64 * This is the same as rowCount, added in order to keep compatibility with QML ListModels.
65 */
66 Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
68 /**
69 * @brief The input method surface, if any
70 *
71 * The surface of a onscreen keyboard (akak "virtual keyboard") would be kept here and not in the model itself.
72 */
73 Q_PROPERTY(unity::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)
75 /**
76 * @brief The currently focused window, if any
77 */
78 Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)
80 /**
81 The id to be used on the next entry created
82 Useful for tests
83 */
84 Q_PROPERTY(int nextId READ nextId NOTIFY nextIdChanged)
86public:
87 /**
88 * @brief The Roles supported by the model
89 *
90 * WindowRole - A Window.
91 * ApplicationRole - An ApplicationInfoInterface
92 */
93 enum Roles {
94 WindowRole = Qt::UserRole,
95 ApplicationRole = Qt::UserRole + 1,
96 };
98 TopLevelWindowModel(Workspace* workspace);
99 ~TopLevelWindowModel();
101 // From QAbstractItemModel
102 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
103 QVariant data(const QModelIndex& index, int role) const override;
104 QHash<int, QByteArray> roleNames() const override {
105 QHash<int, QByteArray> roleNames { {WindowRole, "window"},
106 {ApplicationRole, "application"} };
107 return roleNames;
108 }
110 // Own API;
112 unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
113 Window* focusedWindow() const;
115 int nextId() const { return m_nextId; }
117public:
118 /**
119 * @brief Returns the surface at the given index
120 *
121 * It will be a nullptr if the application is still starting up and thus hasn't yet created
122 * and drawn into a surface.
123 *
124 * Same as windowAt(index).surface()
125 */
126 Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
128 /**
129 * @brief Returns the window at the given index
130 *
131 * Will always be valid
132 */
133 Q_INVOKABLE Window *windowAt(int index) const;
135 /**
136 * @brief Returns the application at the given index
137 */
138 Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
140 /**
141 * @brief Returns the unique id of the element at the given index
142 */
143 Q_INVOKABLE int idAt(int index) const;
145 /**
146 * @brief Returns the index where the row with the given id is located
147 *
148 * Returns -1 if there's no row with the given id.
149 */
150 Q_INVOKABLE int indexForId(int id) const;
152 /**
153 * @brief Raises the row with the given id to the top of the window stack (index == count-1)
154 */
155 Q_INVOKABLE void raiseId(int id);
157 void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
158 void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
160Q_SIGNALS:
161 void countChanged();
162 void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
163 void focusedWindowChanged(Window *focusedWindow);
165 /**
166 * @brief Emitted when the list changes
167 *
168 * Emitted when model gains an element, loses an element or when elements exchange positions.
169 */
170 void listChanged();
172 void nextIdChanged();
174private Q_SLOTS:
175 void onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
176 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces);
177 void onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
178 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces);
179 void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
181 void onModificationsStarted();
182 void onModificationsEnded();
184private:
185 void doRaiseId(int id);
186 int generateId();
187 int nextFreeId(int candidateId, const int latestId);
188 int nextId(int id) const;
189 QString toString();
190 int indexOf(unity::shell::application::MirSurfaceInterface *surface);
192 void setInputMethodWindow(Window *window);
193 void setFocusedWindow(Window *window);
194 void removeInputMethodWindow();
195 int findIndexOf(const unity::shell::application::MirSurfaceInterface *surface) const;
196 void deleteAt(int index);
197 void removeAt(int index);
199 void addApplication(unity::shell::application::ApplicationInfoInterface *application);
200 void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
202 void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
203 void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
204 unity::shell::application::ApplicationInfoInterface *application);
205 void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
206 unity::shell::application::ApplicationInfoInterface *application);
207 void prependWindow(Window *window, unity::shell::application::ApplicationInfoInterface *application);
209 void connectWindow(Window *window);
210 void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
212 void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
213 void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
215 void move(int from, int to);
217 void activateEmptyWindow(Window *window);
219 void activateTopMostWindowWithoutId(int forbiddenId);
220 void refreshWindows();
221 void clear();
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};
238 Workspace* m_workspace{nullptr};
239 // track all the surfaces we've been told about.
240 QSet<unity::shell::application::MirSurfaceInterface*> m_allSurfaces;
242 int m_nextId{1};
243 // Just something big enough that we don't risk running out of unused id numbers.
244 // Not sure if QML int type supports something close to std::numeric_limits<int>::max() and
245 // there's no reason to try out its limits.
246 static const int m_maxId{1000000};
248 unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
249 unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
251 enum ModelState {
252 IdleState,
253 InsertingState,
254 RemovingState,
255 MovingState,
256 ResettingState
257 };
258 ModelState m_modelState{IdleState};
260 // Valid between modificationsStarted and modificationsEnded
261 bool m_focusedWindowCleared{false};
262};
264#endif // TOPLEVELWINDOWMODEL_H