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::modificationsStarted,
this, &TopLevelWindowModel::onModificationsStarted);
134 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::modificationsEnded,
this, &TopLevelWindowModel::onModificationsEnded);
140 m_modelState = IdleState;
143void TopLevelWindowModel::addApplication(lomiriapi::ApplicationInfoInterface *application)
145 DEBUG_MSG <<
"(" << application->appId() <<
")";
147 if (application->state() != lomiriapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
148 prependPlaceholder(application);
152void TopLevelWindowModel::removeApplication(lomiriapi::ApplicationInfoInterface *application)
154 DEBUG_MSG <<
"(" << application->appId() <<
")";
156 Q_ASSERT(m_modelState == IdleState);
159 while (i < m_windowModel.count()) {
160 if (m_windowModel.at(i).application == application) {
168void TopLevelWindowModel::prependPlaceholder(lomiriapi::ApplicationInfoInterface *application)
170 INFO_MSG <<
"(" << application->appId() <<
")";
172 prependSurfaceHelper(
nullptr, application);
175void TopLevelWindowModel::prependSurface(lomiriapi::MirSurfaceInterface *surface, lomiriapi::ApplicationInfoInterface *application)
177 Q_ASSERT(surface !=
nullptr);
179 connectSurface(surface);
180 m_allSurfaces.insert(surface);
182 bool filledPlaceholder =
false;
183 for (
int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
184 ModelEntry &entry = m_windowModel[i];
185 if (entry.application == application && entry.window->surface() ==
nullptr) {
186 entry.window->setSurface(surface);
187 INFO_MSG <<
" appId=" << application->appId() <<
" surface=" << surface
188 <<
", filling out placeholder. after: " << toString();
189 filledPlaceholder =
true;
193 if (!filledPlaceholder) {
194 INFO_MSG <<
" appId=" << application->appId() <<
" surface=" << surface <<
", adding new row";
195 prependSurfaceHelper(surface, application);
199void TopLevelWindowModel::prependSurfaceHelper(lomiriapi::MirSurfaceInterface *surface, lomiriapi::ApplicationInfoInterface *application)
202 Window *window = createWindow(surface);
204 connect(window, &Window::stateChanged,
this, [=](Mir::State newState) {
205 if (newState == Mir::HiddenState) {
211 auto *application = m_applicationManager->findApplicationWithSurface(window->
surface());
212 Q_ASSERT(application);
213 prependWindow(window, application);
218 prependWindow(window, application);
223 INFO_MSG <<
" after " << toString();
226void TopLevelWindowModel::prependWindow(
Window *window, lomiriapi::ApplicationInfoInterface *application)
228 if (m_modelState == IdleState) {
229 m_modelState = InsertingState;
230 beginInsertRows(QModelIndex(), 0 , 0 );
232 Q_ASSERT(m_modelState == ResettingState);
236 m_windowModel.prepend(ModelEntry(window, application));
238 if (m_modelState == InsertingState) {
240 Q_EMIT countChanged();
242 m_modelState = IdleState;
246void TopLevelWindowModel::connectWindow(
Window *window)
250 activateEmptyWindow(window);
254 connect(window, &Window::focusedChanged,
this, [
this, window](
bool focused) {
257 setFocusedWindow(window);
258 m_focusedWindowCleared = false;
259 }
else if (m_focusedWindow == window) {
263 m_focusedWindowCleared =
true;
271 connect(window, &Window::closeRequested,
this, [
this, window]() {
274 int id = window->id();
275 int index = indexForId(id);
276 bool focusOther = false;
277 Q_ASSERT(index >= 0);
278 if (window->focused()) {
281 m_windowModel[index].application->close();
283 activateTopMostWindowWithoutId(
id);
288 connect(window, &Window::emptyWindowActivated,
this, [
this, window]() {
289 activateEmptyWindow(window);
292 connect(window, &Window::liveChanged,
this, [
this, window](
bool isAlive) {
293 if (!isAlive && window->
state() == Mir::HiddenState) {
300void TopLevelWindowModel::activateEmptyWindow(
Window *window)
303 DEBUG_MSG <<
"(" << window <<
")";
308 window->setFocused(
true);
310 Window *previousWindow = m_focusedWindow;
311 setFocusedWindow(window);
312 if (previousWindow && previousWindow->
surface() && previousWindow->
surface()->focused()) {
313 m_surfaceManager->activate(
nullptr);
317void TopLevelWindowModel::connectSurface(lomiriapi::MirSurfaceInterface *surface)
319 connect(surface, &lomiriapi::MirSurfaceInterface::liveChanged,
this, [
this, surface](
bool live){
321 onSurfaceDied(surface);
324 connect(surface, &QObject::destroyed,
this, [
this, surface](){ this->onSurfaceDestroyed(surface); });
327void TopLevelWindowModel::onSurfaceDied(lomiriapi::MirSurfaceInterface *surface)
329 if (surface->type() == Mir::InputMethodType) {
330 removeInputMethodWindow();
334 int i = indexOf(surface);
339 auto application = m_windowModel[i].application;
341 DEBUG_MSG <<
" application->name()=" << application->name()
342 <<
" application->state()=" << application->state();
344 if (application->state() == lomiriapi::ApplicationInfoInterface::Running
345 || application->state() == lomiriapi::ApplicationInfoInterface::Starting) {
346 m_windowModel[i].removeOnceSurfaceDestroyed =
true;
352 m_windowModel[i].removeOnceSurfaceDestroyed =
false;
356void TopLevelWindowModel::onSurfaceDestroyed(lomiriapi::MirSurfaceInterface *surface)
358 int i = indexOf(surface);
363 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
366 auto window = m_windowModel[i].window;
367 window->setSurface(
nullptr);
368 window->setFocused(
false);
369 m_allSurfaces.remove(surface);
370 INFO_MSG <<
" Removed surface from entry. After: " << toString();
374Window *TopLevelWindowModel::createWindow(lomiriapi::MirSurfaceInterface *surface)
376 int id = m_nextId.fetchAndAddAcquire(1);
377 return createWindowWithId(surface,
id);
380Window *TopLevelWindowModel::createNullWindow()
382 return createWindowWithId(
nullptr, 0);
385Window *TopLevelWindowModel::createWindowWithId(lomiriapi::MirSurfaceInterface *surface,
int id)
388 connectWindow(qmlWindow);
390 qmlWindow->setSurface(surface);
395void TopLevelWindowModel::onSurfacesAddedToWorkspace(
const std::shared_ptr<miral::Workspace>& workspace,
396 const QVector<lomiri::shell::application::MirSurfaceInterface*> surfaces)
398 if (!m_workspace || !m_applicationManager)
return;
399 if (workspace != m_workspace->workspace()) {
400 removeSurfaces(surfaces);
404 Q_FOREACH(
auto surface, surfaces) {
405 if (m_allSurfaces.contains(surface))
continue;
407 if (surface->parentSurface()) {
409 Window *window = createWindow(surface);
410 connect(surface, &QObject::destroyed, window, [=](){
411 window->setSurface(
nullptr);
412 window->deleteLater();
415 if (surface->type() == Mir::InputMethodType) {
416 connectSurface(surface);
417 setInputMethodWindow(createWindow(surface));
419 auto *application = m_applicationManager->findApplicationWithSurface(surface);
421 if (surface->state() == Mir::HiddenState) {
423 connect(surface, &lomiriapi::MirSurfaceInterface::stateChanged,
this, [=](Mir::State newState) {
424 Q_ASSERT(newState != Mir::HiddenState);
425 disconnect(surface, &lomiriapi::MirSurfaceInterface::stateChanged,
this, 0);
426 prependSurface(surface, application);
429 prependSurface(surface, application);
435 Window *promptWindow = createWindow(surface);
436 connect(surface, &QObject::destroyed, promptWindow, [=](){
437 promptWindow->setSurface(
nullptr);
438 promptWindow->deleteLater();
446void TopLevelWindowModel::removeSurfaces(
const QVector<lomiri::shell::application::MirSurfaceInterface *> surfaces)
450 for (
auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
451 auto surface = *iter;
455 start = end = indexOf(surface);
458 m_allSurfaces.remove(surface);
461 while(iter != surfaces.constEnd()) {
462 int index = indexOf(*iter);
463 if (index != end+1) {
470 if (m_modelState == IdleState) {
471 beginRemoveRows(QModelIndex(), start, end);
472 m_modelState = RemovingState;
474 Q_ASSERT(m_modelState == ResettingState);
478 for (
int index = start; index <= end; index++) {
479 auto window = m_windowModel[start].window;
480 window->setSurface(
nullptr);
481 window->setFocused(
false);
483 if (window == m_previousWindow) {
484 m_previousWindow =
nullptr;
487 m_windowModel.removeAt(start);
488 m_allSurfaces.remove(surface);
491 if (m_modelState == RemovingState) {
493 Q_EMIT countChanged();
495 m_modelState = IdleState;
500void TopLevelWindowModel::deleteAt(
int index)
502 auto window = m_windowModel[index].window;
506 window->setSurface(
nullptr);
511void TopLevelWindowModel::removeAt(
int index)
513 if (m_modelState == IdleState) {
514 beginRemoveRows(QModelIndex(), index, index);
515 m_modelState = RemovingState;
517 Q_ASSERT(m_modelState == ResettingState);
521 auto window = m_windowModel[index].window;
522 auto surface = window->
surface();
525 window->setFocused(
false);
528 if (window == m_previousWindow) {
529 m_previousWindow =
nullptr;
532 m_windowModel.removeAt(index);
533 m_allSurfaces.remove(surface);
535 if (m_modelState == RemovingState) {
537 Q_EMIT countChanged();
539 m_modelState = IdleState;
542 if (m_focusedWindow == window) {
543 setFocusedWindow(
nullptr);
544 m_focusedWindowCleared =
false;
547 if (m_previousWindow == window) {
548 m_previousWindow =
nullptr;
551 if (m_closingAllApps) {
552 if (m_windowModel.isEmpty()) {
553 Q_EMIT closedAllWindows();
557 INFO_MSG <<
" after " << toString() <<
" apps left " << m_windowModel.count();
560void TopLevelWindowModel::setInputMethodWindow(
Window *window)
562 if (m_inputMethodWindow) {
563 qWarning(
"Multiple Input Method Surfaces created, removing the old one!");
564 delete m_inputMethodWindow;
566 m_inputMethodWindow = window;
567 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->
surface());
568 InputMethodManager::instance()->setWindow(window);
571void TopLevelWindowModel::removeInputMethodWindow()
573 if (m_inputMethodWindow) {
574 auto surface = m_inputMethodWindow->
surface();
576 m_allSurfaces.remove(surface);
578 if (m_focusedWindow == m_inputMethodWindow) {
579 setFocusedWindow(
nullptr);
580 m_focusedWindowCleared =
false;
583 delete m_inputMethodWindow;
584 m_inputMethodWindow =
nullptr;
585 Q_EMIT inputMethodSurfaceChanged(
nullptr);
586 InputMethodManager::instance()->setWindow(
nullptr);
590void TopLevelWindowModel::onSurfacesRaised(
const QVector<lomiriapi::MirSurfaceInterface*> &surfaces)
592 DEBUG_MSG <<
"(" << surfaces <<
")";
593 const int raiseCount = surfaces.size();
594 for (
int i = 0; i < raiseCount; i++) {
595 int fromIndex = indexOf(surfaces[i]);
596 if (fromIndex != -1) {
602int TopLevelWindowModel::rowCount(
const QModelIndex &)
const
604 return m_windowModel.count();
607QVariant TopLevelWindowModel::data(
const QModelIndex& index,
int role)
const
609 if (index.row() < 0 || index.row() >= m_windowModel.size())
612 if (role == WindowRole) {
613 Window *window = m_windowModel.at(index.row()).window;
614 return QVariant::fromValue(window);
615 }
else if (role == ApplicationRole) {
616 return QVariant::fromValue(m_windowModel.at(index.row()).application);
622QString TopLevelWindowModel::toString()
625 for (
int i = 0; i < m_windowModel.count(); ++i) {
626 auto item = m_windowModel.at(i);
628 QString itemStr = QString(
"(index=%1,appId=%2,surface=0x%3,id=%4)")
629 .arg(QString::number(i),
630 item.application->appId(),
631 QString::number((qintptr)item.window->surface(), 16),
632 QString::number(item.window->id()));
642int TopLevelWindowModel::indexOf(lomiriapi::MirSurfaceInterface *surface)
644 for (
int i = 0; i < m_windowModel.count(); ++i) {
645 if (m_windowModel.at(i).window->surface() == surface) {
654 for (
int i = 0; i < m_windowModel.count(); ++i) {
655 if (m_windowModel[i].window->
id() ==
id) {
664 if (index >=0 && index < m_windowModel.count()) {
665 return m_windowModel[index].window;
673 if (index >=0 && index < m_windowModel.count()) {
674 return m_windowModel[index].window->surface();
682 if (index >=0 && index < m_windowModel.count()) {
683 return m_windowModel[index].application;
691 if (index >=0 && index < m_windowModel.count()) {
692 return m_windowModel[index].window->id();
700 if (m_modelState == IdleState) {
701 DEBUG_MSG <<
"(id=" <<
id <<
") - do it now.";
704 DEBUG_MSG <<
"(id=" <<
id <<
") - Model busy (modelState=" << m_modelState <<
"). Try again in the next event loop.";
710 QMetaObject::invokeMethod(
this,
"raiseId", Qt::QueuedConnection, Q_ARG(
int,
id));
714void TopLevelWindowModel::doRaiseId(
int id)
718 if (fromIndex != -1 && fromIndex != 0) {
719 auto surface = m_windowModel[fromIndex].window->surface();
721 m_surfaceManager->raise(surface);
730void TopLevelWindowModel::setFocusedWindow(
Window *window)
732 if (window != m_focusedWindow) {
733 INFO_MSG <<
"(" << window <<
")";
735 m_previousWindow = m_focusedWindow;
737 m_focusedWindow = window;
738 Q_EMIT focusedWindowChanged(m_focusedWindow);
740 if (m_previousWindow && m_previousWindow->
focused() && !m_previousWindow->
surface()) {
742 m_previousWindow->setFocused(
false);
747 m_pendingActivation =
false;
752 return m_inputMethodWindow ? m_inputMethodWindow->
surface() :
nullptr;
757 return m_focusedWindow;
760void TopLevelWindowModel::move(
int from,
int to)
762 if (from == to)
return;
763 DEBUG_MSG <<
" from=" << from <<
" to=" << to;
765 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
771 Q_ASSERT(m_modelState == IdleState);
772 m_modelState = MovingState;
774 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
775#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
776 const auto &window = m_windowModel.takeAt(from);
777 m_windowModel.insert(to, window);
779 m_windowModel.move(from, to);
784 m_modelState = IdleState;
786 INFO_MSG <<
" after " << toString();
789void TopLevelWindowModel::onModificationsStarted()
791 m_surfaceManagerBusy =
true;
794void TopLevelWindowModel::onModificationsEnded()
796 if (m_focusedWindowCleared) {
797 setFocusedWindow(
nullptr);
800 m_focusedWindowCleared =
false;
801 m_surfaceManagerBusy =
false;
804void TopLevelWindowModel::activateTopMostWindowWithoutId(
int forbiddenId)
806 DEBUG_MSG <<
"(" << forbiddenId <<
")";
808 for (
int i = 0; i < m_windowModel.count(); ++i) {
809 Window *window = m_windowModel[i].window;
810 if (window->
id() != forbiddenId) {
817void TopLevelWindowModel::refreshWindows()
822 if (!m_workspace || !m_applicationManager || !m_surfaceManager)
return;
824 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [
this](lomiri::shell::application::MirSurfaceInterface* surface) {
825 if (surface->parentSurface()) {
827 Window *window = createWindow(surface);
828 connect(surface, &QObject::destroyed, window, [=](){
829 window->setSurface(nullptr);
830 window->deleteLater();
833 if (surface->type() == Mir::InputMethodType) {
834 setInputMethodWindow(createWindow(surface));
836 auto *application = m_applicationManager->findApplicationWithSurface(surface);
838 prependSurface(surface, application);
843 Window *promptWindow = createWindow(surface);
844 connect(surface, &QObject::destroyed, promptWindow, [=](){
845 promptWindow->setSurface(nullptr);
846 promptWindow->deleteLater();
854void TopLevelWindowModel::clear()
858 while(m_windowModel.count() > 0) {
859 ModelEntry entry = m_windowModel.takeAt(0);
860 disconnect(entry.window, 0,
this, 0);
863 m_allSurfaces.clear();
864 setFocusedWindow(
nullptr);
865 m_focusedWindowCleared =
false;
866 m_previousWindow =
nullptr;
871 m_closingAllApps =
true;
872 for (
auto win : m_windowModel) {
878 if (m_windowModel.isEmpty()) {
879 Q_EMIT closedAllWindows();
885 return !m_nullWindow->
focused();
888void TopLevelWindowModel::setRootFocus(
bool focus)
890 INFO_MSG <<
"(" << focus <<
"), surfaceManagerBusy is " << m_surfaceManagerBusy;
892 if (m_surfaceManagerBusy) {
902 if (m_previousWindow && !m_previousWindow->
focused() && !m_pendingActivation &&
903 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
905 }
else if (!m_pendingActivation) {
907 activateTopMostWindowWithoutId(-1);
910 if (!m_nullWindow->
focused()) {
923 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.
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.