@@ -24,11 +24,16 @@
2424#include <unity/shell/application/SurfaceManagerInterface.h>
2525
2626// Qt
27−#include <QGuiApplication>
2827#include <QDebug>
2928
3029// local
3130#include "Window.h"
31+#include "Workspace.h"
32+#include "wmpolicyinterface.h"
33+
34+// qtmir
35+#include <qtmir/mirserverapplication.h>
36+#include <qtmir/windowmodelnotifier.h>
3237
3338Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
3439
@@ -55,7 +60,6 @@ void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInte
5560 beginResetModel();
5661
5762 if (m_applicationManager) {
58− m_windowModel.clear();
5963 disconnect(m_applicationManager, 0, this, 0);
6064 }
6165
@@ -64,26 +68,27 @@ void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInte
6468 if (m_applicationManager) {
6569 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
6670 this, [this](const QModelIndex &/*parent*/, int first, int last) {
67− for (int i = first; i <= last; ++i) {
68− auto application = m_applicationManager->get(i);
69− addApplication(application);
70− }
71− });
71+ if (!m_workspace || !m_workspace->isActive())
72+ return;
73+
74+ for (int i = first; i <= last; ++i) {
75+ auto application = m_applicationManager->get(i);
76+ addApplication(application);
77+ }
78+ });
7279
7380 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
7481 this, [this](const QModelIndex &/*parent*/, int first, int last) {
75− for (int i = first; i <= last; ++i) {
76− auto application = m_applicationManager->get(i);
77− removeApplication(application);
78− }
79− });
80−
81− for (int i = 0; i < m_applicationManager->rowCount(); ++i) {
82− auto application = m_applicationManager->get(i);
83− addApplication(application);
84− }
82+ for (int i = first; i <= last; ++i) {
83+ auto application = m_applicationManager->get(i);
84+ removeApplication(application);
85+ }
86+ });
8587 }
8688
89+ Q_EMIT applicationManagerChanged(m_applicationManager);
90+ refreshWindows();
91+
8792 endResetModel();
8893 m_modelState = IdleState;
8994}
@@ -96,6 +101,11 @@ void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *s
96101
97102 DEBUG_MSG << "(" << surfaceManager << ")";
98103
104+ Q_ASSERT(m_modelState == IdleState);
105+ m_modelState = ResettingState;
106+
107+ beginResetModel();
108+
99109 if (m_surfaceManager) {
100110 disconnect(m_surfaceManager, 0, this, 0);
101111 }
@@ -107,9 +117,44 @@ void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *s
107117 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
108118 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
109119 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
120+
121+ connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
122+ this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
123+ connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
124+ this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
110125 }
111126
127+ refreshWindows();
112128 Q_EMIT surfaceManagerChanged(m_surfaceManager);
129+
130+ endResetModel();
131+ m_modelState = IdleState;
132+}
133+
134+void TopLevelWindowModel::setWorkspace(Workspace *workspace)
135+{
136+ if (workspace == m_workspace) {
137+ return;
138+ }
139+
140+ DEBUG_MSG << "(" << workspace << ")";
141+
142+ Q_ASSERT(m_modelState == IdleState);
143+ m_modelState = ResettingState;
144+
145+ beginResetModel();
146+
147+ if (m_workspace) {
148+ disconnect(m_workspace, 0, this, 0);
149+ }
150+
151+ m_workspace = workspace;
152+
153+ refreshWindows();
154+ Q_EMIT workspaceChanged(workspace);
155+
156+ endResetModel();
157+ m_modelState = IdleState;
113158}
114159
115160void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
@@ -153,6 +198,7 @@ void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface,
153198 ModelEntry &entry = m_windowModel[i];
154199 if (entry.application == application && entry.window->surface() == nullptr) {
155200 entry.window->setSurface(surface);
201+ m_allSurfaces.insert(surface);
156202 connectSurface(surface);
157203 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
158204 << ", filling out placeholder. after: " << toString();
@@ -180,6 +226,7 @@ void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *su
180226
181227 m_windowModel.prepend(ModelEntry(window, application));
182228 if (surface) {
229+ m_allSurfaces.insert(surface);
183230 connectSurface(surface);
184231 }
185232
@@ -211,7 +258,6 @@ void TopLevelWindowModel::connectWindow(Window *window)
211258 // eg: Do focusedWindow=A to focusedWindow=B instead of
212259 // focusedWindow=A to focusedWindow=null to focusedWindow=B
213260 if (focused) {
214− Q_ASSERT(m_newlyFocusedWindow == nullptr);
215261 m_focusedWindowChanged = true;
216262 m_newlyFocusedWindow = window;
217263 } else if (m_focusedWindow == window) {
@@ -313,6 +359,7 @@ void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surf
313359 auto window = m_windowModel[i].window;
314360 window->setSurface(nullptr);
315361 window->setFocused(false);
362+ m_allSurfaces.remove(surface);
316363 INFO_MSG << " Removed surface from entry. After: " << toString();
317364 }
318365}
@@ -328,38 +375,102 @@ Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface
328375 return qmlWindow;
329376}
330377
331−void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface *surface)
378+
379+void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
332380{
333− DEBUG_MSG << "(" << surface << ")";
381+}
334382
335− if (surface->parentSurface()) {
336− // Wrap it in a Window so that we keep focusedWindow() up to date.
337− Window *window = createWindow(surface);
338− connect(surface, &QObject::destroyed, window, [=](){
339− window->setSurface(nullptr);
340− window->deleteLater();
341− });
342− } else {
343− if (surface->type() == Mir::InputMethodType) {
344− setInputMethodWindow(createWindow(surface));
383+void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
384+ const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
385+{
386+ if (!m_workspace || !m_applicationManager) return;
387+ if (workspace != m_workspace->workspace()) return;
388+
389+ Q_FOREACH(auto surface, surfaces) {
390+ if (m_allSurfaces.contains(surface)) continue;
391+
392+ if (surface->parentSurface()) {
393+ // Wrap it in a Window so that we keep focusedWindow() up to date.
394+ Window *window = createWindow(surface);
395+ connect(surface, &QObject::destroyed, window, [=](){
396+ window->setSurface(nullptr);
397+ window->deleteLater();
398+ });
345399 } else {
346− auto *application = m_applicationManager->findApplicationWithSurface(surface);
347− if (application) {
348− prependSurface(surface, application);
400+ if (surface->type() == Mir::InputMethodType) {
401+ setInputMethodWindow(createWindow(surface));
349402 } else {
350− // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
351− // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
352− // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
353− Window *promptWindow = createWindow(surface);
354− connect(surface, &QObject::destroyed, promptWindow, [=](){
355− promptWindow->setSurface(nullptr);
356− promptWindow->deleteLater();
357− });
403+ auto *application = m_applicationManager->findApplicationWithSurface(surface);
404+ if (application) {
405+ prependSurface(surface, application);
406+ } else {
407+ // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
408+ // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
409+ // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
410+ Window *promptWindow = createWindow(surface);
411+ connect(surface, &QObject::destroyed, promptWindow, [=](){
412+ promptWindow->setSurface(nullptr);
413+ promptWindow->deleteLater();
414+ });
415+ }
358416 }
359417 }
360418 }
361419}
362420
421+void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
422+ const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
423+{
424+ if (!m_workspace || workspace != m_workspace->workspace()) return;
425+
426+ int start = -1;
427+ int end = -1;
428+ for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
429+ auto surface = *iter;
430+ iter++;
431+
432+ // Do removals in adjacent blocks.
433+ start = end = indexOf(surface);
434+ if (start == -1) {
435+ // probably a child surface
436+ m_allSurfaces.remove(surface);
437+ continue;
438+ }
439+ while(iter != surfaces.constEnd()) {
440+ int index = indexOf(*iter);
441+ if (index != end+1) {
442+ break;
443+ }
444+ end++;
445+ iter++;
446+ }
447+
448+ if (m_modelState == IdleState) {
449+ beginRemoveRows(QModelIndex(), start, end);
450+ m_modelState = RemovingState;
451+ } else {
452+ Q_ASSERT(m_modelState == ResettingState);
453+ // No point in signaling anything if we're resetting the whole model
454+ }
455+
456+ for (int index = start; index <= end; index++) {
457+ auto window = m_windowModel[start].window;
458+ window->setSurface(nullptr);
459+ window->setFocused(false);
460+
461+ m_windowModel.removeAt(start);
462+ m_allSurfaces.remove(surface);
463+ }
464+
465+ if (m_modelState == RemovingState) {
466+ endRemoveRows();
467+ Q_EMIT countChanged();
468+ Q_EMIT listChanged();
469+ m_modelState = IdleState;
470+ }
471+ }
472+}
473+
363474void TopLevelWindowModel::removeAt(int index)
364475{
365476 if (m_modelState == IdleState) {
@@ -371,11 +482,13 @@ void TopLevelWindowModel::removeAt(int index)
371482 }
372483
373484 auto window = m_windowModel[index].window;
485+ auto surface = window->surface();
374486
375487 window->setSurface(nullptr);
376488 window->setFocused(false);
377489
378490 m_windowModel.removeAt(index);
491+ m_allSurfaces.remove(surface);
379492
380493 if (m_modelState == RemovingState) {
381494 endRemoveRows();
@@ -388,6 +501,10 @@ void TopLevelWindowModel::removeAt(int index)
388501 if (m_focusedWindow == window) {
389502 setFocusedWindow(nullptr);
390503 }
504+ if (m_newlyFocusedWindow == window) {
505+ m_focusedWindowChanged = false;
506+ m_newlyFocusedWindow = nullptr;
507+ }
391508 delete window;
392509
393510 INFO_MSG << " after " << toString();
@@ -674,3 +791,55 @@ void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
674791 }
675792 }
676793}
794+
795+void TopLevelWindowModel::refreshWindows()
796+{
797+ DEBUG_MSG << "()";
798+ clear();
799+
800+ if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
801+
802+ m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](unity::shell::application::MirSurfaceInterface* surface) {
803+ if (surface->parentSurface()) {
804+ // Wrap it in a Window so that we keep focusedWindow() up to date.
805+ Window *window = createWindow(surface);
806+ connect(surface, &QObject::destroyed, window, [=](){
807+ window->setSurface(nullptr);
808+ window->deleteLater();
809+ });
810+ } else {
811+ if (surface->type() == Mir::InputMethodType) {
812+ setInputMethodWindow(createWindow(surface));
813+ } else {
814+ auto *application = m_applicationManager->findApplicationWithSurface(surface);
815+ if (application) {
816+ prependSurface(surface, application);
817+ } else {
818+ // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
819+ // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
820+ // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
821+ Window *promptWindow = createWindow(surface);
822+ connect(surface, &QObject::destroyed, promptWindow, [=](){
823+ promptWindow->setSurface(nullptr);
824+ promptWindow->deleteLater();
825+ });
826+ }
827+ }
828+ }
829+ });
830+}
831+
832+void TopLevelWindowModel::clear()
833+{
834+ DEBUG_MSG << "()";
835+
836+ while(m_windowModel.count() > 0) {
837+ ModelEntry entry = m_windowModel.takeAt(0);
838+ disconnect(entry.window, 0, this, 0);
839+ delete entry.window;
840+ }
841+ m_allSurfaces.clear();
842+ setFocusedWindow(nullptr);
843+ m_focusedWindowChanged = false;
844+ m_newlyFocusedWindow = nullptr;
845+}