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>
33#include "InputMethodManager.h"
35Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
37#define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
38#define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
40namespace unityapi = unity::shell::application;
42TopLevelWindowModel::TopLevelWindowModel()
43 : m_nullWindow(createNullWindow()),
44 m_surfaceManagerBusy(false)
46 connect(m_nullWindow, &Window::focusedChanged, this, [this] {
47 Q_EMIT rootFocusChanged();
51void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInterface* value)
53 if (m_applicationManager == value) {
57 DEBUG_MSG << "(" << value << ")";
59 Q_ASSERT(m_modelState == IdleState);
60 m_modelState = ResettingState;
64 if (m_applicationManager) {
65 m_windowModel.clear();
66 disconnect(m_applicationManager, 0, this, 0);
69 m_applicationManager = value;
71 if (m_applicationManager) {
72 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
73 this, [this](const QModelIndex &/*parent*/, int first, int last) {
74 for (int i = first; i <= last; ++i) {
75 auto application = m_applicationManager->get(i);
76 addApplication(application);
80 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
81 this, [this](const QModelIndex &/*parent*/, int first, int last) {
82 for (int i = first; i <= last; ++i) {
83 auto application = m_applicationManager->get(i);
84 removeApplication(application);
88 for (int i = 0; i < m_applicationManager->rowCount(); ++i) {
89 auto application = m_applicationManager->get(i);
90 addApplication(application);
95 m_modelState = IdleState;
98void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
100 if (surfaceManager == m_surfaceManager) {
104 DEBUG_MSG << "(" << surfaceManager << ")";
106 if (m_surfaceManager) {
107 disconnect(m_surfaceManager, 0, this, 0);
110 m_surfaceManager = surfaceManager;
112 if (m_surfaceManager) {
113 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
114 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
115 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
116 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
119 Q_EMIT surfaceManagerChanged(m_surfaceManager);
122void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
124 DEBUG_MSG << "(" << application->appId() << ")";
126 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
127 prependPlaceholder(application);
131void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
133 DEBUG_MSG << "(" << application->appId() << ")";
135 Q_ASSERT(m_modelState == IdleState);
138 while (i < m_windowModel.count()) {
139 if (m_windowModel.at(i).application == application) {
147void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
149 INFO_MSG << "(" << application->appId() << ")";
151 prependSurfaceHelper(nullptr, application);
154void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
156 Q_ASSERT(surface != nullptr);
158 connectSurface(surface);
160 bool filledPlaceholder = false;
161 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
162 ModelEntry &entry = m_windowModel[i];
163 if (entry.application == application && entry.window->surface() == nullptr) {
164 entry.window->setSurface(surface);
165 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
166 << ", filling out placeholder. after: " << toString();
167 filledPlaceholder = true;
171 if (!filledPlaceholder) {
172 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
173 prependSurfaceHelper(surface, application);
177void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
180 Window *window = createWindow(surface);
182 connect(window, &Window::stateChanged, this, [=](Mir::State newState) {
183 if (newState == Mir::HiddenState) {
184 // Comply, removing it from our model. Just as if it didn't exist anymore.
185 removeAt(indexForId(window->id()));
187 if (indexForId(window->id()) == -1) {
188 // was probably hidden before. put it back on the list
189 auto *application = m_applicationManager->findApplicationWithSurface(window->surface());
190 Q_ASSERT(application);
191 prependWindow(window, application);
196 prependWindow(window, application);
198 // Activate the newly-prepended 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());
475 InputMethodManager::instance()->setWindow(window);
478void TopLevelWindowModel::removeInputMethodWindow()
480 if (m_inputMethodWindow) {
481 delete m_inputMethodWindow;
482 m_inputMethodWindow = nullptr;
483 Q_EMIT inputMethodSurfaceChanged(nullptr);
484 InputMethodManager::instance()->setWindow(nullptr);
488void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
490 DEBUG_MSG << "(" << surfaces << ")";
491 const int raiseCount = surfaces.size();
492 for (int i = 0; i < raiseCount; i++) {
493 int fromIndex = findIndexOf(surfaces[i]);
494 if (fromIndex != -1) {
500int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
502 return m_windowModel.count();
505QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
507 if (index.row() < 0 || index.row() >= m_windowModel.size())
510 if (role == WindowRole) {
511 Window *window = m_windowModel.at(index.row()).window;
512 return QVariant::fromValue(window);
513 } else if (role == ApplicationRole) {
514 return QVariant::fromValue(m_windowModel.at(index.row()).application);
520int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
522 for (int i=0; i<m_windowModel.count(); i++) {
523 if (m_windowModel[i].window->surface() == surface) {
530QString TopLevelWindowModel::toString()
533 for (int i = 0; i < m_windowModel.count(); ++i) {
534 auto item = m_windowModel.at(i);
536 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
537 .arg(QString::number(i),
538 item.application->appId(),
539 QString::number((qintptr)item.window->surface(), 16),
540 QString::number(item.window->id()));
550int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
552 for (int i = 0; i < m_windowModel.count(); ++i) {
553 if (m_windowModel.at(i).window->surface() == surface) {
560int TopLevelWindowModel::indexForId(int id) const
562 for (int i = 0; i < m_windowModel.count(); ++i) {
563 if (m_windowModel[i].window->id() == id) {
570Window *TopLevelWindowModel::windowAt(int index) const
572 if (index >=0 && index < m_windowModel.count()) {
573 return m_windowModel[index].window;
579unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
581 if (index >=0 && index < m_windowModel.count()) {
582 return m_windowModel[index].window->surface();
588unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
590 if (index >=0 && index < m_windowModel.count()) {
591 return m_windowModel[index].application;
597int TopLevelWindowModel::idAt(int index) const
599 if (index >=0 && index < m_windowModel.count()) {
600 return m_windowModel[index].window->id();
606void TopLevelWindowModel::raiseId(int id)
608 if (m_modelState == IdleState) {
609 DEBUG_MSG << "(id=" << id << ") - do it now.";
612 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
613 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
614 // if we perform yet another model change straight away.
616 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
617 // the index is definitely within bounds.
618 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
622void TopLevelWindowModel::doRaiseId(int id)
624 int fromIndex = indexForId(id);
625 // can't raise something that doesn't exist or that it's already on top
626 if (fromIndex != -1 && fromIndex != 0) {
627 auto surface = m_windowModel[fromIndex].window->surface();
629 m_surfaceManager->raise(surface);
631 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
632 // miral can do about it.
638void TopLevelWindowModel::setFocusedWindow(Window *window)
640 if (window != m_focusedWindow) {
641 INFO_MSG << "(" << window << ")";
643 m_previousWindow = m_focusedWindow;
645 m_focusedWindow = window;
646 Q_EMIT focusedWindowChanged(m_focusedWindow);
648 if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
649 // do it ourselves. miral doesn't know about this window
650 m_previousWindow->setFocused(false);
655 m_pendingActivation = false;
658unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
660 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
663Window* TopLevelWindowModel::focusedWindow() const
665 return m_focusedWindow;
668void TopLevelWindowModel::move(int from, int to)
670 if (from == to) return;
671 DEBUG_MSG << " from=" << from << " to=" << to;
673 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
675 /* When moving an item down, the destination index needs to be incremented
676 by one, as explained in the documentation:
677 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
679 Q_ASSERT(m_modelState == IdleState);
680 m_modelState = MovingState;
682 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
683#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
684 const auto &window = m_windowModel.takeAt(from);
685 m_windowModel.insert(to, window);
687 m_windowModel.move(from, to);
691 Q_EMIT listChanged();
692 m_modelState = IdleState;
694 INFO_MSG << " after " << toString();
697void TopLevelWindowModel::onModificationsStarted()
699 m_surfaceManagerBusy = true;
702void TopLevelWindowModel::onModificationsEnded()
704 if (m_focusedWindowChanged) {
705 setFocusedWindow(m_newlyFocusedWindow);
708 m_focusedWindowChanged = false;
709 m_newlyFocusedWindow = nullptr;
710 m_surfaceManagerBusy = false;
713void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
715 DEBUG_MSG << "(" << forbiddenId << ")";
717 for (int i = 0; i < m_windowModel.count(); ++i) {
718 Window *window = m_windowModel[i].window;
719 if (window->id() != forbiddenId) {
726void TopLevelWindowModel::closeAllWindows()
728 m_closingAllApps = true;
729 for (auto win : m_windowModel) {
733 // This is done after the for loop in the unlikely event that
734 // an app starts in between this
735 if (m_windowModel.isEmpty()) {
736 Q_EMIT closedAllWindows();
740bool TopLevelWindowModel::rootFocus()
742 return !m_nullWindow->focused();
745void TopLevelWindowModel::setRootFocus(bool focus)
747 INFO_MSG << "(" << focus << "), surfaceManagerBusy is " << m_surfaceManagerBusy;
749 if (m_surfaceManagerBusy) {
750 // Something else is probably being focused already, let's not add to
756 // Give focus back to previous focused window, only if null window is focused.
757 // If null window is not focused, a different app had taken the focus and we
758 // should repect that, or if a pendingActivation is going on.
759 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
760 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
761 m_previousWindow->activate();
762 } else if (!m_pendingActivation) {
763 // The previous window does not exist any more, focus top window.
764 activateTopMostWindowWithoutId(-1);
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