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"
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>
27#include <QGuiApplication>
33Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
35#define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
36#define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
38namespace unityapi = unity::shell::application;
40TopLevelWindowModel::TopLevelWindowModel()
41 : m_nullWindow(createWindow(nullptr))
43 connect(m_nullWindow, &Window::focusedChanged, this, [this] {
44 Q_EMIT rootFocusChanged();
48void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInterface* value)
50 if (m_applicationManager == value) {
54 DEBUG_MSG << "(" << value << ")";
56 Q_ASSERT(m_modelState == IdleState);
57 m_modelState = ResettingState;
61 if (m_applicationManager) {
62 m_windowModel.clear();
63 disconnect(m_applicationManager, 0, this, 0);
66 m_applicationManager = value;
68 if (m_applicationManager) {
69 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
70 this, [this](const QModelIndex &/*parent*/, int first, int last) {
71 for (int i = first; i <= last; ++i) {
72 auto application = m_applicationManager->get(i);
73 addApplication(application);
77 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
78 this, [this](const QModelIndex &/*parent*/, int first, int last) {
79 for (int i = first; i <= last; ++i) {
80 auto application = m_applicationManager->get(i);
81 removeApplication(application);
85 for (int i = 0; i < m_applicationManager->rowCount(); ++i) {
86 auto application = m_applicationManager->get(i);
87 addApplication(application);
92 m_modelState = IdleState;
95void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
97 if (surfaceManager == m_surfaceManager) {
101 DEBUG_MSG << "(" << surfaceManager << ")";
103 if (m_surfaceManager) {
104 disconnect(m_surfaceManager, 0, this, 0);
107 m_surfaceManager = surfaceManager;
109 if (m_surfaceManager) {
110 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
111 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
112 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
113 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
116 Q_EMIT surfaceManagerChanged(m_surfaceManager);
119void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
121 DEBUG_MSG << "(" << application->appId() << ")";
123 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
124 prependPlaceholder(application);
128void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
130 DEBUG_MSG << "(" << application->appId() << ")";
132 Q_ASSERT(m_modelState == IdleState);
135 while (i < m_windowModel.count()) {
136 if (m_windowModel.at(i).application == application) {
144void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
146 INFO_MSG << "(" << application->appId() << ")";
148 prependSurfaceHelper(nullptr, application);
151void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
153 Q_ASSERT(surface != nullptr);
155 connectSurface(surface);
157 bool filledPlaceholder = false;
158 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
159 ModelEntry &entry = m_windowModel[i];
160 if (entry.application == application && entry.window->surface() == nullptr) {
161 entry.window->setSurface(surface);
162 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
163 << ", filling out placeholder. after: " << toString();
164 filledPlaceholder = true;
168 if (!filledPlaceholder) {
169 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
170 prependSurfaceHelper(surface, application);
174void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
177 Window *window = createWindow(surface);
179 connect(window, &Window::stateChanged, this, [=](Mir::State newState) {
180 if (newState == Mir::HiddenState) {
181 // Comply, removing it from our model. Just as if it didn't exist anymore.
182 removeAt(indexForId(window->id()));
184 if (indexForId(window->id()) == -1) {
185 // was probably hidden before. put it back on the list
186 auto *application = m_applicationManager->findApplicationWithSurface(window->surface());
187 Q_ASSERT(application);
188 prependWindow(window, application);
193 prependWindow(window, application);
196 activateEmptyWindow(window);
199 INFO_MSG << " after " << toString();
202void TopLevelWindowModel::prependWindow(Window *window, unityapi::ApplicationInfoInterface *application)
204 if (m_modelState == IdleState) {
205 m_modelState = InsertingState;
206 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
208 Q_ASSERT(m_modelState == ResettingState);
209 // No point in signaling anything if we're resetting the whole model
212 m_windowModel.prepend(ModelEntry(window, application));
214 if (m_modelState == InsertingState) {
216 Q_EMIT countChanged();
217 Q_EMIT listChanged();
218 m_modelState = IdleState;
222void TopLevelWindowModel::connectWindow(Window *window)
224 connect(window, &Window::focusRequested, this, [this, window]() {
225 if (!window->surface()) {
226 activateEmptyWindow(window);
230 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
231 if (window->surface()) {
232 // Condense changes to the focused window
233 // eg: Do focusedWindow=A to focusedWindow=B instead of
234 // focusedWindow=A to focusedWindow=null to focusedWindow=B
236 Q_ASSERT(m_newlyFocusedWindow == nullptr);
237 m_focusedWindowChanged = true;
238 m_newlyFocusedWindow = window;
239 } else if (m_focusedWindow == window) {
240 m_focusedWindowChanged = 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);
269 connect(window, &Window::liveChanged, this, [this, window](bool isAlive) {
270 if (!isAlive && window->state() == Mir::HiddenState) {
271 // Hidden windows are not in the model. So just delete it right away.
277void TopLevelWindowModel::activateEmptyWindow(Window *window)
279 Q_ASSERT(!window->surface());
280 DEBUG_MSG << "(" << window << ")";
282 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
283 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
285 window->setFocused(true);
286 raiseId(window->id());
287 Window *previousWindow = m_focusedWindow;
288 setFocusedWindow(window);
289 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
290 m_surfaceManager->activate(nullptr);
294void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
296 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
298 onSurfaceDied(surface);
301 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
304void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
306 if (surface->type() == Mir::InputMethodType) {
307 removeInputMethodWindow();
311 int i = indexOf(surface);
316 auto application = m_windowModel[i].application;
318 // can't be starting if it already has a surface
319 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
321 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
322 m_windowModel[i].removeOnceSurfaceDestroyed = true;
324 // assume it got killed by the out-of-memory daemon.
326 // So leave entry in the model and only remove its surface, so shell can display a screenshot
328 m_windowModel[i].removeOnceSurfaceDestroyed = false;
332void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
334 int i = indexOf(surface);
339 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
342 auto window = m_windowModel[i].window;
343 window->setSurface(nullptr);
344 window->setFocused(false);
345 INFO_MSG << " Removed surface from entry. After: " << toString();
349Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
351 int id = generateId();
352 Window *qmlWindow = new Window(id, this);
353 connectWindow(qmlWindow);
355 qmlWindow->setSurface(surface);
360void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface *surface)
362 DEBUG_MSG << "(" << surface << ")";
364 if (surface->parentSurface()) {
365 // Wrap it in a Window so that we keep focusedWindow() up to date.
366 Window *window = createWindow(surface);
367 connect(surface, &QObject::destroyed, window, [=](){
368 window->setSurface(nullptr);
369 window->deleteLater();
372 if (surface->type() == Mir::InputMethodType) {
373 connectSurface(surface);
374 setInputMethodWindow(createWindow(surface));
376 auto *application = m_applicationManager->findApplicationWithSurface(surface);
378 if (surface->state() == Mir::HiddenState) {
379 // Ignore it until it's finally shown
380 connect(surface, &unityapi::MirSurfaceInterface::stateChanged, this, [=](Mir::State newState) {
381 Q_ASSERT(newState != Mir::HiddenState);
382 disconnect(surface, &unityapi::MirSurfaceInterface::stateChanged, this, 0);
383 prependSurface(surface, application);
386 prependSurface(surface, application);
389 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
390 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
391 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
392 Window *promptWindow = createWindow(surface);
393 connect(surface, &QObject::destroyed, promptWindow, [=](){
394 promptWindow->setSurface(nullptr);
395 promptWindow->deleteLater();
402void TopLevelWindowModel::deleteAt(int index)
404 auto window = m_windowModel[index].window;
408 window->setSurface(nullptr);
413void TopLevelWindowModel::removeAt(int index)
415 if (m_modelState == IdleState) {
416 beginRemoveRows(QModelIndex(), index, index);
417 m_modelState = RemovingState;
419 Q_ASSERT(m_modelState == ResettingState);
420 // No point in signaling anything if we're resetting the whole model
423 auto window = m_windowModel[index].window;
425 if (!window->surface()) {
426 window->setFocused(false);
429 m_windowModel.removeAt(index);
431 if (m_modelState == RemovingState) {
433 Q_EMIT countChanged();
434 Q_EMIT listChanged();
435 m_modelState = IdleState;
438 if (m_focusedWindow == window) {
439 setFocusedWindow(nullptr);
442 if (m_closingAllApps) {
443 if (m_windowModel.isEmpty()) {
444 Q_EMIT closedAllWindows();
448 INFO_MSG << " after " << toString() << " apps left " << m_windowModel.count();
451void TopLevelWindowModel::setInputMethodWindow(Window *window)
453 if (m_inputMethodWindow) {
454 qWarning("Multiple Input Method Surfaces created, removing the old one!");
455 delete m_inputMethodWindow;
457 m_inputMethodWindow = window;
458 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
461void TopLevelWindowModel::removeInputMethodWindow()
463 if (m_inputMethodWindow) {
464 delete m_inputMethodWindow;
465 m_inputMethodWindow = nullptr;
466 Q_EMIT inputMethodSurfaceChanged(nullptr);
470void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
472 DEBUG_MSG << "(" << surfaces << ")";
473 const int raiseCount = surfaces.size();
474 for (int i = 0; i < raiseCount; i++) {
475 int fromIndex = findIndexOf(surfaces[i]);
476 if (fromIndex != -1) {
482int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
484 return m_windowModel.count();
487QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
489 if (index.row() < 0 || index.row() >= m_windowModel.size())
492 if (role == WindowRole) {
493 Window *window = m_windowModel.at(index.row()).window;
494 return QVariant::fromValue(window);
495 } else if (role == ApplicationRole) {
496 return QVariant::fromValue(m_windowModel.at(index.row()).application);
502int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
504 for (int i=0; i<m_windowModel.count(); i++) {
505 if (m_windowModel[i].window->surface() == surface) {
512int TopLevelWindowModel::generateId()
515 m_nextId = nextFreeId(nextId(id), id);
519int TopLevelWindowModel::nextId(int id) const
528int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
530 int firstCandidateId = candidateId;
532 while (indexForId(candidateId) != -1 || candidateId == latestId) {
533 candidateId = nextId(candidateId);
535 if (candidateId == firstCandidateId) {
536 qFatal("TopLevelWindowModel: run out of window ids.");
543QString TopLevelWindowModel::toString()
546 for (int i = 0; i < m_windowModel.count(); ++i) {
547 auto item = m_windowModel.at(i);
549 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
550 .arg(QString::number(i),
551 item.application->appId(),
552 QString::number((qintptr)item.window->surface(), 16),
553 QString::number(item.window->id()));
563int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
565 for (int i = 0; i < m_windowModel.count(); ++i) {
566 if (m_windowModel.at(i).window->surface() == surface) {
573int TopLevelWindowModel::indexForId(int id) const
575 for (int i = 0; i < m_windowModel.count(); ++i) {
576 if (m_windowModel[i].window->id() == id) {
583Window *TopLevelWindowModel::windowAt(int index) const
585 if (index >=0 && index < m_windowModel.count()) {
586 return m_windowModel[index].window;
592unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
594 if (index >=0 && index < m_windowModel.count()) {
595 return m_windowModel[index].window->surface();
601unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
603 if (index >=0 && index < m_windowModel.count()) {
604 return m_windowModel[index].application;
610int TopLevelWindowModel::idAt(int index) const
612 if (index >=0 && index < m_windowModel.count()) {
613 return m_windowModel[index].window->id();
619void TopLevelWindowModel::raiseId(int id)
621 if (m_modelState == IdleState) {
622 DEBUG_MSG << "(id=" << id << ") - do it now.";
625 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
626 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
627 // if we perform yet another model change straight away.
629 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
630 // the index is definitely within bounds.
631 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
635void TopLevelWindowModel::doRaiseId(int id)
637 int fromIndex = indexForId(id);
638 // can't raise something that doesn't exist or that it's already on top
639 if (fromIndex != -1 && fromIndex != 0) {
640 auto surface = m_windowModel[fromIndex].window->surface();
642 m_surfaceManager->raise(surface);
644 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
645 // miral can do about it.
651void TopLevelWindowModel::setFocusedWindow(Window *window)
653 if (window != m_focusedWindow) {
654 INFO_MSG << "(" << window << ")";
656 m_previousWindow = m_focusedWindow;
658 m_focusedWindow = window;
659 Q_EMIT focusedWindowChanged(m_focusedWindow);
661 if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
662 // do it ourselves. miral doesn't know about this window
663 m_previousWindow->setFocused(false);
668 m_pendingActivation = false;
671unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
673 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
676Window* TopLevelWindowModel::focusedWindow() const
678 return m_focusedWindow;
681void TopLevelWindowModel::move(int from, int to)
683 if (from == to) return;
684 DEBUG_MSG << " from=" << from << " to=" << to;
686 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
688 /* When moving an item down, the destination index needs to be incremented
689 by one, as explained in the documentation:
690 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
692 Q_ASSERT(m_modelState == IdleState);
693 m_modelState = MovingState;
695 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
696#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
697 const auto &window = m_windowModel.takeAt(from);
698 m_windowModel.insert(to, window);
700 m_windowModel.move(from, to);
704 Q_EMIT listChanged();
705 m_modelState = IdleState;
707 INFO_MSG << " after " << toString();
710void TopLevelWindowModel::onModificationsStarted()
714void TopLevelWindowModel::onModificationsEnded()
716 if (m_focusedWindowChanged) {
717 setFocusedWindow(m_newlyFocusedWindow);
720 m_focusedWindowChanged = false;
721 m_newlyFocusedWindow = nullptr;
724void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
726 DEBUG_MSG << "(" << forbiddenId << ")";
728 for (int i = 0; i < m_windowModel.count(); ++i) {
729 Window *window = m_windowModel[i].window;
730 if (window->id() != forbiddenId) {
736void TopLevelWindowModel::closeAllWindows()
738 m_closingAllApps = true;
739 for (auto win : m_windowModel) {
743 // This is done after the for loop in the unlikely event that
744 // an app starts in between this
745 if (m_windowModel.isEmpty()) {
746 Q_EMIT closedAllWindows();
750bool TopLevelWindowModel::rootFocus()
752 return !m_nullWindow->focused();
755void TopLevelWindowModel::setRootFocus(bool focus)
757 INFO_MSG << "(" << focus << ")";
759 // Give focus back to previous focused window, only if null window is focused.
760 // If null window is not focused, a different app had taken the focus and we
761 // should repect that, or if a pendingActivation is going on.
762 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
763 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
764 m_previousWindow->activate();
767 if (!m_nullWindow->focused()) {
768 m_nullWindow->activate();
773// Pending Activation will block refocus of previous focused window
774// this is needed since surface activation with miral is async,
775// and activation of placeholder is sync. This causes a race condition
776// between placeholder and prev window. This results in prev window
777// gets focused, as it will always be later than the placeholder.
778void TopLevelWindowModel::pendingActivation()
780 m_pendingActivation = true;
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