18#include "TopLevelWindowModel.h"
19#include "WindowManagerObjects.h"
22#include <lomiri/shell/application/ApplicationInfoInterface.h>
23#include <lomiri/shell/application/ApplicationManagerInterface.h>
24#include <lomiri/shell/application/MirSurfaceInterface.h>
25#include <lomiri/shell/application/MirSurfaceListInterface.h>
26#include <lomiri/shell/application/SurfaceManagerInterface.h>
34#include "InputMethodManager.h"
36Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL,
"toplevelwindowmodel", QtInfoMsg)
38#define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
39#define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
41namespace lomiriapi = lomiri::shell::application;
43TopLevelWindowModel::TopLevelWindowModel(Workspace* workspace)
44 : m_nullWindow(createWindow(nullptr)),
45 m_workspace(workspace),
46 m_surfaceManagerBusy(false)
48 connect(WindowManagerObjects::instance(), &WindowManagerObjects::applicationManagerChanged,
49 this, &TopLevelWindowModel::setApplicationManager);
50 connect(WindowManagerObjects::instance(), &WindowManagerObjects::surfaceManagerChanged,
51 this, &TopLevelWindowModel::setSurfaceManager);
53 setApplicationManager(WindowManagerObjects::instance()->applicationManager());
54 setSurfaceManager(WindowManagerObjects::instance()->surfaceManager());
56 connect(m_nullWindow, &Window::focusedChanged,
this, [
this] {
57 Q_EMIT rootFocusChanged();
61TopLevelWindowModel::~TopLevelWindowModel()
65void TopLevelWindowModel::setApplicationManager(lomiriapi::ApplicationManagerInterface* value)
67 if (m_applicationManager == value) {
71 DEBUG_MSG <<
"(" << value <<
")";
73 Q_ASSERT(m_modelState == IdleState);
74 m_modelState = ResettingState;
78 if (m_applicationManager) {
79 disconnect(m_applicationManager, 0,
this, 0);
82 m_applicationManager = value;
84 if (m_applicationManager) {
85 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
86 this, [
this](
const QModelIndex &,
int first,
int last) {
87 if (!m_workspace || !m_workspace->isActive())
90 for (
int i = first; i <= last; ++i) {
91 auto application = m_applicationManager->get(i);
92 addApplication(application);
96 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
97 this, [
this](
const QModelIndex &,
int first,
int last) {
98 for (
int i = first; i <= last; ++i) {
99 auto application = m_applicationManager->get(i);
100 removeApplication(application);
108 m_modelState = IdleState;
111void TopLevelWindowModel::setSurfaceManager(lomiriapi::SurfaceManagerInterface *surfaceManager)
113 if (surfaceManager == m_surfaceManager) {
117 DEBUG_MSG <<
"(" << surfaceManager <<
")";
119 Q_ASSERT(m_modelState == IdleState);
120 m_modelState = ResettingState;
124 if (m_surfaceManager) {
125 disconnect(m_surfaceManager, 0,
this, 0);
128 m_surfaceManager = surfaceManager;
130 if (m_surfaceManager) {
131 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
132 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::surfacesRaised,
this, &TopLevelWindowModel::onSurfacesRaised);
133 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::surfaceRemoved,
this, &TopLevelWindowModel::onSurfaceDestroyed);
134 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::modificationsStarted,
this, &TopLevelWindowModel::onModificationsStarted);
135 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::modificationsEnded,
this, &TopLevelWindowModel::onModificationsEnded);
141 m_modelState = IdleState;
144void TopLevelWindowModel::addApplication(lomiriapi::ApplicationInfoInterface *application)
146 DEBUG_MSG <<
"(" << application->appId() <<
")";
148 if (application->state() != lomiriapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
149 prependPlaceholder(application);
153void TopLevelWindowModel::removeApplication(lomiriapi::ApplicationInfoInterface *application)
155 DEBUG_MSG <<
"(" << application->appId() <<
")";
157 Q_ASSERT(m_modelState == IdleState);
160 while (i < m_windowModel.count()) {
161 if (m_windowModel.at(i).application == application) {
169void TopLevelWindowModel::prependPlaceholder(lomiriapi::ApplicationInfoInterface *application)
171 DEBUG_MSG <<
"(" << application->appId() <<
")";
173 if (!application->showSplash())
176 prependSurfaceHelper(
nullptr, application);
179void TopLevelWindowModel::prependSurface(lomiriapi::MirSurfaceInterface *surface, lomiriapi::ApplicationInfoInterface *application)
181 Q_ASSERT(surface !=
nullptr);
183 connectSurface(surface);
184 m_allSurfaces.insert(surface);
186 bool filledPlaceholder =
false;
187 for (
int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
188 ModelEntry &entry = m_windowModel[i];
189 if (entry.application == application && (entry.window->surface() ==
nullptr || !entry.window->surface()->live())) {
190 entry.window->setSurface(surface);
191 DEBUG_MSG <<
" appId=" << application->appId() <<
" surface=" << surface
192 <<
", filling out placeholder. after: " << toString();
193 filledPlaceholder =
true;
197 if (!filledPlaceholder) {
198 DEBUG_MSG <<
" appId=" << application->appId() <<
" surface=" << surface <<
", adding new row";
199 prependSurfaceHelper(surface, application);
203void TopLevelWindowModel::prependSurfaceHelper(lomiriapi::MirSurfaceInterface *surface, lomiriapi::ApplicationInfoInterface *application)
206 Window *window = createWindow(surface);
208 connect(window, &Window::stateChanged,
this, [=](Mir::State newState) {
209 if (newState == Mir::HiddenState) {
215 auto *application = m_applicationManager->findApplicationWithSurface(window->
surface());
216 Q_ASSERT(application);
217 prependWindow(window, application);
222 prependWindow(window, application);
227 DEBUG_MSG <<
" after " << toString();
230void TopLevelWindowModel::prependWindow(
Window *window, lomiriapi::ApplicationInfoInterface *application)
232 if (m_modelState == IdleState) {
233 m_modelState = InsertingState;
234 beginInsertRows(QModelIndex(), 0 , 0 );
236 Q_ASSERT(m_modelState == ResettingState);
240 m_windowModel.prepend(ModelEntry(window, application));
242 if (m_modelState == InsertingState) {
244 Q_EMIT countChanged();
246 m_modelState = IdleState;
250void TopLevelWindowModel::connectWindow(
Window *window)
254 activateEmptyWindow(window);
258 connect(window, &Window::focusedChanged,
this, [
this, window](
bool focused) {
261 setFocusedWindow(window);
262 m_focusedWindowCleared =
false;
263 }
else if (m_focusedWindow == window) {
267 m_focusedWindowCleared =
true;
275 connect(window, &Window::closeRequested,
this, [
this, window]() {
278 int id = window->
id();
280 bool focusOther =
false;
281 Q_ASSERT(index >= 0);
285 m_windowModel[index].application->close();
287 activateTopMostWindowWithoutId(
id);
292 connect(window, &Window::emptyWindowActivated,
this, [
this, window]() {
293 activateEmptyWindow(window);
296 connect(window, &Window::liveChanged,
this, [
this, window](
bool isAlive) {
297 if (!isAlive && window->
state() == Mir::HiddenState) {
304void TopLevelWindowModel::activateEmptyWindow(
Window *window)
307 DEBUG_MSG <<
"(" << window <<
")";
312 window->setFocused(
true);
314 Window *previousWindow = m_focusedWindow;
315 setFocusedWindow(window);
316 if (previousWindow && previousWindow->
surface() && previousWindow->
surface()->focused()) {
317 m_surfaceManager->activate(
nullptr);
321void TopLevelWindowModel::connectSurface(lomiriapi::MirSurfaceInterface *surface)
323 connect(surface, &lomiriapi::MirSurfaceInterface::liveChanged,
this, [
this, surface](
bool live){
325 onSurfaceDied(surface);
328 connect(surface, &QObject::destroyed,
this, [
this, surface](QObject*){
329 this->onSurfaceDestroyed(surface);
333int TopLevelWindowModel::countWindowsWithApplication(lomiri::shell::application::ApplicationInfoInterface *application)
337 for (
const auto & item : qAsConst(m_windowModel)) {
338 if (item.application == application)
345void TopLevelWindowModel::onSurfaceDied(lomiriapi::MirSurfaceInterface *surface)
347 if (surface->type() == Mir::InputMethodType) {
348 removeInputMethodWindow();
352 int i = indexOf(surface);
357 auto application = m_windowModel[i].application;
359 DEBUG_MSG <<
" application->name()=" << application->name()
360 <<
" application->state()=" << application->state();
366 if (application->isTouchApp() && countWindowsWithApplication(application) == 1)
367 m_windowModel[i].removeOnceSurfaceDestroyed =
false;
369 m_windowModel[i].removeOnceSurfaceDestroyed =
true;
372void TopLevelWindowModel::onSurfaceDestroyed(lomiriapi::MirSurfaceInterface *surface)
374 int i = indexOf(surface);
379 auto application = m_windowModel[i].application;
383 if (application->appId() == QStringLiteral(
"xwayland.qtmir") || !application->isTouchApp()) {
384 m_windowModel[i].removeOnceSurfaceDestroyed =
true;
387 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
390 auto window = m_windowModel[i].window;
391 window->setFocused(
false);
392 m_allSurfaces.remove(surface);
393 DEBUG_MSG <<
" Removed surface from entry. After: " << toString();
397Window *TopLevelWindowModel::createWindow(lomiriapi::MirSurfaceInterface *surface)
399 int id = m_nextId.fetchAndAddAcquire(1);
400 return createWindowWithId(surface,
id);
403Window *TopLevelWindowModel::createNullWindow()
405 return createWindowWithId(
nullptr, 0);
408Window *TopLevelWindowModel::createWindowWithId(lomiriapi::MirSurfaceInterface *surface,
int id)
410 Window *qmlWindow =
new Window(
id,
this);
411 connectWindow(qmlWindow);
413 qmlWindow->setSurface(surface);
418void TopLevelWindowModel::onSurfacesAddedToWorkspace(
const std::shared_ptr<miral::Workspace>& workspace,
419 const QVector<lomiri::shell::application::MirSurfaceInterface*> surfaces)
421 if (!m_workspace || !m_applicationManager)
return;
422 if (workspace != m_workspace->workspace()) {
423 removeSurfaces(surfaces);
427 Q_FOREACH(
auto surface, surfaces) {
428 if (m_allSurfaces.contains(surface))
continue;
430 if (surface->parentSurface()) {
432 Window *window = createWindow(surface);
433 connect(surface, &QObject::destroyed, window, [=](){
434 window->setSurface(
nullptr);
435 window->deleteLater();
438 if (surface->type() == Mir::InputMethodType) {
439 connectSurface(surface);
440 setInputMethodWindow(createWindow(surface));
442 auto *application = m_applicationManager->findApplicationWithSurface(surface);
444 if (surface->state() == Mir::HiddenState) {
446 connect(surface, &lomiriapi::MirSurfaceInterface::stateChanged,
this, [=](Mir::State newState) {
447 Q_ASSERT(newState != Mir::HiddenState);
448 disconnect(surface, &lomiriapi::MirSurfaceInterface::stateChanged,
this, 0);
449 prependSurface(surface, application);
452 prependSurface(surface, application);
458 Window *promptWindow = createWindow(surface);
459 connect(surface, &QObject::destroyed, promptWindow, [=](){
460 promptWindow->setSurface(
nullptr);
461 promptWindow->deleteLater();
469void TopLevelWindowModel::removeSurfaces(
const QVector<lomiri::shell::application::MirSurfaceInterface *> surfaces)
473 for (
auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
474 auto surface = *iter;
478 start = end = indexOf(surface);
481 m_allSurfaces.remove(surface);
484 while(iter != surfaces.constEnd()) {
485 int index = indexOf(*iter);
486 if (index != end+1) {
493 if (m_modelState == IdleState) {
494 beginRemoveRows(QModelIndex(), start, end);
495 m_modelState = RemovingState;
497 Q_ASSERT(m_modelState == ResettingState);
501 for (
int index = start; index <= end; index++) {
502 auto window = m_windowModel[start].window;
503 window->setSurface(
nullptr);
504 window->setFocused(
false);
506 if (window == m_previousWindow) {
507 m_previousWindow =
nullptr;
510 m_windowModel.removeAt(start);
511 m_allSurfaces.remove(surface);
514 if (m_modelState == RemovingState) {
516 Q_EMIT countChanged();
518 m_modelState = IdleState;
523void TopLevelWindowModel::deleteAt(
int index)
525 auto window = m_windowModel[index].window;
529 window->setSurface(
nullptr);
534void TopLevelWindowModel::removeAt(
int index)
536 if (m_modelState == IdleState) {
537 beginRemoveRows(QModelIndex(), index, index);
538 m_modelState = RemovingState;
540 Q_ASSERT(m_modelState == ResettingState);
544 auto window = m_windowModel[index].window;
545 auto surface = window->
surface();
548 window->setFocused(
false);
551 if (window == m_previousWindow) {
552 m_previousWindow =
nullptr;
555 m_windowModel.removeAt(index);
556 m_allSurfaces.remove(surface);
558 if (m_modelState == RemovingState) {
560 Q_EMIT countChanged();
562 m_modelState = IdleState;
565 if (m_focusedWindow == window) {
566 setFocusedWindow(
nullptr);
567 m_focusedWindowCleared =
false;
570 if (m_previousWindow == window) {
571 m_previousWindow =
nullptr;
574 if (m_closingAllApps) {
575 if (m_windowModel.isEmpty()) {
576 Q_EMIT closedAllWindows();
580 DEBUG_MSG <<
" after " << toString() <<
" apps left " << m_windowModel.count();
583void TopLevelWindowModel::setInputMethodWindow(
Window *window)
585 if (m_inputMethodWindow) {
586 qWarning(
"Multiple Input Method Surfaces created, removing the old one!");
587 delete m_inputMethodWindow;
589 m_inputMethodWindow = window;
590 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
591 InputMethodManager::instance()->setWindow(window);
594void TopLevelWindowModel::removeInputMethodWindow()
596 if (m_inputMethodWindow) {
597 auto surface = m_inputMethodWindow->surface();
599 m_allSurfaces.remove(surface);
601 if (m_focusedWindow == m_inputMethodWindow) {
602 setFocusedWindow(
nullptr);
603 m_focusedWindowCleared =
false;
606 delete m_inputMethodWindow;
607 m_inputMethodWindow =
nullptr;
608 Q_EMIT inputMethodSurfaceChanged(
nullptr);
609 InputMethodManager::instance()->setWindow(
nullptr);
613void TopLevelWindowModel::onSurfacesRaised(
const QVector<lomiriapi::MirSurfaceInterface*> &surfaces)
615 DEBUG_MSG <<
"(" << surfaces <<
")";
616 const int raiseCount = surfaces.size();
617 for (
int i = 0; i < raiseCount; i++) {
618 int fromIndex = indexOf(surfaces[i]);
619 if (fromIndex != -1) {
625int TopLevelWindowModel::rowCount(
const QModelIndex &)
const
627 return m_windowModel.count();
630QVariant TopLevelWindowModel::data(
const QModelIndex& index,
int role)
const
632 if (index.row() < 0 || index.row() >= m_windowModel.size())
635 if (role == WindowRole) {
636 Window *window = m_windowModel.at(index.row()).window;
637 return QVariant::fromValue(window);
638 }
else if (role == ApplicationRole) {
639 return QVariant::fromValue(m_windowModel.at(index.row()).application);
645QString TopLevelWindowModel::toString()
648 for (
int i = 0; i < m_windowModel.count(); ++i) {
649 auto item = m_windowModel.at(i);
651 QString itemStr = QString(
"(index=%1,appId=%2,surface=0x%3,id=%4)")
652 .arg(QString::number(i),
653 item.application->appId(),
654 QString::number((qintptr)item.window->surface(), 16),
655 QString::number(item.window->id()));
665int TopLevelWindowModel::indexOf(lomiriapi::MirSurfaceInterface *surface)
667 for (
int i = 0; i < m_windowModel.count(); ++i) {
668 if (m_windowModel.at(i).window->surface() == surface) {
677 for (
int i = 0; i < m_windowModel.count(); ++i) {
678 if (m_windowModel[i].window->
id() ==
id) {
687 if (index >=0 && index < m_windowModel.count()) {
688 return m_windowModel[index].window;
696 if (index >=0 && index < m_windowModel.count()) {
697 return m_windowModel[index].window->surface();
705 if (index >=0 && index < m_windowModel.count()) {
706 return m_windowModel[index].application;
714 if (index >=0 && index < m_windowModel.count()) {
715 return m_windowModel[index].window->id();
723 if (m_modelState == IdleState) {
724 DEBUG_MSG <<
"(id=" <<
id <<
") - do it now.";
727 DEBUG_MSG <<
"(id=" <<
id <<
") - Model busy (modelState=" << m_modelState <<
"). Try again in the next event loop.";
733 QMetaObject::invokeMethod(
this,
"raiseId", Qt::QueuedConnection, Q_ARG(
int,
id));
737void TopLevelWindowModel::doRaiseId(
int id)
741 if (fromIndex != -1 && fromIndex != 0) {
742 auto surface = m_windowModel[fromIndex].window->surface();
743 if (surface && surface->live()) {
744 m_surfaceManager->raise(surface);
753void TopLevelWindowModel::setFocusedWindow(
Window *window)
755 if (window != m_focusedWindow) {
756 DEBUG_MSG <<
"(" << window <<
")";
758 m_previousWindow = m_focusedWindow;
760 m_focusedWindow = window;
761 Q_EMIT focusedWindowChanged(m_focusedWindow);
763 if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
765 m_previousWindow->setFocused(
false);
770 m_pendingActivation =
false;
775 return m_inputMethodWindow ? m_inputMethodWindow->surface() :
nullptr;
780 return m_focusedWindow;
783void TopLevelWindowModel::move(
int from,
int to)
785 if (from == to)
return;
786 DEBUG_MSG <<
" from=" << from <<
" to=" << to;
788 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
794 Q_ASSERT(m_modelState == IdleState);
795 m_modelState = MovingState;
797 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
798#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
799 const auto &window = m_windowModel.takeAt(from);
800 m_windowModel.insert(to, window);
802 m_windowModel.move(from, to);
807 m_modelState = IdleState;
809 DEBUG_MSG <<
" after " << toString();
812void TopLevelWindowModel::onModificationsStarted()
814 m_surfaceManagerBusy =
true;
817void TopLevelWindowModel::onModificationsEnded()
819 if (m_focusedWindowCleared) {
820 setFocusedWindow(
nullptr);
823 m_focusedWindowCleared =
false;
824 m_surfaceManagerBusy =
false;
827void TopLevelWindowModel::activateTopMostWindowWithoutId(
int forbiddenId)
829 DEBUG_MSG <<
"(" << forbiddenId <<
")";
831 for (
int i = 0; i < m_windowModel.count(); ++i) {
832 Window *window = m_windowModel[i].window;
833 if (window->
id() != forbiddenId) {
840void TopLevelWindowModel::refreshWindows()
845 if (!m_workspace || !m_applicationManager || !m_surfaceManager)
return;
847 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [
this](lomiri::shell::application::MirSurfaceInterface* surface) {
848 if (surface->parentSurface()) {
850 Window *window = createWindow(surface);
851 connect(surface, &QObject::destroyed, window, [=](){
852 window->setSurface(nullptr);
853 window->deleteLater();
856 if (surface->type() == Mir::InputMethodType) {
857 setInputMethodWindow(createWindow(surface));
859 auto *application = m_applicationManager->findApplicationWithSurface(surface);
861 prependSurface(surface, application);
866 Window *promptWindow = createWindow(surface);
867 connect(surface, &QObject::destroyed, promptWindow, [=](){
868 promptWindow->setSurface(nullptr);
869 promptWindow->deleteLater();
877void TopLevelWindowModel::clear()
881 while(m_windowModel.count() > 0) {
882 ModelEntry entry = m_windowModel.takeAt(0);
883 disconnect(entry.window, 0,
this, 0);
886 m_allSurfaces.clear();
887 setFocusedWindow(
nullptr);
888 m_focusedWindowCleared =
false;
889 m_previousWindow =
nullptr;
894 m_closingAllApps =
true;
895 for (
auto win : m_windowModel) {
901 if (m_windowModel.isEmpty()) {
902 Q_EMIT closedAllWindows();
908 return !m_nullWindow->
focused();
911void TopLevelWindowModel::setRootFocus(
bool focus)
913 DEBUG_MSG <<
"(" << focus <<
"), surfaceManagerBusy is " << m_surfaceManagerBusy;
915 if (m_surfaceManagerBusy) {
925 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
926 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
927 m_previousWindow->activate();
928 }
else if (!m_pendingActivation) {
930 activateTopMostWindowWithoutId(-1);
933 if (!m_nullWindow->focused()) {
934 m_nullWindow->activate();
946 m_pendingActivation =
true;
Q_INVOKABLE void raiseId(int id)
Raises the row with the given id to the top of the window stack (index == count-1).
bool rootFocus
Sets whether a user Window or "nothing" should be focused.
Q_INVOKABLE Window * windowAt(int index) const
Returns the window at the given index.
Q_INVOKABLE lomiri::shell::application::MirSurfaceInterface * surfaceAt(int index) const
Returns the surface at the given index.
Q_INVOKABLE void pendingActivation()
Sets pending activation flag.
Q_INVOKABLE int indexForId(int id) const
Returns the index where the row with the given id is located.
lomiri::shell::application::MirSurfaceInterface * inputMethodSurface
The input method surface, if any.
Q_INVOKABLE int idAt(int index) const
Returns the unique id of the element at the given index.
Q_INVOKABLE void closeAllWindows()
Closes all windows, emits closedAllWindows when done.
int count
Number of top-level surfaces in this model.
void listChanged()
Emitted when the list changes.
Q_INVOKABLE lomiri::shell::application::ApplicationInfoInterface * applicationAt(int index) const
Returns the application at the given index.
Window * focusedWindow
The currently focused window, if any.
A slightly higher concept than MirSurface.
int id
A unique identifier for this window. Useful for telling windows apart in a list model as they get mov...
void focusRequested()
Emitted when focus for this window is requested by an external party.
lomiri::shell::application::MirSurfaceInterface * surface
Surface backing up this window It might be null if a surface hasn't been created yet (application is ...
bool focused
Whether the surface is focused.
void activate()
Focuses and raises the window.
Mir::State state
State of the surface.