on b3c03fa help sign in
unity8/plugins/WindowManager/TopLevelWindowModel.cpp
865 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 (!m_workspace || !m_applicationManager) return;
403 if (workspace != m_workspace->workspace()) return;
405 Q_FOREACH(auto surface, surfaces) {
406 if (m_allSurfaces.contains(surface)) continue;
408 if (surface->parentSurface()) {
409 // Wrap it in a Window so that we keep focusedWindow() up to date.
410 Window *window = createWindow(surface);
411 connect(surface, &QObject::destroyed, window, [=](){
412 window->setSurface(nullptr);
413 window->deleteLater();
414 });
415 } else {
416 if (surface->type() == Mir::InputMethodType) {
417 setInputMethodWindow(createWindow(surface));
418 } else {
419 auto *application = m_applicationManager->findApplicationWithSurface(surface);
420 if (application) {
421 prependSurface(surface, application);
422 } else {
423 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
424 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
425 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
426 Window *promptWindow = createWindow(surface);
427 connect(surface, &QObject::destroyed, promptWindow, [=](){
428 promptWindow->setSurface(nullptr);
429 promptWindow->deleteLater();
430 });
431 }
432 }
433 }
434 }
437void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
438 const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
440 if (workspace != m_workspace->workspace()) return;
442 int start = -1;
443 int end = -1;
444 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
445 auto surface = *iter;
446 iter++;
448 // Do removals in adjacent blocks.
449 start = end = indexOf(surface);
450 if (start == -1) {
451 // probably a child surface
452 m_allSurfaces.remove(surface);
453 continue;
454 }
455 while(iter != surfaces.constEnd()) {
456 int index = indexOf(*iter);
457 if (index != end+1) {
458 break;
459 }
460 end++;
461 iter++;
462 }
464 if (m_modelState == IdleState) {
465 beginRemoveRows(QModelIndex(), start, end);
466 m_modelState = RemovingState;
467 } else {
468 Q_ASSERT(m_modelState == ResettingState);
469 // No point in signaling anything if we're resetting the whole model
470 }
472 for (int index = start; index <= end; index++) {
473 auto window = m_windowModel[start].window;
474 window->setSurface(nullptr);
475 window->setFocused(false);
477 m_windowModel.removeAt(start);
478 m_allSurfaces.remove(window->surface());
479 }
481 if (m_modelState == RemovingState) {
482 endRemoveRows();
483 Q_EMIT countChanged();
484 Q_EMIT listChanged();
485 m_modelState = IdleState;
486 }
487 }
490void TopLevelWindowModel::removeAt(int index)
492 if (m_modelState == IdleState) {
493 beginRemoveRows(QModelIndex(), index, index);
494 m_modelState = RemovingState;
495 } else {
496 Q_ASSERT(m_modelState == ResettingState);
497 // No point in signaling anything if we're resetting the whole model
498 }
500 auto window = m_windowModel[index].window;
502 window->setSurface(nullptr);
503 window->setFocused(false);
505 m_windowModel.removeAt(index);
506 m_allSurfaces.remove(window->surface());
508 if (m_modelState == RemovingState) {
509 endRemoveRows();
510 Q_EMIT countChanged();
511 Q_EMIT listChanged();
512 m_modelState = IdleState;
513 }
515 disconnect(window, 0, this, 0);
516 if (m_focusedWindow == window) {
517 setFocusedWindow(nullptr);
518 }
519 delete window;
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 delete m_inputMethodWindow;
538 m_inputMethodWindow = nullptr;
539 Q_EMIT inputMethodSurfaceChanged(nullptr);
540 }
543void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
545 DEBUG_MSG << "(" << surfaces << ")";
546 const int raiseCount = surfaces.size();
547 for (int i = 0; i < raiseCount; i++) {
548 int fromIndex = findIndexOf(surfaces[i]);
549 if (fromIndex != -1) {
550 move(fromIndex, 0);
551 }
552 }
555int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
557 return m_windowModel.count();
560QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
562 if (index.row() < 0 || index.row() >= m_windowModel.size())
563 return QVariant();
565 if (role == WindowRole) {
566 Window *window = m_windowModel.at(index.row()).window;
567 return QVariant::fromValue(window);
568 } else if (role == ApplicationRole) {
569 return QVariant::fromValue(m_windowModel.at(index.row()).application);
570 } else {
571 return QVariant();
572 }
575void TopLevelWindowModel::connectApplicationManager()
577 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
578 this, [this](const QModelIndex &/*parent*/, int first, int last) {
579 for (int i = first; i <= last; ++i) {
580 auto application = m_applicationManager->get(i);
581 addApplication(application);
582 }
583 });
585 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
586 this, [this](const QModelIndex &/*parent*/, int first, int last) {
587 for (int i = first; i <= last; ++i) {
588 auto application = m_applicationManager->get(i);
589 removeApplication(application);
590 }
591 });
595int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
597 for (int i=0; i<m_windowModel.count(); i++) {
598 if (m_windowModel[i].window->surface() == surface) {
599 return i;
600 }
601 }
602 return -1;
605int TopLevelWindowModel::generateId()
607 int id = m_nextId;
608 m_nextId = nextFreeId(nextId(id), id);
609 return id;
612int TopLevelWindowModel::nextId(int id) const
614 if (id == m_maxId) {
615 return id = 1;
616 } else {
617 return id + 1;
618 }
621int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
623 int firstCandidateId = candidateId;
625 while (indexForId(candidateId) != -1 || candidateId == latestId) {
626 candidateId = nextId(candidateId);
628 if (candidateId == firstCandidateId) {
629 qFatal("TopLevelWindowModel: run out of window ids.");
630 }
631 }
633 return candidateId;
636QString TopLevelWindowModel::toString()
638 QString str;
639 for (int i = 0; i < m_windowModel.count(); ++i) {
640 auto item = m_windowModel.at(i);
642 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
643 .arg(QString::number(i),
644 item.application->appId(),
645 QString::number((qintptr)item.window->surface(), 16),
646 QString::number(item.window->id()));
648 if (i > 0) {
649 str.append(",");
650 }
651 str.append(itemStr);
652 }
653 return str;
656int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
658 for (int i = 0; i < m_windowModel.count(); ++i) {
659 if (m_windowModel.at(i).window->surface() == surface) {
660 return i;
661 }
662 }
663 return -1;
666int TopLevelWindowModel::indexForId(int id) const
668 for (int i = 0; i < m_windowModel.count(); ++i) {
669 if (m_windowModel[i].window->id() == id) {
670 return i;
671 }
672 }
673 return -1;
676Window *TopLevelWindowModel::windowAt(int index) const
678 if (index >=0 && index < m_windowModel.count()) {
679 return m_windowModel[index].window;
680 } else {
681 return nullptr;
682 }
685unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
687 if (index >=0 && index < m_windowModel.count()) {
688 return m_windowModel[index].window->surface();
689 } else {
690 return nullptr;
691 }
694unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
696 if (index >=0 && index < m_windowModel.count()) {
697 return m_windowModel[index].application;
698 } else {
699 return nullptr;
700 }
703int TopLevelWindowModel::idAt(int index) const
705 if (index >=0 && index < m_windowModel.count()) {
706 return m_windowModel[index].window->id();
707 } else {
708 return 0;
709 }
712void TopLevelWindowModel::raiseId(int id)
714 if (m_modelState == IdleState) {
715 DEBUG_MSG << "(id=" << id << ") - do it now.";
716 doRaiseId(id);
717 } else {
718 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
719 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
720 // if we perform yet another model change straight away.
721 //
722 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
723 // the index is definitely within bounds.
724 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
725 }
728void TopLevelWindowModel::doRaiseId(int id)
730 int fromIndex = indexForId(id);
731 // can't raise something that doesn't exist or that it's already on top
732 if (fromIndex != -1 && fromIndex != 0) {
733 auto surface = m_windowModel[fromIndex].window->surface();
734 if (surface) {
735 m_surfaceManager->raise(surface);
736 } else {
737 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
738 // miral can do about it.
739 move(fromIndex, 0);
740 }
741 }
744void TopLevelWindowModel::setFocusedWindow(Window *window)
746 if (window != m_focusedWindow) {
747 INFO_MSG << "(" << window << ")";
749 Window* previousWindow = m_focusedWindow;
751 m_focusedWindow = window;
752 Q_EMIT focusedWindowChanged(m_focusedWindow);
754 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
755 // do it ourselves. miral doesn't know about this window
756 previousWindow->setFocused(false);
757 }
758 }
761unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
763 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
766Window* TopLevelWindowModel::focusedWindow() const
768 return m_focusedWindow;
771void TopLevelWindowModel::move(int from, int to)
773 if (from == to) return;
774 DEBUG_MSG << " from=" << from << " to=" << to;
776 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
777 QModelIndex parent;
778 /* When moving an item down, the destination index needs to be incremented
779 by one, as explained in the documentation:
780 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
782 Q_ASSERT(m_modelState == IdleState);
783 m_modelState = MovingState;
785 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
786#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
787 const auto &window = m_windowModel.takeAt(from);
788 m_windowModel.insert(to, window);
789#else
790 m_windowModel.move(from, to);
791#endif
792 endMoveRows();
794 Q_EMIT listChanged();
795 m_modelState = IdleState;
797 INFO_MSG << " after " << toString();
798 }
800void TopLevelWindowModel::onModificationsStarted()
804void TopLevelWindowModel::onModificationsEnded()
806 if (m_focusedWindowChanged) {
807 setFocusedWindow(m_newlyFocusedWindow);
808 }
809 // reset
810 m_focusedWindowChanged = false;
811 m_newlyFocusedWindow = nullptr;
814void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
816 DEBUG_MSG << "(" << forbiddenId << ")";
818 for (int i = 0; i < m_windowModel.count(); ++i) {
819 Window *window = m_windowModel[i].window;
820 if (window->id() != forbiddenId) {
821 window->activate();
822 }
823 }
826void TopLevelWindowModel::refreshWindows()
828 DEBUG_MSG << "()";
830 m_windowModel.clear();
831 m_allSurfaces.clear();
833 if (!m_workspace || !m_applicationManager) return;
834 WindowManagementPolicy::instance()->forEachWindowInWorkspace(m_workspace->workspace(), [this](const miral::Window &window) {
835 auto surface = m_surfaceManager->surfaceFor(window);
836 if (surface) {
837 if (surface->parentSurface()) {
838 // Wrap it in a Window so that we keep focusedWindow() up to date.
839 Window *window = createWindow(surface);
840 connect(surface, &QObject::destroyed, window, [=](){
841 window->setSurface(nullptr);
842 window->deleteLater();
843 });
844 } else {
845 if (surface->type() == Mir::InputMethodType) {
846 setInputMethodWindow(createWindow(surface));
847 } else {
848 auto *application = m_applicationManager->findApplicationWithSurface(surface);
849 if (application) {
850 prependSurface(surface, application);
851 } else {
852 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
853 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
854 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
855 Window *promptWindow = createWindow(surface);
856 connect(surface, &QObject::destroyed, promptWindow, [=](){
857 promptWindow->setSurface(nullptr);
858 promptWindow->deleteLater();
859 });
860 }
861 }
862 }
863 }
864 });