@@ -35,7 +35,7 @@
3535#include <qtmir/mirserverapplication.h>
3636#include <qtmir/windowmodelnotifier.h>
3737
38−Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel")
38+Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
3939
4040#define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
4141#define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
@@ -109,6 +109,8 @@ void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *s
109109
110110 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
111111 this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
112+ connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
113+ this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
112114
113115 if (m_workspace && m_applicationManager) {
114116 refreshWindows();
@@ -431,6 +433,59 @@ void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral
431433 }
432434}
433435
436+void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
437+ const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
438+{
439+ if (workspace != m_workspace->workspace()) return;
440+
441+ int start = -1;
442+ int end = -1;
443+ for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
444+ auto surface = *iter;
445+ iter++;
446+
447+ // Do removals in adjacent blocks.
448+ start = end = indexOf(surface);
449+ if (start == -1) {
450+ // probably a child surface
451+ m_allSurfaces.remove(surface);
452+ continue;
453+ }
454+ while(iter != surfaces.constEnd()) {
455+ int index = indexOf(*iter);
456+ if (index != end+1) {
457+ break;
458+ }
459+ end++;
460+ iter++;
461+ }
462+
463+ if (m_modelState == IdleState) {
464+ beginRemoveRows(QModelIndex(), start, end);
465+ m_modelState = RemovingState;
466+ } else {
467+ Q_ASSERT(m_modelState == ResettingState);
468+ // No point in signaling anything if we're resetting the whole model
469+ }
470+
471+ for (int index = start; index <= end; index++) {
472+ auto window = m_windowModel[start].window;
473+ window->setSurface(nullptr);
474+ window->setFocused(false);
475+
476+ m_windowModel.removeAt(start);
477+ m_allSurfaces.remove(window->surface());
478+ }
479+
480+ if (m_modelState == RemovingState) {
481+ endRemoveRows();
482+ Q_EMIT countChanged();
483+ Q_EMIT listChanged();
484+ m_modelState = IdleState;
485+ }
486+ }
487+}
488+
434489void TopLevelWindowModel::removeAt(int index)
435490{
436491 if (m_modelState == IdleState) {