on 117f2d8 help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.cpp
822 lines 27 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"
33#include "wmpolicyinterface.h"
35// qtmir
36#include <qtmir/mirserverapplication.h>
37#include <qtmir/windowmodelnotifier.h>
39Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
41#define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
42#define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
44namespace unityapi = unity::shell::application;
46TopLevelWindowModel::TopLevelWindowModel(Workspace* workspace)
47 : m_workspace(workspace)
49 connect(WindowManagerObjects::instance(), &WindowManagerObjects::applicationManagerChanged,
50 this, &TopLevelWindowModel::setApplicationManager);
51 connect(WindowManagerObjects::instance(), &WindowManagerObjects::surfaceManagerChanged,
52 this, &TopLevelWindowModel::setSurfaceManager);
54 setApplicationManager(WindowManagerObjects::instance()->applicationManager());
55 setSurfaceManager(WindowManagerObjects::instance()->surfaceManager());
58void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInterface* value)
60 if (m_applicationManager == value) {
61 return;
62 }
64 DEBUG_MSG << "(" << value << ")";
66 Q_ASSERT(m_modelState == IdleState);
67 m_modelState = ResettingState;
69 beginResetModel();
71 if (m_applicationManager) {
72 disconnect(m_applicationManager, 0, this, 0);
73 }
75 m_applicationManager = value;
77 if (m_applicationManager) {
78 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
79 this, [this](const QModelIndex &/*parent*/, int first, int last) {
80 if (!m_workspace || !m_workspace->isActive())
81 return;
83 for (int i = first; i <= last; ++i) {
84 auto application = m_applicationManager->get(i);
85 addApplication(application);
86 }
87 });
89 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
90 this, [this](const QModelIndex &/*parent*/, int first, int last) {
91 for (int i = first; i <= last; ++i) {
92 auto application = m_applicationManager->get(i);
93 removeApplication(application);
94 }
95 });
96 }
98 refreshWindows();
100 endResetModel();
101 m_modelState = IdleState;
104void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
106 if (surfaceManager == m_surfaceManager) {
107 return;
108 }
110 DEBUG_MSG << "(" << surfaceManager << ")";
112 Q_ASSERT(m_modelState == IdleState);
113 m_modelState = ResettingState;
115 beginResetModel();
117 if (m_surfaceManager) {
118 disconnect(m_surfaceManager, 0, this, 0);
119 }
121 m_surfaceManager = surfaceManager;
123 if (m_surfaceManager) {
124 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
125 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
126 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
127 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
129 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
130 this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
131 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
132 this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
133 }
135 refreshWindows();
137 endResetModel();
138 m_modelState = IdleState;
141void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
143 DEBUG_MSG << "(" << application->appId() << ")";
145 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
146 prependPlaceholder(application);
147 }
150void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
152 DEBUG_MSG << "(" << application->appId() << ")";
154 Q_ASSERT(m_modelState == IdleState);
156 int i = 0;
157 while (i < m_windowModel.count()) {
158 if (m_windowModel.at(i).application == application) {
159 removeAt(i);
160 } else {
161 ++i;
162 }
163 }
166void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
168 INFO_MSG << "(" << application->appId() << ")";
170 prependSurfaceHelper(nullptr, application);
173void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
175 Q_ASSERT(surface != nullptr);
177 bool filledPlaceholder = false;
178 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
179 ModelEntry &entry = m_windowModel[i];
180 if (entry.application == application && entry.window->surface() == nullptr) {
181 entry.window->setSurface(surface);
182 m_allSurfaces.insert(surface);
183 connectSurface(surface);
184 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
185 << ", filling out placeholder. after: " << toString();
186 filledPlaceholder = true;
187 }
188 }
190 if (!filledPlaceholder) {
191 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
192 prependSurfaceHelper(surface, application);
193 }
196void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
198 if (m_modelState == IdleState) {
199 m_modelState = InsertingState;
200 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
201 } else {
202 Q_ASSERT(m_modelState == ResettingState);
203 // No point in signaling anything if we're resetting the whole model
204 }
206 Window *window = createWindow(surface);
208 m_windowModel.prepend(ModelEntry(window, application));
209 if (surface) {
210 m_allSurfaces.insert(surface);
211 connectSurface(surface);
212 }
214 if (m_modelState == InsertingState) {
215 endInsertRows();
216 Q_EMIT countChanged();
217 Q_EMIT listChanged();
218 m_modelState = IdleState;
219 }
221 if (!surface) {
222 activateEmptyWindow(window);
223 }
225 INFO_MSG << " after " << toString();
228void TopLevelWindowModel::connectWindow(Window *window)
230 connect(window, &Window::focusRequested, this, [this, window]() {
231 if (!window->surface()) {
232 activateEmptyWindow(window);
233 }
234 });
236 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
237 if (window->surface()) {
238 if (focused) {
239 setFocusedWindow(window);
240 m_focusedWindowCleared = false;
241 } else if (m_focusedWindow == window) {
242 // Condense changes to the focused window
243 // eg: Do focusedWindow=A to focusedWindow=B instead of
244 // focusedWindow=A to focusedWindow=null to focusedWindow=B
245 m_focusedWindowCleared = true;
246 } else {
247 // don't clear the focused window if you were not there in the first place
248 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
249 }
250 }
251 });
253 connect(window, &Window::closeRequested, this, [this, window]() {
254 if (!window->surface()) {
255 // do things ourselves as miral doesn't know about this window
256 int id = window->id();
257 int index = indexForId(id);
258 bool focusOther = false;
259 Q_ASSERT(index >= 0);
260 if (window->focused()) {
261 focusOther = true;
262 }
263 m_windowModel[index].application->close();
264 if (focusOther) {
265 activateTopMostWindowWithoutId(id);
266 }
267 }
268 });
270 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
271 activateEmptyWindow(window);
272 });
275void TopLevelWindowModel::activateEmptyWindow(Window *window)
277 Q_ASSERT(!window->surface());
278 DEBUG_MSG << "(" << window << ")";
280 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
281 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
283 window->setFocused(true);
284 raiseId(window->id());
285 Window *previousWindow = m_focusedWindow;
286 setFocusedWindow(window);
287 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
288 m_surfaceManager->activate(nullptr);
289 }
292void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
294 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
295 if (!live) {
296 onSurfaceDied(surface);
297 }
298 });
299 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
302void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
304 if (surface->type() == Mir::InputMethodType) {
305 removeInputMethodWindow();
306 return;
307 }
309 int i = indexOf(surface);
310 if (i == -1) {
311 return;
312 }
314 auto application = m_windowModel[i].application;
316 // can't be starting if it already has a surface
317 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
319 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
320 m_windowModel[i].removeOnceSurfaceDestroyed = true;
321 } else {
322 // assume it got killed by the out-of-memory daemon.
323 //
324 // So leave entry in the model and only remove its surface, so shell can display a screenshot
325 // in its place.
326 m_windowModel[i].removeOnceSurfaceDestroyed = false;
327 }
330void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
332 int i = indexOf(surface);
333 if (i == -1) {
334 return;
335 }
337 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
338 removeAt(i);
339 } else {
340 auto window = m_windowModel[i].window;
341 window->setSurface(nullptr);
342 window->setFocused(false);
343 m_allSurfaces.remove(surface);
344 INFO_MSG << " Removed surface from entry. After: " << toString();
345 }
348Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
350 int id = generateId();
351 Window *qmlWindow = new Window(id, this);
352 connectWindow(qmlWindow);
353 if (surface) {
354 qmlWindow->setSurface(surface);
355 }
356 return qmlWindow;
360void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
364void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
365 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
367 if (!m_workspace || !m_applicationManager) return;
368 if (workspace != m_workspace->workspace()) return;
370 Q_FOREACH(auto surface, surfaces) {
371 if (m_allSurfaces.contains(surface)) continue;
373 if (surface->parentSurface()) {
374 // Wrap it in a Window so that we keep focusedWindow() up to date.
375 Window *window = createWindow(surface);
376 connect(surface, &QObject::destroyed, window, [=](){
377 window->setSurface(nullptr);
378 window->deleteLater();
379 });
380 } else {
381 if (surface->type() == Mir::InputMethodType) {
382 connectSurface(surface);
383 setInputMethodWindow(createWindow(surface));
384 } else {
385 auto *application = m_applicationManager->findApplicationWithSurface(surface);
386 if (application) {
387 prependSurface(surface, application);
388 } else {
389 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
390 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
391 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
392 Window *promptWindow = createWindow(surface);
393 connect(surface, &QObject::destroyed, promptWindow, [=](){
394 promptWindow->setSurface(nullptr);
395 promptWindow->deleteLater();
396 });
397 }
398 }
399 }
400 }
403void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
404 const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
406 if (!m_workspace || workspace != m_workspace->workspace()) return;
408 int start = -1;
409 int end = -1;
410 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
411 auto surface = *iter;
412 iter++;
414 // Do removals in adjacent blocks.
415 start = end = indexOf(surface);
416 if (start == -1) {
417 // probably a child surface
418 m_allSurfaces.remove(surface);
419 continue;
420 }
421 while(iter != surfaces.constEnd()) {
422 int index = indexOf(*iter);
423 if (index != end+1) {
424 break;
425 }
426 end++;
427 iter++;
428 }
430 if (m_modelState == IdleState) {
431 beginRemoveRows(QModelIndex(), start, end);
432 m_modelState = RemovingState;
433 } else {
434 Q_ASSERT(m_modelState == ResettingState);
435 // No point in signaling anything if we're resetting the whole model
436 }
438 for (int index = start; index <= end; index++) {
439 auto window = m_windowModel[start].window;
440 window->setSurface(nullptr);
441 window->setFocused(false);
443 m_windowModel.removeAt(start);
444 m_allSurfaces.remove(surface);
445 }
447 if (m_modelState == RemovingState) {
448 endRemoveRows();
449 Q_EMIT countChanged();
450 Q_EMIT listChanged();
451 m_modelState = IdleState;
452 }
453 }
456void TopLevelWindowModel::removeAt(int index)
458 if (m_modelState == IdleState) {
459 beginRemoveRows(QModelIndex(), index, index);
460 m_modelState = RemovingState;
461 } else {
462 Q_ASSERT(m_modelState == ResettingState);
463 // No point in signaling anything if we're resetting the whole model
464 }
466 auto window = m_windowModel[index].window;
467 auto surface = window->surface();
469 window->setSurface(nullptr);
470 window->setFocused(false);
472 m_windowModel.removeAt(index);
473 m_allSurfaces.remove(surface);
475 if (m_modelState == RemovingState) {
476 endRemoveRows();
477 Q_EMIT countChanged();
478 Q_EMIT listChanged();
479 m_modelState = IdleState;
480 }
482 disconnect(window, 0, this, 0);
483 if (m_focusedWindow == window) {
484 setFocusedWindow(nullptr);
485 m_focusedWindowCleared = false;
486 }
487 delete window;
489 INFO_MSG << " after " << toString();
492void TopLevelWindowModel::setInputMethodWindow(Window *window)
494 if (m_inputMethodWindow) {
495 qWarning("Multiple Input Method Surfaces created, removing the old one!");
496 delete m_inputMethodWindow;
497 }
498 m_inputMethodWindow = window;
499 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
502void TopLevelWindowModel::removeInputMethodWindow()
504 if (m_inputMethodWindow) {
505 delete m_inputMethodWindow;
506 m_inputMethodWindow = nullptr;
507 Q_EMIT inputMethodSurfaceChanged(nullptr);
508 }
511void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
513 DEBUG_MSG << "(" << surfaces << ")";
514 const int raiseCount = surfaces.size();
515 for (int i = 0; i < raiseCount; i++) {
516 int fromIndex = findIndexOf(surfaces[i]);
517 if (fromIndex != -1) {
518 move(fromIndex, 0);
519 }
520 }
523int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
525 return m_windowModel.count();
528QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
530 if (index.row() < 0 || index.row() >= m_windowModel.size())
531 return QVariant();
533 if (role == WindowRole) {
534 Window *window = m_windowModel.at(index.row()).window;
535 return QVariant::fromValue(window);
536 } else if (role == ApplicationRole) {
537 return QVariant::fromValue(m_windowModel.at(index.row()).application);
538 } else {
539 return QVariant();
540 }
543int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
545 for (int i=0; i<m_windowModel.count(); i++) {
546 if (m_windowModel[i].window->surface() == surface) {
547 return i;
548 }
549 }
550 return -1;
553int TopLevelWindowModel::generateId()
555 int id = m_nextId;
556 m_nextId = nextFreeId(nextId(id), id);
557 return id;
560int TopLevelWindowModel::nextId(int id) const
562 if (id == m_maxId) {
563 return id = 1;
564 } else {
565 return id + 1;
566 }
569int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
571 int firstCandidateId = candidateId;
573 while (indexForId(candidateId) != -1 || candidateId == latestId) {
574 candidateId = nextId(candidateId);
576 if (candidateId == firstCandidateId) {
577 qFatal("TopLevelWindowModel: run out of window ids.");
578 }
579 }
581 return candidateId;
584QString TopLevelWindowModel::toString()
586 QString str;
587 for (int i = 0; i < m_windowModel.count(); ++i) {
588 auto item = m_windowModel.at(i);
590 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
591 .arg(QString::number(i),
592 item.application->appId(),
593 QString::number((qintptr)item.window->surface(), 16),
594 QString::number(item.window->id()));
596 if (i > 0) {
597 str.append(",");
598 }
599 str.append(itemStr);
600 }
601 return str;
604int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
606 for (int i = 0; i < m_windowModel.count(); ++i) {
607 if (m_windowModel.at(i).window->surface() == surface) {
608 return i;
609 }
610 }
611 return -1;
614int TopLevelWindowModel::indexForId(int id) const
616 for (int i = 0; i < m_windowModel.count(); ++i) {
617 if (m_windowModel[i].window->id() == id) {
618 return i;
619 }
620 }
621 return -1;
624Window *TopLevelWindowModel::windowAt(int index) const
626 if (index >=0 && index < m_windowModel.count()) {
627 return m_windowModel[index].window;
628 } else {
629 return nullptr;
630 }
633unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
635 if (index >=0 && index < m_windowModel.count()) {
636 return m_windowModel[index].window->surface();
637 } else {
638 return nullptr;
639 }
642unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
644 if (index >=0 && index < m_windowModel.count()) {
645 return m_windowModel[index].application;
646 } else {
647 return nullptr;
648 }
651int TopLevelWindowModel::idAt(int index) const
653 if (index >=0 && index < m_windowModel.count()) {
654 return m_windowModel[index].window->id();
655 } else {
656 return 0;
657 }
660void TopLevelWindowModel::raiseId(int id)
662 if (m_modelState == IdleState) {
663 DEBUG_MSG << "(id=" << id << ") - do it now.";
664 doRaiseId(id);
665 } else {
666 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
667 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
668 // if we perform yet another model change straight away.
669 //
670 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
671 // the index is definitely within bounds.
672 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
673 }
676void TopLevelWindowModel::doRaiseId(int id)
678 int fromIndex = indexForId(id);
679 // can't raise something that doesn't exist or that it's already on top
680 if (fromIndex != -1 && fromIndex != 0) {
681 auto surface = m_windowModel[fromIndex].window->surface();
682 if (surface) {
683 m_surfaceManager->raise(surface);
684 } else {
685 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
686 // miral can do about it.
687 move(fromIndex, 0);
688 }
689 }
692void TopLevelWindowModel::setFocusedWindow(Window *window)
694 if (window != m_focusedWindow) {
695 INFO_MSG << "(" << window << ")";
697 Window* previousWindow = m_focusedWindow;
699 m_focusedWindow = window;
700 Q_EMIT focusedWindowChanged(m_focusedWindow);
702 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
703 // do it ourselves. miral doesn't know about this window
704 previousWindow->setFocused(false);
705 }
706 }
709unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
711 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
714Window* TopLevelWindowModel::focusedWindow() const
716 return m_focusedWindow;
719void TopLevelWindowModel::move(int from, int to)
721 if (from == to) return;
722 DEBUG_MSG << " from=" << from << " to=" << to;
724 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
725 QModelIndex parent;
726 /* When moving an item down, the destination index needs to be incremented
727 by one, as explained in the documentation:
728 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
730 Q_ASSERT(m_modelState == IdleState);
731 m_modelState = MovingState;
733 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
734#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
735 const auto &window = m_windowModel.takeAt(from);
736 m_windowModel.insert(to, window);
737#else
738 m_windowModel.move(from, to);
739#endif
740 endMoveRows();
742 Q_EMIT listChanged();
743 m_modelState = IdleState;
745 INFO_MSG << " after " << toString();
746 }
748void TopLevelWindowModel::onModificationsStarted()
752void TopLevelWindowModel::onModificationsEnded()
754 if (m_focusedWindowCleared) {
755 setFocusedWindow(nullptr);
756 }
757 // reset
758 m_focusedWindowCleared = false;
761void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
763 DEBUG_MSG << "(" << forbiddenId << ")";
765 for (int i = 0; i < m_windowModel.count(); ++i) {
766 Window *window = m_windowModel[i].window;
767 if (window->id() != forbiddenId) {
768 window->activate();
769 }
770 }
773void TopLevelWindowModel::refreshWindows()
775 DEBUG_MSG << "()";
776 clear();
778 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
780 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](unity::shell::application::MirSurfaceInterface* surface) {
781 if (surface->parentSurface()) {
782 // Wrap it in a Window so that we keep focusedWindow() up to date.
783 Window *window = createWindow(surface);
784 connect(surface, &QObject::destroyed, window, [=](){
785 window->setSurface(nullptr);
786 window->deleteLater();
787 });
788 } else {
789 if (surface->type() == Mir::InputMethodType) {
790 setInputMethodWindow(createWindow(surface));
791 } else {
792 auto *application = m_applicationManager->findApplicationWithSurface(surface);
793 if (application) {
794 prependSurface(surface, application);
795 } else {
796 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
797 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
798 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
799 Window *promptWindow = createWindow(surface);
800 connect(surface, &QObject::destroyed, promptWindow, [=](){
801 promptWindow->setSurface(nullptr);
802 promptWindow->deleteLater();
803 });
804 }
805 }
806 }
807 });
810void TopLevelWindowModel::clear()
812 DEBUG_MSG << "()";
814 while(m_windowModel.count() > 0) {
815 ModelEntry entry = m_windowModel.takeAt(0);
816 disconnect(entry.window, 0, this, 0);
817 delete entry.window;
818 }
819 m_allSurfaces.clear();
820 setFocusedWindow(nullptr);
821 m_focusedWindowCleared = false;