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::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);
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);
148void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
150 DEBUG_MSG << "(" << application->appId() << ")";
152 Q_ASSERT(m_modelState == IdleState);
155 while (i < m_windowModel.count()) {
156 if (m_windowModel.at(i).application == application) {
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;
189 if (!filledPlaceholder) {
190 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
191 prependSurfaceHelper(surface, application);
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()));
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);
214 prependWindow(window, application);
217 activateEmptyWindow(window);
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*/);
229 Q_ASSERT(m_modelState == ResettingState);
230 // No point in signaling anything if we're resetting the whole model
233 m_windowModel.prepend(ModelEntry(window, application));
235 if (m_modelState == InsertingState) {
237 Q_EMIT countChanged();
238 Q_EMIT listChanged();
239 m_modelState = IdleState;
243void TopLevelWindowModel::connectWindow(Window *window)
245 connect(window, &Window::focusRequested, this, [this, window]() {
246 if (!window->surface()) {
247 activateEmptyWindow(window);
251 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
252 if (window->surface()) {
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;
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.
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()) {
278 m_windowModel[index].application->close();
280 activateTopMostWindowWithoutId(id);
285 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
286 activateEmptyWindow(window);
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.
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);
314void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
316 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
318 onSurfaceDied(surface);
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();
331 int i = indexOf(surface);
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;
344 // assume it got killed by the out-of-memory daemon.
346 // So leave entry in the model and only remove its surface, so shell can display a screenshot
348 m_windowModel[i].removeOnceSurfaceDestroyed = false;
352void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
354 int i = indexOf(surface);
359 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
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();
370Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
372 int id = generateId();
373 Window *qmlWindow = new Window(id, this);
374 connectWindow(qmlWindow);
376 qmlWindow->setSurface(surface);
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();
398 if (surface->type() == Mir::InputMethodType) {
399 connectSurface(surface);
400 setInputMethodWindow(createWindow(surface));
402 auto *application = m_applicationManager->findApplicationWithSurface(surface);
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);
412 prependSurface(surface, application);
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();
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;
436 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
437 auto surface = *iter;
440 // Do removals in adjacent blocks.
441 start = end = indexOf(surface);
443 // probably a child surface
444 m_allSurfaces.remove(surface);
447 while(iter != surfaces.constEnd()) {
448 int index = indexOf(*iter);
449 if (index != end+1) {
456 if (m_modelState == IdleState) {
457 beginRemoveRows(QModelIndex(), start, end);
458 m_modelState = RemovingState;
460 Q_ASSERT(m_modelState == ResettingState);
461 // No point in signaling anything if we're resetting the whole model
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);
473 if (m_modelState == RemovingState) {
475 Q_EMIT countChanged();
476 Q_EMIT listChanged();
477 m_modelState = IdleState;
482void TopLevelWindowModel::deleteAt(int index)
484 auto window = m_windowModel[index].window;
488 window->setSurface(nullptr);
493void TopLevelWindowModel::removeAt(int index)
495 if (m_modelState == IdleState) {
496 beginRemoveRows(QModelIndex(), index, index);
497 m_modelState = RemovingState;
499 Q_ASSERT(m_modelState == ResettingState);
500 // No point in signaling anything if we're resetting the whole model
503 auto window = m_windowModel[index].window;
504 auto surface = window->surface();
506 if (!window->surface()) {
507 window->setFocused(false);
510 m_windowModel.removeAt(index);
511 m_allSurfaces.remove(surface);
513 if (m_modelState == RemovingState) {
515 Q_EMIT countChanged();
516 Q_EMIT listChanged();
517 m_modelState = IdleState;
520 if (m_focusedWindow == window) {
521 setFocusedWindow(nullptr);
522 m_focusedWindowCleared = false;
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;
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);
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) {
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())
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);
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) {
589int TopLevelWindowModel::generateId()
592 m_nextId = nextFreeId(nextId(id), id);
596int TopLevelWindowModel::nextId(int id) const
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.");
620QString TopLevelWindowModel::toString()
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()));
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) {
650int TopLevelWindowModel::indexForId(int id) const
652 for (int i = 0; i < m_windowModel.count(); ++i) {
653 if (m_windowModel[i].window->id() == id) {
660Window *TopLevelWindowModel::windowAt(int index) const
662 if (index >=0 && index < m_windowModel.count()) {
663 return m_windowModel[index].window;
669unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
671 if (index >=0 && index < m_windowModel.count()) {
672 return m_windowModel[index].window->surface();
678unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
680 if (index >=0 && index < m_windowModel.count()) {
681 return m_windowModel[index].application;
687int TopLevelWindowModel::idAt(int index) const
689 if (index >=0 && index < m_windowModel.count()) {
690 return m_windowModel[index].window->id();
696void TopLevelWindowModel::raiseId(int id)
698 if (m_modelState == IdleState) {
699 DEBUG_MSG << "(id=" << id << ") - do it now.";
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.
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));
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();
719 m_surfaceManager->raise(surface);
721 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
722 // miral can do about it.
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);
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()) {
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);
774 m_windowModel.move(from, to);
778 Q_EMIT listChanged();
779 m_modelState = IdleState;
781 INFO_MSG << " after " << toString();
784void TopLevelWindowModel::onModificationsStarted()
788void TopLevelWindowModel::onModificationsEnded()
790 if (m_focusedWindowCleared) {
791 setFocusedWindow(nullptr);
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) {
809void TopLevelWindowModel::refreshWindows()
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();
825 if (surface->type() == Mir::InputMethodType) {
826 setInputMethodWindow(createWindow(surface));
828 auto *application = m_applicationManager->findApplicationWithSurface(surface);
830 prependSurface(surface, application);
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();
846void TopLevelWindowModel::clear()
850 while(m_windowModel.count() > 0) {
851 ModelEntry entry = m_windowModel.takeAt(0);
852 disconnect(entry.window, 0, this, 0);
855 m_allSurfaces.clear();
856 setFocusedWindow(nullptr);
857 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