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());
53void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInterface* value)
55 if (m_applicationManager == value) {
59 DEBUG_MSG << "(" << value << ")";
61 Q_ASSERT(m_modelState == IdleState);
62 m_modelState = ResettingState;
66 if (m_applicationManager) {
67 disconnect(m_applicationManager, 0, this, 0);
70 m_applicationManager = value;
72 if (m_applicationManager) {
73 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
74 this, [this](const QModelIndex &/*parent*/, int first, int last) {
75 if (!m_workspace || !m_workspace->isActive())
78 for (int i = first; i <= last; ++i) {
79 auto application = m_applicationManager->get(i);
80 addApplication(application);
84 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
85 this, [this](const QModelIndex &/*parent*/, int first, int last) {
86 for (int i = first; i <= last; ++i) {
87 auto application = m_applicationManager->get(i);
88 removeApplication(application);
96 m_modelState = IdleState;
99void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
101 if (surfaceManager == m_surfaceManager) {
105 DEBUG_MSG << "(" << surfaceManager << ")";
107 Q_ASSERT(m_modelState == IdleState);
108 m_modelState = ResettingState;
112 if (m_surfaceManager) {
113 disconnect(m_surfaceManager, 0, this, 0);
116 m_surfaceManager = surfaceManager;
118 if (m_surfaceManager) {
119 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
120 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
121 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
122 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
124 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
125 this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
126 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
127 this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
133 m_modelState = IdleState;
136void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
138 DEBUG_MSG << "(" << application->appId() << ")";
140 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
141 prependPlaceholder(application);
145void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
147 DEBUG_MSG << "(" << application->appId() << ")";
149 Q_ASSERT(m_modelState == IdleState);
152 while (i < m_windowModel.count()) {
153 if (m_windowModel.at(i).application == application) {
161void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
163 INFO_MSG << "(" << application->appId() << ")";
165 prependSurfaceHelper(nullptr, application);
168void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
170 Q_ASSERT(surface != nullptr);
172 bool filledPlaceholder = false;
173 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
174 ModelEntry &entry = m_windowModel[i];
175 if (entry.application == application && entry.window->surface() == nullptr) {
176 entry.window->setSurface(surface);
177 m_allSurfaces.insert(surface);
178 connectSurface(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)
193 if (m_modelState == IdleState) {
194 m_modelState = InsertingState;
195 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
197 Q_ASSERT(m_modelState == ResettingState);
198 // No point in signaling anything if we're resetting the whole model
201 Window *window = createWindow(surface);
203 m_windowModel.prepend(ModelEntry(window, application));
205 m_allSurfaces.insert(surface);
206 connectSurface(surface);
209 if (m_modelState == InsertingState) {
211 Q_EMIT countChanged();
212 Q_EMIT listChanged();
213 m_modelState = IdleState;
217 activateEmptyWindow(window);
220 INFO_MSG << " after " << toString();
223void TopLevelWindowModel::connectWindow(Window *window)
225 connect(window, &Window::focusRequested, this, [this, window]() {
226 if (!window->surface()) {
227 activateEmptyWindow(window);
231 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
232 if (window->surface()) {
234 setFocusedWindow(window);
235 m_focusedWindowCleared = false;
236 } else if (m_focusedWindow == window) {
237 // Condense changes to the focused window
238 // eg: Do focusedWindow=A to focusedWindow=B instead of
239 // focusedWindow=A to focusedWindow=null to focusedWindow=B
240 m_focusedWindowCleared = true;
242 // don't clear the focused window if you were not there in the first place
243 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
248 connect(window, &Window::closeRequested, this, [this, window]() {
249 if (!window->surface()) {
250 // do things ourselves as miral doesn't know about this window
251 int id = window->id();
252 int index = indexForId(id);
253 bool focusOther = false;
254 Q_ASSERT(index >= 0);
255 if (window->focused()) {
258 m_windowModel[index].application->close();
260 activateTopMostWindowWithoutId(id);
265 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
266 activateEmptyWindow(window);
270void TopLevelWindowModel::activateEmptyWindow(Window *window)
272 Q_ASSERT(!window->surface());
273 DEBUG_MSG << "(" << window << ")";
275 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
276 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
278 window->setFocused(true);
279 raiseId(window->id());
280 Window *previousWindow = m_focusedWindow;
281 setFocusedWindow(window);
282 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
283 m_surfaceManager->activate(nullptr);
287void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
289 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
291 onSurfaceDied(surface);
294 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
297void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
299 if (surface->type() == Mir::InputMethodType) {
300 removeInputMethodWindow();
304 int i = indexOf(surface);
309 auto application = m_windowModel[i].application;
311 // can't be starting if it already has a surface
312 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
314 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
315 m_windowModel[i].removeOnceSurfaceDestroyed = true;
317 // assume it got killed by the out-of-memory daemon.
319 // So leave entry in the model and only remove its surface, so shell can display a screenshot
321 m_windowModel[i].removeOnceSurfaceDestroyed = false;
325void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
327 int i = indexOf(surface);
332 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
335 auto window = m_windowModel[i].window;
336 window->setSurface(nullptr);
337 window->setFocused(false);
338 m_allSurfaces.remove(surface);
339 INFO_MSG << " Removed surface from entry. After: " << toString();
343Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
345 int id = generateId();
346 Window *qmlWindow = new Window(id, this);
347 connectWindow(qmlWindow);
349 qmlWindow->setSurface(surface);
355void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
359void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
360 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
362 if (!m_workspace || !m_applicationManager) return;
363 if (workspace != m_workspace->workspace()) return;
365 Q_FOREACH(auto surface, surfaces) {
366 if (m_allSurfaces.contains(surface)) continue;
368 if (surface->parentSurface()) {
369 // Wrap it in a Window so that we keep focusedWindow() up to date.
370 Window *window = createWindow(surface);
371 connect(surface, &QObject::destroyed, window, [=](){
372 window->setSurface(nullptr);
373 window->deleteLater();
376 if (surface->type() == Mir::InputMethodType) {
377 connectSurface(surface);
378 setInputMethodWindow(createWindow(surface));
380 auto *application = m_applicationManager->findApplicationWithSurface(surface);
382 prependSurface(surface, application);
384 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
385 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
386 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
387 Window *promptWindow = createWindow(surface);
388 connect(surface, &QObject::destroyed, promptWindow, [=](){
389 promptWindow->setSurface(nullptr);
390 promptWindow->deleteLater();
398void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
399 const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
401 if (!m_workspace || workspace != m_workspace->workspace()) return;
405 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
406 auto surface = *iter;
409 // Do removals in adjacent blocks.
410 start = end = indexOf(surface);
412 // probably a child surface
413 m_allSurfaces.remove(surface);
416 while(iter != surfaces.constEnd()) {
417 int index = indexOf(*iter);
418 if (index != end+1) {
425 if (m_modelState == IdleState) {
426 beginRemoveRows(QModelIndex(), start, end);
427 m_modelState = RemovingState;
429 Q_ASSERT(m_modelState == ResettingState);
430 // No point in signaling anything if we're resetting the whole model
433 for (int index = start; index <= end; index++) {
434 auto window = m_windowModel[start].window;
435 window->setSurface(nullptr);
436 window->setFocused(false);
438 m_windowModel.removeAt(start);
439 m_allSurfaces.remove(surface);
442 if (m_modelState == RemovingState) {
444 Q_EMIT countChanged();
445 Q_EMIT listChanged();
446 m_modelState = IdleState;
451void TopLevelWindowModel::removeAt(int index)
453 if (m_modelState == IdleState) {
454 beginRemoveRows(QModelIndex(), index, index);
455 m_modelState = RemovingState;
457 Q_ASSERT(m_modelState == ResettingState);
458 // No point in signaling anything if we're resetting the whole model
461 auto window = m_windowModel[index].window;
462 auto surface = window->surface();
464 window->setSurface(nullptr);
465 window->setFocused(false);
467 m_windowModel.removeAt(index);
468 m_allSurfaces.remove(surface);
470 if (m_modelState == RemovingState) {
472 Q_EMIT countChanged();
473 Q_EMIT listChanged();
474 m_modelState = IdleState;
477 disconnect(window, 0, this, 0);
478 if (m_focusedWindow == window) {
479 setFocusedWindow(nullptr);
480 m_focusedWindowCleared = false;
484 INFO_MSG << " after " << toString();
487void TopLevelWindowModel::setInputMethodWindow(Window *window)
489 if (m_inputMethodWindow) {
490 qWarning("Multiple Input Method Surfaces created, removing the old one!");
491 delete m_inputMethodWindow;
493 m_inputMethodWindow = window;
494 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
497void TopLevelWindowModel::removeInputMethodWindow()
499 if (m_inputMethodWindow) {
500 delete m_inputMethodWindow;
501 m_inputMethodWindow = nullptr;
502 Q_EMIT inputMethodSurfaceChanged(nullptr);
506void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
508 DEBUG_MSG << "(" << surfaces << ")";
509 const int raiseCount = surfaces.size();
510 for (int i = 0; i < raiseCount; i++) {
511 int fromIndex = findIndexOf(surfaces[i]);
512 if (fromIndex != -1) {
518int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
520 return m_windowModel.count();
523QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
525 if (index.row() < 0 || index.row() >= m_windowModel.size())
528 if (role == WindowRole) {
529 Window *window = m_windowModel.at(index.row()).window;
530 return QVariant::fromValue(window);
531 } else if (role == ApplicationRole) {
532 return QVariant::fromValue(m_windowModel.at(index.row()).application);
538int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
540 for (int i=0; i<m_windowModel.count(); i++) {
541 if (m_windowModel[i].window->surface() == surface) {
548int TopLevelWindowModel::generateId()
551 m_nextId = nextFreeId(nextId(id), id);
555int TopLevelWindowModel::nextId(int id) const
564int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
566 int firstCandidateId = candidateId;
568 while (indexForId(candidateId) != -1 || candidateId == latestId) {
569 candidateId = nextId(candidateId);
571 if (candidateId == firstCandidateId) {
572 qFatal("TopLevelWindowModel: run out of window ids.");
579QString TopLevelWindowModel::toString()
582 for (int i = 0; i < m_windowModel.count(); ++i) {
583 auto item = m_windowModel.at(i);
585 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
586 .arg(QString::number(i),
587 item.application->appId(),
588 QString::number((qintptr)item.window->surface(), 16),
589 QString::number(item.window->id()));
599int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
601 for (int i = 0; i < m_windowModel.count(); ++i) {
602 if (m_windowModel.at(i).window->surface() == surface) {
609int TopLevelWindowModel::indexForId(int id) const
611 for (int i = 0; i < m_windowModel.count(); ++i) {
612 if (m_windowModel[i].window->id() == id) {
619Window *TopLevelWindowModel::windowAt(int index) const
621 if (index >=0 && index < m_windowModel.count()) {
622 return m_windowModel[index].window;
628unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
630 if (index >=0 && index < m_windowModel.count()) {
631 return m_windowModel[index].window->surface();
637unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
639 if (index >=0 && index < m_windowModel.count()) {
640 return m_windowModel[index].application;
646int TopLevelWindowModel::idAt(int index) const
648 if (index >=0 && index < m_windowModel.count()) {
649 return m_windowModel[index].window->id();
655void TopLevelWindowModel::raiseId(int id)
657 if (m_modelState == IdleState) {
658 DEBUG_MSG << "(id=" << id << ") - do it now.";
661 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
662 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
663 // if we perform yet another model change straight away.
665 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
666 // the index is definitely within bounds.
667 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
671void TopLevelWindowModel::doRaiseId(int id)
673 int fromIndex = indexForId(id);
674 // can't raise something that doesn't exist or that it's already on top
675 if (fromIndex != -1 && fromIndex != 0) {
676 auto surface = m_windowModel[fromIndex].window->surface();
678 m_surfaceManager->raise(surface);
680 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
681 // miral can do about it.
687void TopLevelWindowModel::setFocusedWindow(Window *window)
689 if (window != m_focusedWindow) {
690 INFO_MSG << "(" << window << ")";
692 Window* previousWindow = m_focusedWindow;
694 m_focusedWindow = window;
695 Q_EMIT focusedWindowChanged(m_focusedWindow);
697 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
698 // do it ourselves. miral doesn't know about this window
699 previousWindow->setFocused(false);
704unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
706 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
709Window* TopLevelWindowModel::focusedWindow() const
711 return m_focusedWindow;
714void TopLevelWindowModel::move(int from, int to)
716 if (from == to) return;
717 DEBUG_MSG << " from=" << from << " to=" << to;
719 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
721 /* When moving an item down, the destination index needs to be incremented
722 by one, as explained in the documentation:
723 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
725 Q_ASSERT(m_modelState == IdleState);
726 m_modelState = MovingState;
728 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
729#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
730 const auto &window = m_windowModel.takeAt(from);
731 m_windowModel.insert(to, window);
733 m_windowModel.move(from, to);
737 Q_EMIT listChanged();
738 m_modelState = IdleState;
740 INFO_MSG << " after " << toString();
743void TopLevelWindowModel::onModificationsStarted()
747void TopLevelWindowModel::onModificationsEnded()
749 if (m_focusedWindowCleared) {
750 setFocusedWindow(nullptr);
753 m_focusedWindowCleared = false;
756void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
758 DEBUG_MSG << "(" << forbiddenId << ")";
760 for (int i = 0; i < m_windowModel.count(); ++i) {
761 Window *window = m_windowModel[i].window;
762 if (window->id() != forbiddenId) {
768void TopLevelWindowModel::refreshWindows()
773 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
775 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](unity::shell::application::MirSurfaceInterface* surface) {
776 if (surface->parentSurface()) {
777 // Wrap it in a Window so that we keep focusedWindow() up to date.
778 Window *window = createWindow(surface);
779 connect(surface, &QObject::destroyed, window, [=](){
780 window->setSurface(nullptr);
781 window->deleteLater();
784 if (surface->type() == Mir::InputMethodType) {
785 setInputMethodWindow(createWindow(surface));
787 auto *application = m_applicationManager->findApplicationWithSurface(surface);
789 prependSurface(surface, application);
791 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
792 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
793 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
794 Window *promptWindow = createWindow(surface);
795 connect(surface, &QObject::destroyed, promptWindow, [=](){
796 promptWindow->setSurface(nullptr);
797 promptWindow->deleteLater();
805void TopLevelWindowModel::clear()
809 while(m_windowModel.count() > 0) {
810 ModelEntry entry = m_windowModel.takeAt(0);
811 disconnect(entry.window, 0, this, 0);
814 m_allSurfaces.clear();
815 setFocusedWindow(nullptr);
816 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