on cf4f2ea help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.cpp
840 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 m_allSurfaces.insert(surface);
202 connectSurface(surface);
203 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
204 << ", filling out placeholder. after: " << toString();
205 filledPlaceholder = true;
206 }
207 }
209 if (!filledPlaceholder) {
210 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
211 prependSurfaceHelper(surface, application);
212 }
215void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
217 if (m_modelState == IdleState) {
218 m_modelState = InsertingState;
219 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
220 } else {
221 Q_ASSERT(m_modelState == ResettingState);
222 // No point in signaling anything if we're resetting the whole model
223 }
225 Window *window = createWindow(surface);
227 m_windowModel.prepend(ModelEntry(window, application));
228 if (surface) {
229 m_allSurfaces.insert(surface);
230 connectSurface(surface);
231 }
233 if (m_modelState == InsertingState) {
234 endInsertRows();
235 Q_EMIT countChanged();
236 Q_EMIT listChanged();
237 m_modelState = IdleState;
238 }
240 if (!surface) {
241 activateEmptyWindow(window);
242 }
244 INFO_MSG << " after " << toString();
247void TopLevelWindowModel::connectWindow(Window *window)
249 connect(window, &Window::focusRequested, this, [this, window]() {
250 if (!window->surface()) {
251 activateEmptyWindow(window);
252 }
253 });
255 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
256 if (window->surface()) {
257 if (focused) {
258 setFocusedWindow(window);
259 m_focusedWindowCleared = false;
260 } else if (m_focusedWindow == window) {
261 // Condense changes to the focused window
262 // eg: Do focusedWindow=A to focusedWindow=B instead of
263 // focusedWindow=A to focusedWindow=null to focusedWindow=B
264 m_focusedWindowCleared = 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 (!m_workspace || 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(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;
485 auto surface = window->surface();
487 window->setSurface(nullptr);
488 window->setFocused(false);
490 m_windowModel.removeAt(index);
491 m_allSurfaces.remove(surface);
493 if (m_modelState == RemovingState) {
494 endRemoveRows();
495 Q_EMIT countChanged();
496 Q_EMIT listChanged();
497 m_modelState = IdleState;
498 }
500 disconnect(window, 0, this, 0);
501 if (m_focusedWindow == window) {
502 setFocusedWindow(nullptr);
503 m_focusedWindowCleared = false;
504 }
505 delete window;
507 INFO_MSG << " after " << toString();
510void TopLevelWindowModel::setInputMethodWindow(Window *window)
512 if (m_inputMethodWindow) {
513 qWarning("Multiple Input Method Surfaces created, removing the old one!");
514 delete m_inputMethodWindow;
515 }
516 m_inputMethodWindow = window;
517 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
520void TopLevelWindowModel::removeInputMethodWindow()
522 if (m_inputMethodWindow) {
523 delete m_inputMethodWindow;
524 m_inputMethodWindow = nullptr;
525 Q_EMIT inputMethodSurfaceChanged(nullptr);
526 }
529void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
531 DEBUG_MSG << "(" << surfaces << ")";
532 const int raiseCount = surfaces.size();
533 for (int i = 0; i < raiseCount; i++) {
534 int fromIndex = findIndexOf(surfaces[i]);
535 if (fromIndex != -1) {
536 move(fromIndex, 0);
537 }
538 }
541int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
543 return m_windowModel.count();
546QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
548 if (index.row() < 0 || index.row() >= m_windowModel.size())
549 return QVariant();
551 if (role == WindowRole) {
552 Window *window = m_windowModel.at(index.row()).window;
553 return QVariant::fromValue(window);
554 } else if (role == ApplicationRole) {
555 return QVariant::fromValue(m_windowModel.at(index.row()).application);
556 } else {
557 return QVariant();
558 }
561int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
563 for (int i=0; i<m_windowModel.count(); i++) {
564 if (m_windowModel[i].window->surface() == surface) {
565 return i;
566 }
567 }
568 return -1;
571int TopLevelWindowModel::generateId()
573 int id = m_nextId;
574 m_nextId = nextFreeId(nextId(id), id);
575 return id;
578int TopLevelWindowModel::nextId(int id) const
580 if (id == m_maxId) {
581 return id = 1;
582 } else {
583 return id + 1;
584 }
587int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
589 int firstCandidateId = candidateId;
591 while (indexForId(candidateId) != -1 || candidateId == latestId) {
592 candidateId = nextId(candidateId);
594 if (candidateId == firstCandidateId) {
595 qFatal("TopLevelWindowModel: run out of window ids.");
596 }
597 }
599 return candidateId;
602QString TopLevelWindowModel::toString()
604 QString str;
605 for (int i = 0; i < m_windowModel.count(); ++i) {
606 auto item = m_windowModel.at(i);
608 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
609 .arg(QString::number(i),
610 item.application->appId(),
611 QString::number((qintptr)item.window->surface(), 16),
612 QString::number(item.window->id()));
614 if (i > 0) {
615 str.append(",");
616 }
617 str.append(itemStr);
618 }
619 return str;
622int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
624 for (int i = 0; i < m_windowModel.count(); ++i) {
625 if (m_windowModel.at(i).window->surface() == surface) {
626 return i;
627 }
628 }
629 return -1;
632int TopLevelWindowModel::indexForId(int id) const
634 for (int i = 0; i < m_windowModel.count(); ++i) {
635 if (m_windowModel[i].window->id() == id) {
636 return i;
637 }
638 }
639 return -1;
642Window *TopLevelWindowModel::windowAt(int index) const
644 if (index >=0 && index < m_windowModel.count()) {
645 return m_windowModel[index].window;
646 } else {
647 return nullptr;
648 }
651unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
653 if (index >=0 && index < m_windowModel.count()) {
654 return m_windowModel[index].window->surface();
655 } else {
656 return nullptr;
657 }
660unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
662 if (index >=0 && index < m_windowModel.count()) {
663 return m_windowModel[index].application;
664 } else {
665 return nullptr;
666 }
669int TopLevelWindowModel::idAt(int index) const
671 if (index >=0 && index < m_windowModel.count()) {
672 return m_windowModel[index].window->id();
673 } else {
674 return 0;
675 }
678void TopLevelWindowModel::raiseId(int id)
680 if (m_modelState == IdleState) {
681 DEBUG_MSG << "(id=" << id << ") - do it now.";
682 doRaiseId(id);
683 } else {
684 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
685 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
686 // if we perform yet another model change straight away.
687 //
688 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
689 // the index is definitely within bounds.
690 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
691 }
694void TopLevelWindowModel::doRaiseId(int id)
696 int fromIndex = indexForId(id);
697 // can't raise something that doesn't exist or that it's already on top
698 if (fromIndex != -1 && fromIndex != 0) {
699 auto surface = m_windowModel[fromIndex].window->surface();
700 if (surface) {
701 m_surfaceManager->raise(surface);
702 } else {
703 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
704 // miral can do about it.
705 move(fromIndex, 0);
706 }
707 }
710void TopLevelWindowModel::setFocusedWindow(Window *window)
712 if (window != m_focusedWindow) {
713 INFO_MSG << "(" << window << ")";
715 Window* previousWindow = m_focusedWindow;
717 m_focusedWindow = window;
718 Q_EMIT focusedWindowChanged(m_focusedWindow);
720 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
721 // do it ourselves. miral doesn't know about this window
722 previousWindow->setFocused(false);
723 }
724 }
727unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
729 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
732Window* TopLevelWindowModel::focusedWindow() const
734 return m_focusedWindow;
737void TopLevelWindowModel::move(int from, int to)
739 if (from == to) return;
740 DEBUG_MSG << " from=" << from << " to=" << to;
742 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
743 QModelIndex parent;
744 /* When moving an item down, the destination index needs to be incremented
745 by one, as explained in the documentation:
746 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
748 Q_ASSERT(m_modelState == IdleState);
749 m_modelState = MovingState;
751 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
752#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
753 const auto &window = m_windowModel.takeAt(from);
754 m_windowModel.insert(to, window);
755#else
756 m_windowModel.move(from, to);
757#endif
758 endMoveRows();
760 Q_EMIT listChanged();
761 m_modelState = IdleState;
763 INFO_MSG << " after " << toString();
764 }
766void TopLevelWindowModel::onModificationsStarted()
770void TopLevelWindowModel::onModificationsEnded()
772 if (m_focusedWindowCleared) {
773 setFocusedWindow(nullptr);
774 }
775 // reset
776 m_focusedWindowCleared = false;
779void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
781 DEBUG_MSG << "(" << forbiddenId << ")";
783 for (int i = 0; i < m_windowModel.count(); ++i) {
784 Window *window = m_windowModel[i].window;
785 if (window->id() != forbiddenId) {
786 window->activate();
787 }
788 }
791void TopLevelWindowModel::refreshWindows()
793 DEBUG_MSG << "()";
794 clear();
796 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
798 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](unity::shell::application::MirSurfaceInterface* surface) {
799 if (surface->parentSurface()) {
800 // Wrap it in a Window so that we keep focusedWindow() up to date.
801 Window *window = createWindow(surface);
802 connect(surface, &QObject::destroyed, window, [=](){
803 window->setSurface(nullptr);
804 window->deleteLater();
805 });
806 } else {
807 if (surface->type() == Mir::InputMethodType) {
808 setInputMethodWindow(createWindow(surface));
809 } else {
810 auto *application = m_applicationManager->findApplicationWithSurface(surface);
811 if (application) {
812 prependSurface(surface, application);
813 } else {
814 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
815 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
816 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
817 Window *promptWindow = createWindow(surface);
818 connect(surface, &QObject::destroyed, promptWindow, [=](){
819 promptWindow->setSurface(nullptr);
820 promptWindow->deleteLater();
821 });
822 }
823 }
824 }
825 });
828void TopLevelWindowModel::clear()
830 DEBUG_MSG << "()";
832 while(m_windowModel.count() > 0) {
833 ModelEntry entry = m_windowModel.takeAt(0);
834 disconnect(entry.window, 0, this, 0);
835 delete entry.window;
836 }
837 m_allSurfaces.clear();
838 setFocusedWindow(nullptr);
839 m_focusedWindowCleared = false;