@@ -650,16 +650,19 @@ void TopLevelWindowModel::setFocusedWindow(Window *window)
650650 if (window != m_focusedWindow) {
651651 INFO_MSG << "(" << window << ")";
652652
653− Window* previousWindow = m_focusedWindow;
653+ m_previousWindow = m_focusedWindow;
654654
655655 m_focusedWindow = window;
656656 Q_EMIT focusedWindowChanged(m_focusedWindow);
657657
658− if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
658+ if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
659659 // do it ourselves. miral doesn't know about this window
660− previousWindow->setFocused(false);
660+ m_previousWindow->setFocused(false);
661661 }
662662 }
663+
664+ // Reset
665+ m_pendingActivation = false;
663666}
664667
665668unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
@@ -727,12 +730,6 @@ void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
727730 }
728731}
729732
730−void TopLevelWindowModel::activateNullWindow()
731−{
732− if (!m_nullWindow->focused())
733− m_nullWindow->activate();
734−}
735−
736733void TopLevelWindowModel::closeAllWindows()
737734{
738735 m_closingAllApps = true;
@@ -740,3 +737,30 @@ void TopLevelWindowModel::closeAllWindows()
740737 win.window->close();
741738 }
742739}
740+
741+void TopLevelWindowModel::rootFocus(bool focus)
742+{
743+ if (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();
750+ }
751+ } else {
752+ if (!m_nullWindow->focused()) {
753+ m_nullWindow->activate();
754+ }
755+ }
756+}
757+
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()
764+{
765+ m_pendingActivation = true;
766+}