18 #include "TopLevelWindowModel.h" 19 #include "WindowManagerObjects.h" 22 #include <unity/shell/application/ApplicationInfoInterface.h> 23 #include <unity/shell/application/ApplicationManagerInterface.h> 24 #include <unity/shell/application/MirSurfaceInterface.h> 25 #include <unity/shell/application/MirSurfaceListInterface.h> 26 #include <unity/shell/application/SurfaceManagerInterface.h> 33 #include "Workspace.h" 34 #include "InputMethodManager.h" 36 Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL,
"toplevelwindowmodel", QtInfoMsg)
38 #define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__ 39 #define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__ 43 TopLevelWindowModel::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();
61 TopLevelWindowModel::~TopLevelWindowModel()
65 void TopLevelWindowModel::setApplicationManager(unityapi::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;
111 void TopLevelWindowModel::setSurfaceManager(unityapi::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, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
132 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised,
this, &TopLevelWindowModel::onSurfacesRaised);
133 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted,
this, &TopLevelWindowModel::onModificationsStarted);
134 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded,
this, &TopLevelWindowModel::onModificationsEnded);
140 m_modelState = IdleState;
143 void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
145 DEBUG_MSG <<
"(" << application->appId() <<
")";
147 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
148 prependPlaceholder(application);
152 void TopLevelWindowModel::removeApplication(unityapi::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) {
168 void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
170 INFO_MSG <<
"(" << application->appId() <<
")";
172 prependSurfaceHelper(
nullptr, application);
175 void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::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);
199 void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
202 Window *window = createWindow(surface);
204 connect(window, &Window::stateChanged,
this, [=](Mir::State newState) {
205 if (newState == Mir::HiddenState) {
207 removeAt(indexForId(window->
id()));
209 if (indexForId(window->
id()) == -1) {
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();
226 void TopLevelWindowModel::prependWindow(
Window *window, unityapi::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();
241 Q_EMIT listChanged();
242 m_modelState = IdleState;
246 void 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);
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) {
300 void TopLevelWindowModel::activateEmptyWindow(
Window *window)
303 DEBUG_MSG <<
"(" << window <<
")";
308 window->setFocused(
true);
309 raiseId(window->id());
310 Window *previousWindow = m_focusedWindow;
311 setFocusedWindow(window);
312 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
313 m_surfaceManager->activate(
nullptr);
317 void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
319 connect(surface, &unityapi::MirSurfaceInterface::liveChanged,
this, [
this, surface](
bool live){
321 onSurfaceDied(surface);
324 connect(surface, &QObject::destroyed,
this, [
this, surface](){ this->onSurfaceDestroyed(surface); });
327 void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
329 if (surface->type() == Mir::InputMethodType) {
330 removeInputMethodWindow();
334 int i = indexOf(surface);
339 auto application = m_windowModel[i].application;
342 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
344 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
345 m_windowModel[i].removeOnceSurfaceDestroyed =
true;
351 m_windowModel[i].removeOnceSurfaceDestroyed =
false;
355 void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
357 int i = indexOf(surface);
362 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
365 auto window = m_windowModel[i].window;
366 window->setSurface(
nullptr);
367 window->setFocused(
false);
368 m_allSurfaces.remove(surface);
369 INFO_MSG <<
" Removed surface from entry. After: " << toString();
373 Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
375 int id = m_nextId.fetchAndAddAcquire(1);
376 return createWindowWithId(surface,
id);
379 Window *TopLevelWindowModel::createNullWindow()
381 return createWindowWithId(
nullptr, 0);
384 Window *TopLevelWindowModel::createWindowWithId(unityapi::MirSurfaceInterface *surface,
int id)
387 connectWindow(qmlWindow);
389 qmlWindow->setSurface(surface);
394 void TopLevelWindowModel::onSurfacesAddedToWorkspace(
const std::shared_ptr<miral::Workspace>& workspace,
395 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
397 if (!m_workspace || !m_applicationManager)
return;
398 if (workspace != m_workspace->workspace()) {
399 removeSurfaces(surfaces);
403 Q_FOREACH(
auto surface, surfaces) {
404 if (m_allSurfaces.contains(surface))
continue;
406 if (surface->parentSurface()) {
408 Window *window = createWindow(surface);
409 connect(surface, &QObject::destroyed, window, [=](){
410 window->setSurface(
nullptr);
411 window->deleteLater();
414 if (surface->type() == Mir::InputMethodType) {
415 connectSurface(surface);
416 setInputMethodWindow(createWindow(surface));
418 auto *application = m_applicationManager->findApplicationWithSurface(surface);
420 if (surface->state() == Mir::HiddenState) {
422 connect(surface, &unityapi::MirSurfaceInterface::stateChanged,
this, [=](Mir::State newState) {
423 Q_ASSERT(newState != Mir::HiddenState);
424 disconnect(surface, &unityapi::MirSurfaceInterface::stateChanged,
this, 0);
425 prependSurface(surface, application);
428 prependSurface(surface, application);
434 Window *promptWindow = createWindow(surface);
435 connect(surface, &QObject::destroyed, promptWindow, [=](){
436 promptWindow->setSurface(
nullptr);
437 promptWindow->deleteLater();
445 void TopLevelWindowModel::removeSurfaces(
const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
449 for (
auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
450 auto surface = *iter;
454 start = end = indexOf(surface);
457 m_allSurfaces.remove(surface);
460 while(iter != surfaces.constEnd()) {
461 int index = indexOf(*iter);
462 if (index != end+1) {
469 if (m_modelState == IdleState) {
470 beginRemoveRows(QModelIndex(), start, end);
471 m_modelState = RemovingState;
473 Q_ASSERT(m_modelState == ResettingState);
477 for (
int index = start; index <= end; index++) {
478 auto window = m_windowModel[start].window;
479 window->setSurface(
nullptr);
480 window->setFocused(
false);
482 if (window == m_previousWindow) {
483 m_previousWindow =
nullptr;
486 m_windowModel.removeAt(start);
487 m_allSurfaces.remove(surface);
490 if (m_modelState == RemovingState) {
492 Q_EMIT countChanged();
493 Q_EMIT listChanged();
494 m_modelState = IdleState;
499 void TopLevelWindowModel::deleteAt(
int index)
501 auto window = m_windowModel[index].window;
505 window->setSurface(
nullptr);
510 void TopLevelWindowModel::removeAt(
int index)
512 if (m_modelState == IdleState) {
513 beginRemoveRows(QModelIndex(), index, index);
514 m_modelState = RemovingState;
516 Q_ASSERT(m_modelState == ResettingState);
520 auto window = m_windowModel[index].window;
521 auto surface = window->
surface();
524 window->setFocused(
false);
527 if (window == m_previousWindow) {
528 m_previousWindow =
nullptr;
531 m_windowModel.removeAt(index);
532 m_allSurfaces.remove(surface);
534 if (m_modelState == RemovingState) {
536 Q_EMIT countChanged();
537 Q_EMIT listChanged();
538 m_modelState = IdleState;
541 if (m_focusedWindow == window) {
542 setFocusedWindow(
nullptr);
543 m_focusedWindowCleared =
false;
546 if (m_previousWindow == window) {
547 m_previousWindow =
nullptr;
550 if (m_closingAllApps) {
551 if (m_windowModel.isEmpty()) {
552 Q_EMIT closedAllWindows();
556 INFO_MSG <<
" after " << toString() <<
" apps left " << m_windowModel.count();
559 void TopLevelWindowModel::setInputMethodWindow(
Window *window)
561 if (m_inputMethodWindow) {
562 qWarning(
"Multiple Input Method Surfaces created, removing the old one!");
563 delete m_inputMethodWindow;
565 m_inputMethodWindow = window;
566 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
567 InputMethodManager::instance()->setWindow(window);
570 void TopLevelWindowModel::removeInputMethodWindow()
572 if (m_inputMethodWindow) {
573 auto surface = m_inputMethodWindow->surface();
575 m_allSurfaces.remove(surface);
577 if (m_focusedWindow == m_inputMethodWindow) {
578 setFocusedWindow(
nullptr);
579 m_focusedWindowCleared =
false;
582 delete m_inputMethodWindow;
583 m_inputMethodWindow =
nullptr;
584 Q_EMIT inputMethodSurfaceChanged(
nullptr);
585 InputMethodManager::instance()->setWindow(
nullptr);
589 void TopLevelWindowModel::onSurfacesRaised(
const QVector<unityapi::MirSurfaceInterface*> &surfaces)
591 DEBUG_MSG <<
"(" << surfaces <<
")";
592 const int raiseCount = surfaces.size();
593 for (
int i = 0; i < raiseCount; i++) {
594 int fromIndex = indexOf(surfaces[i]);
595 if (fromIndex != -1) {
601 int TopLevelWindowModel::rowCount(
const QModelIndex &)
const 603 return m_windowModel.count();
606 QVariant TopLevelWindowModel::data(
const QModelIndex& index,
int role)
const 608 if (index.row() < 0 || index.row() >= m_windowModel.size())
611 if (role == WindowRole) {
612 Window *window = m_windowModel.at(index.row()).window;
613 return QVariant::fromValue(window);
614 }
else if (role == ApplicationRole) {
615 return QVariant::fromValue(m_windowModel.at(index.row()).application);
621 QString TopLevelWindowModel::toString()
624 for (
int i = 0; i < m_windowModel.count(); ++i) {
625 auto item = m_windowModel.at(i);
627 QString itemStr = QString(
"(index=%1,appId=%2,surface=0x%3,id=%4)")
628 .arg(QString::number(i),
629 item.application->appId(),
630 QString::number((qintptr)item.window->surface(), 16),
631 QString::number(item.window->id()));
641 int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
643 for (
int i = 0; i < m_windowModel.count(); ++i) {
644 if (m_windowModel.at(i).window->surface() == surface) {
653 for (
int i = 0; i < m_windowModel.count(); ++i) {
654 if (m_windowModel[i].window->
id() == id) {
663 if (index >=0 && index < m_windowModel.count()) {
664 return m_windowModel[index].window;
672 if (index >=0 && index < m_windowModel.count()) {
673 return m_windowModel[index].window->surface();
681 if (index >=0 && index < m_windowModel.count()) {
682 return m_windowModel[index].application;
690 if (index >=0 && index < m_windowModel.count()) {
691 return m_windowModel[index].window->id();
699 if (m_modelState == IdleState) {
700 DEBUG_MSG <<
"(id=" <<
id <<
") - do it now.";
703 DEBUG_MSG <<
"(id=" <<
id <<
") - Model busy (modelState=" << m_modelState <<
"). Try again in the next event loop.";
709 QMetaObject::invokeMethod(
this,
"raiseId", Qt::QueuedConnection, Q_ARG(
int,
id));
713 void TopLevelWindowModel::doRaiseId(
int id)
715 int fromIndex = indexForId(
id);
717 if (fromIndex != -1 && fromIndex != 0) {
718 auto surface = m_windowModel[fromIndex].window->surface();
720 m_surfaceManager->raise(surface);
729 void TopLevelWindowModel::setFocusedWindow(
Window *window)
731 if (window != m_focusedWindow) {
732 INFO_MSG <<
"(" << window <<
")";
734 m_previousWindow = m_focusedWindow;
736 m_focusedWindow = window;
737 Q_EMIT focusedWindowChanged(m_focusedWindow);
739 if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
741 m_previousWindow->setFocused(
false);
746 m_pendingActivation =
false;
751 return m_inputMethodWindow ? m_inputMethodWindow->surface() :
nullptr;
756 return m_focusedWindow;
759 void TopLevelWindowModel::move(
int from,
int to)
761 if (from == to)
return;
762 DEBUG_MSG <<
" from=" << from <<
" to=" << to;
764 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
770 Q_ASSERT(m_modelState == IdleState);
771 m_modelState = MovingState;
773 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
774 #if QT_VERSION < QT_VERSION_CHECK(5, 6, 0) 775 const auto &window = m_windowModel.takeAt(from);
776 m_windowModel.insert(to, window);
778 m_windowModel.move(from, to);
782 Q_EMIT listChanged();
783 m_modelState = IdleState;
785 INFO_MSG <<
" after " << toString();
788 void TopLevelWindowModel::onModificationsStarted()
790 m_surfaceManagerBusy =
true;
793 void TopLevelWindowModel::onModificationsEnded()
795 if (m_focusedWindowCleared) {
796 setFocusedWindow(
nullptr);
799 m_focusedWindowCleared =
false;
800 m_surfaceManagerBusy =
false;
803 void TopLevelWindowModel::activateTopMostWindowWithoutId(
int forbiddenId)
805 DEBUG_MSG <<
"(" << forbiddenId <<
")";
807 for (
int i = 0; i < m_windowModel.count(); ++i) {
808 Window *window = m_windowModel[i].window;
809 if (window->
id() != forbiddenId) {
816 void TopLevelWindowModel::refreshWindows()
821 if (!m_workspace || !m_applicationManager || !m_surfaceManager)
return;
823 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [
this](unity::shell::application::MirSurfaceInterface* surface) {
824 if (surface->parentSurface()) {
826 Window *window = createWindow(surface);
827 connect(surface, &QObject::destroyed, window, [=](){
828 window->setSurface(
nullptr);
829 window->deleteLater();
832 if (surface->type() == Mir::InputMethodType) {
833 setInputMethodWindow(createWindow(surface));
835 auto *application = m_applicationManager->findApplicationWithSurface(surface);
837 prependSurface(surface, application);
842 Window *promptWindow = createWindow(surface);
843 connect(surface, &QObject::destroyed, promptWindow, [=](){
844 promptWindow->setSurface(
nullptr);
845 promptWindow->deleteLater();
853 void TopLevelWindowModel::clear()
857 while(m_windowModel.count() > 0) {
858 ModelEntry entry = m_windowModel.takeAt(0);
859 disconnect(entry.window, 0,
this, 0);
862 m_allSurfaces.clear();
863 setFocusedWindow(
nullptr);
864 m_focusedWindowCleared =
false;
865 m_previousWindow =
nullptr;
870 m_closingAllApps =
true;
871 for (
auto win : m_windowModel) {
877 if (m_windowModel.isEmpty()) {
878 Q_EMIT closedAllWindows();
884 return !m_nullWindow->focused();
887 void TopLevelWindowModel::setRootFocus(
bool focus)
889 INFO_MSG <<
"(" << focus <<
"), surfaceManagerBusy is " << m_surfaceManagerBusy;
891 if (m_surfaceManagerBusy) {
901 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
902 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
903 m_previousWindow->activate();
904 }
else if (!m_pendingActivation) {
906 activateTopMostWindowWithoutId(-1);
909 if (!m_nullWindow->focused()) {
910 m_nullWindow->activate();
922 m_pendingActivation =
true;
bool rootFocus
Sets whether a user Window or "nothing" should be focused.
Q_INVOKABLE unity::shell::application::MirSurfaceInterface * surfaceAt(int index) const
Returns the surface at the given index.
Q_INVOKABLE Window * windowAt(int index) const
Returns the window at the given index.
A slightly higher concept than MirSurface.
void focusRequested()
Emitted when focus for this window is requested by an external party.
Mir::State state
State of the surface.
Q_INVOKABLE int idAt(int index) const
Returns the unique id of the element at the given index.
Q_INVOKABLE unity::shell::application::ApplicationInfoInterface * applicationAt(int index) const
Returns the application at the given index.
void activate()
Focuses and raises the window.
unity::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.
Q_INVOKABLE void pendingActivation()
Sets pending activation flag.
int id
A unique identifier for this window. Useful for telling windows apart in a list model as they get mov...
unity::shell::application::MirSurfaceInterface inputMethodSurface
The input method surface, if any.
Q_INVOKABLE int indexForId(int id) const
Returns the index where the row with the given id is located.
Window focusedWindow
The currently focused window, if any.
Q_INVOKABLE void raiseId(int id)
Raises the row with the given id to the top of the window stack (index == count-1) ...
Q_INVOKABLE void closeAllWindows()
Closes all windows, emits closedAllWindows when done.