on 59e31a6 help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.cpp
817 lines 26 kB history raw
1/*
2 * Copyright (C) 2016 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "TopLevelWindowModel.h"
18#include "WindowManagerObjects.h"
20// unity-api
21#include <unity/shell/application/ApplicationInfoInterface.h>
22#include <unity/shell/application/ApplicationManagerInterface.h>
23#include <unity/shell/application/MirSurfaceInterface.h>
24#include <unity/shell/application/MirSurfaceListInterface.h>
25#include <unity/shell/application/SurfaceManagerInterface.h>
27// Qt
28#include <QDebug>
30// local
31#include "Window.h"
32#include "Workspace.h"
34Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
36#define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
37#define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
39namespace unityapi = unity::shell::application;
41TopLevelWindowModel::TopLevelWindowModel(Workspace* workspace)
42 : m_workspace(workspace)
44 connect(WindowManagerObjects::instance(), &WindowManagerObjects::applicationManagerChanged,
45 this, &TopLevelWindowModel::setApplicationManager);
46 connect(WindowManagerObjects::instance(), &WindowManagerObjects::surfaceManagerChanged,
47 this, &TopLevelWindowModel::setSurfaceManager);
49 setApplicationManager(WindowManagerObjects::instance()->applicationManager());
50 setSurfaceManager(WindowManagerObjects::instance()->surfaceManager());
53void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInterface* value)
55 if (m_applicationManager == value) {
56 return;
57 }
59 DEBUG_MSG << "(" << value << ")";
61 Q_ASSERT(m_modelState == IdleState);
62 m_modelState = ResettingState;
64 beginResetModel();
66 if (m_applicationManager) {
67 disconnect(m_applicationManager, 0, this, 0);
68 }
70 m_applicationManager = value;
72 if (m_applicationManager) {
73 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
74 this, [this](const QModelIndex &/*parent*/, int first, int last) {
75 if (!m_workspace || !m_workspace->isActive())
76 return;
78 for (int i = first; i <= last; ++i) {
79 auto application = m_applicationManager->get(i);
80 addApplication(application);
81 }
82 });
84 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
85 this, [this](const QModelIndex &/*parent*/, int first, int last) {
86 for (int i = first; i <= last; ++i) {
87 auto application = m_applicationManager->get(i);
88 removeApplication(application);
89 }
90 });
91 }
93 refreshWindows();
95 endResetModel();
96 m_modelState = IdleState;
99void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
101 if (surfaceManager == m_surfaceManager) {
102 return;
103 }
105 DEBUG_MSG << "(" << surfaceManager << ")";
107 Q_ASSERT(m_modelState == IdleState);
108 m_modelState = ResettingState;
110 beginResetModel();
112 if (m_surfaceManager) {
113 disconnect(m_surfaceManager, 0, this, 0);
114 }
116 m_surfaceManager = surfaceManager;
118 if (m_surfaceManager) {
119 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
120 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
121 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
122 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
124 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
125 this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
126 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
127 this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
128 }
130 refreshWindows();
132 endResetModel();
133 m_modelState = IdleState;
136void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
138 DEBUG_MSG << "(" << application->appId() << ")";
140 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
141 prependPlaceholder(application);
142 }
145void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
147 DEBUG_MSG << "(" << application->appId() << ")";
149 Q_ASSERT(m_modelState == IdleState);
151 int i = 0;
152 while (i < m_windowModel.count()) {
153 if (m_windowModel.at(i).application == application) {
154 removeAt(i);
155 } else {
156 ++i;
157 }
158 }
161void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
163 INFO_MSG << "(" << application->appId() << ")";
165 prependSurfaceHelper(nullptr, application);
168void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
170 Q_ASSERT(surface != nullptr);
172 bool filledPlaceholder = false;
173 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
174 ModelEntry &entry = m_windowModel[i];
175 if (entry.application == application && entry.window->surface() == nullptr) {
176 entry.window->setSurface(surface);
177 m_allSurfaces.insert(surface);
178 connectSurface(surface);
179 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
180 << ", filling out placeholder. after: " << toString();
181 filledPlaceholder = true;
182 }
183 }
185 if (!filledPlaceholder) {
186 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
187 prependSurfaceHelper(surface, application);
188 }
191void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
193 if (m_modelState == IdleState) {
194 m_modelState = InsertingState;
195 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
196 } else {
197 Q_ASSERT(m_modelState == ResettingState);
198 // No point in signaling anything if we're resetting the whole model
199 }
201 Window *window = createWindow(surface);
203 m_windowModel.prepend(ModelEntry(window, application));
204 if (surface) {
205 m_allSurfaces.insert(surface);
206 connectSurface(surface);
207 }
209 if (m_modelState == InsertingState) {
210 endInsertRows();
211 Q_EMIT countChanged();
212 Q_EMIT listChanged();
213 m_modelState = IdleState;
214 }
216 if (!surface) {
217 activateEmptyWindow(window);
218 }
220 INFO_MSG << " after " << toString();
223void TopLevelWindowModel::connectWindow(Window *window)
225 connect(window, &Window::focusRequested, this, [this, window]() {
226 if (!window->surface()) {
227 activateEmptyWindow(window);
228 }
229 });
231 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
232 if (window->surface()) {
233 if (focused) {
234 setFocusedWindow(window);
235 m_focusedWindowCleared = false;
236 } else if (m_focusedWindow == window) {
237 // Condense changes to the focused window
238 // eg: Do focusedWindow=A to focusedWindow=B instead of
239 // focusedWindow=A to focusedWindow=null to focusedWindow=B
240 m_focusedWindowCleared = true;
241 } else {
242 // don't clear the focused window if you were not there in the first place
243 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
244 }
245 }
246 });
248 connect(window, &Window::closeRequested, this, [this, window]() {
249 if (!window->surface()) {
250 // do things ourselves as miral doesn't know about this window
251 int id = window->id();
252 int index = indexForId(id);
253 bool focusOther = false;
254 Q_ASSERT(index >= 0);
255 if (window->focused()) {
256 focusOther = true;
257 }
258 m_windowModel[index].application->close();
259 if (focusOther) {
260 activateTopMostWindowWithoutId(id);
261 }
262 }
263 });
265 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
266 activateEmptyWindow(window);
267 });
270void TopLevelWindowModel::activateEmptyWindow(Window *window)
272 Q_ASSERT(!window->surface());
273 DEBUG_MSG << "(" << window << ")";
275 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
276 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
278 window->setFocused(true);
279 raiseId(window->id());
280 Window *previousWindow = m_focusedWindow;
281 setFocusedWindow(window);
282 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
283 m_surfaceManager->activate(nullptr);
284 }
287void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
289 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
290 if (!live) {
291 onSurfaceDied(surface);
292 }
293 });
294 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
297void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
299 if (surface->type() == Mir::InputMethodType) {
300 removeInputMethodWindow();
301 return;
302 }
304 int i = indexOf(surface);
305 if (i == -1) {
306 return;
307 }
309 auto application = m_windowModel[i].application;
311 // can't be starting if it already has a surface
312 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
314 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
315 m_windowModel[i].removeOnceSurfaceDestroyed = true;
316 } else {
317 // assume it got killed by the out-of-memory daemon.
318 //
319 // So leave entry in the model and only remove its surface, so shell can display a screenshot
320 // in its place.
321 m_windowModel[i].removeOnceSurfaceDestroyed = false;
322 }
325void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
327 int i = indexOf(surface);
328 if (i == -1) {
329 return;
330 }
332 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
333 removeAt(i);
334 } else {
335 auto window = m_windowModel[i].window;
336 window->setSurface(nullptr);
337 window->setFocused(false);
338 m_allSurfaces.remove(surface);
339 INFO_MSG << " Removed surface from entry. After: " << toString();
340 }
343Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
345 int id = generateId();
346 Window *qmlWindow = new Window(id, this);
347 connectWindow(qmlWindow);
348 if (surface) {
349 qmlWindow->setSurface(surface);
350 }
351 return qmlWindow;
355void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
359void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
360 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
362 if (!m_workspace || !m_applicationManager) return;
363 if (workspace != m_workspace->workspace()) return;
365 Q_FOREACH(auto surface, surfaces) {
366 if (m_allSurfaces.contains(surface)) continue;
368 if (surface->parentSurface()) {
369 // Wrap it in a Window so that we keep focusedWindow() up to date.
370 Window *window = createWindow(surface);
371 connect(surface, &QObject::destroyed, window, [=](){
372 window->setSurface(nullptr);
373 window->deleteLater();
374 });
375 } else {
376 if (surface->type() == Mir::InputMethodType) {
377 connectSurface(surface);
378 setInputMethodWindow(createWindow(surface));
379 } else {
380 auto *application = m_applicationManager->findApplicationWithSurface(surface);
381 if (application) {
382 prependSurface(surface, application);
383 } else {
384 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
385 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
386 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
387 Window *promptWindow = createWindow(surface);
388 connect(surface, &QObject::destroyed, promptWindow, [=](){
389 promptWindow->setSurface(nullptr);
390 promptWindow->deleteLater();
391 });
392 }
393 }
394 }
395 }
398void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
399 const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
401 if (!m_workspace || workspace != m_workspace->workspace()) return;
403 int start = -1;
404 int end = -1;
405 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
406 auto surface = *iter;
407 iter++;
409 // Do removals in adjacent blocks.
410 start = end = indexOf(surface);
411 if (start == -1) {
412 // probably a child surface
413 m_allSurfaces.remove(surface);
414 continue;
415 }
416 while(iter != surfaces.constEnd()) {
417 int index = indexOf(*iter);
418 if (index != end+1) {
419 break;
420 }
421 end++;
422 iter++;
423 }
425 if (m_modelState == IdleState) {
426 beginRemoveRows(QModelIndex(), start, end);
427 m_modelState = RemovingState;
428 } else {
429 Q_ASSERT(m_modelState == ResettingState);
430 // No point in signaling anything if we're resetting the whole model
431 }
433 for (int index = start; index <= end; index++) {
434 auto window = m_windowModel[start].window;
435 window->setSurface(nullptr);
436 window->setFocused(false);
438 m_windowModel.removeAt(start);
439 m_allSurfaces.remove(surface);
440 }
442 if (m_modelState == RemovingState) {
443 endRemoveRows();
444 Q_EMIT countChanged();
445 Q_EMIT listChanged();
446 m_modelState = IdleState;
447 }
448 }
451void TopLevelWindowModel::removeAt(int index)
453 if (m_modelState == IdleState) {
454 beginRemoveRows(QModelIndex(), index, index);
455 m_modelState = RemovingState;
456 } else {
457 Q_ASSERT(m_modelState == ResettingState);
458 // No point in signaling anything if we're resetting the whole model
459 }
461 auto window = m_windowModel[index].window;
462 auto surface = window->surface();
464 window->setSurface(nullptr);
465 window->setFocused(false);
467 m_windowModel.removeAt(index);
468 m_allSurfaces.remove(surface);
470 if (m_modelState == RemovingState) {
471 endRemoveRows();
472 Q_EMIT countChanged();
473 Q_EMIT listChanged();
474 m_modelState = IdleState;
475 }
477 disconnect(window, 0, this, 0);
478 if (m_focusedWindow == window) {
479 setFocusedWindow(nullptr);
480 m_focusedWindowCleared = false;
481 }
482 delete window;
484 INFO_MSG << " after " << toString();
487void TopLevelWindowModel::setInputMethodWindow(Window *window)
489 if (m_inputMethodWindow) {
490 qWarning("Multiple Input Method Surfaces created, removing the old one!");
491 delete m_inputMethodWindow;
492 }
493 m_inputMethodWindow = window;
494 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
497void TopLevelWindowModel::removeInputMethodWindow()
499 if (m_inputMethodWindow) {
500 delete m_inputMethodWindow;
501 m_inputMethodWindow = nullptr;
502 Q_EMIT inputMethodSurfaceChanged(nullptr);
503 }
506void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
508 DEBUG_MSG << "(" << surfaces << ")";
509 const int raiseCount = surfaces.size();
510 for (int i = 0; i < raiseCount; i++) {
511 int fromIndex = findIndexOf(surfaces[i]);
512 if (fromIndex != -1) {
513 move(fromIndex, 0);
514 }
515 }
518int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
520 return m_windowModel.count();
523QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
525 if (index.row() < 0 || index.row() >= m_windowModel.size())
526 return QVariant();
528 if (role == WindowRole) {
529 Window *window = m_windowModel.at(index.row()).window;
530 return QVariant::fromValue(window);
531 } else if (role == ApplicationRole) {
532 return QVariant::fromValue(m_windowModel.at(index.row()).application);
533 } else {
534 return QVariant();
535 }
538int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
540 for (int i=0; i<m_windowModel.count(); i++) {
541 if (m_windowModel[i].window->surface() == surface) {
542 return i;
543 }
544 }
545 return -1;
548int TopLevelWindowModel::generateId()
550 int id = m_nextId;
551 m_nextId = nextFreeId(nextId(id), id);
552 return id;
555int TopLevelWindowModel::nextId(int id) const
557 if (id == m_maxId) {
558 return id = 1;
559 } else {
560 return id + 1;
561 }
564int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
566 int firstCandidateId = candidateId;
568 while (indexForId(candidateId) != -1 || candidateId == latestId) {
569 candidateId = nextId(candidateId);
571 if (candidateId == firstCandidateId) {
572 qFatal("TopLevelWindowModel: run out of window ids.");
573 }
574 }
576 return candidateId;
579QString TopLevelWindowModel::toString()
581 QString str;
582 for (int i = 0; i < m_windowModel.count(); ++i) {
583 auto item = m_windowModel.at(i);
585 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
586 .arg(QString::number(i),
587 item.application->appId(),
588 QString::number((qintptr)item.window->surface(), 16),
589 QString::number(item.window->id()));
591 if (i > 0) {
592 str.append(",");
593 }
594 str.append(itemStr);
595 }
596 return str;
599int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
601 for (int i = 0; i < m_windowModel.count(); ++i) {
602 if (m_windowModel.at(i).window->surface() == surface) {
603 return i;
604 }
605 }
606 return -1;
609int TopLevelWindowModel::indexForId(int id) const
611 for (int i = 0; i < m_windowModel.count(); ++i) {
612 if (m_windowModel[i].window->id() == id) {
613 return i;
614 }
615 }
616 return -1;
619Window *TopLevelWindowModel::windowAt(int index) const
621 if (index >=0 && index < m_windowModel.count()) {
622 return m_windowModel[index].window;
623 } else {
624 return nullptr;
625 }
628unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
630 if (index >=0 && index < m_windowModel.count()) {
631 return m_windowModel[index].window->surface();
632 } else {
633 return nullptr;
634 }
637unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
639 if (index >=0 && index < m_windowModel.count()) {
640 return m_windowModel[index].application;
641 } else {
642 return nullptr;
643 }
646int TopLevelWindowModel::idAt(int index) const
648 if (index >=0 && index < m_windowModel.count()) {
649 return m_windowModel[index].window->id();
650 } else {
651 return 0;
652 }
655void TopLevelWindowModel::raiseId(int id)
657 if (m_modelState == IdleState) {
658 DEBUG_MSG << "(id=" << id << ") - do it now.";
659 doRaiseId(id);
660 } else {
661 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
662 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
663 // if we perform yet another model change straight away.
664 //
665 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
666 // the index is definitely within bounds.
667 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
668 }
671void TopLevelWindowModel::doRaiseId(int id)
673 int fromIndex = indexForId(id);
674 // can't raise something that doesn't exist or that it's already on top
675 if (fromIndex != -1 && fromIndex != 0) {
676 auto surface = m_windowModel[fromIndex].window->surface();
677 if (surface) {
678 m_surfaceManager->raise(surface);
679 } else {
680 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
681 // miral can do about it.
682 move(fromIndex, 0);
683 }
684 }
687void TopLevelWindowModel::setFocusedWindow(Window *window)
689 if (window != m_focusedWindow) {
690 INFO_MSG << "(" << window << ")";
692 Window* previousWindow = m_focusedWindow;
694 m_focusedWindow = window;
695 Q_EMIT focusedWindowChanged(m_focusedWindow);
697 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
698 // do it ourselves. miral doesn't know about this window
699 previousWindow->setFocused(false);
700 }
701 }
704unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
706 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
709Window* TopLevelWindowModel::focusedWindow() const
711 return m_focusedWindow;
714void TopLevelWindowModel::move(int from, int to)
716 if (from == to) return;
717 DEBUG_MSG << " from=" << from << " to=" << to;
719 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
720 QModelIndex parent;
721 /* When moving an item down, the destination index needs to be incremented
722 by one, as explained in the documentation:
723 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
725 Q_ASSERT(m_modelState == IdleState);
726 m_modelState = MovingState;
728 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
729#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
730 const auto &window = m_windowModel.takeAt(from);
731 m_windowModel.insert(to, window);
732#else
733 m_windowModel.move(from, to);
734#endif
735 endMoveRows();
737 Q_EMIT listChanged();
738 m_modelState = IdleState;
740 INFO_MSG << " after " << toString();
741 }
743void TopLevelWindowModel::onModificationsStarted()
747void TopLevelWindowModel::onModificationsEnded()
749 if (m_focusedWindowCleared) {
750 setFocusedWindow(nullptr);
751 }
752 // reset
753 m_focusedWindowCleared = false;
756void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
758 DEBUG_MSG << "(" << forbiddenId << ")";
760 for (int i = 0; i < m_windowModel.count(); ++i) {
761 Window *window = m_windowModel[i].window;
762 if (window->id() != forbiddenId) {
763 window->activate();
764 }
765 }
768void TopLevelWindowModel::refreshWindows()
770 DEBUG_MSG << "()";
771 clear();
773 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
775 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](unity::shell::application::MirSurfaceInterface* surface) {
776 if (surface->parentSurface()) {
777 // Wrap it in a Window so that we keep focusedWindow() up to date.
778 Window *window = createWindow(surface);
779 connect(surface, &QObject::destroyed, window, [=](){
780 window->setSurface(nullptr);
781 window->deleteLater();
782 });
783 } else {
784 if (surface->type() == Mir::InputMethodType) {
785 setInputMethodWindow(createWindow(surface));
786 } else {
787 auto *application = m_applicationManager->findApplicationWithSurface(surface);
788 if (application) {
789 prependSurface(surface, application);
790 } else {
791 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
792 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
793 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
794 Window *promptWindow = createWindow(surface);
795 connect(surface, &QObject::destroyed, promptWindow, [=](){
796 promptWindow->setSurface(nullptr);
797 promptWindow->deleteLater();
798 });
799 }
800 }
801 }
802 });
805void TopLevelWindowModel::clear()
807 DEBUG_MSG << "()";
809 while(m_windowModel.count() > 0) {
810 ModelEntry entry = m_windowModel.takeAt(0);
811 disconnect(entry.window, 0, this, 0);
812 delete entry.window;
813 }
814 m_allSurfaces.clear();
815 setFocusedWindow(nullptr);
816 m_focusedWindowCleared = false;