2 * Copyright (C) 2016-2017 Canonical, Ltd.
3 * Copyright 2019 UBports Foundation
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 3, as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
11 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18#include "TopLevelWindowModel.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>
28#include <QGuiApplication>
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()
42 : m_nullWindow(createNullWindow()),
43 m_surfaceManagerBusy(false)
45 connect(m_nullWindow, &Window::focusedChanged, this, [this] {
46 Q_EMIT rootFocusChanged();
50void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInterface* value)
52 if (m_applicationManager == value) {
56 DEBUG_MSG << "(" << value << ")";
58 Q_ASSERT(m_modelState == IdleState);
59 m_modelState = ResettingState;
63 if (m_applicationManager) {
64 m_windowModel.clear();
65 disconnect(m_applicationManager, 0, this, 0);
68 m_applicationManager = value;
70 if (m_applicationManager) {
71 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
72 this, [this](const QModelIndex &/*parent*/, int first, int last) {
73 for (int i = first; i <= last; ++i) {
74 auto application = m_applicationManager->get(i);
75 addApplication(application);
79 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
80 this, [this](const QModelIndex &/*parent*/, int first, int last) {
81 for (int i = first; i <= last; ++i) {
82 auto application = m_applicationManager->get(i);
83 removeApplication(application);
87 for (int i = 0; i < m_applicationManager->rowCount(); ++i) {
88 auto application = m_applicationManager->get(i);
89 addApplication(application);
94 m_modelState = IdleState;
97void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
99 if (surfaceManager == m_surfaceManager) {
103 DEBUG_MSG << "(" << surfaceManager << ")";
105 if (m_surfaceManager) {
106 disconnect(m_surfaceManager, 0, this, 0);
109 m_surfaceManager = surfaceManager;
111 if (m_surfaceManager) {
112 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
113 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
114 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
115 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
118 Q_EMIT surfaceManagerChanged(m_surfaceManager);
121void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
123 DEBUG_MSG << "(" << application->appId() << ")";
125 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
126 prependPlaceholder(application);
130void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
132 DEBUG_MSG << "(" << application->appId() << ")";
134 Q_ASSERT(m_modelState == IdleState);
137 while (i < m_windowModel.count()) {
138 if (m_windowModel.at(i).application == application) {
146void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
148 INFO_MSG << "(" << application->appId() << ")";
150 prependSurfaceHelper(nullptr, application);
153void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
155 Q_ASSERT(surface != nullptr);
157 connectSurface(surface);
159 bool filledPlaceholder = false;
160 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
161 ModelEntry &entry = m_windowModel[i];
162 if (entry.application == application && entry.window->surface() == nullptr) {
163 entry.window->setSurface(surface);
164 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
165 << ", filling out placeholder. after: " << toString();
166 filledPlaceholder = true;
170 if (!filledPlaceholder) {
171 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
172 prependSurfaceHelper(surface, application);
176void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
179 Window *window = createWindow(surface);
181 connect(window, &Window::stateChanged, this, [=](Mir::State newState) {
182 if (newState == Mir::HiddenState) {
183 // Comply, removing it from our model. Just as if it didn't exist anymore.
184 removeAt(indexForId(window->id()));
186 if (indexForId(window->id()) == -1) {
187 // was probably hidden before. put it back on the list
188 auto *application = m_applicationManager->findApplicationWithSurface(window->surface());
189 Q_ASSERT(application);
190 prependWindow(window, application);
195 prependWindow(window, application);
198 activateEmptyWindow(window);
201 INFO_MSG << " after " << toString();
204void TopLevelWindowModel::prependWindow(Window *window, unityapi::ApplicationInfoInterface *application)
206 if (m_modelState == IdleState) {
207 m_modelState = InsertingState;
208 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
210 Q_ASSERT(m_modelState == ResettingState);
211 // No point in signaling anything if we're resetting the whole model
214 m_windowModel.prepend(ModelEntry(window, application));
216 if (m_modelState == InsertingState) {
218 Q_EMIT countChanged();
219 Q_EMIT listChanged();
220 m_modelState = IdleState;
224void TopLevelWindowModel::connectWindow(Window *window)
226 connect(window, &Window::focusRequested, this, [this, window]() {
227 if (!window->surface()) {
228 activateEmptyWindow(window);
232 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
233 if (window->surface()) {
234 // Condense changes to the focused window
235 // eg: Do focusedWindow=A to focusedWindow=B instead of
236 // focusedWindow=A to focusedWindow=null to focusedWindow=B
238 Q_ASSERT(m_newlyFocusedWindow == nullptr);
239 m_focusedWindowChanged = true;
240 m_newlyFocusedWindow = window;
241 } else if (m_focusedWindow == window) {
242 m_focusedWindowChanged = true;
244 // don't clear the focused window if you were not there in the first place
245 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
250 connect(window, &Window::closeRequested, this, [this, window]() {
251 if (!window->surface()) {
252 // do things ourselves as miral doesn't know about this window
253 int id = window->id();
254 int index = indexForId(id);
255 bool focusOther = false;
256 Q_ASSERT(index >= 0);
257 if (window->focused()) {
260 m_windowModel[index].application->close();
262 activateTopMostWindowWithoutId(id);
267 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
268 activateEmptyWindow(window);
271 connect(window, &Window::liveChanged, this, [this, window](bool isAlive) {
272 if (!isAlive && window->state() == Mir::HiddenState) {
273 // Hidden windows are not in the model. So just delete it right away.
279void TopLevelWindowModel::activateEmptyWindow(Window *window)
281 Q_ASSERT(!window->surface());
282 DEBUG_MSG << "(" << window << ")";
284 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
285 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
287 window->setFocused(true);
288 raiseId(window->id());
289 Window *previousWindow = m_focusedWindow;
290 setFocusedWindow(window);
291 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
292 m_surfaceManager->activate(nullptr);
296void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
298 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
300 onSurfaceDied(surface);
303 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
306void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
308 if (surface->type() == Mir::InputMethodType) {
309 removeInputMethodWindow();
313 int i = indexOf(surface);
318 auto application = m_windowModel[i].application;
320 // can't be starting if it already has a surface
321 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
323 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
324 m_windowModel[i].removeOnceSurfaceDestroyed = true;
326 // assume it got killed by the out-of-memory daemon.
328 // So leave entry in the model and only remove its surface, so shell can display a screenshot
330 m_windowModel[i].removeOnceSurfaceDestroyed = false;
334void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
336 int i = indexOf(surface);
341 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
344 auto window = m_windowModel[i].window;
345 window->setSurface(nullptr);
346 window->setFocused(false);
347 INFO_MSG << " Removed surface from entry. After: " << toString();
351Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
353 int id = m_nextId.fetchAndAddAcquire(1);
354 return createWindowWithId(surface, id);
357Window *TopLevelWindowModel::createNullWindow()
359 return createWindowWithId(nullptr, 0);
362Window *TopLevelWindowModel::createWindowWithId(unityapi::MirSurfaceInterface *surface, int id)
364 Window *qmlWindow = new Window(id, this);
365 connectWindow(qmlWindow);
367 qmlWindow->setSurface(surface);
372void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface *surface)
374 DEBUG_MSG << "(" << surface << ")";
376 if (surface->parentSurface()) {
377 // Wrap it in a Window so that we keep focusedWindow() up to date.
378 Window *window = createWindow(surface);
379 connect(surface, &QObject::destroyed, window, [=](){
380 window->setSurface(nullptr);
381 window->deleteLater();
384 if (surface->type() == Mir::InputMethodType) {
385 connectSurface(surface);
386 setInputMethodWindow(createWindow(surface));
388 auto *application = m_applicationManager->findApplicationWithSurface(surface);
390 if (surface->state() == Mir::HiddenState) {
391 // Ignore it until it's finally shown
392 connect(surface, &unityapi::MirSurfaceInterface::stateChanged, this, [=](Mir::State newState) {
393 Q_ASSERT(newState != Mir::HiddenState);
394 disconnect(surface, &unityapi::MirSurfaceInterface::stateChanged, this, 0);
395 prependSurface(surface, application);
398 prependSurface(surface, application);
401 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
402 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
403 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
404 Window *promptWindow = createWindow(surface);
405 connect(surface, &QObject::destroyed, promptWindow, [=](){
406 promptWindow->setSurface(nullptr);
407 promptWindow->deleteLater();
414void TopLevelWindowModel::deleteAt(int index)
416 auto window = m_windowModel[index].window;
420 window->setSurface(nullptr);
425void TopLevelWindowModel::removeAt(int index)
427 if (m_modelState == IdleState) {
428 beginRemoveRows(QModelIndex(), index, index);
429 m_modelState = RemovingState;
431 Q_ASSERT(m_modelState == ResettingState);
432 // No point in signaling anything if we're resetting the whole model
435 auto window = m_windowModel[index].window;
437 if (!window->surface()) {
438 window->setFocused(false);
441 m_windowModel.removeAt(index);
443 if (m_modelState == RemovingState) {
445 Q_EMIT countChanged();
446 Q_EMIT listChanged();
447 m_modelState = IdleState;
450 if (m_focusedWindow == window) {
451 setFocusedWindow(nullptr);
454 if (m_previousWindow == window) {
455 m_previousWindow = nullptr;
458 if (m_closingAllApps) {
459 if (m_windowModel.isEmpty()) {
460 Q_EMIT closedAllWindows();
464 INFO_MSG << " after " << toString() << " apps left " << m_windowModel.count();
467void TopLevelWindowModel::setInputMethodWindow(Window *window)
469 if (m_inputMethodWindow) {
470 qWarning("Multiple Input Method Surfaces created, removing the old one!");
471 delete m_inputMethodWindow;
473 m_inputMethodWindow = window;
474 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
477void TopLevelWindowModel::removeInputMethodWindow()
479 if (m_inputMethodWindow) {
480 delete m_inputMethodWindow;
481 m_inputMethodWindow = nullptr;
482 Q_EMIT inputMethodSurfaceChanged(nullptr);
486void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
488 DEBUG_MSG << "(" << surfaces << ")";
489 const int raiseCount = surfaces.size();
490 for (int i = 0; i < raiseCount; i++) {
491 int fromIndex = findIndexOf(surfaces[i]);
492 if (fromIndex != -1) {
498int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
500 return m_windowModel.count();
503QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
505 if (index.row() < 0 || index.row() >= m_windowModel.size())
508 if (role == WindowRole) {
509 Window *window = m_windowModel.at(index.row()).window;
510 return QVariant::fromValue(window);
511 } else if (role == ApplicationRole) {
512 return QVariant::fromValue(m_windowModel.at(index.row()).application);
518int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
520 for (int i=0; i<m_windowModel.count(); i++) {
521 if (m_windowModel[i].window->surface() == surface) {
528QString TopLevelWindowModel::toString()
531 for (int i = 0; i < m_windowModel.count(); ++i) {
532 auto item = m_windowModel.at(i);
534 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
535 .arg(QString::number(i),
536 item.application->appId(),
537 QString::number((qintptr)item.window->surface(), 16),
538 QString::number(item.window->id()));
548int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
550 for (int i = 0; i < m_windowModel.count(); ++i) {
551 if (m_windowModel.at(i).window->surface() == surface) {
558int TopLevelWindowModel::indexForId(int id) const
560 for (int i = 0; i < m_windowModel.count(); ++i) {
561 if (m_windowModel[i].window->id() == id) {
568Window *TopLevelWindowModel::windowAt(int index) const
570 if (index >=0 && index < m_windowModel.count()) {
571 return m_windowModel[index].window;
577unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
579 if (index >=0 && index < m_windowModel.count()) {
580 return m_windowModel[index].window->surface();
586unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
588 if (index >=0 && index < m_windowModel.count()) {
589 return m_windowModel[index].application;
595int TopLevelWindowModel::idAt(int index) const
597 if (index >=0 && index < m_windowModel.count()) {
598 return m_windowModel[index].window->id();
604void TopLevelWindowModel::raiseId(int id)
606 if (m_modelState == IdleState) {
607 DEBUG_MSG << "(id=" << id << ") - do it now.";
610 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
611 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
612 // if we perform yet another model change straight away.
614 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
615 // the index is definitely within bounds.
616 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
620void TopLevelWindowModel::doRaiseId(int id)
622 int fromIndex = indexForId(id);
623 // can't raise something that doesn't exist or that it's already on top
624 if (fromIndex != -1 && fromIndex != 0) {
625 auto surface = m_windowModel[fromIndex].window->surface();
627 m_surfaceManager->raise(surface);
629 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
630 // miral can do about it.
636void TopLevelWindowModel::setFocusedWindow(Window *window)
638 if (window != m_focusedWindow) {
639 INFO_MSG << "(" << window << ")";
641 m_previousWindow = m_focusedWindow;
643 m_focusedWindow = window;
644 Q_EMIT focusedWindowChanged(m_focusedWindow);
646 if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
647 // do it ourselves. miral doesn't know about this window
648 m_previousWindow->setFocused(false);
653 m_pendingActivation = false;
656unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
658 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
661Window* TopLevelWindowModel::focusedWindow() const
663 return m_focusedWindow;
666void TopLevelWindowModel::move(int from, int to)
668 if (from == to) return;
669 DEBUG_MSG << " from=" << from << " to=" << to;
671 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
673 /* When moving an item down, the destination index needs to be incremented
674 by one, as explained in the documentation:
675 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
677 Q_ASSERT(m_modelState == IdleState);
678 m_modelState = MovingState;
680 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
681#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
682 const auto &window = m_windowModel.takeAt(from);
683 m_windowModel.insert(to, window);
685 m_windowModel.move(from, to);
689 Q_EMIT listChanged();
690 m_modelState = IdleState;
692 INFO_MSG << " after " << toString();
695void TopLevelWindowModel::onModificationsStarted()
697 m_surfaceManagerBusy = true;
700void TopLevelWindowModel::onModificationsEnded()
702 if (m_focusedWindowChanged) {
703 setFocusedWindow(m_newlyFocusedWindow);
706 m_focusedWindowChanged = false;
707 m_newlyFocusedWindow = nullptr;
708 m_surfaceManagerBusy = false;
711void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
713 DEBUG_MSG << "(" << forbiddenId << ")";
715 for (int i = 0; i < m_windowModel.count(); ++i) {
716 Window *window = m_windowModel[i].window;
717 if (window->id() != forbiddenId) {
724void TopLevelWindowModel::closeAllWindows()
726 m_closingAllApps = true;
727 for (auto win : m_windowModel) {
731 // This is done after the for loop in the unlikely event that
732 // an app starts in between this
733 if (m_windowModel.isEmpty()) {
734 Q_EMIT closedAllWindows();
738bool TopLevelWindowModel::rootFocus()
740 return !m_nullWindow->focused();
743void TopLevelWindowModel::setRootFocus(bool focus)
745 INFO_MSG << "(" << focus << "), surfaceManagerBusy is " << m_surfaceManagerBusy;
747 if (m_surfaceManagerBusy) {
748 // Something else is probably being focused already, let's not add to
754 // Give focus back to previous focused window, only if null window is focused.
755 // If null window is not focused, a different app had taken the focus and we
756 // should repect that, or if a pendingActivation is going on.
757 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
758 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
759 m_previousWindow->activate();
760 } else if (!m_pendingActivation) {
761 // The previous window does not exist any more, focus top window.
762 activateTopMostWindowWithoutId(-1);
765 if (!m_nullWindow->focused()) {
766 m_nullWindow->activate();
771// Pending Activation will block refocus of previous focused window
772// this is needed since surface activation with miral is async,
773// and activation of placeholder is sync. This causes a race condition
774// between placeholder and prev window. This results in prev window
775// gets focused, as it will always be later than the placeholder.
776void TopLevelWindowModel::pendingActivation()
778 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