on 80fc88d help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.cpp
821 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());
53TopLevelWindowModel::~TopLevelWindowModel()
57void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInterface* value)
59 if (m_applicationManager == value) {
60 return;
61 }
63 DEBUG_MSG << "(" << value << ")";
65 Q_ASSERT(m_modelState == IdleState);
66 m_modelState = ResettingState;
68 beginResetModel();
70 if (m_applicationManager) {
71 disconnect(m_applicationManager, 0, this, 0);
72 }
74 m_applicationManager = value;
76 if (m_applicationManager) {
77 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
78 this, [this](const QModelIndex &/*parent*/, int first, int last) {
79 if (!m_workspace || !m_workspace->isActive())
80 return;
82 for (int i = first; i <= last; ++i) {
83 auto application = m_applicationManager->get(i);
84 addApplication(application);
85 }
86 });
88 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
89 this, [this](const QModelIndex &/*parent*/, int first, int last) {
90 for (int i = first; i <= last; ++i) {
91 auto application = m_applicationManager->get(i);
92 removeApplication(application);
93 }
94 });
95 }
97 refreshWindows();
99 endResetModel();
100 m_modelState = IdleState;
103void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
105 if (surfaceManager == m_surfaceManager) {
106 return;
107 }
109 DEBUG_MSG << "(" << surfaceManager << ")";
111 Q_ASSERT(m_modelState == IdleState);
112 m_modelState = ResettingState;
114 beginResetModel();
116 if (m_surfaceManager) {
117 disconnect(m_surfaceManager, 0, this, 0);
118 }
120 m_surfaceManager = surfaceManager;
122 if (m_surfaceManager) {
123 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
124 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
125 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
126 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
128 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
129 this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
130 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
131 this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
132 }
134 refreshWindows();
136 endResetModel();
137 m_modelState = IdleState;
140void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
142 DEBUG_MSG << "(" << application->appId() << ")";
144 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
145 prependPlaceholder(application);
146 }
149void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
151 DEBUG_MSG << "(" << application->appId() << ")";
153 Q_ASSERT(m_modelState == IdleState);
155 int i = 0;
156 while (i < m_windowModel.count()) {
157 if (m_windowModel.at(i).application == application) {
158 removeAt(i);
159 } else {
160 ++i;
161 }
162 }
165void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
167 INFO_MSG << "(" << application->appId() << ")";
169 prependSurfaceHelper(nullptr, application);
172void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
174 Q_ASSERT(surface != nullptr);
176 bool filledPlaceholder = false;
177 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
178 ModelEntry &entry = m_windowModel[i];
179 if (entry.application == application && entry.window->surface() == nullptr) {
180 entry.window->setSurface(surface);
181 m_allSurfaces.insert(surface);
182 connectSurface(surface);
183 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
184 << ", filling out placeholder. after: " << toString();
185 filledPlaceholder = true;
186 }
187 }
189 if (!filledPlaceholder) {
190 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
191 prependSurfaceHelper(surface, application);
192 }
195void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
197 if (m_modelState == IdleState) {
198 m_modelState = InsertingState;
199 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
200 } else {
201 Q_ASSERT(m_modelState == ResettingState);
202 // No point in signaling anything if we're resetting the whole model
203 }
205 Window *window = createWindow(surface);
207 m_windowModel.prepend(ModelEntry(window, application));
208 if (surface) {
209 m_allSurfaces.insert(surface);
210 connectSurface(surface);
211 }
213 if (m_modelState == InsertingState) {
214 endInsertRows();
215 Q_EMIT countChanged();
216 Q_EMIT listChanged();
217 m_modelState = IdleState;
218 }
220 if (!surface) {
221 activateEmptyWindow(window);
222 }
224 INFO_MSG << " after " << toString();
227void TopLevelWindowModel::connectWindow(Window *window)
229 connect(window, &Window::focusRequested, this, [this, window]() {
230 if (!window->surface()) {
231 activateEmptyWindow(window);
232 }
233 });
235 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
236 if (window->surface()) {
237 if (focused) {
238 setFocusedWindow(window);
239 m_focusedWindowCleared = false;
240 } else if (m_focusedWindow == window) {
241 // Condense changes to the focused window
242 // eg: Do focusedWindow=A to focusedWindow=B instead of
243 // focusedWindow=A to focusedWindow=null to focusedWindow=B
244 m_focusedWindowCleared = true;
245 } else {
246 // don't clear the focused window if you were not there in the first place
247 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
248 }
249 }
250 });
252 connect(window, &Window::closeRequested, this, [this, window]() {
253 if (!window->surface()) {
254 // do things ourselves as miral doesn't know about this window
255 int id = window->id();
256 int index = indexForId(id);
257 bool focusOther = false;
258 Q_ASSERT(index >= 0);
259 if (window->focused()) {
260 focusOther = true;
261 }
262 m_windowModel[index].application->close();
263 if (focusOther) {
264 activateTopMostWindowWithoutId(id);
265 }
266 }
267 });
269 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
270 activateEmptyWindow(window);
271 });
274void TopLevelWindowModel::activateEmptyWindow(Window *window)
276 Q_ASSERT(!window->surface());
277 DEBUG_MSG << "(" << window << ")";
279 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
280 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
282 window->setFocused(true);
283 raiseId(window->id());
284 Window *previousWindow = m_focusedWindow;
285 setFocusedWindow(window);
286 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
287 m_surfaceManager->activate(nullptr);
288 }
291void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
293 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
294 if (!live) {
295 onSurfaceDied(surface);
296 }
297 });
298 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
301void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
303 if (surface->type() == Mir::InputMethodType) {
304 removeInputMethodWindow();
305 return;
306 }
308 int i = indexOf(surface);
309 if (i == -1) {
310 return;
311 }
313 auto application = m_windowModel[i].application;
315 // can't be starting if it already has a surface
316 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
318 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
319 m_windowModel[i].removeOnceSurfaceDestroyed = true;
320 } else {
321 // assume it got killed by the out-of-memory daemon.
322 //
323 // So leave entry in the model and only remove its surface, so shell can display a screenshot
324 // in its place.
325 m_windowModel[i].removeOnceSurfaceDestroyed = false;
326 }
329void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
331 int i = indexOf(surface);
332 if (i == -1) {
333 return;
334 }
336 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
337 removeAt(i);
338 } else {
339 auto window = m_windowModel[i].window;
340 window->setSurface(nullptr);
341 window->setFocused(false);
342 m_allSurfaces.remove(surface);
343 INFO_MSG << " Removed surface from entry. After: " << toString();
344 }
347Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
349 int id = generateId();
350 Window *qmlWindow = new Window(id, this);
351 connectWindow(qmlWindow);
352 if (surface) {
353 qmlWindow->setSurface(surface);
354 }
355 return qmlWindow;
359void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
363void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
364 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
366 if (!m_workspace || !m_applicationManager) return;
367 if (workspace != m_workspace->workspace()) return;
369 Q_FOREACH(auto surface, surfaces) {
370 if (m_allSurfaces.contains(surface)) continue;
372 if (surface->parentSurface()) {
373 // Wrap it in a Window so that we keep focusedWindow() up to date.
374 Window *window = createWindow(surface);
375 connect(surface, &QObject::destroyed, window, [=](){
376 window->setSurface(nullptr);
377 window->deleteLater();
378 });
379 } else {
380 if (surface->type() == Mir::InputMethodType) {
381 connectSurface(surface);
382 setInputMethodWindow(createWindow(surface));
383 } else {
384 auto *application = m_applicationManager->findApplicationWithSurface(surface);
385 if (application) {
386 prependSurface(surface, application);
387 } else {
388 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
389 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
390 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
391 Window *promptWindow = createWindow(surface);
392 connect(surface, &QObject::destroyed, promptWindow, [=](){
393 promptWindow->setSurface(nullptr);
394 promptWindow->deleteLater();
395 });
396 }
397 }
398 }
399 }
402void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
403 const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
405 if (!m_workspace || workspace != m_workspace->workspace()) return;
407 int start = -1;
408 int end = -1;
409 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
410 auto surface = *iter;
411 iter++;
413 // Do removals in adjacent blocks.
414 start = end = indexOf(surface);
415 if (start == -1) {
416 // probably a child surface
417 m_allSurfaces.remove(surface);
418 continue;
419 }
420 while(iter != surfaces.constEnd()) {
421 int index = indexOf(*iter);
422 if (index != end+1) {
423 break;
424 }
425 end++;
426 iter++;
427 }
429 if (m_modelState == IdleState) {
430 beginRemoveRows(QModelIndex(), start, end);
431 m_modelState = RemovingState;
432 } else {
433 Q_ASSERT(m_modelState == ResettingState);
434 // No point in signaling anything if we're resetting the whole model
435 }
437 for (int index = start; index <= end; index++) {
438 auto window = m_windowModel[start].window;
439 window->setSurface(nullptr);
440 window->setFocused(false);
442 m_windowModel.removeAt(start);
443 m_allSurfaces.remove(surface);
444 }
446 if (m_modelState == RemovingState) {
447 endRemoveRows();
448 Q_EMIT countChanged();
449 Q_EMIT listChanged();
450 m_modelState = IdleState;
451 }
452 }
455void TopLevelWindowModel::removeAt(int index)
457 if (m_modelState == IdleState) {
458 beginRemoveRows(QModelIndex(), index, index);
459 m_modelState = RemovingState;
460 } else {
461 Q_ASSERT(m_modelState == ResettingState);
462 // No point in signaling anything if we're resetting the whole model
463 }
465 auto window = m_windowModel[index].window;
466 auto surface = window->surface();
468 window->setSurface(nullptr);
469 window->setFocused(false);
471 m_windowModel.removeAt(index);
472 m_allSurfaces.remove(surface);
474 if (m_modelState == RemovingState) {
475 endRemoveRows();
476 Q_EMIT countChanged();
477 Q_EMIT listChanged();
478 m_modelState = IdleState;
479 }
481 disconnect(window, 0, this, 0);
482 if (m_focusedWindow == window) {
483 setFocusedWindow(nullptr);
484 m_focusedWindowCleared = false;
485 }
486 delete window;
488 INFO_MSG << " after " << toString();
491void TopLevelWindowModel::setInputMethodWindow(Window *window)
493 if (m_inputMethodWindow) {
494 qWarning("Multiple Input Method Surfaces created, removing the old one!");
495 delete m_inputMethodWindow;
496 }
497 m_inputMethodWindow = window;
498 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
501void TopLevelWindowModel::removeInputMethodWindow()
503 if (m_inputMethodWindow) {
504 delete m_inputMethodWindow;
505 m_inputMethodWindow = nullptr;
506 Q_EMIT inputMethodSurfaceChanged(nullptr);
507 }
510void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
512 DEBUG_MSG << "(" << surfaces << ")";
513 const int raiseCount = surfaces.size();
514 for (int i = 0; i < raiseCount; i++) {
515 int fromIndex = findIndexOf(surfaces[i]);
516 if (fromIndex != -1) {
517 move(fromIndex, 0);
518 }
519 }
522int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
524 return m_windowModel.count();
527QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
529 if (index.row() < 0 || index.row() >= m_windowModel.size())
530 return QVariant();
532 if (role == WindowRole) {
533 Window *window = m_windowModel.at(index.row()).window;
534 return QVariant::fromValue(window);
535 } else if (role == ApplicationRole) {
536 return QVariant::fromValue(m_windowModel.at(index.row()).application);
537 } else {
538 return QVariant();
539 }
542int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
544 for (int i=0; i<m_windowModel.count(); i++) {
545 if (m_windowModel[i].window->surface() == surface) {
546 return i;
547 }
548 }
549 return -1;
552int TopLevelWindowModel::generateId()
554 int id = m_nextId;
555 m_nextId = nextFreeId(nextId(id), id);
556 return id;
559int TopLevelWindowModel::nextId(int id) const
561 if (id == m_maxId) {
562 return id = 1;
563 } else {
564 return id + 1;
565 }
568int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
570 int firstCandidateId = candidateId;
572 while (indexForId(candidateId) != -1 || candidateId == latestId) {
573 candidateId = nextId(candidateId);
575 if (candidateId == firstCandidateId) {
576 qFatal("TopLevelWindowModel: run out of window ids.");
577 }
578 }
580 return candidateId;
583QString TopLevelWindowModel::toString()
585 QString str;
586 for (int i = 0; i < m_windowModel.count(); ++i) {
587 auto item = m_windowModel.at(i);
589 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
590 .arg(QString::number(i),
591 item.application->appId(),
592 QString::number((qintptr)item.window->surface(), 16),
593 QString::number(item.window->id()));
595 if (i > 0) {
596 str.append(",");
597 }
598 str.append(itemStr);
599 }
600 return str;
603int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
605 for (int i = 0; i < m_windowModel.count(); ++i) {
606 if (m_windowModel.at(i).window->surface() == surface) {
607 return i;
608 }
609 }
610 return -1;
613int TopLevelWindowModel::indexForId(int id) const
615 for (int i = 0; i < m_windowModel.count(); ++i) {
616 if (m_windowModel[i].window->id() == id) {
617 return i;
618 }
619 }
620 return -1;
623Window *TopLevelWindowModel::windowAt(int index) const
625 if (index >=0 && index < m_windowModel.count()) {
626 return m_windowModel[index].window;
627 } else {
628 return nullptr;
629 }
632unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
634 if (index >=0 && index < m_windowModel.count()) {
635 return m_windowModel[index].window->surface();
636 } else {
637 return nullptr;
638 }
641unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
643 if (index >=0 && index < m_windowModel.count()) {
644 return m_windowModel[index].application;
645 } else {
646 return nullptr;
647 }
650int TopLevelWindowModel::idAt(int index) const
652 if (index >=0 && index < m_windowModel.count()) {
653 return m_windowModel[index].window->id();
654 } else {
655 return 0;
656 }
659void TopLevelWindowModel::raiseId(int id)
661 if (m_modelState == IdleState) {
662 DEBUG_MSG << "(id=" << id << ") - do it now.";
663 doRaiseId(id);
664 } else {
665 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
666 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
667 // if we perform yet another model change straight away.
668 //
669 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
670 // the index is definitely within bounds.
671 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
672 }
675void TopLevelWindowModel::doRaiseId(int id)
677 int fromIndex = indexForId(id);
678 // can't raise something that doesn't exist or that it's already on top
679 if (fromIndex != -1 && fromIndex != 0) {
680 auto surface = m_windowModel[fromIndex].window->surface();
681 if (surface) {
682 m_surfaceManager->raise(surface);
683 } else {
684 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
685 // miral can do about it.
686 move(fromIndex, 0);
687 }
688 }
691void TopLevelWindowModel::setFocusedWindow(Window *window)
693 if (window != m_focusedWindow) {
694 INFO_MSG << "(" << window << ")";
696 Window* previousWindow = m_focusedWindow;
698 m_focusedWindow = window;
699 Q_EMIT focusedWindowChanged(m_focusedWindow);
701 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
702 // do it ourselves. miral doesn't know about this window
703 previousWindow->setFocused(false);
704 }
705 }
708unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
710 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
713Window* TopLevelWindowModel::focusedWindow() const
715 return m_focusedWindow;
718void TopLevelWindowModel::move(int from, int to)
720 if (from == to) return;
721 DEBUG_MSG << " from=" << from << " to=" << to;
723 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
724 QModelIndex parent;
725 /* When moving an item down, the destination index needs to be incremented
726 by one, as explained in the documentation:
727 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
729 Q_ASSERT(m_modelState == IdleState);
730 m_modelState = MovingState;
732 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
733#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
734 const auto &window = m_windowModel.takeAt(from);
735 m_windowModel.insert(to, window);
736#else
737 m_windowModel.move(from, to);
738#endif
739 endMoveRows();
741 Q_EMIT listChanged();
742 m_modelState = IdleState;
744 INFO_MSG << " after " << toString();
745 }
747void TopLevelWindowModel::onModificationsStarted()
751void TopLevelWindowModel::onModificationsEnded()
753 if (m_focusedWindowCleared) {
754 setFocusedWindow(nullptr);
755 }
756 // reset
757 m_focusedWindowCleared = false;
760void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
762 DEBUG_MSG << "(" << forbiddenId << ")";
764 for (int i = 0; i < m_windowModel.count(); ++i) {
765 Window *window = m_windowModel[i].window;
766 if (window->id() != forbiddenId) {
767 window->activate();
768 }
769 }
772void TopLevelWindowModel::refreshWindows()
774 DEBUG_MSG << "()";
775 clear();
777 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
779 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](unity::shell::application::MirSurfaceInterface* surface) {
780 if (surface->parentSurface()) {
781 // Wrap it in a Window so that we keep focusedWindow() up to date.
782 Window *window = createWindow(surface);
783 connect(surface, &QObject::destroyed, window, [=](){
784 window->setSurface(nullptr);
785 window->deleteLater();
786 });
787 } else {
788 if (surface->type() == Mir::InputMethodType) {
789 setInputMethodWindow(createWindow(surface));
790 } else {
791 auto *application = m_applicationManager->findApplicationWithSurface(surface);
792 if (application) {
793 prependSurface(surface, application);
794 } else {
795 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
796 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
797 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
798 Window *promptWindow = createWindow(surface);
799 connect(surface, &QObject::destroyed, promptWindow, [=](){
800 promptWindow->setSurface(nullptr);
801 promptWindow->deleteLater();
802 });
803 }
804 }
805 }
806 });
809void TopLevelWindowModel::clear()
811 DEBUG_MSG << "()";
813 while(m_windowModel.count() > 0) {
814 ModelEntry entry = m_windowModel.takeAt(0);
815 disconnect(entry.window, 0, this, 0);
816 delete entry.window;
817 }
818 m_allSurfaces.clear();
819 setFocusedWindow(nullptr);
820 m_focusedWindowCleared = false;