@@ -15,6 +15,7 @@
1515 */
1616
1717#include "TopLevelWindowModel.h"
18+#include "WindowManagerObjects.h"
1819
1920// unity-api
2021#include <unity/shell/application/ApplicationInfoInterface.h>
@@ -24,11 +25,16 @@
2425#include <unity/shell/application/SurfaceManagerInterface.h>
2526
2627// Qt
27−#include <QGuiApplication>
2828#include <QDebug>
2929
3030// local
3131#include "Window.h"
32+#include "Workspace.h"
33+#include "wmpolicyinterface.h"
34+
35+// qtmir
36+#include <qtmir/mirserverapplication.h>
37+#include <qtmir/windowmodelnotifier.h>
3238
3339Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
3440
@@ -37,8 +43,16 @@ Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
3743
3844namespace unityapi = unity::shell::application;
3945
40−TopLevelWindowModel::TopLevelWindowModel()
46+TopLevelWindowModel::TopLevelWindowModel(Workspace* workspace)
47+ : m_workspace(workspace)
4148{
49+ connect(WindowManagerObjects::instance(), &WindowManagerObjects::applicationManagerChanged,
50+ this, &TopLevelWindowModel::setApplicationManager);
51+ connect(WindowManagerObjects::instance(), &WindowManagerObjects::surfaceManagerChanged,
52+ this, &TopLevelWindowModel::setSurfaceManager);
53+
54+ setApplicationManager(WindowManagerObjects::instance()->applicationManager());
55+ setSurfaceManager(WindowManagerObjects::instance()->surfaceManager());
4256}
4357
4458void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInterface* value)
@@ -55,7 +69,6 @@ void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInte
5569 beginResetModel();
5670
5771 if (m_applicationManager) {
58− m_windowModel.clear();
5972 disconnect(m_applicationManager, 0, this, 0);
6073 }
6174
@@ -64,26 +77,26 @@ void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInte
6477 if (m_applicationManager) {
6578 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
6679 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− });
80+ if (!m_workspace || !m_workspace->isActive())
81+ return;
82+
83+ for (int i = first; i <= last; ++i) {
84+ auto application = m_applicationManager->get(i);
85+ addApplication(application);
86+ }
87+ });
7288
7389 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
7490 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− }
91+ for (int i = first; i <= last; ++i) {
92+ auto application = m_applicationManager->get(i);
93+ removeApplication(application);
94+ }
95+ });
8596 }
8697
98+ refreshWindows();
99+
87100 endResetModel();
88101 m_modelState = IdleState;
89102}
@@ -96,6 +109,11 @@ void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *s
96109
97110 DEBUG_MSG << "(" << surfaceManager << ")";
98111
112+ Q_ASSERT(m_modelState == IdleState);
113+ m_modelState = ResettingState;
114+
115+ beginResetModel();
116+
99117 if (m_surfaceManager) {
100118 disconnect(m_surfaceManager, 0, this, 0);
101119 }
@@ -107,9 +125,17 @@ void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *s
107125 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
108126 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
109127 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
128+
129+ connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
130+ this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
131+ connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
132+ this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
110133 }
111134
112− Q_EMIT surfaceManagerChanged(m_surfaceManager);
135+ refreshWindows();
136+
137+ endResetModel();
138+ m_modelState = IdleState;
113139}
114140
115141void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
@@ -153,6 +179,7 @@ void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface,
153179 ModelEntry &entry = m_windowModel[i];
154180 if (entry.application == application && entry.window->surface() == nullptr) {
155181 entry.window->setSurface(surface);
182+ m_allSurfaces.insert(surface);
156183 connectSurface(surface);
157184 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
158185 << ", filling out placeholder. after: " << toString();
@@ -180,6 +207,7 @@ void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *su
180207
181208 m_windowModel.prepend(ModelEntry(window, application));
182209 if (surface) {
210+ m_allSurfaces.insert(surface);
183211 connectSurface(surface);
184212 }
185213
@@ -207,15 +235,14 @@ void TopLevelWindowModel::connectWindow(Window *window)
207235
208236 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
209237 if (window->surface()) {
210− // Condense changes to the focused window
211− // eg: Do focusedWindow=A to focusedWindow=B instead of
212− // focusedWindow=A to focusedWindow=null to focusedWindow=B
213238 if (focused) {
214− Q_ASSERT(m_newlyFocusedWindow == nullptr);
215− m_focusedWindowChanged = true;
216− m_newlyFocusedWindow = window;
239+ setFocusedWindow(window);
240+ m_focusedWindowCleared = false;
217241 } else if (m_focusedWindow == window) {
218− m_focusedWindowChanged = true;
242+ // Condense changes to the focused window
243+ // eg: Do focusedWindow=A to focusedWindow=B instead of
244+ // focusedWindow=A to focusedWindow=null to focusedWindow=B
245+ m_focusedWindowCleared = true;
219246 } else {
220247 // don't clear the focused window if you were not there in the first place
221248 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
@@ -313,6 +340,7 @@ void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surf
313340 auto window = m_windowModel[i].window;
314341 window->setSurface(nullptr);
315342 window->setFocused(false);
343+ m_allSurfaces.remove(surface);
316344 INFO_MSG << " Removed surface from entry. After: " << toString();
317345 }
318346}
@@ -328,39 +356,103 @@ Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface
328356 return qmlWindow;
329357}
330358
331−void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface *surface)
359+
360+void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
332361{
333− DEBUG_MSG << "(" << surface << ")";
362+}
334363
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− connectSurface(surface);
345− setInputMethodWindow(createWindow(surface));
364+void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
365+ const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
366+{
367+ if (!m_workspace || !m_applicationManager) return;
368+ if (workspace != m_workspace->workspace()) return;
369+
370+ Q_FOREACH(auto surface, surfaces) {
371+ if (m_allSurfaces.contains(surface)) continue;
372+
373+ if (surface->parentSurface()) {
374+ // Wrap it in a Window so that we keep focusedWindow() up to date.
375+ Window *window = createWindow(surface);
376+ connect(surface, &QObject::destroyed, window, [=](){
377+ window->setSurface(nullptr);
378+ window->deleteLater();
379+ });
346380 } else {
347− auto *application = m_applicationManager->findApplicationWithSurface(surface);
348− if (application) {
349− prependSurface(surface, application);
381+ if (surface->type() == Mir::InputMethodType) {
382+ connectSurface(surface);
383+ setInputMethodWindow(createWindow(surface));
350384 } else {
351− // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
352− // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
353− // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
354− Window *promptWindow = createWindow(surface);
355− connect(surface, &QObject::destroyed, promptWindow, [=](){
356− promptWindow->setSurface(nullptr);
357− promptWindow->deleteLater();
358− });
385+ auto *application = m_applicationManager->findApplicationWithSurface(surface);
386+ if (application) {
387+ prependSurface(surface, application);
388+ } else {
389+ // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
390+ // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
391+ // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
392+ Window *promptWindow = createWindow(surface);
393+ connect(surface, &QObject::destroyed, promptWindow, [=](){
394+ promptWindow->setSurface(nullptr);
395+ promptWindow->deleteLater();
396+ });
397+ }
359398 }
360399 }
361400 }
362401}
363402
403+void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
404+ const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
405+{
406+ if (!m_workspace || workspace != m_workspace->workspace()) return;
407+
408+ int start = -1;
409+ int end = -1;
410+ for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
411+ auto surface = *iter;
412+ iter++;
413+
414+ // Do removals in adjacent blocks.
415+ start = end = indexOf(surface);
416+ if (start == -1) {
417+ // probably a child surface
418+ m_allSurfaces.remove(surface);
419+ continue;
420+ }
421+ while(iter != surfaces.constEnd()) {
422+ int index = indexOf(*iter);
423+ if (index != end+1) {
424+ break;
425+ }
426+ end++;
427+ iter++;
428+ }
429+
430+ if (m_modelState == IdleState) {
431+ beginRemoveRows(QModelIndex(), start, end);
432+ m_modelState = RemovingState;
433+ } else {
434+ Q_ASSERT(m_modelState == ResettingState);
435+ // No point in signaling anything if we're resetting the whole model
436+ }
437+
438+ for (int index = start; index <= end; index++) {
439+ auto window = m_windowModel[start].window;
440+ window->setSurface(nullptr);
441+ window->setFocused(false);
442+
443+ m_windowModel.removeAt(start);
444+ m_allSurfaces.remove(surface);
445+ }
446+
447+ if (m_modelState == RemovingState) {
448+ endRemoveRows();
449+ Q_EMIT countChanged();
450+ Q_EMIT listChanged();
451+ m_modelState = IdleState;
452+ }
453+ }
454+}
455+
364456void TopLevelWindowModel::removeAt(int index)
365457{
366458 if (m_modelState == IdleState) {
@@ -372,11 +464,13 @@ void TopLevelWindowModel::removeAt(int index)
372464 }
373465
374466 auto window = m_windowModel[index].window;
467+ auto surface = window->surface();
375468
376469 window->setSurface(nullptr);
377470 window->setFocused(false);
378471
379472 m_windowModel.removeAt(index);
473+ m_allSurfaces.remove(surface);
380474
381475 if (m_modelState == RemovingState) {
382476 endRemoveRows();
@@ -388,6 +482,7 @@ void TopLevelWindowModel::removeAt(int index)
388482 disconnect(window, 0, this, 0);
389483 if (m_focusedWindow == window) {
390484 setFocusedWindow(nullptr);
485+ m_focusedWindowCleared = false;
391486 }
392487 delete window;
393488
@@ -656,12 +751,11 @@ void TopLevelWindowModel::onModificationsStarted()
656751
657752void TopLevelWindowModel::onModificationsEnded()
658753{
659− if (m_focusedWindowChanged) {
660− setFocusedWindow(m_newlyFocusedWindow);
754+ if (m_focusedWindowCleared) {
755+ setFocusedWindow(nullptr);
661756 }
662757 // reset
663− m_focusedWindowChanged = false;
664− m_newlyFocusedWindow = nullptr;
758+ m_focusedWindowCleared = false;
665759}
666760
667761void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
@@ -675,3 +769,54 @@ void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
675769 }
676770 }
677771}
772+
773+void TopLevelWindowModel::refreshWindows()
774+{
775+ DEBUG_MSG << "()";
776+ clear();
777+
778+ if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
779+
780+ m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](unity::shell::application::MirSurfaceInterface* surface) {
781+ if (surface->parentSurface()) {
782+ // Wrap it in a Window so that we keep focusedWindow() up to date.
783+ Window *window = createWindow(surface);
784+ connect(surface, &QObject::destroyed, window, [=](){
785+ window->setSurface(nullptr);
786+ window->deleteLater();
787+ });
788+ } else {
789+ if (surface->type() == Mir::InputMethodType) {
790+ setInputMethodWindow(createWindow(surface));
791+ } else {
792+ auto *application = m_applicationManager->findApplicationWithSurface(surface);
793+ if (application) {
794+ prependSurface(surface, application);
795+ } else {
796+ // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
797+ // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
798+ // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
799+ Window *promptWindow = createWindow(surface);
800+ connect(surface, &QObject::destroyed, promptWindow, [=](){
801+ promptWindow->setSurface(nullptr);
802+ promptWindow->deleteLater();
803+ });
804+ }
805+ }
806+ }
807+ });
808+}
809+
810+void TopLevelWindowModel::clear()
811+{
812+ DEBUG_MSG << "()";
813+
814+ while(m_windowModel.count() > 0) {
815+ ModelEntry entry = m_windowModel.takeAt(0);
816+ disconnect(entry.window, 0, this, 0);
817+ delete entry.window;
818+ }
819+ m_allSurfaces.clear();
820+ setFocusedWindow(nullptr);
821+ m_focusedWindowCleared = false;
822+}