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 INFO_MSG <<
"(" << application->appId() <<
")";
173 prependSurfaceHelper(
nullptr, application);
176void TopLevelWindowModel::prependSurface(lomiriapi::MirSurfaceInterface *surface, lomiriapi::ApplicationInfoInterface *application)
178 Q_ASSERT(surface !=
nullptr);
180 connectSurface(surface);
181 m_allSurfaces.insert(surface);
183 bool filledPlaceholder =
false;
184 for (
int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
185 ModelEntry &entry = m_windowModel[i];
186 if (entry.application == application && (entry.window->surface() ==
nullptr || !entry.window->surface()->live())) {
187 entry.window->setSurface(surface);
188 INFO_MSG <<
" appId=" << application->appId() <<
" surface=" << surface
189 <<
", filling out placeholder. after: " << toString();
190 filledPlaceholder =
true;
194 if (!filledPlaceholder) {
195 INFO_MSG <<
" appId=" << application->appId() <<
" surface=" << surface <<
", adding new row";
196 prependSurfaceHelper(surface, application);
200void TopLevelWindowModel::prependSurfaceHelper(lomiriapi::MirSurfaceInterface *surface, lomiriapi::ApplicationInfoInterface *application)
203 Window *window = createWindow(surface);
205 connect(window, &Window::stateChanged,
this, [=](Mir::State newState) {
206 if (newState == Mir::HiddenState) {
212 auto *application = m_applicationManager->findApplicationWithSurface(window->
surface());
213 Q_ASSERT(application);
214 prependWindow(window, application);
219 prependWindow(window, application);
224 INFO_MSG <<
" after " << toString();
227void TopLevelWindowModel::prependWindow(
Window *window, lomiriapi::ApplicationInfoInterface *application)
229 if (m_modelState == IdleState) {
230 m_modelState = InsertingState;
231 beginInsertRows(QModelIndex(), 0 , 0 );
233 Q_ASSERT(m_modelState == ResettingState);
237 m_windowModel.prepend(ModelEntry(window, application));
239 if (m_modelState == InsertingState) {
241 Q_EMIT countChanged();
243 m_modelState = IdleState;
247void TopLevelWindowModel::connectWindow(
Window *window)
251 activateEmptyWindow(window);
255 connect(window, &Window::focusedChanged,
this, [
this, window](
bool focused) {
258 setFocusedWindow(window);
259 m_focusedWindowCleared = false;
260 }
else if (m_focusedWindow == window) {
264 m_focusedWindowCleared =
true;
272 connect(window, &Window::closeRequested,
this, [
this, window]() {
275 int id = window->id();
276 int index = indexForId(id);
277 bool focusOther = false;
278 Q_ASSERT(index >= 0);
279 if (window->focused()) {
282 m_windowModel[index].application->close();
284 activateTopMostWindowWithoutId(
id);
289 connect(window, &Window::emptyWindowActivated,
this, [
this, window]() {
290 activateEmptyWindow(window);
293 connect(window, &Window::liveChanged,
this, [
this, window](
bool isAlive) {
294 if (!isAlive && window->
state() == Mir::HiddenState) {
301void TopLevelWindowModel::activateEmptyWindow(
Window *window)
304 DEBUG_MSG <<
"(" << window <<
")";
309 window->setFocused(
true);
311 Window *previousWindow = m_focusedWindow;
312 setFocusedWindow(window);
313 if (previousWindow && previousWindow->
surface() && previousWindow->
surface()->focused()) {
314 m_surfaceManager->activate(
nullptr);
318void TopLevelWindowModel::connectSurface(lomiriapi::MirSurfaceInterface *surface)
320 connect(surface, &lomiriapi::MirSurfaceInterface::liveChanged,
this, [
this, surface](
bool live){
322 onSurfaceDied(surface);
325 connect(surface, &QObject::destroyed,
this, [
this, surface](QObject*){ this->onSurfaceDestroyed(surface); });
328void TopLevelWindowModel::onSurfaceDied(lomiriapi::MirSurfaceInterface *surface)
330 if (surface->type() == Mir::InputMethodType) {
331 removeInputMethodWindow();
335 int i = indexOf(surface);
340 auto application = m_windowModel[i].application;
342 DEBUG_MSG <<
" application->name()=" << application->name()
343 <<
" application->state()=" << application->state();
349 if (application->surfaceList()->count() == 1)
350 m_windowModel[i].removeOnceSurfaceDestroyed =
false;
352 m_windowModel[i].removeOnceSurfaceDestroyed =
true;
355void TopLevelWindowModel::onSurfaceDestroyed(lomiriapi::MirSurfaceInterface *surface)
357 int i = indexOf(surface);
362 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
365 auto window = m_windowModel[i].window;
366 window->setFocused(
false);
367 m_allSurfaces.remove(surface);
368 INFO_MSG <<
" Removed surface from entry. After: " << toString();
372Window *TopLevelWindowModel::createWindow(lomiriapi::MirSurfaceInterface *surface)
374 int id = m_nextId.fetchAndAddAcquire(1);
375 return createWindowWithId(surface,
id);
378Window *TopLevelWindowModel::createNullWindow()
380 return createWindowWithId(
nullptr, 0);
383Window *TopLevelWindowModel::createWindowWithId(lomiriapi::MirSurfaceInterface *surface,
int id)
386 connectWindow(qmlWindow);
388 qmlWindow->setSurface(surface);
393void TopLevelWindowModel::onSurfacesAddedToWorkspace(
const std::shared_ptr<miral::Workspace>& workspace,
394 const QVector<lomiri::shell::application::MirSurfaceInterface*> surfaces)
396 if (!m_workspace || !m_applicationManager)
return;
397 if (workspace != m_workspace->workspace()) {
398 removeSurfaces(surfaces);
402 Q_FOREACH(
auto surface, surfaces) {
403 if (m_allSurfaces.contains(surface))
continue;
405 if (surface->parentSurface()) {
407 Window *window = createWindow(surface);
408 connect(surface, &QObject::destroyed, window, [=](){
409 window->setSurface(
nullptr);
410 window->deleteLater();
413 if (surface->type() == Mir::InputMethodType) {
414 connectSurface(surface);
415 setInputMethodWindow(createWindow(surface));
417 auto *application = m_applicationManager->findApplicationWithSurface(surface);
419 if (surface->state() == Mir::HiddenState) {
421 connect(surface, &lomiriapi::MirSurfaceInterface::stateChanged,
this, [=](Mir::State newState) {
422 Q_ASSERT(newState != Mir::HiddenState);
423 disconnect(surface, &lomiriapi::MirSurfaceInterface::stateChanged,
this, 0);
424 prependSurface(surface, application);
427 prependSurface(surface, application);
433 Window *promptWindow = createWindow(surface);
434 connect(surface, &QObject::destroyed, promptWindow, [=](){
435 promptWindow->setSurface(
nullptr);
436 promptWindow->deleteLater();
444void TopLevelWindowModel::removeSurfaces(
const QVector<lomiri::shell::application::MirSurfaceInterface *> surfaces)
448 for (
auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
449 auto surface = *iter;
453 start = end = indexOf(surface);
456 m_allSurfaces.remove(surface);
459 while(iter != surfaces.constEnd()) {
460 int index = indexOf(*iter);
461 if (index != end+1) {
468 if (m_modelState == IdleState) {
469 beginRemoveRows(QModelIndex(), start, end);
470 m_modelState = RemovingState;
472 Q_ASSERT(m_modelState == ResettingState);
476 for (
int index = start; index <= end; index++) {
477 auto window = m_windowModel[start].window;
478 window->setSurface(
nullptr);
479 window->setFocused(
false);
481 if (window == m_previousWindow) {
482 m_previousWindow =
nullptr;
485 m_windowModel.removeAt(start);
486 m_allSurfaces.remove(surface);
489 if (m_modelState == RemovingState) {
491 Q_EMIT countChanged();
493 m_modelState = IdleState;
498void TopLevelWindowModel::deleteAt(
int index)
500 auto window = m_windowModel[index].window;
504 window->setSurface(
nullptr);
509void TopLevelWindowModel::removeAt(
int index)
511 if (m_modelState == IdleState) {
512 beginRemoveRows(QModelIndex(), index, index);
513 m_modelState = RemovingState;
515 Q_ASSERT(m_modelState == ResettingState);
519 auto window = m_windowModel[index].window;
520 auto surface = window->
surface();
523 window->setFocused(
false);
526 if (window == m_previousWindow) {
527 m_previousWindow =
nullptr;
530 m_windowModel.removeAt(index);
531 m_allSurfaces.remove(surface);
533 if (m_modelState == RemovingState) {
535 Q_EMIT countChanged();
537 m_modelState = IdleState;
540 if (m_focusedWindow == window) {
541 setFocusedWindow(
nullptr);
542 m_focusedWindowCleared =
false;
545 if (m_previousWindow == window) {
546 m_previousWindow =
nullptr;
549 if (m_closingAllApps) {
550 if (m_windowModel.isEmpty()) {
551 Q_EMIT closedAllWindows();
555 INFO_MSG <<
" after " << toString() <<
" apps left " << m_windowModel.count();
558void TopLevelWindowModel::setInputMethodWindow(
Window *window)
560 if (m_inputMethodWindow) {
561 qWarning(
"Multiple Input Method Surfaces created, removing the old one!");
562 delete m_inputMethodWindow;
564 m_inputMethodWindow = window;
565 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->
surface());
566 InputMethodManager::instance()->setWindow(window);
569void TopLevelWindowModel::removeInputMethodWindow()
571 if (m_inputMethodWindow) {
572 auto surface = m_inputMethodWindow->
surface();
574 m_allSurfaces.remove(surface);
576 if (m_focusedWindow == m_inputMethodWindow) {
577 setFocusedWindow(
nullptr);
578 m_focusedWindowCleared =
false;
581 delete m_inputMethodWindow;
582 m_inputMethodWindow =
nullptr;
583 Q_EMIT inputMethodSurfaceChanged(
nullptr);
584 InputMethodManager::instance()->setWindow(
nullptr);
588void TopLevelWindowModel::onSurfacesRaised(
const QVector<lomiriapi::MirSurfaceInterface*> &surfaces)
590 DEBUG_MSG <<
"(" << surfaces <<
")";
591 const int raiseCount = surfaces.size();
592 for (
int i = 0; i < raiseCount; i++) {
593 int fromIndex = indexOf(surfaces[i]);
594 if (fromIndex != -1) {
600int TopLevelWindowModel::rowCount(
const QModelIndex &)
const
602 return m_windowModel.count();
605QVariant TopLevelWindowModel::data(
const QModelIndex& index,
int role)
const
607 if (index.row() < 0 || index.row() >= m_windowModel.size())
610 if (role == WindowRole) {
611 Window *window = m_windowModel.at(index.row()).window;
612 return QVariant::fromValue(window);
613 }
else if (role == ApplicationRole) {
614 return QVariant::fromValue(m_windowModel.at(index.row()).application);
620QString TopLevelWindowModel::toString()
623 for (
int i = 0; i < m_windowModel.count(); ++i) {
624 auto item = m_windowModel.at(i);
626 QString itemStr = QString(
"(index=%1,appId=%2,surface=0x%3,id=%4)")
627 .arg(QString::number(i),
628 item.application->appId(),
629 QString::number((qintptr)item.window->surface(), 16),
630 QString::number(item.window->id()));
640int TopLevelWindowModel::indexOf(lomiriapi::MirSurfaceInterface *surface)
642 for (
int i = 0; i < m_windowModel.count(); ++i) {
643 if (m_windowModel.at(i).window->surface() == surface) {
652 for (
int i = 0; i < m_windowModel.count(); ++i) {
653 if (m_windowModel[i].window->
id() ==
id) {
662 if (index >=0 && index < m_windowModel.count()) {
663 return m_windowModel[index].window;
671 if (index >=0 && index < m_windowModel.count()) {
672 return m_windowModel[index].window->surface();
680 if (index >=0 && index < m_windowModel.count()) {
681 return m_windowModel[index].application;
689 if (index >=0 && index < m_windowModel.count()) {
690 return m_windowModel[index].window->id();
698 if (m_modelState == IdleState) {
699 DEBUG_MSG <<
"(id=" <<
id <<
") - do it now.";
702 DEBUG_MSG <<
"(id=" <<
id <<
") - Model busy (modelState=" << m_modelState <<
"). Try again in the next event loop.";
708 QMetaObject::invokeMethod(
this,
"raiseId", Qt::QueuedConnection, Q_ARG(
int,
id));
712void TopLevelWindowModel::doRaiseId(
int id)
716 if (fromIndex != -1 && fromIndex != 0) {
717 auto surface = m_windowModel[fromIndex].window->surface();
718 if (surface && surface->live()) {
719 m_surfaceManager->raise(surface);
728void TopLevelWindowModel::setFocusedWindow(
Window *window)
730 if (window != m_focusedWindow) {
731 INFO_MSG <<
"(" << window <<
")";
733 m_previousWindow = m_focusedWindow;
735 m_focusedWindow = window;
736 Q_EMIT focusedWindowChanged(m_focusedWindow);
738 if (m_previousWindow && m_previousWindow->
focused() && !m_previousWindow->
surface()) {
740 m_previousWindow->setFocused(
false);
745 m_pendingActivation =
false;
750 return m_inputMethodWindow ? m_inputMethodWindow->
surface() :
nullptr;
755 return m_focusedWindow;
758void TopLevelWindowModel::move(
int from,
int to)
760 if (from == to)
return;
761 DEBUG_MSG <<
" from=" << from <<
" to=" << to;
763 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
769 Q_ASSERT(m_modelState == IdleState);
770 m_modelState = MovingState;
772 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
773#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
774 const auto &window = m_windowModel.takeAt(from);
775 m_windowModel.insert(to, window);
777 m_windowModel.move(from, to);
782 m_modelState = IdleState;
784 INFO_MSG <<
" after " << toString();
787void TopLevelWindowModel::onModificationsStarted()
789 m_surfaceManagerBusy =
true;
792void TopLevelWindowModel::onModificationsEnded()
794 if (m_focusedWindowCleared) {
795 setFocusedWindow(
nullptr);
798 m_focusedWindowCleared =
false;
799 m_surfaceManagerBusy =
false;
802void TopLevelWindowModel::activateTopMostWindowWithoutId(
int forbiddenId)
804 DEBUG_MSG <<
"(" << forbiddenId <<
")";
806 for (
int i = 0; i < m_windowModel.count(); ++i) {
807 Window *window = m_windowModel[i].window;
808 if (window->
id() != forbiddenId) {
815void TopLevelWindowModel::refreshWindows()
820 if (!m_workspace || !m_applicationManager || !m_surfaceManager)
return;
822 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [
this](lomiri::shell::application::MirSurfaceInterface* surface) {
823 if (surface->parentSurface()) {
825 Window *window = createWindow(surface);
826 connect(surface, &QObject::destroyed, window, [=](){
827 window->setSurface(nullptr);
828 window->deleteLater();
831 if (surface->type() == Mir::InputMethodType) {
832 setInputMethodWindow(createWindow(surface));
834 auto *application = m_applicationManager->findApplicationWithSurface(surface);
836 prependSurface(surface, application);
841 Window *promptWindow = createWindow(surface);
842 connect(surface, &QObject::destroyed, promptWindow, [=](){
843 promptWindow->setSurface(nullptr);
844 promptWindow->deleteLater();
852void TopLevelWindowModel::clear()
856 while(m_windowModel.count() > 0) {
857 ModelEntry entry = m_windowModel.takeAt(0);
858 disconnect(entry.window, 0,
this, 0);
861 m_allSurfaces.clear();
862 setFocusedWindow(
nullptr);
863 m_focusedWindowCleared =
false;
864 m_previousWindow =
nullptr;
869 m_closingAllApps =
true;
870 for (
auto win : m_windowModel) {
876 if (m_windowModel.isEmpty()) {
877 Q_EMIT closedAllWindows();
883 return !m_nullWindow->
focused();
886void TopLevelWindowModel::setRootFocus(
bool focus)
888 INFO_MSG <<
"(" << focus <<
"), surfaceManagerBusy is " << m_surfaceManagerBusy;
890 if (m_surfaceManagerBusy) {
900 if (m_previousWindow && !m_previousWindow->
focused() && !m_pendingActivation &&
901 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
903 }
else if (!m_pendingActivation) {
905 activateTopMostWindowWithoutId(-1);
908 if (!m_nullWindow->
focused()) {
921 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.