File: TopLevelWindowModel.h

package info (click to toggle)
lomiri 0.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (308 lines) | stat: -rw-r--r-- 10,731 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (C) 2016-2017 Canonical Ltd.
 * Copyright 2019 UBports Foundation
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License version 3, as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TOPLEVELWINDOWMODEL_H
#define TOPLEVELWINDOWMODEL_H

#include <QAbstractListModel>
#include <QAtomicInteger>
#include <QLoggingCategory>

#include <memory>

#include "WindowManagerGlobal.h"

Q_DECLARE_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL)

class Window;
class Workspace;

namespace miral { class Workspace; }

namespace lomiri {
    namespace shell {
        namespace application {
            class ApplicationInfoInterface;
            class ApplicationManagerInterface;
            class MirSurfaceInterface;
            class SurfaceManagerInterface;
        }
    }
}

/**
 * @brief A model of top-level surfaces
 *
 * It's an abstraction of top-level application windows.
 *
 * When an entry first appears, it normaly doesn't have a surface yet, meaning that the application is
 * still starting up. A shell should then display a splash screen or saved screenshot of the application
 * until its surface comes up.
 *
 * As applications can have multiple surfaces and you can also have entries without surfaces at all,
 * the only way to unambiguously refer to an entry in this model is through its id.
 */
class WINDOWMANAGERQML_EXPORT TopLevelWindowModel : public QAbstractListModel
{
    Q_OBJECT

    /**
     * @brief Number of top-level surfaces in this model
     *
     * This is the same as rowCount, added in order to keep compatibility with QML ListModels.
     */
    Q_PROPERTY(int count READ rowCount NOTIFY countChanged)

    /**
     * @brief The input method surface, if any
     *
     * The surface of a onscreen keyboard (akak "virtual keyboard") would be kept here and not in the model itself.
     */
    Q_PROPERTY(lomiri::shell::application::MirSurfaceInterface* inputMethodSurface READ inputMethodSurface NOTIFY inputMethodSurfaceChanged)

    /**
     * @brief The currently focused window, if any
     */
    Q_PROPERTY(Window* focusedWindow READ focusedWindow NOTIFY focusedWindowChanged)

    /**
      The id to be used on the next entry created
      Useful for tests
     */
    Q_PROPERTY(int nextId READ nextId)


   /**
     * @brief Sets whether a user Window or "nothing" should be focused
     *
     * This implementation of TLWM must have something focused. However, the
     * user may wish to have nothing in some cases - for example, when they
     * minimize all their windows on the desktop or unfocus the app they're
     * using by clicking the background or indicators.
     *
     * Unsetting rootFocus effectively focuses "nothing" by setting up a Window
     * that has no displayable Surfaces and bringing it into focus.
     *
     * Setting rootFocus attempts to focus the Window which was focused last -
     * unless another app is attempting to gain focus (as determined by
     * pendingActivation) and that's why we got rootFocus.
     *
     * If the previously-focused Window was closed before rootFocus was set,
     * the next available window will be focused.
     */
    Q_PROPERTY(bool rootFocus READ rootFocus WRITE setRootFocus NOTIFY rootFocusChanged)

public:
    /**
     * @brief The Roles supported by the model
     *
     * WindowRole - A Window.
     * ApplicationRole - An ApplicationInfoInterface
     */
    enum Roles {
        WindowRole = Qt::UserRole,
        ApplicationRole = Qt::UserRole + 1,
    };

    TopLevelWindowModel(Workspace* workspace);
    ~TopLevelWindowModel();

    // From QAbstractItemModel
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex& index, int role) const override;
    QHash<int, QByteArray> roleNames() const override {
        QHash<int, QByteArray> roleNames { {WindowRole, "window"},
                                           {ApplicationRole, "application"} };
        return roleNames;
    }

    // Own API;

    lomiri::shell::application::MirSurfaceInterface* inputMethodSurface() const;
    Window* focusedWindow() const;

#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    int nextId() const { return m_nextId.loadRelaxed(); }
#else
    int nextId() const { return m_nextId.load(); }
#endif

public:
    /**
     * @brief Returns the surface at the given index
     *
     * It will be a nullptr if the application is still starting up and thus hasn't yet created
     * and drawn into a surface.
     *
     * Same as windowAt(index).surface()
     */
    Q_INVOKABLE lomiri::shell::application::MirSurfaceInterface *surfaceAt(int index) const;

    /**
     * @brief Returns the window at the given index
     *
     * Will always be valid
     */
    Q_INVOKABLE Window *windowAt(int index) const;

    /**
     * @brief Returns the application at the given index
     */
    Q_INVOKABLE lomiri::shell::application::ApplicationInfoInterface *applicationAt(int index) const;

    /**
     * @brief Returns the unique id of the element at the given index
     */
    Q_INVOKABLE int idAt(int index) const;

    /**
     * @brief Returns the index where the row with the given id is located
     *
     * Returns -1 if there's no row with the given id.
     */
    Q_INVOKABLE int indexForId(int id) const;

    /**
     * @brief Raises the row with the given id to the top of the window stack (index == count-1)
     */
    Q_INVOKABLE void raiseId(int id);

    /**
     * @brief Closes all windows, emits closedAllWindows when done
     */
    Q_INVOKABLE void closeAllWindows();

    /**
     * @brief Sets pending activation flag
     */
    Q_INVOKABLE void pendingActivation();

    void setApplicationManager(lomiri::shell::application::ApplicationManagerInterface*);
    void setSurfaceManager(lomiri::shell::application::SurfaceManagerInterface*);
    void setRootFocus(bool focus);
    bool rootFocus();

Q_SIGNALS:
    void countChanged();
    void inputMethodSurfaceChanged(lomiri::shell::application::MirSurfaceInterface* inputMethodSurface);
    void focusedWindowChanged(Window *focusedWindow);

    /**
     * @brief Emitted when the list changes
     *
     * Emitted when model gains an element, loses an element or when elements exchange positions.
     */
    void listChanged();

    void closedAllWindows();

    void rootFocusChanged();

private Q_SLOTS:
    void onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
                                    const QVector<lomiri::shell::application::MirSurfaceInterface*> surfaces);
    void onSurfacesRaised(const QVector<lomiri::shell::application::MirSurfaceInterface*> &surfaces);

    void onModificationsStarted();
    void onModificationsEnded();

private:
    void doRaiseId(int id);
    int generateId();
    int nextFreeId(int candidateId, const int latestId);
    int nextId(int id) const;
    QString toString();
    int indexOf(lomiri::shell::application::MirSurfaceInterface *surface);

    void setInputMethodWindow(Window *window);
    void setFocusedWindow(Window *window);
    void removeInputMethodWindow();
    void deleteAt(int index);
    void removeAt(int index);
    void removeSurfaces(const QVector<lomiri::shell::application::MirSurfaceInterface *> surfaces);

    void addApplication(lomiri::shell::application::ApplicationInfoInterface *application);
    void removeApplication(lomiri::shell::application::ApplicationInfoInterface *application);

    void prependPlaceholder(lomiri::shell::application::ApplicationInfoInterface *application);
    void prependSurface(lomiri::shell::application::MirSurfaceInterface *surface,
                        lomiri::shell::application::ApplicationInfoInterface *application);
    void prependSurfaceHelper(lomiri::shell::application::MirSurfaceInterface *surface,
                              lomiri::shell::application::ApplicationInfoInterface *application);
    void prependWindow(Window *window, lomiri::shell::application::ApplicationInfoInterface *application);

    void connectWindow(Window *window);
    void connectSurface(lomiri::shell::application::MirSurfaceInterface *surface);

    int countWindowsWithApplication(lomiri::shell::application::ApplicationInfoInterface *application);
    void onSurfaceDied(lomiri::shell::application::MirSurfaceInterface *surface);
    void onSurfaceDestroyed(lomiri::shell::application::MirSurfaceInterface *surface);

    void move(int from, int to);

    void activateEmptyWindow(Window *window);

    void activateTopMostWindowWithoutId(int forbiddenId);
    void refreshWindows();
    void clear();

    Window *createWindow(lomiri::shell::application::MirSurfaceInterface *surface);
    Window *createWindowWithId(lomiri::shell::application::MirSurfaceInterface *surface, int id);
    Window *createNullWindow();

    struct ModelEntry {
        ModelEntry() {}
        ModelEntry(Window *window,
                   lomiri::shell::application::ApplicationInfoInterface *application)
            : window(window), application(application) {}
        Window *window{nullptr};
        lomiri::shell::application::ApplicationInfoInterface *application{nullptr};
        bool removeOnceSurfaceDestroyed{false};
    };

    QVector<ModelEntry> m_windowModel;
    Window* m_inputMethodWindow{nullptr};
    Window* m_focusedWindow{nullptr};
    Window* m_nullWindow;
    Workspace* m_workspace{nullptr};
    // track all the surfaces we've been told about.
    QSet<lomiri::shell::application::MirSurfaceInterface*> m_allSurfaces;
    Window* m_previousWindow{nullptr};
    bool m_pendingActivation;

    QAtomicInteger<int> m_nextId{1};

    lomiri::shell::application::ApplicationManagerInterface* m_applicationManager{nullptr};
    lomiri::shell::application::SurfaceManagerInterface *m_surfaceManager{nullptr};
    bool m_surfaceManagerBusy;

    enum ModelState {
        IdleState,
        InsertingState,
        RemovingState,
        MovingState,
        ResettingState
    };
    ModelState m_modelState{IdleState};

    // Valid between modificationsStarted and modificationsEnded
    bool m_focusedWindowCleared{false};

    bool m_closingAllApps{false};
};

#endif // TOPLEVELWINDOWMODEL_H