on a705d30 help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.cpp
863 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"
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 "windowmanagementpolicy.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 && m_workspace) {
70 if (m_workspace->isActive()) {
71 connectApplicationManager();
72 }
74 if (m_surfaceManager) {
75 refreshWindows();
76 }
77 }
79 Q_EMIT applicationManagerChanged(m_applicationManager);
81 endResetModel();
82 m_modelState = IdleState;
85void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
87 if (surfaceManager == m_surfaceManager) {
88 return;
89 }
91 DEBUG_MSG << "(" << surfaceManager << ")";
93 Q_ASSERT(m_modelState == IdleState);
94 m_modelState = ResettingState;
96 beginResetModel();
98 if (m_surfaceManager) {
99 disconnect(m_surfaceManager, 0, this, 0);
100 }
102 m_surfaceManager = surfaceManager;
104 if (m_surfaceManager) {
105 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
106 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
107 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
108 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
110 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
111 this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
112 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
113 this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
115 if (m_workspace && m_applicationManager) {
116 refreshWindows();
117 }
118 }
120 Q_EMIT surfaceManagerChanged(m_surfaceManager);
122 endResetModel();
123 m_modelState = IdleState;
126void TopLevelWindowModel::setWorkspace(Workspace *workspace)
128 if (workspace == m_workspace) {
129 return;
130 }
132 DEBUG_MSG << "(" << workspace << ")";
134 Q_ASSERT(m_modelState == IdleState);
135 m_modelState = ResettingState;
137 beginResetModel();
139 if (m_workspace) {
140 m_windowModel.clear();
141 m_allSurfaces.clear();
142 if (m_applicationManager) {
143 disconnect(m_applicationManager, 0, this, 0);
144 }
145 }
147 disconnect(m_applicationManager, 0 ,this, 0);
149 m_workspace = workspace;
151 if (m_workspace) {
152 connect(m_workspace, &Workspace::activeChanged, this, [this](bool active) {
153 if (m_applicationManager) {
154 if (!active) disconnect(m_applicationManager, 0, this, 0);
155 else {
156 connectApplicationManager();
157 }
158 }
159 });
161 if (m_applicationManager && m_workspace->isActive()) {
162 connectApplicationManager();
163 }
165 if (m_surfaceManager && m_applicationManager) {
166 refreshWindows();
167 }
168 }
170 Q_EMIT workspaceChanged(workspace);
172 endResetModel();
173 m_modelState = IdleState;
176void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
178 DEBUG_MSG << "(" << application->appId() << ")";
180 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
181 prependPlaceholder(application);
182 }
185void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
187 DEBUG_MSG << "(" << application->appId() << ")";
189 Q_ASSERT(m_modelState == IdleState);
191 int i = 0;
192 while (i < m_windowModel.count()) {
193 if (m_windowModel.at(i).application == application) {
194 removeAt(i);
195 } else {
196 ++i;
197 }
198 }
201void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
203 INFO_MSG << "(" << application->appId() << ")";
205 prependSurfaceHelper(nullptr, application);
208void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
210 Q_ASSERT(surface != nullptr);
212 bool filledPlaceholder = false;
213 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
214 ModelEntry &entry = m_windowModel[i];
215 if (entry.application == application && entry.window->surface() == nullptr) {
216 entry.window->setSurface(surface);
217 connectSurface(surface);
218 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
219 << ", filling out placeholder. after: " << toString();
220 filledPlaceholder = true;
221 }
222 }
224 if (!filledPlaceholder) {
225 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
226 prependSurfaceHelper(surface, application);
227 }
230void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
232 if (m_modelState == IdleState) {
233 m_modelState = InsertingState;
234 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
235 } else {
236 Q_ASSERT(m_modelState == ResettingState);
237 // No point in signaling anything if we're resetting the whole model
238 }
240 Window *window = createWindow(surface);
242 m_windowModel.prepend(ModelEntry(window, application));
243 m_allSurfaces.insert(surface);
244 if (surface) {
245 connectSurface(surface);
246 }
248 if (m_modelState == InsertingState) {
249 endInsertRows();
250 Q_EMIT countChanged();
251 Q_EMIT listChanged();
252 m_modelState = IdleState;
253 }
255 if (!surface) {
256 activateEmptyWindow(window);
257 }
259 INFO_MSG << " after " << toString();
262void TopLevelWindowModel::connectWindow(Window *window)
264 connect(window, &Window::focusRequested, this, [this, window]() {
265 if (!window->surface()) {
266 activateEmptyWindow(window);
267 }
268 });
270 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
271 if (window->surface()) {
272 // Condense changes to the focused window
273 // eg: Do focusedWindow=A to focusedWindow=B instead of
274 // focusedWindow=A to focusedWindow=null to focusedWindow=B
275 if (focused) {
276 Q_ASSERT(m_newlyFocusedWindow == nullptr);
277 m_focusedWindowChanged = true;
278 m_newlyFocusedWindow = window;
279 } else if (m_focusedWindow == window) {
280 m_focusedWindowChanged = true;
281 } else {
282 // don't clear the focused window if you were not there in the first place
283 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
284 }
285 }
286 });
288 connect(window, &Window::closeRequested, this, [this, window]() {
289 if (!window->surface()) {
290 // do things ourselves as miral doesn't know about this window
291 int id = window->id();
292 int index = indexForId(id);
293 bool focusOther = false;
294 Q_ASSERT(index >= 0);
295 if (window->focused()) {
296 focusOther = true;
297 }
298 m_windowModel[index].application->close();
299 if (focusOther) {
300 activateTopMostWindowWithoutId(id);
301 }
302 }
303 });
305 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
306 activateEmptyWindow(window);
307 });
310void TopLevelWindowModel::activateEmptyWindow(Window *window)
312 Q_ASSERT(!window->surface());
313 DEBUG_MSG << "(" << window << ")";
315 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
316 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
318 window->setFocused(true);
319 raiseId(window->id());
320 Window *previousWindow = m_focusedWindow;
321 setFocusedWindow(window);
322 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
323 m_surfaceManager->activate(nullptr);
324 }
327void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
329 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
330 if (!live) {
331 onSurfaceDied(surface);
332 }
333 });
334 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
337void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
339 if (surface->type() == Mir::InputMethodType) {
340 removeInputMethodWindow();
341 return;
342 }
344 int i = indexOf(surface);
345 if (i == -1) {
346 return;
347 }
349 auto application = m_windowModel[i].application;
351 // can't be starting if it already has a surface
352 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
354 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
355 m_windowModel[i].removeOnceSurfaceDestroyed = true;
356 } else {
357 // assume it got killed by the out-of-memory daemon.
358 //
359 // So leave entry in the model and only remove its surface, so shell can display a screenshot
360 // in its place.
361 m_windowModel[i].removeOnceSurfaceDestroyed = false;
362 }
365void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
367 int i = indexOf(surface);
368 if (i == -1) {
369 return;
370 }
372 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
373 removeAt(i);
374 } else {
375 auto window = m_windowModel[i].window;
376 window->setSurface(nullptr);
377 window->setFocused(false);
378 m_allSurfaces.remove(surface);
379 INFO_MSG << " Removed surface from entry. After: " << toString();
380 }
383Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
385 int id = generateId();
386 Window *qmlWindow = new Window(id, this);
387 connectWindow(qmlWindow);
388 if (surface) {
389 qmlWindow->setSurface(surface);
390 }
391 return qmlWindow;
395void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
399void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
400 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
402 if (workspace != m_workspace->workspace()) return;
404 Q_FOREACH(auto surface, surfaces) {
405 if (m_allSurfaces.contains(surface)) continue;
407 if (surface->parentSurface()) {
408 // Wrap it in a Window so that we keep focusedWindow() up to date.
409 Window *window = createWindow(surface);
410 connect(surface, &QObject::destroyed, window, [=](){
411 window->setSurface(nullptr);
412 window->deleteLater();
413 });
414 } else {
415 if (surface->type() == Mir::InputMethodType) {
416 setInputMethodWindow(createWindow(surface));
417 } else {
418 auto *application = m_applicationManager->findApplicationWithSurface(surface);
419 if (application) {
420 prependSurface(surface, application);
421 } else {
422 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
423 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
424 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
425 Window *promptWindow = createWindow(surface);
426 connect(surface, &QObject::destroyed, promptWindow, [=](){
427 promptWindow->setSurface(nullptr);
428 promptWindow->deleteLater();
429 });
430 }
431 }
432 }
433 }
436void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
437 const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
439 if (workspace != m_workspace->workspace()) return;
441 int start = -1;
442 int end = -1;
443 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
444 auto surface = *iter;
445 iter++;
447 // Do removals in adjacent blocks.
448 start = end = indexOf(surface);
449 if (start == -1) {
450 // probably a child surface
451 m_allSurfaces.remove(surface);
452 continue;
453 }
454 while(iter != surfaces.constEnd()) {
455 int index = indexOf(*iter);
456 if (index != end+1) {
457 break;
458 }
459 end++;
460 iter++;
461 }
463 if (m_modelState == IdleState) {
464 beginRemoveRows(QModelIndex(), start, end);
465 m_modelState = RemovingState;
466 } else {
467 Q_ASSERT(m_modelState == ResettingState);
468 // No point in signaling anything if we're resetting the whole model
469 }
471 for (int index = start; index <= end; index++) {
472 auto window = m_windowModel[start].window;
473 window->setSurface(nullptr);
474 window->setFocused(false);
476 m_windowModel.removeAt(start);
477 m_allSurfaces.remove(window->surface());
478 }
480 if (m_modelState == RemovingState) {
481 endRemoveRows();
482 Q_EMIT countChanged();
483 Q_EMIT listChanged();
484 m_modelState = IdleState;
485 }
486 }
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;
501 window->setSurface(nullptr);
502 window->setFocused(false);
504 m_windowModel.removeAt(index);
505 m_allSurfaces.remove(window->surface());
507 if (m_modelState == RemovingState) {
508 endRemoveRows();
509 Q_EMIT countChanged();
510 Q_EMIT listChanged();
511 m_modelState = IdleState;
512 }
514 disconnect(window, 0, this, 0);
515 if (m_focusedWindow == window) {
516 setFocusedWindow(nullptr);
517 }
518 delete window;
520 INFO_MSG << " after " << toString();
523void TopLevelWindowModel::setInputMethodWindow(Window *window)
525 if (m_inputMethodWindow) {
526 qWarning("Multiple Input Method Surfaces created, removing the old one!");
527 delete m_inputMethodWindow;
528 }
529 m_inputMethodWindow = window;
530 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
533void TopLevelWindowModel::removeInputMethodWindow()
535 if (m_inputMethodWindow) {
536 delete m_inputMethodWindow;
537 m_inputMethodWindow = nullptr;
538 Q_EMIT inputMethodSurfaceChanged(nullptr);
539 }
542void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
544 DEBUG_MSG << "(" << surfaces << ")";
545 const int raiseCount = surfaces.size();
546 for (int i = 0; i < raiseCount; i++) {
547 int fromIndex = findIndexOf(surfaces[i]);
548 if (fromIndex != -1) {
549 move(fromIndex, 0);
550 }
551 }
554int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
556 return m_windowModel.count();
559QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
561 if (index.row() < 0 || index.row() >= m_windowModel.size())
562 return QVariant();
564 if (role == WindowRole) {
565 Window *window = m_windowModel.at(index.row()).window;
566 return QVariant::fromValue(window);
567 } else if (role == ApplicationRole) {
568 return QVariant::fromValue(m_windowModel.at(index.row()).application);
569 } else {
570 return QVariant();
571 }
574void TopLevelWindowModel::connectApplicationManager()
576 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
577 this, [this](const QModelIndex &/*parent*/, int first, int last) {
578 for (int i = first; i <= last; ++i) {
579 auto application = m_applicationManager->get(i);
580 addApplication(application);
581 }
582 });
584 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
585 this, [this](const QModelIndex &/*parent*/, int first, int last) {
586 for (int i = first; i <= last; ++i) {
587 auto application = m_applicationManager->get(i);
588 removeApplication(application);
589 }
590 });
594int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
596 for (int i=0; i<m_windowModel.count(); i++) {
597 if (m_windowModel[i].window->surface() == surface) {
598 return i;
599 }
600 }
601 return -1;
604int TopLevelWindowModel::generateId()
606 int id = m_nextId;
607 m_nextId = nextFreeId(nextId(id), id);
608 return id;
611int TopLevelWindowModel::nextId(int id) const
613 if (id == m_maxId) {
614 return id = 1;
615 } else {
616 return id + 1;
617 }
620int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
622 int firstCandidateId = candidateId;
624 while (indexForId(candidateId) != -1 || candidateId == latestId) {
625 candidateId = nextId(candidateId);
627 if (candidateId == firstCandidateId) {
628 qFatal("TopLevelWindowModel: run out of window ids.");
629 }
630 }
632 return candidateId;
635QString TopLevelWindowModel::toString()
637 QString str;
638 for (int i = 0; i < m_windowModel.count(); ++i) {
639 auto item = m_windowModel.at(i);
641 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
642 .arg(QString::number(i),
643 item.application->appId(),
644 QString::number((qintptr)item.window->surface(), 16),
645 QString::number(item.window->id()));
647 if (i > 0) {
648 str.append(",");
649 }
650 str.append(itemStr);
651 }
652 return str;
655int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
657 for (int i = 0; i < m_windowModel.count(); ++i) {
658 if (m_windowModel.at(i).window->surface() == surface) {
659 return i;
660 }
661 }
662 return -1;
665int TopLevelWindowModel::indexForId(int id) const
667 for (int i = 0; i < m_windowModel.count(); ++i) {
668 if (m_windowModel[i].window->id() == id) {
669 return i;
670 }
671 }
672 return -1;
675Window *TopLevelWindowModel::windowAt(int index) const
677 if (index >=0 && index < m_windowModel.count()) {
678 return m_windowModel[index].window;
679 } else {
680 return nullptr;
681 }
684unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
686 if (index >=0 && index < m_windowModel.count()) {
687 return m_windowModel[index].window->surface();
688 } else {
689 return nullptr;
690 }
693unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
695 if (index >=0 && index < m_windowModel.count()) {
696 return m_windowModel[index].application;
697 } else {
698 return nullptr;
699 }
702int TopLevelWindowModel::idAt(int index) const
704 if (index >=0 && index < m_windowModel.count()) {
705 return m_windowModel[index].window->id();
706 } else {
707 return 0;
708 }
711void TopLevelWindowModel::raiseId(int id)
713 if (m_modelState == IdleState) {
714 DEBUG_MSG << "(id=" << id << ") - do it now.";
715 doRaiseId(id);
716 } else {
717 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
718 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
719 // if we perform yet another model change straight away.
720 //
721 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
722 // the index is definitely within bounds.
723 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
724 }
727void TopLevelWindowModel::doRaiseId(int id)
729 int fromIndex = indexForId(id);
730 // can't raise something that doesn't exist or that it's already on top
731 if (fromIndex != -1 && fromIndex != 0) {
732 auto surface = m_windowModel[fromIndex].window->surface();
733 if (surface) {
734 m_surfaceManager->raise(surface);
735 } else {
736 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
737 // miral can do about it.
738 move(fromIndex, 0);
739 }
740 }
743void TopLevelWindowModel::setFocusedWindow(Window *window)
745 if (window != m_focusedWindow) {
746 INFO_MSG << "(" << window << ")";
748 Window* previousWindow = m_focusedWindow;
750 m_focusedWindow = window;
751 Q_EMIT focusedWindowChanged(m_focusedWindow);
753 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
754 // do it ourselves. miral doesn't know about this window
755 previousWindow->setFocused(false);
756 }
757 }
760unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
762 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
765Window* TopLevelWindowModel::focusedWindow() const
767 return m_focusedWindow;
770void TopLevelWindowModel::move(int from, int to)
772 if (from == to) return;
773 DEBUG_MSG << " from=" << from << " to=" << to;
775 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
776 QModelIndex parent;
777 /* When moving an item down, the destination index needs to be incremented
778 by one, as explained in the documentation:
779 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
781 Q_ASSERT(m_modelState == IdleState);
782 m_modelState = MovingState;
784 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
785#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
786 const auto &window = m_windowModel.takeAt(from);
787 m_windowModel.insert(to, window);
788#else
789 m_windowModel.move(from, to);
790#endif
791 endMoveRows();
793 Q_EMIT listChanged();
794 m_modelState = IdleState;
796 INFO_MSG << " after " << toString();
797 }
799void TopLevelWindowModel::onModificationsStarted()
803void TopLevelWindowModel::onModificationsEnded()
805 if (m_focusedWindowChanged) {
806 setFocusedWindow(m_newlyFocusedWindow);
807 }
808 // reset
809 m_focusedWindowChanged = false;
810 m_newlyFocusedWindow = nullptr;
813void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
815 DEBUG_MSG << "(" << forbiddenId << ")";
817 for (int i = 0; i < m_windowModel.count(); ++i) {
818 Window *window = m_windowModel[i].window;
819 if (window->id() != forbiddenId) {
820 window->activate();
821 }
822 }
825void TopLevelWindowModel::refreshWindows()
827 DEBUG_MSG << "()";
829 m_windowModel.clear();
830 m_allSurfaces.clear();
832 WindowManagementPolicy::instance()->forEachWindowInWorkspace(m_workspace->workspace(), [this](const miral::Window &window) {
833 auto surface = m_surfaceManager->surfaceFor(window);
834 if (surface) {
835 if (surface->parentSurface()) {
836 // Wrap it in a Window so that we keep focusedWindow() up to date.
837 Window *window = createWindow(surface);
838 connect(surface, &QObject::destroyed, window, [=](){
839 window->setSurface(nullptr);
840 window->deleteLater();
841 });
842 } else {
843 if (surface->type() == Mir::InputMethodType) {
844 setInputMethodWindow(createWindow(surface));
845 } else {
846 auto *application = m_applicationManager->findApplicationWithSurface(surface);
847 if (application) {
848 prependSurface(surface, application);
849 } else {
850 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
851 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
852 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
853 Window *promptWindow = createWindow(surface);
854 connect(surface, &QObject::destroyed, promptWindow, [=](){
855 promptWindow->setSurface(nullptr);
856 promptWindow->deleteLater();
857 });
858 }
859 }
860 }
861 }
862 });