@@ -196,11 +196,7 @@ void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *su
196196 }
197197
198198 if (!surface) {
199− // focus the newly added window. miral can't help with that as it doesn't know about it.
200− window->setFocused(true);
201− if (m_focusedWindow && m_focusedWindow->surface()) {
202− m_surfaceManager->activate(nullptr);
203− }
199+ activateEmptyWindow(window);
204200 }
205201
206202 INFO_MSG << " after " << toString();
@@ -219,19 +215,33 @@ void TopLevelWindowModel::connectWindow(Window *window)
219215 // Condense changes to the focused window
220216 // eg: Do focusedWindow=A to focusedWindow=B instead of
221217 // focusedWindow=A to focusedWindow=null to focusedWindow=B
222− m_focusedWindowChanged = true;
223218 if (focused) {
224219 Q_ASSERT(m_newlyFocusedWindow == nullptr);
220+ m_focusedWindowChanged = true;
225221 m_newlyFocusedWindow = window;
222+ } else if (m_focusedWindow == window) {
223+ m_focusedWindowChanged = true;
224+ } else {
225+ // don't clear the focused window if you were not there in the first place
226+ // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
226227 }
227228 }
228229 });
229230
230231 connect(window, &Window::closeRequested, this, [this, window]() {
231232 if (!window->surface()) {
232− int index = indexForId(window->id());
233+ // do things ourselves as miral doesn't know about this window
234+ int id = window->id();
235+ int index = indexForId(id);
236+ bool focusOther = false;
233237 Q_ASSERT(index >= 0);
238+ if (window->focused()) {
239+ focusOther = true;
240+ }
234241 m_windowModel[index].application->close();
242+ if (focusOther) {
243+ activateTopMostWindowWithoutId(id);
244+ }
235245 }
236246 });
237247
@@ -243,6 +253,7 @@ void TopLevelWindowModel::connectWindow(Window *window)
243253void TopLevelWindowModel::activateEmptyWindow(Window *window)
244254{
245255 Q_ASSERT(!window->surface());
256+ DEBUG_MSG << "(" << window << ")";
246257
247258 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
248259 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
@@ -561,7 +572,8 @@ void TopLevelWindowModel::raiseId(int id)
561572void TopLevelWindowModel::doRaiseId(int id)
562573{
563574 int fromIndex = indexForId(id);
564− if (fromIndex != -1) {
575+ // can't raise something that doesn't exist or that it's already on top
576+ if (fromIndex != -1 && fromIndex != 0) {
565577 auto surface = m_windowModel[fromIndex].window->surface();
566578 if (surface) {
567579 m_surfaceManager->raise(surface);
@@ -642,3 +654,15 @@ void TopLevelWindowModel::onModificationsEnded()
642654 m_focusedWindowChanged = false;
643655 m_newlyFocusedWindow = nullptr;
644656}
657+
658+void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
659+{
660+ DEBUG_MSG << "(" << forbiddenId << ")";
661+
662+ for (int i = 0; i < m_windowModel.count(); ++i) {
663+ Window *window = m_windowModel[i].window;
664+ if (window->id() != forbiddenId) {
665+ window->activate();
666+ }
667+ }
668+}