on 43f0793 help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.cpp
853 lines 28 kB history raw
1/*
2 * Copyright (C) 2016-2017 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::surfacesAddedToWorkspace, this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
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);
127 }
129 refreshWindows();
131 endResetModel();
132 m_modelState = IdleState;
135void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
137 DEBUG_MSG << "(" << application->appId() << ")";
139 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
140 prependPlaceholder(application);
141 }
144void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
146 DEBUG_MSG << "(" << application->appId() << ")";
148 Q_ASSERT(m_modelState == IdleState);
150 int i = 0;
151 while (i < m_windowModel.count()) {
152 if (m_windowModel.at(i).application == application) {
153 deleteAt(i);
154 } else {
155 ++i;
156 }
157 }
160void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
162 INFO_MSG << "(" << application->appId() << ")";
164 prependSurfaceHelper(nullptr, application);
167void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
169 Q_ASSERT(surface != nullptr);
171 connectSurface(surface);
172 m_allSurfaces.insert(surface);
174 bool filledPlaceholder = false;
175 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
176 ModelEntry &entry = m_windowModel[i];
177 if (entry.application == application && entry.window->surface() == nullptr) {
178 entry.window->setSurface(surface);
179 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
180 << ", filling out placeholder. after: " << toString();
181 filledPlaceholder = true;
182 }
183 }
185 if (!filledPlaceholder) {
186 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
187 prependSurfaceHelper(surface, application);
188 }
191void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
194 Window *window = createWindow(surface);
196 connect(window, &Window::stateChanged, this, [=](Mir::State newState) {
197 if (newState == Mir::HiddenState) {
198 // Comply, removing it from our model. Just as if it didn't exist anymore.
199 removeAt(indexForId(window->id()));
200 } else {
201 if (indexForId(window->id()) == -1) {
202 // was probably hidden before. put it back on the list
203 auto *application = m_applicationManager->findApplicationWithSurface(window->surface());
204 Q_ASSERT(application);
205 prependWindow(window, application);
206 }
207 }
208 });
210 prependWindow(window, application);
212 if (!surface) {
213 activateEmptyWindow(window);
214 }
216 INFO_MSG << " after " << toString();
219void TopLevelWindowModel::prependWindow(Window *window, unityapi::ApplicationInfoInterface *application)
221 if (m_modelState == IdleState) {
222 m_modelState = InsertingState;
223 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
224 } else {
225 Q_ASSERT(m_modelState == ResettingState);
226 // No point in signaling anything if we're resetting the whole model
227 }
229 m_windowModel.prepend(ModelEntry(window, application));
231 if (m_modelState == InsertingState) {
232 endInsertRows();
233 Q_EMIT countChanged();
234 Q_EMIT listChanged();
235 m_modelState = IdleState;
236 }
239void TopLevelWindowModel::connectWindow(Window *window)
241 connect(window, &Window::focusRequested, this, [this, window]() {
242 if (!window->surface()) {
243 activateEmptyWindow(window);
244 }
245 });
247 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
248 if (window->surface()) {
249 if (focused) {
250 setFocusedWindow(window);
251 m_focusedWindowCleared = false;
252 } else if (m_focusedWindow == window) {
253 // Condense changes to the focused window
254 // eg: Do focusedWindow=A to focusedWindow=B instead of
255 // focusedWindow=A to focusedWindow=null to focusedWindow=B
256 m_focusedWindowCleared = true;
257 } else {
258 // don't clear the focused window if you were not there in the first place
259 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
260 }
261 }
262 });
264 connect(window, &Window::closeRequested, this, [this, window]() {
265 if (!window->surface()) {
266 // do things ourselves as miral doesn't know about this window
267 int id = window->id();
268 int index = indexForId(id);
269 bool focusOther = false;
270 Q_ASSERT(index >= 0);
271 if (window->focused()) {
272 focusOther = true;
273 }
274 m_windowModel[index].application->close();
275 if (focusOther) {
276 activateTopMostWindowWithoutId(id);
277 }
278 }
279 });
281 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
282 activateEmptyWindow(window);
283 });
285 connect(window, &Window::liveChanged, this, [this, window](bool isAlive) {
286 if (!isAlive && window->state() == Mir::HiddenState) {
287 // Hidden windows are not in the model. So just delete it right away.
288 delete window;
289 }
290 });
293void TopLevelWindowModel::activateEmptyWindow(Window *window)
295 Q_ASSERT(!window->surface());
296 DEBUG_MSG << "(" << window << ")";
298 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
299 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
301 window->setFocused(true);
302 raiseId(window->id());
303 Window *previousWindow = m_focusedWindow;
304 setFocusedWindow(window);
305 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
306 m_surfaceManager->activate(nullptr);
307 }
310void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
312 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
313 if (!live) {
314 onSurfaceDied(surface);
315 }
316 });
317 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
320void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
322 if (surface->type() == Mir::InputMethodType) {
323 removeInputMethodWindow();
324 return;
325 }
327 int i = indexOf(surface);
328 if (i == -1) {
329 return;
330 }
332 auto application = m_windowModel[i].application;
334 // can't be starting if it already has a surface
335 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
337 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
338 m_windowModel[i].removeOnceSurfaceDestroyed = true;
339 } else {
340 // assume it got killed by the out-of-memory daemon.
341 //
342 // So leave entry in the model and only remove its surface, so shell can display a screenshot
343 // in its place.
344 m_windowModel[i].removeOnceSurfaceDestroyed = false;
345 }
348void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
350 int i = indexOf(surface);
351 if (i == -1) {
352 return;
353 }
355 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
356 deleteAt(i);
357 } else {
358 auto window = m_windowModel[i].window;
359 window->setSurface(nullptr);
360 window->setFocused(false);
361 m_allSurfaces.remove(surface);
362 INFO_MSG << " Removed surface from entry. After: " << toString();
363 }
366Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
368 int id = generateId();
369 Window *qmlWindow = new Window(id, this);
370 connectWindow(qmlWindow);
371 if (surface) {
372 qmlWindow->setSurface(surface);
373 }
374 return qmlWindow;
377void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
378 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
380 if (!m_workspace || !m_applicationManager) return;
381 if (workspace != m_workspace->workspace()) {
382 removeSurfaces(surfaces);
383 return;
384 }
386 Q_FOREACH(auto surface, surfaces) {
387 if (m_allSurfaces.contains(surface)) continue;
389 if (surface->parentSurface()) {
390 // Wrap it in a Window so that we keep focusedWindow() up to date.
391 Window *window = createWindow(surface);
392 connect(surface, &QObject::destroyed, window, [=](){
393 window->setSurface(nullptr);
394 window->deleteLater();
395 });
396 } else {
397 if (surface->type() == Mir::InputMethodType) {
398 connectSurface(surface);
399 setInputMethodWindow(createWindow(surface));
400 } else {
401 auto *application = m_applicationManager->findApplicationWithSurface(surface);
402 if (application) {
403 if (surface->state() == Mir::HiddenState) {
404 // Ignore it until it's finally shown
405 connect(surface, &unityapi::MirSurfaceInterface::stateChanged, this, [=](Mir::State newState) {
406 Q_ASSERT(newState != Mir::HiddenState);
407 disconnect(surface, &unityapi::MirSurfaceInterface::stateChanged, this, 0);
408 prependSurface(surface, application);
409 });
410 } else {
411 prependSurface(surface, application);
412 }
413 } else {
414 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
415 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
416 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
417 Window *promptWindow = createWindow(surface);
418 connect(surface, &QObject::destroyed, promptWindow, [=](){
419 promptWindow->setSurface(nullptr);
420 promptWindow->deleteLater();
421 });
422 }
423 }
424 }
425 }
428void TopLevelWindowModel::removeSurfaces(const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
430 int start = -1;
431 int end = -1;
432 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
433 auto surface = *iter;
434 iter++;
436 // Do removals in adjacent blocks.
437 start = end = indexOf(surface);
438 if (start == -1) {
439 // could be a child surface
440 m_allSurfaces.remove(surface);
441 continue;
442 }
443 while(iter != surfaces.constEnd()) {
444 int index = indexOf(*iter);
445 if (index != end+1) {
446 break;
447 }
448 end++;
449 iter++;
450 }
452 if (m_modelState == IdleState) {
453 beginRemoveRows(QModelIndex(), start, end);
454 m_modelState = RemovingState;
455 } else {
456 Q_ASSERT(m_modelState == ResettingState);
457 // No point in signaling anything if we're resetting the whole model
458 }
460 for (int index = start; index <= end; index++) {
461 auto window = m_windowModel[start].window;
462 window->setSurface(nullptr);
463 window->setFocused(false);
465 m_windowModel.removeAt(start);
466 m_allSurfaces.remove(surface);
467 }
469 if (m_modelState == RemovingState) {
470 endRemoveRows();
471 Q_EMIT countChanged();
472 Q_EMIT listChanged();
473 m_modelState = IdleState;
474 }
475 }
478void TopLevelWindowModel::deleteAt(int index)
480 auto window = m_windowModel[index].window;
482 removeAt(index);
484 window->setSurface(nullptr);
486 delete window;
489void TopLevelWindowModel::removeAt(int index)
491 if (m_modelState == IdleState) {
492 beginRemoveRows(QModelIndex(), index, index);
493 m_modelState = RemovingState;
494 } else {
495 Q_ASSERT(m_modelState == ResettingState);
496 // No point in signaling anything if we're resetting the whole model
497 }
499 auto window = m_windowModel[index].window;
500 auto surface = window->surface();
502 if (!window->surface()) {
503 window->setFocused(false);
504 }
506 m_windowModel.removeAt(index);
507 m_allSurfaces.remove(surface);
509 if (m_modelState == RemovingState) {
510 endRemoveRows();
511 Q_EMIT countChanged();
512 Q_EMIT listChanged();
513 m_modelState = IdleState;
514 }
516 if (m_focusedWindow == window) {
517 setFocusedWindow(nullptr);
518 m_focusedWindowCleared = false;
519 }
521 INFO_MSG << " after " << toString();
524void TopLevelWindowModel::setInputMethodWindow(Window *window)
526 if (m_inputMethodWindow) {
527 qWarning("Multiple Input Method Surfaces created, removing the old one!");
528 delete m_inputMethodWindow;
529 }
530 m_inputMethodWindow = window;
531 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
534void TopLevelWindowModel::removeInputMethodWindow()
536 if (m_inputMethodWindow) {
537 auto surface = m_inputMethodWindow->surface();
538 if (surface) {
539 m_allSurfaces.remove(surface);
540 }
541 if (m_focusedWindow == m_inputMethodWindow) {
542 setFocusedWindow(nullptr);
543 m_focusedWindowCleared = false;
544 }
546 delete m_inputMethodWindow;
547 m_inputMethodWindow = nullptr;
548 Q_EMIT inputMethodSurfaceChanged(nullptr);
549 }
552void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
554 DEBUG_MSG << "(" << surfaces << ")";
555 const int raiseCount = surfaces.size();
556 for (int i = 0; i < raiseCount; i++) {
557 int fromIndex = indexOf(surfaces[i]);
558 if (fromIndex != -1) {
559 move(fromIndex, 0);
560 }
561 }
564int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
566 return m_windowModel.count();
569QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
571 if (index.row() < 0 || index.row() >= m_windowModel.size())
572 return QVariant();
574 if (role == WindowRole) {
575 Window *window = m_windowModel.at(index.row()).window;
576 return QVariant::fromValue(window);
577 } else if (role == ApplicationRole) {
578 return QVariant::fromValue(m_windowModel.at(index.row()).application);
579 } else {
580 return QVariant();
581 }
584int TopLevelWindowModel::generateId()
586 int id = m_nextId;
587 m_nextId = nextFreeId(nextId(id), id);
588 return id;
591int TopLevelWindowModel::nextId(int id) const
593 if (id == m_maxId) {
594 return id = 1;
595 } else {
596 return id + 1;
597 }
600int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
602 int firstCandidateId = candidateId;
604 while (indexForId(candidateId) != -1 || candidateId == latestId) {
605 candidateId = nextId(candidateId);
607 if (candidateId == firstCandidateId) {
608 qFatal("TopLevelWindowModel: run out of window ids.");
609 }
610 }
612 return candidateId;
615QString TopLevelWindowModel::toString()
617 QString str;
618 for (int i = 0; i < m_windowModel.count(); ++i) {
619 auto item = m_windowModel.at(i);
621 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
622 .arg(QString::number(i),
623 item.application->appId(),
624 QString::number((qintptr)item.window->surface(), 16),
625 QString::number(item.window->id()));
627 if (i > 0) {
628 str.append(",");
629 }
630 str.append(itemStr);
631 }
632 return str;
635int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
637 for (int i = 0; i < m_windowModel.count(); ++i) {
638 if (m_windowModel.at(i).window->surface() == surface) {
639 return i;
640 }
641 }
642 return -1;
645int TopLevelWindowModel::indexForId(int id) const
647 for (int i = 0; i < m_windowModel.count(); ++i) {
648 if (m_windowModel[i].window->id() == id) {
649 return i;
650 }
651 }
652 return -1;
655Window *TopLevelWindowModel::windowAt(int index) const
657 if (index >=0 && index < m_windowModel.count()) {
658 return m_windowModel[index].window;
659 } else {
660 return nullptr;
661 }
664unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
666 if (index >=0 && index < m_windowModel.count()) {
667 return m_windowModel[index].window->surface();
668 } else {
669 return nullptr;
670 }
673unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
675 if (index >=0 && index < m_windowModel.count()) {
676 return m_windowModel[index].application;
677 } else {
678 return nullptr;
679 }
682int TopLevelWindowModel::idAt(int index) const
684 if (index >=0 && index < m_windowModel.count()) {
685 return m_windowModel[index].window->id();
686 } else {
687 return 0;
688 }
691void TopLevelWindowModel::raiseId(int id)
693 if (m_modelState == IdleState) {
694 DEBUG_MSG << "(id=" << id << ") - do it now.";
695 doRaiseId(id);
696 } else {
697 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
698 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
699 // if we perform yet another model change straight away.
700 //
701 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
702 // the index is definitely within bounds.
703 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
704 }
707void TopLevelWindowModel::doRaiseId(int id)
709 int fromIndex = indexForId(id);
710 // can't raise something that doesn't exist or that it's already on top
711 if (fromIndex != -1 && fromIndex != 0) {
712 auto surface = m_windowModel[fromIndex].window->surface();
713 if (surface) {
714 m_surfaceManager->raise(surface);
715 } else {
716 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
717 // miral can do about it.
718 move(fromIndex, 0);
719 }
720 }
723void TopLevelWindowModel::setFocusedWindow(Window *window)
725 if (window != m_focusedWindow) {
726 INFO_MSG << "(" << window << ")";
728 Window* previousWindow = m_focusedWindow;
730 m_focusedWindow = window;
731 Q_EMIT focusedWindowChanged(m_focusedWindow);
733 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
734 // do it ourselves. miral doesn't know about this window
735 previousWindow->setFocused(false);
736 }
737 }
740unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
742 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
745Window* TopLevelWindowModel::focusedWindow() const
747 return m_focusedWindow;
750void TopLevelWindowModel::move(int from, int to)
752 if (from == to) return;
753 DEBUG_MSG << " from=" << from << " to=" << to;
755 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
756 QModelIndex parent;
757 /* When moving an item down, the destination index needs to be incremented
758 by one, as explained in the documentation:
759 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
761 Q_ASSERT(m_modelState == IdleState);
762 m_modelState = MovingState;
764 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
765#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
766 const auto &window = m_windowModel.takeAt(from);
767 m_windowModel.insert(to, window);
768#else
769 m_windowModel.move(from, to);
770#endif
771 endMoveRows();
773 Q_EMIT listChanged();
774 m_modelState = IdleState;
776 INFO_MSG << " after " << toString();
777 }
779void TopLevelWindowModel::onModificationsStarted()
783void TopLevelWindowModel::onModificationsEnded()
785 if (m_focusedWindowCleared) {
786 setFocusedWindow(nullptr);
787 }
788 // reset
789 m_focusedWindowCleared = false;
792void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
794 DEBUG_MSG << "(" << forbiddenId << ")";
796 for (int i = 0; i < m_windowModel.count(); ++i) {
797 Window *window = m_windowModel[i].window;
798 if (window->id() != forbiddenId) {
799 window->activate();
800 }
801 }
804void TopLevelWindowModel::refreshWindows()
806 DEBUG_MSG << "()";
807 clear();
809 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
811 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](unity::shell::application::MirSurfaceInterface* surface) {
812 if (surface->parentSurface()) {
813 // Wrap it in a Window so that we keep focusedWindow() up to date.
814 Window *window = createWindow(surface);
815 connect(surface, &QObject::destroyed, window, [=](){
816 window->setSurface(nullptr);
817 window->deleteLater();
818 });
819 } else {
820 if (surface->type() == Mir::InputMethodType) {
821 setInputMethodWindow(createWindow(surface));
822 } else {
823 auto *application = m_applicationManager->findApplicationWithSurface(surface);
824 if (application) {
825 prependSurface(surface, application);
826 } else {
827 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
828 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
829 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
830 Window *promptWindow = createWindow(surface);
831 connect(surface, &QObject::destroyed, promptWindow, [=](){
832 promptWindow->setSurface(nullptr);
833 promptWindow->deleteLater();
834 });
835 }
836 }
837 }
838 });
841void TopLevelWindowModel::clear()
843 DEBUG_MSG << "()";
845 while(m_windowModel.count() > 0) {
846 ModelEntry entry = m_windowModel.takeAt(0);
847 disconnect(entry.window, 0, this, 0);
848 delete entry.window;
849 }
850 m_allSurfaces.clear();
851 setFocusedWindow(nullptr);
852 m_focusedWindowCleared = false;