@@ -650,16 +650,19 @@ void TopLevelWindowModel::setFocusedWindow(Window *window)
650 if (window != m_focusedWindow) {
650 if (window != m_focusedWindow) {
651 INFO_MSG << "(" << window << ")";
651 INFO_MSG << "(" << window << ")";
653− Window* previousWindow = m_focusedWindow;
653+ m_previousWindow = m_focusedWindow;
655 m_focusedWindow = window;
655 m_focusedWindow = window;
656 Q_EMIT focusedWindowChanged(m_focusedWindow);
656 Q_EMIT focusedWindowChanged(m_focusedWindow);
658− if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
658+ if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
659 // do it ourselves. miral doesn't know about this window
659 // do it ourselves. miral doesn't know about this window
660− previousWindow->setFocused(false);
660+ m_previousWindow->setFocused(false);
665+ m_pendingActivation = false;
665unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
668unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
@@ -727,12 +730,6 @@ void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
730−void TopLevelWindowModel::activateNullWindow()
732− if (!m_nullWindow->focused())
733− m_nullWindow->activate();
736void TopLevelWindowModel::closeAllWindows()
733void TopLevelWindowModel::closeAllWindows()
738 m_closingAllApps = true;
735 m_closingAllApps = true;
@@ -740,3 +737,30 @@ void TopLevelWindowModel::closeAllWindows()
740 win.window->close();
737 win.window->close();
741+void TopLevelWindowModel::rootFocus(bool focus)
744+ // Give focus back to previous focused window, only if null window is focused.
745+ // If null window is not focused, a different app had taken the focus and we
746+ // should repect that, or if a pendingActivation is going on.
747+ if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
748+ m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
749+ m_previousWindow->activate();
752+ if (!m_nullWindow->focused()) {
753+ m_nullWindow->activate();
758+// Pending Activation will block refocus of previous focused window
759+// this is needed since surface activation with miral is async,
760+// and activation of placeholder is sync. This causes a race condition
761+// between placeholder and prev window. This results in prev window
762+// gets focused, as it will always be later than the placeholder.
763+void TopLevelWindowModel::pendingActivation()
765+ m_pendingActivation = true;