on 4e8f28f help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.cpp
829 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"
19// unity-api
20#include <unity/shell/application/ApplicationInfoInterface.h>
21#include <unity/shell/application/ApplicationManagerInterface.h>
22#include <unity/shell/application/MirSurfaceInterface.h>
23#include <unity/shell/application/MirSurfaceListInterface.h>
24#include <unity/shell/application/SurfaceManagerInterface.h>
26// Qt
27#include <QDebug>
29// local
30#include "Window.h"
31#include "Workspace.h"
32#include "wmpolicyinterface.h"
34// qtmir
35#include <qtmir/mirserverapplication.h>
36#include <qtmir/windowmodelnotifier.h>
38Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
40#define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
41#define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
43namespace unityapi = unity::shell::application;
45TopLevelWindowModel::TopLevelWindowModel()
49void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInterface* value)
51 if (m_applicationManager == value) {
52 return;
53 }
55 DEBUG_MSG << "(" << value << ")";
57 Q_ASSERT(m_modelState == IdleState);
58 m_modelState = ResettingState;
60 beginResetModel();
62 if (m_applicationManager) {
63 disconnect(m_applicationManager, 0, this, 0);
64 }
66 m_applicationManager = value;
68 if (m_applicationManager) {
69 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
70 this, [this](const QModelIndex &/*parent*/, int first, int last) {
71 if (!m_workspace || !m_workspace->isActive())
72 return;
74 for (int i = first; i <= last; ++i) {
75 auto application = m_applicationManager->get(i);
76 addApplication(application);
77 }
78 });
80 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
81 this, [this](const QModelIndex &/*parent*/, int first, int last) {
82 for (int i = first; i <= last; ++i) {
83 auto application = m_applicationManager->get(i);
84 removeApplication(application);
85 }
86 });
87 }
89 Q_EMIT applicationManagerChanged(m_applicationManager);
90 refreshWindows();
92 endResetModel();
93 m_modelState = IdleState;
96void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
98 if (surfaceManager == m_surfaceManager) {
99 return;
100 }
102 DEBUG_MSG << "(" << surfaceManager << ")";
104 Q_ASSERT(m_modelState == IdleState);
105 m_modelState = ResettingState;
107 beginResetModel();
109 if (m_surfaceManager) {
110 disconnect(m_surfaceManager, 0, this, 0);
111 }
113 m_surfaceManager = surfaceManager;
115 if (m_surfaceManager) {
116 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
117 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
118 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
119 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
121 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
122 this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
123 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
124 this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
125 }
127 refreshWindows();
128 Q_EMIT surfaceManagerChanged(m_surfaceManager);
130 endResetModel();
131 m_modelState = IdleState;
134void TopLevelWindowModel::setWorkspace(Workspace *workspace)
136 if (workspace == m_workspace) {
137 return;
138 }
140 DEBUG_MSG << "(" << workspace << ")";
142 Q_ASSERT(m_modelState == IdleState);
143 m_modelState = ResettingState;
145 beginResetModel();
147 if (m_workspace) {
148 disconnect(m_workspace, 0, this, 0);
149 }
151 m_workspace = workspace;
153 refreshWindows();
154 Q_EMIT workspaceChanged(workspace);
156 endResetModel();
157 m_modelState = IdleState;
160void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
162 DEBUG_MSG << "(" << application->appId() << ")";
164 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
165 prependPlaceholder(application);
166 }
169void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
171 DEBUG_MSG << "(" << application->appId() << ")";
173 Q_ASSERT(m_modelState == IdleState);
175 int i = 0;
176 while (i < m_windowModel.count()) {
177 if (m_windowModel.at(i).application == application) {
178 removeAt(i);
179 } else {
180 ++i;
181 }
182 }
185void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
187 INFO_MSG << "(" << application->appId() << ")";
189 prependSurfaceHelper(nullptr, application);
192void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
194 Q_ASSERT(surface != nullptr);
196 bool filledPlaceholder = false;
197 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
198 ModelEntry &entry = m_windowModel[i];
199 if (entry.application == application && entry.window->surface() == nullptr) {
200 entry.window->setSurface(surface);
201 connectSurface(surface);
202 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
203 << ", filling out placeholder. after: " << toString();
204 filledPlaceholder = true;
205 }
206 }
208 if (!filledPlaceholder) {
209 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
210 prependSurfaceHelper(surface, application);
211 }
214void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
216 if (m_modelState == IdleState) {
217 m_modelState = InsertingState;
218 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
219 } else {
220 Q_ASSERT(m_modelState == ResettingState);
221 // No point in signaling anything if we're resetting the whole model
222 }
224 Window *window = createWindow(surface);
226 m_windowModel.prepend(ModelEntry(window, application));
227 m_allSurfaces.insert(surface);
228 if (surface) {
229 connectSurface(surface);
230 }
232 if (m_modelState == InsertingState) {
233 endInsertRows();
234 Q_EMIT countChanged();
235 Q_EMIT listChanged();
236 m_modelState = IdleState;
237 }
239 if (!surface) {
240 activateEmptyWindow(window);
241 }
243 INFO_MSG << " after " << toString();
246void TopLevelWindowModel::connectWindow(Window *window)
248 connect(window, &Window::focusRequested, this, [this, window]() {
249 if (!window->surface()) {
250 activateEmptyWindow(window);
251 }
252 });
254 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
255 if (window->surface()) {
256 // Condense changes to the focused window
257 // eg: Do focusedWindow=A to focusedWindow=B instead of
258 // focusedWindow=A to focusedWindow=null to focusedWindow=B
259 if (focused) {
260 Q_ASSERT(m_newlyFocusedWindow == nullptr);
261 m_focusedWindowChanged = true;
262 m_newlyFocusedWindow = window;
263 } else if (m_focusedWindow == window) {
264 m_focusedWindowChanged = true;
265 } else {
266 // don't clear the focused window if you were not there in the first place
267 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
268 }
269 }
270 });
272 connect(window, &Window::closeRequested, this, [this, window]() {
273 if (!window->surface()) {
274 // do things ourselves as miral doesn't know about this window
275 int id = window->id();
276 int index = indexForId(id);
277 bool focusOther = false;
278 Q_ASSERT(index >= 0);
279 if (window->focused()) {
280 focusOther = true;
281 }
282 m_windowModel[index].application->close();
283 if (focusOther) {
284 activateTopMostWindowWithoutId(id);
285 }
286 }
287 });
289 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
290 activateEmptyWindow(window);
291 });
294void TopLevelWindowModel::activateEmptyWindow(Window *window)
296 Q_ASSERT(!window->surface());
297 DEBUG_MSG << "(" << window << ")";
299 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
300 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
302 window->setFocused(true);
303 raiseId(window->id());
304 Window *previousWindow = m_focusedWindow;
305 setFocusedWindow(window);
306 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
307 m_surfaceManager->activate(nullptr);
308 }
311void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
313 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
314 if (!live) {
315 onSurfaceDied(surface);
316 }
317 });
318 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
321void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
323 if (surface->type() == Mir::InputMethodType) {
324 removeInputMethodWindow();
325 return;
326 }
328 int i = indexOf(surface);
329 if (i == -1) {
330 return;
331 }
333 auto application = m_windowModel[i].application;
335 // can't be starting if it already has a surface
336 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
338 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
339 m_windowModel[i].removeOnceSurfaceDestroyed = true;
340 } else {
341 // assume it got killed by the out-of-memory daemon.
342 //
343 // So leave entry in the model and only remove its surface, so shell can display a screenshot
344 // in its place.
345 m_windowModel[i].removeOnceSurfaceDestroyed = false;
346 }
349void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
351 int i = indexOf(surface);
352 if (i == -1) {
353 return;
354 }
356 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
357 removeAt(i);
358 } else {
359 auto window = m_windowModel[i].window;
360 window->setSurface(nullptr);
361 window->setFocused(false);
362 m_allSurfaces.remove(surface);
363 INFO_MSG << " Removed surface from entry. After: " << toString();
364 }
367Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
369 int id = generateId();
370 Window *qmlWindow = new Window(id, this);
371 connectWindow(qmlWindow);
372 if (surface) {
373 qmlWindow->setSurface(surface);
374 }
375 return qmlWindow;
379void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
383void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
384 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
386 if (!m_workspace || !m_applicationManager) return;
387 if (workspace != m_workspace->workspace()) return;
389 Q_FOREACH(auto surface, surfaces) {
390 if (m_allSurfaces.contains(surface)) continue;
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 });
399 } else {
400 if (surface->type() == Mir::InputMethodType) {
401 setInputMethodWindow(createWindow(surface));
402 } else {
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 }
416 }
417 }
418 }
421void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
422 const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
424 if (workspace != m_workspace->workspace()) return;
426 int start = -1;
427 int end = -1;
428 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
429 auto surface = *iter;
430 iter++;
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 }
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 }
456 for (int index = start; index <= end; index++) {
457 auto window = m_windowModel[start].window;
458 window->setSurface(nullptr);
459 window->setFocused(false);
461 m_windowModel.removeAt(start);
462 m_allSurfaces.remove(window->surface());
463 }
465 if (m_modelState == RemovingState) {
466 endRemoveRows();
467 Q_EMIT countChanged();
468 Q_EMIT listChanged();
469 m_modelState = IdleState;
470 }
471 }
474void TopLevelWindowModel::removeAt(int index)
476 if (m_modelState == IdleState) {
477 beginRemoveRows(QModelIndex(), index, index);
478 m_modelState = RemovingState;
479 } else {
480 Q_ASSERT(m_modelState == ResettingState);
481 // No point in signaling anything if we're resetting the whole model
482 }
484 auto window = m_windowModel[index].window;
486 window->setSurface(nullptr);
487 window->setFocused(false);
489 m_windowModel.removeAt(index);
490 m_allSurfaces.remove(window->surface());
492 if (m_modelState == RemovingState) {
493 endRemoveRows();
494 Q_EMIT countChanged();
495 Q_EMIT listChanged();
496 m_modelState = IdleState;
497 }
499 disconnect(window, 0, this, 0);
500 if (m_focusedWindow == window) {
501 setFocusedWindow(nullptr);
502 }
503 delete window;
505 INFO_MSG << " after " << toString();
508void TopLevelWindowModel::setInputMethodWindow(Window *window)
510 if (m_inputMethodWindow) {
511 qWarning("Multiple Input Method Surfaces created, removing the old one!");
512 delete m_inputMethodWindow;
513 }
514 m_inputMethodWindow = window;
515 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
518void TopLevelWindowModel::removeInputMethodWindow()
520 if (m_inputMethodWindow) {
521 delete m_inputMethodWindow;
522 m_inputMethodWindow = nullptr;
523 Q_EMIT inputMethodSurfaceChanged(nullptr);
524 }
527void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
529 DEBUG_MSG << "(" << surfaces << ")";
530 const int raiseCount = surfaces.size();
531 for (int i = 0; i < raiseCount; i++) {
532 int fromIndex = findIndexOf(surfaces[i]);
533 if (fromIndex != -1) {
534 move(fromIndex, 0);
535 }
536 }
539int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
541 return m_windowModel.count();
544QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
546 if (index.row() < 0 || index.row() >= m_windowModel.size())
547 return QVariant();
549 if (role == WindowRole) {
550 Window *window = m_windowModel.at(index.row()).window;
551 return QVariant::fromValue(window);
552 } else if (role == ApplicationRole) {
553 return QVariant::fromValue(m_windowModel.at(index.row()).application);
554 } else {
555 return QVariant();
556 }
559int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
561 for (int i=0; i<m_windowModel.count(); i++) {
562 if (m_windowModel[i].window->surface() == surface) {
563 return i;
564 }
565 }
566 return -1;
569int TopLevelWindowModel::generateId()
571 int id = m_nextId;
572 m_nextId = nextFreeId(nextId(id), id);
573 return id;
576int TopLevelWindowModel::nextId(int id) const
578 if (id == m_maxId) {
579 return id = 1;
580 } else {
581 return id + 1;
582 }
585int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
587 int firstCandidateId = candidateId;
589 while (indexForId(candidateId) != -1 || candidateId == latestId) {
590 candidateId = nextId(candidateId);
592 if (candidateId == firstCandidateId) {
593 qFatal("TopLevelWindowModel: run out of window ids.");
594 }
595 }
597 return candidateId;
600QString TopLevelWindowModel::toString()
602 QString str;
603 for (int i = 0; i < m_windowModel.count(); ++i) {
604 auto item = m_windowModel.at(i);
606 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
607 .arg(QString::number(i),
608 item.application->appId(),
609 QString::number((qintptr)item.window->surface(), 16),
610 QString::number(item.window->id()));
612 if (i > 0) {
613 str.append(",");
614 }
615 str.append(itemStr);
616 }
617 return str;
620int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
622 for (int i = 0; i < m_windowModel.count(); ++i) {
623 if (m_windowModel.at(i).window->surface() == surface) {
624 return i;
625 }
626 }
627 return -1;
630int TopLevelWindowModel::indexForId(int id) const
632 for (int i = 0; i < m_windowModel.count(); ++i) {
633 if (m_windowModel[i].window->id() == id) {
634 return i;
635 }
636 }
637 return -1;
640Window *TopLevelWindowModel::windowAt(int index) const
642 if (index >=0 && index < m_windowModel.count()) {
643 return m_windowModel[index].window;
644 } else {
645 return nullptr;
646 }
649unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
651 if (index >=0 && index < m_windowModel.count()) {
652 return m_windowModel[index].window->surface();
653 } else {
654 return nullptr;
655 }
658unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
660 if (index >=0 && index < m_windowModel.count()) {
661 return m_windowModel[index].application;
662 } else {
663 return nullptr;
664 }
667int TopLevelWindowModel::idAt(int index) const
669 if (index >=0 && index < m_windowModel.count()) {
670 return m_windowModel[index].window->id();
671 } else {
672 return 0;
673 }
676void TopLevelWindowModel::raiseId(int id)
678 if (m_modelState == IdleState) {
679 DEBUG_MSG << "(id=" << id << ") - do it now.";
680 doRaiseId(id);
681 } else {
682 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
683 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
684 // if we perform yet another model change straight away.
685 //
686 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
687 // the index is definitely within bounds.
688 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
689 }
692void TopLevelWindowModel::doRaiseId(int id)
694 int fromIndex = indexForId(id);
695 // can't raise something that doesn't exist or that it's already on top
696 if (fromIndex != -1 && fromIndex != 0) {
697 auto surface = m_windowModel[fromIndex].window->surface();
698 if (surface) {
699 m_surfaceManager->raise(surface);
700 } else {
701 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
702 // miral can do about it.
703 move(fromIndex, 0);
704 }
705 }
708void TopLevelWindowModel::setFocusedWindow(Window *window)
710 if (window != m_focusedWindow) {
711 INFO_MSG << "(" << window << ")";
713 Window* previousWindow = m_focusedWindow;
715 m_focusedWindow = window;
716 Q_EMIT focusedWindowChanged(m_focusedWindow);
718 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
719 // do it ourselves. miral doesn't know about this window
720 previousWindow->setFocused(false);
721 }
722 }
725unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
727 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
730Window* TopLevelWindowModel::focusedWindow() const
732 return m_focusedWindow;
735void TopLevelWindowModel::move(int from, int to)
737 if (from == to) return;
738 DEBUG_MSG << " from=" << from << " to=" << to;
740 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
741 QModelIndex parent;
742 /* When moving an item down, the destination index needs to be incremented
743 by one, as explained in the documentation:
744 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
746 Q_ASSERT(m_modelState == IdleState);
747 m_modelState = MovingState;
749 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
750#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
751 const auto &window = m_windowModel.takeAt(from);
752 m_windowModel.insert(to, window);
753#else
754 m_windowModel.move(from, to);
755#endif
756 endMoveRows();
758 Q_EMIT listChanged();
759 m_modelState = IdleState;
761 INFO_MSG << " after " << toString();
762 }
764void TopLevelWindowModel::onModificationsStarted()
768void TopLevelWindowModel::onModificationsEnded()
770 if (m_focusedWindowChanged) {
771 setFocusedWindow(m_newlyFocusedWindow);
772 }
773 // reset
774 m_focusedWindowChanged = false;
775 m_newlyFocusedWindow = nullptr;
778void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
780 DEBUG_MSG << "(" << forbiddenId << ")";
782 for (int i = 0; i < m_windowModel.count(); ++i) {
783 Window *window = m_windowModel[i].window;
784 if (window->id() != forbiddenId) {
785 window->activate();
786 }
787 }
790void TopLevelWindowModel::refreshWindows()
792 DEBUG_MSG << "()";
794 m_windowModel.clear();
795 m_allSurfaces.clear();
797 if (!m_workspace || !m_applicationManager) return;
798 WMPolicyInterface::instance()->forEachWindowInWorkspace(m_workspace->workspace(), [this](const miral::Window &window) {
799 auto surface = m_surfaceManager->surfaceFor(window);
800 if (surface) {
801 if (surface->parentSurface()) {
802 // Wrap it in a Window so that we keep focusedWindow() up to date.
803 Window *window = createWindow(surface);
804 connect(surface, &QObject::destroyed, window, [=](){
805 window->setSurface(nullptr);
806 window->deleteLater();
807 });
808 } else {
809 if (surface->type() == Mir::InputMethodType) {
810 setInputMethodWindow(createWindow(surface));
811 } else {
812 auto *application = m_applicationManager->findApplicationWithSurface(surface);
813 if (application) {
814 prependSurface(surface, application);
815 } else {
816 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
817 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
818 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
819 Window *promptWindow = createWindow(surface);
820 connect(surface, &QObject::destroyed, promptWindow, [=](){
821 promptWindow->setSurface(nullptr);
822 promptWindow->deleteLater();
823 });
824 }
825 }
826 }
827 }
828 });