on 43f0793 help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.h
262 lines 9.1 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 onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
179 void onModificationsStarted();
180 void onModificationsEnded();
182private:
183 void doRaiseId(int id);
184 int generateId();
185 int nextFreeId(int candidateId, const int latestId);
186 int nextId(int id) const;
187 QString toString();
188 int indexOf(unity::shell::application::MirSurfaceInterface *surface);
190 void setInputMethodWindow(Window *window);
191 void setFocusedWindow(Window *window);
192 void removeInputMethodWindow();
193 void deleteAt(int index);
194 void removeAt(int index);
195 void removeSurfaces(const QVector<unity::shell::application::MirSurfaceInterface *> surfaces);
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);
205 void prependWindow(Window *window, unity::shell::application::ApplicationInfoInterface *application);
207 void connectWindow(Window *window);
208 void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
210 void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
211 void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
213 void move(int from, int to);
215 void activateEmptyWindow(Window *window);
217 void activateTopMostWindowWithoutId(int forbiddenId);
218 void refreshWindows();
219 void clear();
221 Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
223 struct ModelEntry {
224 ModelEntry() {}
225 ModelEntry(Window *window,
226 unity::shell::application::ApplicationInfoInterface *application)
227 : window(window), application(application) {}
228 Window *window{nullptr};
229 unity::shell::application::ApplicationInfoInterface *application{nullptr};
230 bool removeOnceSurfaceDestroyed{false};
231 };
233 QVector<ModelEntry> m_windowModel;
234 Window* m_inputMethodWindow{nullptr};
235 Window* m_focusedWindow{nullptr};
236 Workspace* m_workspace{nullptr};
237 // track all the surfaces we've been told about.
238 QSet<unity::shell::application::MirSurfaceInterface*> m_allSurfaces;
240 int m_nextId{1};
241 // Just something big enough that we don't risk running out of unused id numbers.
242 // Not sure if QML int type supports something close to std::numeric_limits<int>::max() and
243 // there's no reason to try out its limits.
244 static const int m_maxId{1000000};
246 unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
247 unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
249 enum ModelState {
250 IdleState,
251 InsertingState,
252 RemovingState,
253 MovingState,
254 ResettingState
255 };
256 ModelState m_modelState{IdleState};
258 // Valid between modificationsStarted and modificationsEnded
259 bool m_focusedWindowCleared{false};
260};
262#endif // TOPLEVELWINDOWMODEL_H