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"
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>
32#include "windowmanagementpolicy.h"
35#include <qtmir/mirserverapplication.h>
36#include <qtmir/windowmodelnotifier.h>
38Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel")
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) {
55 DEBUG_MSG << "(" << value << ")";
57 Q_ASSERT(m_modelState == IdleState);
58 m_modelState = ResettingState;
62 if (m_applicationManager) {
63 disconnect(m_applicationManager, 0, this, 0);
66 m_applicationManager = value;
68 if (m_applicationManager && m_workspace) {
70 if (m_workspace->isActive()) {
71 connectApplicationManager();
74 if (m_surfaceManager) {
79 Q_EMIT applicationManagerChanged(m_applicationManager);
82 m_modelState = IdleState;
85void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
87 if (surfaceManager == m_surfaceManager) {
91 DEBUG_MSG << "(" << surfaceManager << ")";
93 Q_ASSERT(m_modelState == IdleState);
94 m_modelState = ResettingState;
98 if (m_surfaceManager) {
99 disconnect(m_surfaceManager, 0, this, 0);
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);
113 if (m_workspace && m_applicationManager) {
118 Q_EMIT surfaceManagerChanged(m_surfaceManager);
121 m_modelState = IdleState;
124void TopLevelWindowModel::setWorkspace(Workspace *workspace)
126 if (workspace == m_workspace) {
130 DEBUG_MSG << "(" << workspace << ")";
132 Q_ASSERT(m_modelState == IdleState);
133 m_modelState = ResettingState;
138 m_windowModel.clear();
139 m_allSurfaces.clear();
140 if (m_applicationManager) {
141 disconnect(m_applicationManager, 0, this, 0);
145 disconnect(m_applicationManager, 0 ,this, 0);
147 m_workspace = workspace;
150 connect(m_workspace, &Workspace::activeChanged, this, [this](bool active) {
151 if (m_applicationManager) {
152 if (!active) disconnect(m_applicationManager, 0, this, 0);
154 connectApplicationManager();
159 if (m_applicationManager && m_workspace->isActive()) {
160 connectApplicationManager();
163 if (m_surfaceManager && m_applicationManager) {
168 Q_EMIT workspaceChanged(workspace);
171 m_modelState = IdleState;
174void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
176 DEBUG_MSG << "(" << application->appId() << ")";
178 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
179 prependPlaceholder(application);
183void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
185 DEBUG_MSG << "(" << application->appId() << ")";
187 Q_ASSERT(m_modelState == IdleState);
190 while (i < m_windowModel.count()) {
191 if (m_windowModel.at(i).application == application) {
199void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
201 INFO_MSG << "(" << application->appId() << ")";
203 prependSurfaceHelper(nullptr, application);
206void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
208 Q_ASSERT(surface != nullptr);
210 bool filledPlaceholder = false;
211 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
212 ModelEntry &entry = m_windowModel[i];
213 if (entry.application == application && entry.window->surface() == nullptr) {
214 entry.window->setSurface(surface);
215 connectSurface(surface);
216 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
217 << ", filling out placeholder. after: " << toString();
218 filledPlaceholder = true;
222 if (!filledPlaceholder) {
223 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
224 prependSurfaceHelper(surface, application);
228void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
230 if (m_modelState == IdleState) {
231 m_modelState = InsertingState;
232 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
234 Q_ASSERT(m_modelState == ResettingState);
235 // No point in signaling anything if we're resetting the whole model
238 Window *window = createWindow(surface);
240 m_windowModel.prepend(ModelEntry(window, application));
241 m_allSurfaces.insert(surface);
243 connectSurface(surface);
246 if (m_modelState == InsertingState) {
248 Q_EMIT countChanged();
249 Q_EMIT listChanged();
250 m_modelState = IdleState;
254 activateEmptyWindow(window);
257 INFO_MSG << " after " << toString();
260void TopLevelWindowModel::connectWindow(Window *window)
262 connect(window, &Window::focusRequested, this, [this, window]() {
263 if (!window->surface()) {
264 activateEmptyWindow(window);
268 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
269 if (window->surface()) {
270 // Condense changes to the focused window
271 // eg: Do focusedWindow=A to focusedWindow=B instead of
272 // focusedWindow=A to focusedWindow=null to focusedWindow=B
274 Q_ASSERT(m_newlyFocusedWindow == nullptr);
275 m_focusedWindowChanged = true;
276 m_newlyFocusedWindow = window;
277 } else if (m_focusedWindow == window) {
278 m_focusedWindowChanged = true;
280 // don't clear the focused window if you were not there in the first place
281 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
286 connect(window, &Window::closeRequested, this, [this, window]() {
287 if (!window->surface()) {
288 // do things ourselves as miral doesn't know about this window
289 int id = window->id();
290 int index = indexForId(id);
291 bool focusOther = false;
292 Q_ASSERT(index >= 0);
293 if (window->focused()) {
296 m_windowModel[index].application->close();
298 activateTopMostWindowWithoutId(id);
303 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
304 activateEmptyWindow(window);
308void TopLevelWindowModel::activateEmptyWindow(Window *window)
310 Q_ASSERT(!window->surface());
311 DEBUG_MSG << "(" << window << ")";
313 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
314 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
316 window->setFocused(true);
317 raiseId(window->id());
318 Window *previousWindow = m_focusedWindow;
319 setFocusedWindow(window);
320 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
321 m_surfaceManager->activate(nullptr);
325void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
327 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
329 onSurfaceDied(surface);
332 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
335void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
337 if (surface->type() == Mir::InputMethodType) {
338 removeInputMethodWindow();
342 int i = indexOf(surface);
347 auto application = m_windowModel[i].application;
349 // can't be starting if it already has a surface
350 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
352 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
353 m_windowModel[i].removeOnceSurfaceDestroyed = true;
355 // assume it got killed by the out-of-memory daemon.
357 // So leave entry in the model and only remove its surface, so shell can display a screenshot
359 m_windowModel[i].removeOnceSurfaceDestroyed = false;
363void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
365 int i = indexOf(surface);
370 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
373 auto window = m_windowModel[i].window;
374 window->setSurface(nullptr);
375 window->setFocused(false);
376 m_allSurfaces.remove(surface);
377 INFO_MSG << " Removed surface from entry. After: " << toString();
381Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
383 int id = generateId();
384 Window *qmlWindow = new Window(id, this);
385 connectWindow(qmlWindow);
387 qmlWindow->setSurface(surface);
393void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
397void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
398 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
400 if (workspace != m_workspace->workspace()) return;
402 Q_FOREACH(auto surface, surfaces) {
403 if (m_allSurfaces.contains(surface)) continue;
405 if (surface->parentSurface()) {
406 // Wrap it in a Window so that we keep focusedWindow() up to date.
407 Window *window = createWindow(surface);
408 connect(surface, &QObject::destroyed, window, [=](){
409 window->setSurface(nullptr);
410 window->deleteLater();
413 if (surface->type() == Mir::InputMethodType) {
414 setInputMethodWindow(createWindow(surface));
416 auto *application = m_applicationManager->findApplicationWithSurface(surface);
418 prependSurface(surface, application);
420 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
421 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
422 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
423 Window *promptWindow = createWindow(surface);
424 connect(surface, &QObject::destroyed, promptWindow, [=](){
425 promptWindow->setSurface(nullptr);
426 promptWindow->deleteLater();
434void TopLevelWindowModel::removeAt(int index)
436 if (m_modelState == IdleState) {
437 beginRemoveRows(QModelIndex(), index, index);
438 m_modelState = RemovingState;
440 Q_ASSERT(m_modelState == ResettingState);
441 // No point in signaling anything if we're resetting the whole model
444 auto window = m_windowModel[index].window;
446 window->setSurface(nullptr);
447 window->setFocused(false);
449 m_windowModel.removeAt(index);
450 m_allSurfaces.remove(window->surface());
452 if (m_modelState == RemovingState) {
454 Q_EMIT countChanged();
455 Q_EMIT listChanged();
456 m_modelState = IdleState;
459 disconnect(window, 0, this, 0);
460 if (m_focusedWindow == window) {
461 setFocusedWindow(nullptr);
465 INFO_MSG << " after " << toString();
468void TopLevelWindowModel::setInputMethodWindow(Window *window)
470 if (m_inputMethodWindow) {
471 qWarning("Multiple Input Method Surfaces created, removing the old one!");
472 delete m_inputMethodWindow;
474 m_inputMethodWindow = window;
475 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
478void TopLevelWindowModel::removeInputMethodWindow()
480 if (m_inputMethodWindow) {
481 delete m_inputMethodWindow;
482 m_inputMethodWindow = nullptr;
483 Q_EMIT inputMethodSurfaceChanged(nullptr);
487void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
489 DEBUG_MSG << "(" << surfaces << ")";
490 const int raiseCount = surfaces.size();
491 for (int i = 0; i < raiseCount; i++) {
492 int fromIndex = findIndexOf(surfaces[i]);
493 if (fromIndex != -1) {
499int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
501 return m_windowModel.count();
504QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
506 if (index.row() < 0 || index.row() >= m_windowModel.size())
509 if (role == WindowRole) {
510 Window *window = m_windowModel.at(index.row()).window;
511 return QVariant::fromValue(window);
512 } else if (role == ApplicationRole) {
513 return QVariant::fromValue(m_windowModel.at(index.row()).application);
519void TopLevelWindowModel::connectApplicationManager()
521 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
522 this, [this](const QModelIndex &/*parent*/, int first, int last) {
523 for (int i = first; i <= last; ++i) {
524 auto application = m_applicationManager->get(i);
525 addApplication(application);
529 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
530 this, [this](const QModelIndex &/*parent*/, int first, int last) {
531 for (int i = first; i <= last; ++i) {
532 auto application = m_applicationManager->get(i);
533 removeApplication(application);
539int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
541 for (int i=0; i<m_windowModel.count(); i++) {
542 if (m_windowModel[i].window->surface() == surface) {
549int TopLevelWindowModel::generateId()
552 m_nextId = nextFreeId(nextId(id), id);
556int TopLevelWindowModel::nextId(int id) const
565int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
567 int firstCandidateId = candidateId;
569 while (indexForId(candidateId) != -1 || candidateId == latestId) {
570 candidateId = nextId(candidateId);
572 if (candidateId == firstCandidateId) {
573 qFatal("TopLevelWindowModel: run out of window ids.");
580QString TopLevelWindowModel::toString()
583 for (int i = 0; i < m_windowModel.count(); ++i) {
584 auto item = m_windowModel.at(i);
586 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
587 .arg(QString::number(i),
588 item.application->appId(),
589 QString::number((qintptr)item.window->surface(), 16),
590 QString::number(item.window->id()));
600int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
602 for (int i = 0; i < m_windowModel.count(); ++i) {
603 if (m_windowModel.at(i).window->surface() == surface) {
610int TopLevelWindowModel::indexForId(int id) const
612 for (int i = 0; i < m_windowModel.count(); ++i) {
613 if (m_windowModel[i].window->id() == id) {
620Window *TopLevelWindowModel::windowAt(int index) const
622 if (index >=0 && index < m_windowModel.count()) {
623 return m_windowModel[index].window;
629unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
631 if (index >=0 && index < m_windowModel.count()) {
632 return m_windowModel[index].window->surface();
638unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
640 if (index >=0 && index < m_windowModel.count()) {
641 return m_windowModel[index].application;
647int TopLevelWindowModel::idAt(int index) const
649 if (index >=0 && index < m_windowModel.count()) {
650 return m_windowModel[index].window->id();
656void TopLevelWindowModel::raiseId(int id)
658 if (m_modelState == IdleState) {
659 DEBUG_MSG << "(id=" << id << ") - do it now.";
662 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
663 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
664 // if we perform yet another model change straight away.
666 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
667 // the index is definitely within bounds.
668 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
672void TopLevelWindowModel::doRaiseId(int id)
674 int fromIndex = indexForId(id);
675 // can't raise something that doesn't exist or that it's already on top
676 if (fromIndex != -1 && fromIndex != 0) {
677 auto surface = m_windowModel[fromIndex].window->surface();
679 m_surfaceManager->raise(surface);
681 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
682 // miral can do about it.
688void TopLevelWindowModel::setFocusedWindow(Window *window)
690 if (window != m_focusedWindow) {
691 INFO_MSG << "(" << window << ")";
693 Window* previousWindow = m_focusedWindow;
695 m_focusedWindow = window;
696 Q_EMIT focusedWindowChanged(m_focusedWindow);
698 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
699 // do it ourselves. miral doesn't know about this window
700 previousWindow->setFocused(false);
705unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
707 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
710Window* TopLevelWindowModel::focusedWindow() const
712 return m_focusedWindow;
715void TopLevelWindowModel::move(int from, int to)
717 if (from == to) return;
718 DEBUG_MSG << " from=" << from << " to=" << to;
720 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
722 /* When moving an item down, the destination index needs to be incremented
723 by one, as explained in the documentation:
724 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
726 Q_ASSERT(m_modelState == IdleState);
727 m_modelState = MovingState;
729 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
730#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
731 const auto &window = m_windowModel.takeAt(from);
732 m_windowModel.insert(to, window);
734 m_windowModel.move(from, to);
738 Q_EMIT listChanged();
739 m_modelState = IdleState;
741 INFO_MSG << " after " << toString();
744void TopLevelWindowModel::onModificationsStarted()
748void TopLevelWindowModel::onModificationsEnded()
750 if (m_focusedWindowChanged) {
751 setFocusedWindow(m_newlyFocusedWindow);
754 m_focusedWindowChanged = false;
755 m_newlyFocusedWindow = nullptr;
758void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
760 DEBUG_MSG << "(" << forbiddenId << ")";
762 for (int i = 0; i < m_windowModel.count(); ++i) {
763 Window *window = m_windowModel[i].window;
764 if (window->id() != forbiddenId) {
770void TopLevelWindowModel::refreshWindows()
774 m_windowModel.clear();
775 m_allSurfaces.clear();
777 WindowManagementPolicy::instance()->forEachWindowInWorkspace(m_workspace->workspace(), [this](const miral::Window &window) {
778 auto surface = m_surfaceManager->surfaceFor(window);
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();
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