Unity 8
TopLevelWindowModel.h
1 /*
2  * Copyright (C) 2016-2017 Canonical, Ltd.
3  * Copyright 2019 UBports Foundation
4  *
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 3, as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
11  * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef TOPLEVELWINDOWMODEL_H
19 #define TOPLEVELWINDOWMODEL_H
20 
21 #include <QAbstractListModel>
22 #include <QAtomicInteger>
23 #include <QLoggingCategory>
24 
25 #include <memory>
26 
27 #include "WindowManagerGlobal.h"
28 
29 Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)
30 
31 class Window;
32 class Workspace;
33 
34 namespace miral { class Workspace; }
35 
36 namespace unity {
37  namespace shell {
38  namespace application {
39  class ApplicationInfoInterface;
40  class ApplicationManagerInterface;
41  class MirSurfaceInterface;
42  class SurfaceManagerInterface;
43  }
44  }
45 }
46 
59 class WINDOWMANAGERQML_EXPORT TopLevelWindowModel : public QAbstractListModel
60 {
61  Q_OBJECT
62 
68  Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
69 
70 
75  Q_PROPERTY(unity::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)
76 
80  Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)
81 
86  Q_PROPERTY(int nextId READ nextId)
87 
88 
107  Q_PROPERTY(bool rootFocus READ rootFocus WRITE setRootFocus NOTIFY rootFocusChanged)
108 
109 public:
116  enum Roles {
117  WindowRole = Qt::UserRole,
118  ApplicationRole = Qt::UserRole + 1,
119  };
120 
121  TopLevelWindowModel(Workspace* workspace);
123 
124  // From QAbstractItemModel
125  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
126  QVariant data(const QModelIndex& index, int role) const override;
127  QHash<int, QByteArray> roleNames() const override {
128  QHash<int, QByteArray> roleNames { {WindowRole, "window"},
129  {ApplicationRole, "application"} };
130  return roleNames;
131  }
132 
133  // Own API;
134 
135  unity::shell::application::MirSurfaceInterface* inputMethodSurface() const;
136  Window* focusedWindow() const;
137 
138  int nextId() const { return m_nextId.load(); }
139 
140 public:
149  Q_INVOKABLE unity::shell::application::MirSurfaceInterface *surfaceAt(int index) const;
150 
156  Q_INVOKABLE Window *windowAt(int index) const;
157 
161  Q_INVOKABLE unity::shell::application::ApplicationInfoInterface *applicationAt(int index) const;
162 
166  Q_INVOKABLE int idAt(int index) const;
167 
173  Q_INVOKABLE int indexForId(int id) const;
174 
178  Q_INVOKABLE void raiseId(int id);
179 
183  Q_INVOKABLE void closeAllWindows();
184 
188  Q_INVOKABLE void pendingActivation();
189 
190  void setApplicationManager(unity::shell::application::ApplicationManagerInterface*);
191  void setSurfaceManager(unity::shell::application::SurfaceManagerInterface*);
192  void setRootFocus(bool focus);
193  bool rootFocus();
194 
195 Q_SIGNALS:
196  void countChanged();
197  void inputMethodSurfaceChanged(unity::shell::application::MirSurfaceInterface* inputMethodSurface);
198  void focusedWindowChanged(Window *focusedWindow);
199 
205  void listChanged();
206 
207  void closedAllWindows();
208 
209  void rootFocusChanged();
210 
211 private Q_SLOTS:
212  void onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
213  const QVector<unity::shell::application::MirSurfaceInterface*> surfaces);
214  void onSurfacesRaised(const QVector<unity::shell::application::MirSurfaceInterface*> &surfaces);
215 
216  void onModificationsStarted();
217  void onModificationsEnded();
218 
219 private:
220  void doRaiseId(int id);
221  int generateId();
222  int nextFreeId(int candidateId, const int latestId);
223  int nextId(int id) const;
224  QString toString();
225  int indexOf(unity::shell::application::MirSurfaceInterface *surface);
226 
227  void setInputMethodWindow(Window *window);
228  void setFocusedWindow(Window *window);
229  void removeInputMethodWindow();
230  void deleteAt(int index);
231  void removeAt(int index);
232  void removeSurfaces(const QVector<unity::shell::application::MirSurfaceInterface *> surfaces);
233 
234  void addApplication(unity::shell::application::ApplicationInfoInterface *application);
235  void removeApplication(unity::shell::application::ApplicationInfoInterface *application);
236 
237  void prependPlaceholder(unity::shell::application::ApplicationInfoInterface *application);
238  void prependSurface(unity::shell::application::MirSurfaceInterface *surface,
239  unity::shell::application::ApplicationInfoInterface *application);
240  void prependSurfaceHelper(unity::shell::application::MirSurfaceInterface *surface,
241  unity::shell::application::ApplicationInfoInterface *application);
242  void prependWindow(Window *window, unity::shell::application::ApplicationInfoInterface *application);
243 
244  void connectWindow(Window *window);
245  void connectSurface(unity::shell::application::MirSurfaceInterface *surface);
246 
247  void onSurfaceDied(unity::shell::application::MirSurfaceInterface *surface);
248  void onSurfaceDestroyed(unity::shell::application::MirSurfaceInterface *surface);
249 
250  void move(int from, int to);
251 
252  void activateEmptyWindow(Window *window);
253 
254  void activateTopMostWindowWithoutId(int forbiddenId);
255  void refreshWindows();
256  void clear();
257 
258  Window *createWindow(unity::shell::application::MirSurfaceInterface *surface);
259  Window *createWindowWithId(unity::shell::application::MirSurfaceInterface *surface, int id);
260  Window *createNullWindow();
261 
262  struct ModelEntry {
263  ModelEntry() {}
264  ModelEntry(Window *window,
265  unity::shell::application::ApplicationInfoInterface *application)
266  : window(window), application(application) {}
267  Window *window{nullptr};
268  unity::shell::application::ApplicationInfoInterface *application{nullptr};
269  bool removeOnceSurfaceDestroyed{false};
270  };
271 
272  QVector<ModelEntry> m_windowModel;
273  Window* m_inputMethodWindow{nullptr};
274  Window* m_focusedWindow{nullptr};
275  Window* m_nullWindow;
276  Workspace* m_workspace{nullptr};
277  // track all the surfaces we've been told about.
278  QSet<unity::shell::application::MirSurfaceInterface*> m_allSurfaces;
279  Window* m_previousWindow{nullptr};
280  bool m_pendingActivation;
281 
282  QAtomicInteger<int> m_nextId{1};
283 
284  unity::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
285  unity::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
286  bool m_surfaceManagerBusy;
287 
288  enum ModelState {
289  IdleState,
290  InsertingState,
291  RemovingState,
292  MovingState,
293  ResettingState
294  };
295  ModelState m_modelState{IdleState};
296 
297  // Valid between modificationsStarted and modificationsEnded
298  bool m_focusedWindowCleared{false};
299 
300  bool m_closingAllApps{false};
301 };
302 
303 #endif // TOPLEVELWINDOWMODEL_H
A slightly higher concept than MirSurface.
Definition: Window.h:47
Roles
The Roles supported by the model.
A model of top-level surfaces.