on abe56ae help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.cpp
858 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::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
124 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
125 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
127 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
128 this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
129 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
130 this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
131 }
133 refreshWindows();
135 endResetModel();
136 m_modelState = IdleState;
139void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
141 DEBUG_MSG << "(" << application->appId() << ")";
143 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
144 prependPlaceholder(application);
145 }
148void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
150 DEBUG_MSG << "(" << application->appId() << ")";
152 Q_ASSERT(m_modelState == IdleState);
154 int i = 0;
155 while (i < m_windowModel.count()) {
156 if (m_windowModel.at(i).application == application) {
157 deleteAt(i);
158 } else {
159 ++i;
160 }
161 }
164void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
166 INFO_MSG << "(" << application->appId() << ")";
168 prependSurfaceHelper(nullptr, application);
171void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
173 Q_ASSERT(surface != nullptr);
175 connectSurface(surface);
176 m_allSurfaces.insert(surface);
178 bool filledPlaceholder = false;
179 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
180 ModelEntry &entry = m_windowModel[i];
181 if (entry.application == application && entry.window->surface() == nullptr) {
182 entry.window->setSurface(surface);
183 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
184 << ", filling out placeholder. after: " << toString();
185 filledPlaceholder = true;
186 }
187 }
189 if (!filledPlaceholder) {
190 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
191 prependSurfaceHelper(surface, application);
192 }
195void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
198 Window *window = createWindow(surface);
200 connect(window, &Window::stateChanged, this, [=](Mir::State newState) {
201 if (newState == Mir::HiddenState) {
202 // Comply, removing it from our model. Just as if it didn't exist anymore.
203 removeAt(indexForId(window->id()));
204 } else {
205 if (indexForId(window->id()) == -1) {
206 // was probably hidden before. put it back on the list
207 auto *application = m_applicationManager->findApplicationWithSurface(window->surface());
208 Q_ASSERT(application);
209 prependWindow(window, application);
210 }
211 }
212 });
214 prependWindow(window, application);
216 if (!surface) {
217 activateEmptyWindow(window);
218 }
220 INFO_MSG << " after " << toString();
223void TopLevelWindowModel::prependWindow(Window *window, unityapi::ApplicationInfoInterface *application)
225 if (m_modelState == IdleState) {
226 m_modelState = InsertingState;
227 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
228 } else {
229 Q_ASSERT(m_modelState == ResettingState);
230 // No point in signaling anything if we're resetting the whole model
231 }
233 m_windowModel.prepend(ModelEntry(window, application));
235 if (m_modelState == InsertingState) {
236 endInsertRows();
237 Q_EMIT countChanged();
238 Q_EMIT listChanged();
239 m_modelState = IdleState;
240 }
243void TopLevelWindowModel::connectWindow(Window *window)
245 connect(window, &Window::focusRequested, this, [this, window]() {
246 if (!window->surface()) {
247 activateEmptyWindow(window);
248 }
249 });
251 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
252 if (window->surface()) {
253 if (focused) {
254 setFocusedWindow(window);
255 m_focusedWindowCleared = false;
256 } else if (m_focusedWindow == window) {
257 // Condense changes to the focused window
258 // eg: Do focusedWindow=A to focusedWindow=B instead of
259 // focusedWindow=A to focusedWindow=null to focusedWindow=B
260 m_focusedWindowCleared = true;
261 } else {
262 // don't clear the focused window if you were not there in the first place
263 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
264 }
265 }
266 });
268 connect(window, &Window::closeRequested, this, [this, window]() {
269 if (!window->surface()) {
270 // do things ourselves as miral doesn't know about this window
271 int id = window->id();
272 int index = indexForId(id);
273 bool focusOther = false;
274 Q_ASSERT(index >= 0);
275 if (window->focused()) {
276 focusOther = true;
277 }
278 m_windowModel[index].application->close();
279 if (focusOther) {
280 activateTopMostWindowWithoutId(id);
281 }
282 }
283 });
285 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
286 activateEmptyWindow(window);
287 });
289 connect(window, &Window::liveChanged, this, [this, window](bool isAlive) {
290 if (!isAlive && window->state() == Mir::HiddenState) {
291 // Hidden windows are not in the model. So just delete it right away.
292 delete window;
293 }
294 });
297void TopLevelWindowModel::activateEmptyWindow(Window *window)
299 Q_ASSERT(!window->surface());
300 DEBUG_MSG << "(" << window << ")";
302 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
303 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
305 window->setFocused(true);
306 raiseId(window->id());
307 Window *previousWindow = m_focusedWindow;
308 setFocusedWindow(window);
309 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
310 m_surfaceManager->activate(nullptr);
311 }
314void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
316 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
317 if (!live) {
318 onSurfaceDied(surface);
319 }
320 });
321 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
324void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
326 if (surface->type() == Mir::InputMethodType) {
327 removeInputMethodWindow();
328 return;
329 }
331 int i = indexOf(surface);
332 if (i == -1) {
333 return;
334 }
336 auto application = m_windowModel[i].application;
338 // can't be starting if it already has a surface
339 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
341 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
342 m_windowModel[i].removeOnceSurfaceDestroyed = true;
343 } else {
344 // assume it got killed by the out-of-memory daemon.
345 //
346 // So leave entry in the model and only remove its surface, so shell can display a screenshot
347 // in its place.
348 m_windowModel[i].removeOnceSurfaceDestroyed = false;
349 }
352void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
354 int i = indexOf(surface);
355 if (i == -1) {
356 return;
357 }
359 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
360 deleteAt(i);
361 } else {
362 auto window = m_windowModel[i].window;
363 window->setSurface(nullptr);
364 window->setFocused(false);
365 m_allSurfaces.remove(surface);
366 INFO_MSG << " Removed surface from entry. After: " << toString();
367 }
370Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
372 int id = generateId();
373 Window *qmlWindow = new Window(id, this);
374 connectWindow(qmlWindow);
375 if (surface) {
376 qmlWindow->setSurface(surface);
377 }
378 return qmlWindow;
381void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
382 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
384 if (!m_workspace || !m_applicationManager) return;
385 if (workspace != m_workspace->workspace()) return;
387 Q_FOREACH(auto surface, surfaces) {
388 if (m_allSurfaces.contains(surface)) continue;
390 if (surface->parentSurface()) {
391 // Wrap it in a Window so that we keep focusedWindow() up to date.
392 Window *window = createWindow(surface);
393 connect(surface, &QObject::destroyed, window, [=](){
394 window->setSurface(nullptr);
395 window->deleteLater();
396 });
397 } else {
398 if (surface->type() == Mir::InputMethodType) {
399 connectSurface(surface);
400 setInputMethodWindow(createWindow(surface));
401 } else {
402 auto *application = m_applicationManager->findApplicationWithSurface(surface);
403 if (application) {
404 if (surface->state() == Mir::HiddenState) {
405 // Ignore it until it's finally shown
406 connect(surface, &unityapi::MirSurfaceInterface::stateChanged, this, [=](Mir::State newState) {
407 Q_ASSERT(newState != Mir::HiddenState);
408 disconnect(surface, &unityapi::MirSurfaceInterface::stateChanged, this, 0);
409 prependSurface(surface, application);
410 });
411 } else {
412 prependSurface(surface, application);
413 }
414 } else {
415 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
416 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
417 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
418 Window *promptWindow = createWindow(surface);
419 connect(surface, &QObject::destroyed, promptWindow, [=](){
420 promptWindow->setSurface(nullptr);
421 promptWindow->deleteLater();
422 });
423 }
424 }
425 }
426 }
429void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
430 const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
432 if (!m_workspace || workspace != m_workspace->workspace()) return;
434 int start = -1;
435 int end = -1;
436 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
437 auto surface = *iter;
438 iter++;
440 // Do removals in adjacent blocks.
441 start = end = indexOf(surface);
442 if (start == -1) {
443 // probably a child surface
444 m_allSurfaces.remove(surface);
445 continue;
446 }
447 while(iter != surfaces.constEnd()) {
448 int index = indexOf(*iter);
449 if (index != end+1) {
450 break;
451 }
452 end++;
453 iter++;
454 }
456 if (m_modelState == IdleState) {
457 beginRemoveRows(QModelIndex(), start, end);
458 m_modelState = RemovingState;
459 } else {
460 Q_ASSERT(m_modelState == ResettingState);
461 // No point in signaling anything if we're resetting the whole model
462 }
464 for (int index = start; index <= end; index++) {
465 auto window = m_windowModel[start].window;
466 window->setSurface(nullptr);
467 window->setFocused(false);
469 m_windowModel.removeAt(start);
470 m_allSurfaces.remove(surface);
471 }
473 if (m_modelState == RemovingState) {
474 endRemoveRows();
475 Q_EMIT countChanged();
476 Q_EMIT listChanged();
477 m_modelState = IdleState;
478 }
479 }
482void TopLevelWindowModel::deleteAt(int index)
484 auto window = m_windowModel[index].window;
486 removeAt(index);
488 window->setSurface(nullptr);
490 delete window;
493void TopLevelWindowModel::removeAt(int index)
495 if (m_modelState == IdleState) {
496 beginRemoveRows(QModelIndex(), index, index);
497 m_modelState = RemovingState;
498 } else {
499 Q_ASSERT(m_modelState == ResettingState);
500 // No point in signaling anything if we're resetting the whole model
501 }
503 auto window = m_windowModel[index].window;
504 auto surface = window->surface();
506 if (!window->surface()) {
507 window->setFocused(false);
508 }
510 m_windowModel.removeAt(index);
511 m_allSurfaces.remove(surface);
513 if (m_modelState == RemovingState) {
514 endRemoveRows();
515 Q_EMIT countChanged();
516 Q_EMIT listChanged();
517 m_modelState = IdleState;
518 }
520 if (m_focusedWindow == window) {
521 setFocusedWindow(nullptr);
522 m_focusedWindowCleared = false;
523 }
525 INFO_MSG << " after " << toString();
528void TopLevelWindowModel::setInputMethodWindow(Window *window)
530 if (m_inputMethodWindow) {
531 qWarning("Multiple Input Method Surfaces created, removing the old one!");
532 delete m_inputMethodWindow;
533 }
534 m_inputMethodWindow = window;
535 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
538void TopLevelWindowModel::removeInputMethodWindow()
540 if (m_inputMethodWindow) {
541 delete m_inputMethodWindow;
542 m_inputMethodWindow = nullptr;
543 Q_EMIT inputMethodSurfaceChanged(nullptr);
544 }
547void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
549 DEBUG_MSG << "(" << surfaces << ")";
550 const int raiseCount = surfaces.size();
551 for (int i = 0; i < raiseCount; i++) {
552 int fromIndex = findIndexOf(surfaces[i]);
553 if (fromIndex != -1) {
554 move(fromIndex, 0);
555 }
556 }
559int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
561 return m_windowModel.count();
564QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
566 if (index.row() < 0 || index.row() >= m_windowModel.size())
567 return QVariant();
569 if (role == WindowRole) {
570 Window *window = m_windowModel.at(index.row()).window;
571 return QVariant::fromValue(window);
572 } else if (role == ApplicationRole) {
573 return QVariant::fromValue(m_windowModel.at(index.row()).application);
574 } else {
575 return QVariant();
576 }
579int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
581 for (int i=0; i<m_windowModel.count(); i++) {
582 if (m_windowModel[i].window->surface() == surface) {
583 return i;
584 }
585 }
586 return -1;
589int TopLevelWindowModel::generateId()
591 int id = m_nextId;
592 m_nextId = nextFreeId(nextId(id), id);
593 return id;
596int TopLevelWindowModel::nextId(int id) const
598 if (id == m_maxId) {
599 return id = 1;
600 } else {
601 return id + 1;
602 }
605int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
607 int firstCandidateId = candidateId;
609 while (indexForId(candidateId) != -1 || candidateId == latestId) {
610 candidateId = nextId(candidateId);
612 if (candidateId == firstCandidateId) {
613 qFatal("TopLevelWindowModel: run out of window ids.");
614 }
615 }
617 return candidateId;
620QString TopLevelWindowModel::toString()
622 QString str;
623 for (int i = 0; i < m_windowModel.count(); ++i) {
624 auto item = m_windowModel.at(i);
626 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
627 .arg(QString::number(i),
628 item.application->appId(),
629 QString::number((qintptr)item.window->surface(), 16),
630 QString::number(item.window->id()));
632 if (i > 0) {
633 str.append(",");
634 }
635 str.append(itemStr);
636 }
637 return str;
640int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
642 for (int i = 0; i < m_windowModel.count(); ++i) {
643 if (m_windowModel.at(i).window->surface() == surface) {
644 return i;
645 }
646 }
647 return -1;
650int TopLevelWindowModel::indexForId(int id) const
652 for (int i = 0; i < m_windowModel.count(); ++i) {
653 if (m_windowModel[i].window->id() == id) {
654 return i;
655 }
656 }
657 return -1;
660Window *TopLevelWindowModel::windowAt(int index) const
662 if (index >=0 && index < m_windowModel.count()) {
663 return m_windowModel[index].window;
664 } else {
665 return nullptr;
666 }
669unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
671 if (index >=0 && index < m_windowModel.count()) {
672 return m_windowModel[index].window->surface();
673 } else {
674 return nullptr;
675 }
678unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
680 if (index >=0 && index < m_windowModel.count()) {
681 return m_windowModel[index].application;
682 } else {
683 return nullptr;
684 }
687int TopLevelWindowModel::idAt(int index) const
689 if (index >=0 && index < m_windowModel.count()) {
690 return m_windowModel[index].window->id();
691 } else {
692 return 0;
693 }
696void TopLevelWindowModel::raiseId(int id)
698 if (m_modelState == IdleState) {
699 DEBUG_MSG << "(id=" << id << ") - do it now.";
700 doRaiseId(id);
701 } else {
702 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
703 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
704 // if we perform yet another model change straight away.
705 //
706 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
707 // the index is definitely within bounds.
708 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
709 }
712void TopLevelWindowModel::doRaiseId(int id)
714 int fromIndex = indexForId(id);
715 // can't raise something that doesn't exist or that it's already on top
716 if (fromIndex != -1 && fromIndex != 0) {
717 auto surface = m_windowModel[fromIndex].window->surface();
718 if (surface) {
719 m_surfaceManager->raise(surface);
720 } else {
721 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
722 // miral can do about it.
723 move(fromIndex, 0);
724 }
725 }
728void TopLevelWindowModel::setFocusedWindow(Window *window)
730 if (window != m_focusedWindow) {
731 INFO_MSG << "(" << window << ")";
733 Window* previousWindow = m_focusedWindow;
735 m_focusedWindow = window;
736 Q_EMIT focusedWindowChanged(m_focusedWindow);
738 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
739 // do it ourselves. miral doesn't know about this window
740 previousWindow->setFocused(false);
741 }
742 }
745unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
747 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
750Window* TopLevelWindowModel::focusedWindow() const
752 return m_focusedWindow;
755void TopLevelWindowModel::move(int from, int to)
757 if (from == to) return;
758 DEBUG_MSG << " from=" << from << " to=" << to;
760 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
761 QModelIndex parent;
762 /* When moving an item down, the destination index needs to be incremented
763 by one, as explained in the documentation:
764 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
766 Q_ASSERT(m_modelState == IdleState);
767 m_modelState = MovingState;
769 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
770#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
771 const auto &window = m_windowModel.takeAt(from);
772 m_windowModel.insert(to, window);
773#else
774 m_windowModel.move(from, to);
775#endif
776 endMoveRows();
778 Q_EMIT listChanged();
779 m_modelState = IdleState;
781 INFO_MSG << " after " << toString();
782 }
784void TopLevelWindowModel::onModificationsStarted()
788void TopLevelWindowModel::onModificationsEnded()
790 if (m_focusedWindowCleared) {
791 setFocusedWindow(nullptr);
792 }
793 // reset
794 m_focusedWindowCleared = false;
797void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
799 DEBUG_MSG << "(" << forbiddenId << ")";
801 for (int i = 0; i < m_windowModel.count(); ++i) {
802 Window *window = m_windowModel[i].window;
803 if (window->id() != forbiddenId) {
804 window->activate();
805 }
806 }
809void TopLevelWindowModel::refreshWindows()
811 DEBUG_MSG << "()";
812 clear();
814 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
816 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](unity::shell::application::MirSurfaceInterface* surface) {
817 if (surface->parentSurface()) {
818 // Wrap it in a Window so that we keep focusedWindow() up to date.
819 Window *window = createWindow(surface);
820 connect(surface, &QObject::destroyed, window, [=](){
821 window->setSurface(nullptr);
822 window->deleteLater();
823 });
824 } else {
825 if (surface->type() == Mir::InputMethodType) {
826 setInputMethodWindow(createWindow(surface));
827 } else {
828 auto *application = m_applicationManager->findApplicationWithSurface(surface);
829 if (application) {
830 prependSurface(surface, application);
831 } else {
832 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
833 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
834 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
835 Window *promptWindow = createWindow(surface);
836 connect(surface, &QObject::destroyed, promptWindow, [=](){
837 promptWindow->setSurface(nullptr);
838 promptWindow->deleteLater();
839 });
840 }
841 }
842 }
843 });
846void TopLevelWindowModel::clear()
848 DEBUG_MSG << "()";
850 while(m_windowModel.count() > 0) {
851 ModelEntry entry = m_windowModel.takeAt(0);
852 disconnect(entry.window, 0, this, 0);
853 delete entry.window;
854 }
855 m_allSurfaces.clear();
856 setFocusedWindow(nullptr);
857 m_focusedWindowCleared = false;