@@ -38,7 +38,8 @@ Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
38namespace unityapi = unity::shell::application;
38namespace unityapi = unity::shell::application;
40TopLevelWindowModel::TopLevelWindowModel()
40TopLevelWindowModel::TopLevelWindowModel()
41− : m_nullWindow(createWindow(nullptr))
41+ : m_nullWindow(createWindow(nullptr)),
42+ m_surfaceManagerBusy(false)
43 connect(m_nullWindow, &Window::focusedChanged, this, [this] {
44 connect(m_nullWindow, &Window::focusedChanged, this, [this] {
44 Q_EMIT rootFocusChanged();
45 Q_EMIT rootFocusChanged();
@@ -439,6 +440,10 @@ void TopLevelWindowModel::removeAt(int index)
439 setFocusedWindow(nullptr);
440 setFocusedWindow(nullptr);
443+ if (m_previousWindow == window) {
444+ m_previousWindow = nullptr;
442 if (m_closingAllApps) {
447 if (m_closingAllApps) {
443 if (m_windowModel.isEmpty()) {
448 if (m_windowModel.isEmpty()) {
444 Q_EMIT closedAllWindows();
449 Q_EMIT closedAllWindows();
@@ -709,6 +714,7 @@ void TopLevelWindowModel::move(int from, int to)
710void TopLevelWindowModel::onModificationsStarted()
715void TopLevelWindowModel::onModificationsStarted()
717+ m_surfaceManagerBusy = true;
714void TopLevelWindowModel::onModificationsEnded()
720void TopLevelWindowModel::onModificationsEnded()
@@ -719,6 +725,7 @@ void TopLevelWindowModel::onModificationsEnded()
720 m_focusedWindowChanged = false;
726 m_focusedWindowChanged = false;
721 m_newlyFocusedWindow = nullptr;
727 m_newlyFocusedWindow = nullptr;
728+ m_surfaceManagerBusy = false;
724void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
731void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
@@ -754,7 +761,14 @@ bool TopLevelWindowModel::rootFocus()
755void TopLevelWindowModel::setRootFocus(bool focus)
762void TopLevelWindowModel::setRootFocus(bool focus)
757− INFO_MSG << "(" << focus << ")";
764+ INFO_MSG << "(" << focus << "), surfaceManagerBusy is " << m_surfaceManagerBusy;
766+ if (m_surfaceManagerBusy) {
767+ // Something else is probably being focused already, let's not add to
758 if (focus) {
772 if (focus) {
759 // Give focus back to previous focused window, only if null window is focused.
773 // Give focus back to previous focused window, only if null window is focused.
760 // If null window is not focused, a different app had taken the focus and we
774 // If null window is not focused, a different app had taken the focus and we
@@ -762,6 +776,9 @@ void TopLevelWindowModel::setRootFocus(bool focus)
762 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
776 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
763 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
777 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
764 m_previousWindow->activate();
778 m_previousWindow->activate();
779+ } else if (!m_pendingActivation) {
780+ // The previous window does not exist any more, focus top window.
781+ activateTopMostWindowWithoutId(-1);
767 if (!m_nullWindow->focused()) {
784 if (!m_nullWindow->focused()) {