2 * Copyright (C) 2016-2017 Canonical, Ltd.
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.
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.
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/>.
17#include "TopLevelWindowModel.h"
18#include "WindowManagerObjects.h"
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>
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) {
63 DEBUG_MSG << "(" << value << ")";
65 Q_ASSERT(m_modelState == IdleState);
66 m_modelState = ResettingState;
70 if (m_applicationManager) {
71 disconnect(m_applicationManager, 0, this, 0);
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())
82 for (int i = first; i <= last; ++i) {
83 auto application = m_applicationManager->get(i);
84 addApplication(application);
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);
100 m_modelState = IdleState;
103void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
105 if (surfaceManager == m_surfaceManager) {
109 DEBUG_MSG << "(" << surfaceManager << ")";
111 Q_ASSERT(m_modelState == IdleState);
112 m_modelState = ResettingState;
116 if (m_surfaceManager) {
117 disconnect(m_surfaceManager, 0, this, 0);
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);
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);
144void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
146 DEBUG_MSG << "(" << application->appId() << ")";
148 Q_ASSERT(m_modelState == IdleState);
151 while (i < m_windowModel.count()) {
152 if (m_windowModel.at(i).application == application) {
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;
185 if (!filledPlaceholder) {
186 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
187 prependSurfaceHelper(surface, application);
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()));
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);
210 prependWindow(window, application);
213 activateEmptyWindow(window);
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*/);
225 Q_ASSERT(m_modelState == ResettingState);
226 // No point in signaling anything if we're resetting the whole model
229 m_windowModel.prepend(ModelEntry(window, application));
231 if (m_modelState == InsertingState) {
233 Q_EMIT countChanged();
234 Q_EMIT listChanged();
235 m_modelState = IdleState;
239void TopLevelWindowModel::connectWindow(Window *window)
241 connect(window, &Window::focusRequested, this, [this, window]() {
242 if (!window->surface()) {
243 activateEmptyWindow(window);
247 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
248 if (window->surface()) {
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;
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.
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()) {
274 m_windowModel[index].application->close();
276 activateTopMostWindowWithoutId(id);
281 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
282 activateEmptyWindow(window);
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.
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);
310void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
312 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
314 onSurfaceDied(surface);
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();
327 int i = indexOf(surface);
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;
340 // assume it got killed by the out-of-memory daemon.
342 // So leave entry in the model and only remove its surface, so shell can display a screenshot
344 m_windowModel[i].removeOnceSurfaceDestroyed = false;
348void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
350 int i = indexOf(surface);
355 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
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();
366Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
368 int id = generateId();
369 Window *qmlWindow = new Window(id, this);
370 connectWindow(qmlWindow);
372 qmlWindow->setSurface(surface);
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);
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();
397 if (surface->type() == Mir::InputMethodType) {
398 connectSurface(surface);
399 setInputMethodWindow(createWindow(surface));
401 auto *application = m_applicationManager->findApplicationWithSurface(surface);
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);
411 prependSurface(surface, application);
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();
428void TopLevelWindowModel::removeSurfaces(const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
432 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
433 auto surface = *iter;
436 // Do removals in adjacent blocks.
437 start = end = indexOf(surface);
439 // could be a child surface
440 m_allSurfaces.remove(surface);
443 while(iter != surfaces.constEnd()) {
444 int index = indexOf(*iter);
445 if (index != end+1) {
452 if (m_modelState == IdleState) {
453 beginRemoveRows(QModelIndex(), start, end);
454 m_modelState = RemovingState;
456 Q_ASSERT(m_modelState == ResettingState);
457 // No point in signaling anything if we're resetting the whole model
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);
469 if (m_modelState == RemovingState) {
471 Q_EMIT countChanged();
472 Q_EMIT listChanged();
473 m_modelState = IdleState;
478void TopLevelWindowModel::deleteAt(int index)
480 auto window = m_windowModel[index].window;
484 window->setSurface(nullptr);
489void TopLevelWindowModel::removeAt(int index)
491 if (m_modelState == IdleState) {
492 beginRemoveRows(QModelIndex(), index, index);
493 m_modelState = RemovingState;
495 Q_ASSERT(m_modelState == ResettingState);
496 // No point in signaling anything if we're resetting the whole model
499 auto window = m_windowModel[index].window;
500 auto surface = window->surface();
502 if (!window->surface()) {
503 window->setFocused(false);
506 m_windowModel.removeAt(index);
507 m_allSurfaces.remove(surface);
509 if (m_modelState == RemovingState) {
511 Q_EMIT countChanged();
512 Q_EMIT listChanged();
513 m_modelState = IdleState;
516 if (m_focusedWindow == window) {
517 setFocusedWindow(nullptr);
518 m_focusedWindowCleared = false;
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;
530 m_inputMethodWindow = window;
531 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
534void TopLevelWindowModel::removeInputMethodWindow()
536 if (m_inputMethodWindow) {
537 auto surface = m_inputMethodWindow->surface();
539 m_allSurfaces.remove(surface);
541 if (m_focusedWindow == m_inputMethodWindow) {
542 setFocusedWindow(nullptr);
543 m_focusedWindowCleared = false;
546 delete m_inputMethodWindow;
547 m_inputMethodWindow = nullptr;
548 Q_EMIT inputMethodSurfaceChanged(nullptr);
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) {
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())
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);
584int TopLevelWindowModel::generateId()
587 m_nextId = nextFreeId(nextId(id), id);
591int TopLevelWindowModel::nextId(int id) const
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.");
615QString TopLevelWindowModel::toString()
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()));
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) {
645int TopLevelWindowModel::indexForId(int id) const
647 for (int i = 0; i < m_windowModel.count(); ++i) {
648 if (m_windowModel[i].window->id() == id) {
655Window *TopLevelWindowModel::windowAt(int index) const
657 if (index >=0 && index < m_windowModel.count()) {
658 return m_windowModel[index].window;
664unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
666 if (index >=0 && index < m_windowModel.count()) {
667 return m_windowModel[index].window->surface();
673unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
675 if (index >=0 && index < m_windowModel.count()) {
676 return m_windowModel[index].application;
682int TopLevelWindowModel::idAt(int index) const
684 if (index >=0 && index < m_windowModel.count()) {
685 return m_windowModel[index].window->id();
691void TopLevelWindowModel::raiseId(int id)
693 if (m_modelState == IdleState) {
694 DEBUG_MSG << "(id=" << id << ") - do it now.";
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.
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));
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();
714 m_surfaceManager->raise(surface);
716 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
717 // miral can do about it.
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);
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()) {
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);
769 m_windowModel.move(from, to);
773 Q_EMIT listChanged();
774 m_modelState = IdleState;
776 INFO_MSG << " after " << toString();
779void TopLevelWindowModel::onModificationsStarted()
783void TopLevelWindowModel::onModificationsEnded()
785 if (m_focusedWindowCleared) {
786 setFocusedWindow(nullptr);
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) {
804void TopLevelWindowModel::refreshWindows()
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();
820 if (surface->type() == Mir::InputMethodType) {
821 setInputMethodWindow(createWindow(surface));
823 auto *application = m_applicationManager->findApplicationWithSurface(surface);
825 prependSurface(surface, application);
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();
841void TopLevelWindowModel::clear()
845 while(m_windowModel.count() > 0) {
846 ModelEntry entry = m_windowModel.takeAt(0);
847 disconnect(entry.window, 0, this, 0);
850 m_allSurfaces.clear();
851 setFocusedWindow(nullptr);
852 m_focusedWindowCleared = false;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list