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" 35 Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL,
"toplevelwindowmodel", QtInfoMsg)
37 #define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__ 38 #define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__ 42 TopLevelWindowModel::TopLevelWindowModel()
43 : m_nullWindow(createNullWindow()),
44 m_surfaceManagerBusy(false)
46 connect(m_nullWindow, &Window::focusedChanged,
this, [
this] {
47 Q_EMIT rootFocusChanged();
51 void 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 &,
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 &,
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;
98 void 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);
122 void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
124 DEBUG_MSG <<
"(" << application->appId() <<
")";
126 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
127 prependPlaceholder(application);
131 void 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) {
147 void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
149 INFO_MSG <<
"(" << application->appId() <<
")";
151 prependSurfaceHelper(
nullptr, application);
154 void 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);
177 void 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) {
185 removeAt(indexForId(window->
id()));
187 if (indexForId(window->
id()) == -1) {
189 auto *application = m_applicationManager->findApplicationWithSurface(window->
surface());
190 Q_ASSERT(application);
191 prependWindow(window, application);
196 prependWindow(window, application);
201 INFO_MSG <<
" after " << toString();
204 void TopLevelWindowModel::prependWindow(
Window *window, unityapi::ApplicationInfoInterface *application)
206 if (m_modelState == IdleState) {
207 m_modelState = InsertingState;
208 beginInsertRows(QModelIndex(), 0 , 0 );
210 Q_ASSERT(m_modelState == ResettingState);
214 m_windowModel.prepend(ModelEntry(window, application));
216 if (m_modelState == InsertingState) {
218 Q_EMIT countChanged();
219 Q_EMIT listChanged();
220 m_modelState = IdleState;
224 void TopLevelWindowModel::connectWindow(
Window *window)
228 activateEmptyWindow(window);
232 connect(window, &Window::focusedChanged,
this, [
this, window](
bool focused) {
238 Q_ASSERT(m_newlyFocusedWindow ==
nullptr);
239 m_focusedWindowChanged =
true;
240 m_newlyFocusedWindow = window;
241 }
else if (m_focusedWindow == window) {
242 m_focusedWindowChanged =
true;
250 connect(window, &Window::closeRequested,
this, [
this, window]() {
253 int id = window->
id();
254 int index = indexForId(
id);
255 bool focusOther =
false;
256 Q_ASSERT(index >= 0);
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) {
279 void TopLevelWindowModel::activateEmptyWindow(
Window *window)
282 DEBUG_MSG <<
"(" << window <<
")";
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);
296 void 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); });
306 void 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 DEBUG_MSG <<
" application->name()=" << application->name()
321 <<
" application->state()=" << application->state();
323 if (application->state() == unityapi::ApplicationInfoInterface::Running
324 || application->state() == unityapi::ApplicationInfoInterface::Starting) {
325 m_windowModel[i].removeOnceSurfaceDestroyed =
true;
331 m_windowModel[i].removeOnceSurfaceDestroyed =
false;
335 void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
337 int i = indexOf(surface);
342 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
345 auto window = m_windowModel[i].window;
346 window->setSurface(
nullptr);
347 window->setFocused(
false);
348 INFO_MSG <<
" Removed surface from entry. After: " << toString();
352 Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
354 int id = m_nextId.fetchAndAddAcquire(1);
355 return createWindowWithId(surface,
id);
358 Window *TopLevelWindowModel::createNullWindow()
360 return createWindowWithId(
nullptr, 0);
363 Window *TopLevelWindowModel::createWindowWithId(unityapi::MirSurfaceInterface *surface,
int id)
366 connectWindow(qmlWindow);
368 qmlWindow->setSurface(surface);
373 void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface *surface)
375 DEBUG_MSG <<
"(" << surface <<
")";
377 if (surface->parentSurface()) {
379 Window *window = createWindow(surface);
380 connect(surface, &QObject::destroyed, window, [=](){
381 window->setSurface(
nullptr);
382 window->deleteLater();
385 if (surface->type() == Mir::InputMethodType) {
386 connectSurface(surface);
387 setInputMethodWindow(createWindow(surface));
389 auto *application = m_applicationManager->findApplicationWithSurface(surface);
391 if (surface->state() == Mir::HiddenState) {
393 connect(surface, &unityapi::MirSurfaceInterface::stateChanged,
this, [=](Mir::State newState) {
394 Q_ASSERT(newState != Mir::HiddenState);
395 disconnect(surface, &unityapi::MirSurfaceInterface::stateChanged,
this, 0);
396 prependSurface(surface, application);
399 prependSurface(surface, application);
405 Window *promptWindow = createWindow(surface);
406 connect(surface, &QObject::destroyed, promptWindow, [=](){
407 promptWindow->setSurface(
nullptr);
408 promptWindow->deleteLater();
415 void TopLevelWindowModel::deleteAt(
int index)
417 auto window = m_windowModel[index].window;
421 window->setSurface(
nullptr);
426 void TopLevelWindowModel::removeAt(
int index)
428 if (m_modelState == IdleState) {
429 beginRemoveRows(QModelIndex(), index, index);
430 m_modelState = RemovingState;
432 Q_ASSERT(m_modelState == ResettingState);
436 auto window = m_windowModel[index].window;
439 window->setFocused(
false);
442 m_windowModel.removeAt(index);
444 if (m_modelState == RemovingState) {
446 Q_EMIT countChanged();
447 Q_EMIT listChanged();
448 m_modelState = IdleState;
451 if (m_focusedWindow == window) {
452 setFocusedWindow(
nullptr);
455 if (m_previousWindow == window) {
456 m_previousWindow =
nullptr;
459 if (m_closingAllApps) {
460 if (m_windowModel.isEmpty()) {
461 Q_EMIT closedAllWindows();
465 INFO_MSG <<
" after " << toString() <<
" apps left " << m_windowModel.count();
468 void TopLevelWindowModel::setInputMethodWindow(
Window *window)
470 if (m_inputMethodWindow) {
471 qWarning(
"Multiple Input Method Surfaces created, removing the old one!");
472 delete m_inputMethodWindow;
474 m_inputMethodWindow = window;
475 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
476 InputMethodManager::instance()->setWindow(window);
479 void TopLevelWindowModel::removeInputMethodWindow()
481 if (m_inputMethodWindow) {
482 delete m_inputMethodWindow;
483 m_inputMethodWindow =
nullptr;
484 Q_EMIT inputMethodSurfaceChanged(
nullptr);
485 InputMethodManager::instance()->setWindow(
nullptr);
489 void TopLevelWindowModel::onSurfacesRaised(
const QVector<unityapi::MirSurfaceInterface*> &surfaces)
491 DEBUG_MSG <<
"(" << surfaces <<
")";
492 const int raiseCount = surfaces.size();
493 for (
int i = 0; i < raiseCount; i++) {
494 int fromIndex = findIndexOf(surfaces[i]);
495 if (fromIndex != -1) {
501 int TopLevelWindowModel::rowCount(
const QModelIndex &)
const 503 return m_windowModel.count();
506 QVariant TopLevelWindowModel::data(
const QModelIndex& index,
int role)
const 508 if (index.row() < 0 || index.row() >= m_windowModel.size())
511 if (role == WindowRole) {
512 Window *window = m_windowModel.at(index.row()).window;
513 return QVariant::fromValue(window);
514 }
else if (role == ApplicationRole) {
515 return QVariant::fromValue(m_windowModel.at(index.row()).application);
521 int TopLevelWindowModel::findIndexOf(
const unityapi::MirSurfaceInterface *surface)
const 523 for (
int i=0; i<m_windowModel.count(); i++) {
524 if (m_windowModel[i].window->
surface() == surface) {
531 QString TopLevelWindowModel::toString()
534 for (
int i = 0; i < m_windowModel.count(); ++i) {
535 auto item = m_windowModel.at(i);
537 QString itemStr = QString(
"(index=%1,appId=%2,surface=0x%3,id=%4)")
538 .arg(QString::number(i),
539 item.application->appId(),
540 QString::number((qintptr)item.window->surface(), 16),
541 QString::number(item.window->id()));
551 int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
553 for (
int i = 0; i < m_windowModel.count(); ++i) {
554 if (m_windowModel.at(i).window->surface() == surface) {
563 for (
int i = 0; i < m_windowModel.count(); ++i) {
564 if (m_windowModel[i].window->
id() == id) {
573 if (index >=0 && index < m_windowModel.count()) {
574 return m_windowModel[index].window;
582 if (index >=0 && index < m_windowModel.count()) {
583 return m_windowModel[index].window->surface();
591 if (index >=0 && index < m_windowModel.count()) {
592 return m_windowModel[index].application;
600 if (index >=0 && index < m_windowModel.count()) {
601 return m_windowModel[index].window->id();
609 if (m_modelState == IdleState) {
610 DEBUG_MSG <<
"(id=" <<
id <<
") - do it now.";
613 DEBUG_MSG <<
"(id=" <<
id <<
") - Model busy (modelState=" << m_modelState <<
"). Try again in the next event loop.";
619 QMetaObject::invokeMethod(
this,
"raiseId", Qt::QueuedConnection, Q_ARG(
int,
id));
623 void TopLevelWindowModel::doRaiseId(
int id)
625 int fromIndex = indexForId(
id);
627 if (fromIndex != -1 && fromIndex != 0) {
628 auto surface = m_windowModel[fromIndex].window->surface();
630 m_surfaceManager->raise(surface);
639 void TopLevelWindowModel::setFocusedWindow(
Window *window)
641 if (window != m_focusedWindow) {
642 INFO_MSG <<
"(" << window <<
")";
644 m_previousWindow = m_focusedWindow;
646 m_focusedWindow = window;
647 Q_EMIT focusedWindowChanged(m_focusedWindow);
649 if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
651 m_previousWindow->setFocused(
false);
656 m_pendingActivation =
false;
661 return m_inputMethodWindow ? m_inputMethodWindow->surface() :
nullptr;
666 return m_focusedWindow;
669 void TopLevelWindowModel::move(
int from,
int to)
671 if (from == to)
return;
672 DEBUG_MSG <<
" from=" << from <<
" to=" << to;
674 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
680 Q_ASSERT(m_modelState == IdleState);
681 m_modelState = MovingState;
683 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
684 #if QT_VERSION < QT_VERSION_CHECK(5, 6, 0) 685 const auto &window = m_windowModel.takeAt(from);
686 m_windowModel.insert(to, window);
688 m_windowModel.move(from, to);
692 Q_EMIT listChanged();
693 m_modelState = IdleState;
695 INFO_MSG <<
" after " << toString();
698 void TopLevelWindowModel::onModificationsStarted()
700 m_surfaceManagerBusy =
true;
703 void TopLevelWindowModel::onModificationsEnded()
705 if (m_focusedWindowChanged) {
706 setFocusedWindow(m_newlyFocusedWindow);
709 m_focusedWindowChanged =
false;
710 m_newlyFocusedWindow =
nullptr;
711 m_surfaceManagerBusy =
false;
714 void TopLevelWindowModel::activateTopMostWindowWithoutId(
int forbiddenId)
716 DEBUG_MSG <<
"(" << forbiddenId <<
")";
718 for (
int i = 0; i < m_windowModel.count(); ++i) {
719 Window *window = m_windowModel[i].window;
720 if (window->
id() != forbiddenId) {
729 m_closingAllApps =
true;
730 for (
auto win : m_windowModel) {
736 if (m_windowModel.isEmpty()) {
737 Q_EMIT closedAllWindows();
743 return !m_nullWindow->focused();
746 void TopLevelWindowModel::setRootFocus(
bool focus)
748 INFO_MSG <<
"(" << focus <<
"), surfaceManagerBusy is " << m_surfaceManagerBusy;
750 if (m_surfaceManagerBusy) {
760 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
761 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
762 m_previousWindow->activate();
763 }
else if (!m_pendingActivation) {
765 activateTopMostWindowWithoutId(-1);
768 if (!m_nullWindow->focused()) {
769 m_nullWindow->activate();
781 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.