2 * Copyright (C) 2016 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::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
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);
128 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
129 this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
130 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
131 this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
137 m_modelState = IdleState;
140void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
142 DEBUG_MSG << "(" << application->appId() << ")";
144 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
145 prependPlaceholder(application);
149void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
151 DEBUG_MSG << "(" << application->appId() << ")";
153 Q_ASSERT(m_modelState == IdleState);
156 while (i < m_windowModel.count()) {
157 if (m_windowModel.at(i).application == application) {
165void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
167 INFO_MSG << "(" << application->appId() << ")";
169 prependSurfaceHelper(nullptr, application);
172void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
174 Q_ASSERT(surface != nullptr);
176 bool filledPlaceholder = false;
177 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
178 ModelEntry &entry = m_windowModel[i];
179 if (entry.application == application && entry.window->surface() == nullptr) {
180 entry.window->setSurface(surface);
181 m_allSurfaces.insert(surface);
182 connectSurface(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)
197 if (m_modelState == IdleState) {
198 m_modelState = InsertingState;
199 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
201 Q_ASSERT(m_modelState == ResettingState);
202 // No point in signaling anything if we're resetting the whole model
205 Window *window = createWindow(surface);
207 m_windowModel.prepend(ModelEntry(window, application));
209 m_allSurfaces.insert(surface);
210 connectSurface(surface);
213 if (m_modelState == InsertingState) {
215 Q_EMIT countChanged();
216 Q_EMIT listChanged();
217 m_modelState = IdleState;
221 activateEmptyWindow(window);
224 INFO_MSG << " after " << toString();
227void TopLevelWindowModel::connectWindow(Window *window)
229 connect(window, &Window::focusRequested, this, [this, window]() {
230 if (!window->surface()) {
231 activateEmptyWindow(window);
235 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
236 if (window->surface()) {
238 setFocusedWindow(window);
239 m_focusedWindowCleared = false;
240 } else if (m_focusedWindow == window) {
241 // Condense changes to the focused window
242 // eg: Do focusedWindow=A to focusedWindow=B instead of
243 // focusedWindow=A to focusedWindow=null to focusedWindow=B
244 m_focusedWindowCleared = true;
246 // don't clear the focused window if you were not there in the first place
247 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
252 connect(window, &Window::closeRequested, this, [this, window]() {
253 if (!window->surface()) {
254 // do things ourselves as miral doesn't know about this window
255 int id = window->id();
256 int index = indexForId(id);
257 bool focusOther = false;
258 Q_ASSERT(index >= 0);
259 if (window->focused()) {
262 m_windowModel[index].application->close();
264 activateTopMostWindowWithoutId(id);
269 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
270 activateEmptyWindow(window);
274void TopLevelWindowModel::activateEmptyWindow(Window *window)
276 Q_ASSERT(!window->surface());
277 DEBUG_MSG << "(" << window << ")";
279 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
280 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
282 window->setFocused(true);
283 raiseId(window->id());
284 Window *previousWindow = m_focusedWindow;
285 setFocusedWindow(window);
286 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
287 m_surfaceManager->activate(nullptr);
291void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
293 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
295 onSurfaceDied(surface);
298 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
301void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
303 if (surface->type() == Mir::InputMethodType) {
304 removeInputMethodWindow();
308 int i = indexOf(surface);
313 auto application = m_windowModel[i].application;
315 // can't be starting if it already has a surface
316 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
318 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
319 m_windowModel[i].removeOnceSurfaceDestroyed = true;
321 // assume it got killed by the out-of-memory daemon.
323 // So leave entry in the model and only remove its surface, so shell can display a screenshot
325 m_windowModel[i].removeOnceSurfaceDestroyed = false;
329void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
331 int i = indexOf(surface);
336 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
339 auto window = m_windowModel[i].window;
340 window->setSurface(nullptr);
341 window->setFocused(false);
342 m_allSurfaces.remove(surface);
343 INFO_MSG << " Removed surface from entry. After: " << toString();
347Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
349 int id = generateId();
350 Window *qmlWindow = new Window(id, this);
351 connectWindow(qmlWindow);
353 qmlWindow->setSurface(surface);
359void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
363void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
364 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
366 if (!m_workspace || !m_applicationManager) return;
367 if (workspace != m_workspace->workspace()) return;
369 Q_FOREACH(auto surface, surfaces) {
370 if (m_allSurfaces.contains(surface)) continue;
372 if (surface->parentSurface()) {
373 // Wrap it in a Window so that we keep focusedWindow() up to date.
374 Window *window = createWindow(surface);
375 connect(surface, &QObject::destroyed, window, [=](){
376 window->setSurface(nullptr);
377 window->deleteLater();
380 if (surface->type() == Mir::InputMethodType) {
381 connectSurface(surface);
382 setInputMethodWindow(createWindow(surface));
384 auto *application = m_applicationManager->findApplicationWithSurface(surface);
386 prependSurface(surface, application);
388 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
389 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
390 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
391 Window *promptWindow = createWindow(surface);
392 connect(surface, &QObject::destroyed, promptWindow, [=](){
393 promptWindow->setSurface(nullptr);
394 promptWindow->deleteLater();
402void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
403 const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
405 if (!m_workspace || workspace != m_workspace->workspace()) return;
409 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
410 auto surface = *iter;
413 // Do removals in adjacent blocks.
414 start = end = indexOf(surface);
416 // probably a child surface
417 m_allSurfaces.remove(surface);
420 while(iter != surfaces.constEnd()) {
421 int index = indexOf(*iter);
422 if (index != end+1) {
429 if (m_modelState == IdleState) {
430 beginRemoveRows(QModelIndex(), start, end);
431 m_modelState = RemovingState;
433 Q_ASSERT(m_modelState == ResettingState);
434 // No point in signaling anything if we're resetting the whole model
437 for (int index = start; index <= end; index++) {
438 auto window = m_windowModel[start].window;
439 window->setSurface(nullptr);
440 window->setFocused(false);
442 m_windowModel.removeAt(start);
443 m_allSurfaces.remove(surface);
446 if (m_modelState == RemovingState) {
448 Q_EMIT countChanged();
449 Q_EMIT listChanged();
450 m_modelState = IdleState;
455void TopLevelWindowModel::removeAt(int index)
457 if (m_modelState == IdleState) {
458 beginRemoveRows(QModelIndex(), index, index);
459 m_modelState = RemovingState;
461 Q_ASSERT(m_modelState == ResettingState);
462 // No point in signaling anything if we're resetting the whole model
465 auto window = m_windowModel[index].window;
466 auto surface = window->surface();
468 window->setSurface(nullptr);
469 window->setFocused(false);
471 m_windowModel.removeAt(index);
472 m_allSurfaces.remove(surface);
474 if (m_modelState == RemovingState) {
476 Q_EMIT countChanged();
477 Q_EMIT listChanged();
478 m_modelState = IdleState;
481 disconnect(window, 0, this, 0);
482 if (m_focusedWindow == window) {
483 setFocusedWindow(nullptr);
484 m_focusedWindowCleared = false;
488 INFO_MSG << " after " << toString();
491void TopLevelWindowModel::setInputMethodWindow(Window *window)
493 if (m_inputMethodWindow) {
494 qWarning("Multiple Input Method Surfaces created, removing the old one!");
495 delete m_inputMethodWindow;
497 m_inputMethodWindow = window;
498 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
501void TopLevelWindowModel::removeInputMethodWindow()
503 if (m_inputMethodWindow) {
504 delete m_inputMethodWindow;
505 m_inputMethodWindow = nullptr;
506 Q_EMIT inputMethodSurfaceChanged(nullptr);
510void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
512 DEBUG_MSG << "(" << surfaces << ")";
513 const int raiseCount = surfaces.size();
514 for (int i = 0; i < raiseCount; i++) {
515 int fromIndex = findIndexOf(surfaces[i]);
516 if (fromIndex != -1) {
522int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
524 return m_windowModel.count();
527QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
529 if (index.row() < 0 || index.row() >= m_windowModel.size())
532 if (role == WindowRole) {
533 Window *window = m_windowModel.at(index.row()).window;
534 return QVariant::fromValue(window);
535 } else if (role == ApplicationRole) {
536 return QVariant::fromValue(m_windowModel.at(index.row()).application);
542int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
544 for (int i=0; i<m_windowModel.count(); i++) {
545 if (m_windowModel[i].window->surface() == surface) {
552int TopLevelWindowModel::generateId()
555 m_nextId = nextFreeId(nextId(id), id);
559int TopLevelWindowModel::nextId(int id) const
568int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
570 int firstCandidateId = candidateId;
572 while (indexForId(candidateId) != -1 || candidateId == latestId) {
573 candidateId = nextId(candidateId);
575 if (candidateId == firstCandidateId) {
576 qFatal("TopLevelWindowModel: run out of window ids.");
583QString TopLevelWindowModel::toString()
586 for (int i = 0; i < m_windowModel.count(); ++i) {
587 auto item = m_windowModel.at(i);
589 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
590 .arg(QString::number(i),
591 item.application->appId(),
592 QString::number((qintptr)item.window->surface(), 16),
593 QString::number(item.window->id()));
603int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
605 for (int i = 0; i < m_windowModel.count(); ++i) {
606 if (m_windowModel.at(i).window->surface() == surface) {
613int TopLevelWindowModel::indexForId(int id) const
615 for (int i = 0; i < m_windowModel.count(); ++i) {
616 if (m_windowModel[i].window->id() == id) {
623Window *TopLevelWindowModel::windowAt(int index) const
625 if (index >=0 && index < m_windowModel.count()) {
626 return m_windowModel[index].window;
632unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
634 if (index >=0 && index < m_windowModel.count()) {
635 return m_windowModel[index].window->surface();
641unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
643 if (index >=0 && index < m_windowModel.count()) {
644 return m_windowModel[index].application;
650int TopLevelWindowModel::idAt(int index) const
652 if (index >=0 && index < m_windowModel.count()) {
653 return m_windowModel[index].window->id();
659void TopLevelWindowModel::raiseId(int id)
661 if (m_modelState == IdleState) {
662 DEBUG_MSG << "(id=" << id << ") - do it now.";
665 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
666 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
667 // if we perform yet another model change straight away.
669 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
670 // the index is definitely within bounds.
671 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
675void TopLevelWindowModel::doRaiseId(int id)
677 int fromIndex = indexForId(id);
678 // can't raise something that doesn't exist or that it's already on top
679 if (fromIndex != -1 && fromIndex != 0) {
680 auto surface = m_windowModel[fromIndex].window->surface();
682 m_surfaceManager->raise(surface);
684 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
685 // miral can do about it.
691void TopLevelWindowModel::setFocusedWindow(Window *window)
693 if (window != m_focusedWindow) {
694 INFO_MSG << "(" << window << ")";
696 Window* previousWindow = m_focusedWindow;
698 m_focusedWindow = window;
699 Q_EMIT focusedWindowChanged(m_focusedWindow);
701 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
702 // do it ourselves. miral doesn't know about this window
703 previousWindow->setFocused(false);
708unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
710 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
713Window* TopLevelWindowModel::focusedWindow() const
715 return m_focusedWindow;
718void TopLevelWindowModel::move(int from, int to)
720 if (from == to) return;
721 DEBUG_MSG << " from=" << from << " to=" << to;
723 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
725 /* When moving an item down, the destination index needs to be incremented
726 by one, as explained in the documentation:
727 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
729 Q_ASSERT(m_modelState == IdleState);
730 m_modelState = MovingState;
732 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
733#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
734 const auto &window = m_windowModel.takeAt(from);
735 m_windowModel.insert(to, window);
737 m_windowModel.move(from, to);
741 Q_EMIT listChanged();
742 m_modelState = IdleState;
744 INFO_MSG << " after " << toString();
747void TopLevelWindowModel::onModificationsStarted()
751void TopLevelWindowModel::onModificationsEnded()
753 if (m_focusedWindowCleared) {
754 setFocusedWindow(nullptr);
757 m_focusedWindowCleared = false;
760void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
762 DEBUG_MSG << "(" << forbiddenId << ")";
764 for (int i = 0; i < m_windowModel.count(); ++i) {
765 Window *window = m_windowModel[i].window;
766 if (window->id() != forbiddenId) {
772void TopLevelWindowModel::refreshWindows()
777 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
779 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](unity::shell::application::MirSurfaceInterface* surface) {
780 if (surface->parentSurface()) {
781 // Wrap it in a Window so that we keep focusedWindow() up to date.
782 Window *window = createWindow(surface);
783 connect(surface, &QObject::destroyed, window, [=](){
784 window->setSurface(nullptr);
785 window->deleteLater();
788 if (surface->type() == Mir::InputMethodType) {
789 setInputMethodWindow(createWindow(surface));
791 auto *application = m_applicationManager->findApplicationWithSurface(surface);
793 prependSurface(surface, application);
795 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
796 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
797 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
798 Window *promptWindow = createWindow(surface);
799 connect(surface, &QObject::destroyed, promptWindow, [=](){
800 promptWindow->setSurface(nullptr);
801 promptWindow->deleteLater();
809void TopLevelWindowModel::clear()
813 while(m_windowModel.count() > 0) {
814 ModelEntry entry = m_windowModel.takeAt(0);
815 disconnect(entry.window, 0, this, 0);
818 m_allSurfaces.clear();
819 setFocusedWindow(nullptr);
820 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