@@ -196,11 +196,7 @@ void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *su
198 if (!surface) {
198 if (!surface) {
199− // focus the newly added window. miral can't help with that as it doesn't know about it.
199+ activateEmptyWindow(window);
200− window->setFocused(true);
201− if (m_focusedWindow && m_focusedWindow->surface()) {
202− m_surfaceManager->activate(nullptr);
206 INFO_MSG << " after " << toString();
202 INFO_MSG << " after " << toString();
@@ -219,19 +215,33 @@ void TopLevelWindowModel::connectWindow(Window *window)
219 // Condense changes to the focused window
215 // Condense changes to the focused window
220 // eg: Do focusedWindow=A to focusedWindow=B instead of
216 // eg: Do focusedWindow=A to focusedWindow=B instead of
221 // focusedWindow=A to focusedWindow=null to focusedWindow=B
217 // focusedWindow=A to focusedWindow=null to focusedWindow=B
222− m_focusedWindowChanged = true;
223 if (focused) {
218 if (focused) {
224 Q_ASSERT(m_newlyFocusedWindow == nullptr);
219 Q_ASSERT(m_newlyFocusedWindow == nullptr);
220+ m_focusedWindowChanged = true;
225 m_newlyFocusedWindow = window;
221 m_newlyFocusedWindow = window;
222+ } else if (m_focusedWindow == window) {
223+ m_focusedWindowChanged = true;
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.
230 connect(window, &Window::closeRequested, this, [this, window]() {
231 connect(window, &Window::closeRequested, this, [this, window]() {
231 if (!window->surface()) {
232 if (!window->surface()) {
233+ // do things ourselves as miral doesn't know about this window
234+ int id = window->id();
232− int index = indexForId(window->id());
235+ int index = indexForId(id);
236+ bool focusOther = false;
233 Q_ASSERT(index >= 0);
237 Q_ASSERT(index >= 0);
238+ if (window->focused()) {
234 m_windowModel[index].application->close();
241 m_windowModel[index].application->close();
243+ activateTopMostWindowWithoutId(id);
@@ -243,6 +253,7 @@ void TopLevelWindowModel::connectWindow(Window *window)
243void TopLevelWindowModel::activateEmptyWindow(Window *window)
253void TopLevelWindowModel::activateEmptyWindow(Window *window)
245 Q_ASSERT(!window->surface());
255 Q_ASSERT(!window->surface());
256+ DEBUG_MSG << "(" << window << ")";
247 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
258 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
248 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
259 // 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)
561void TopLevelWindowModel::doRaiseId(int id)
572void TopLevelWindowModel::doRaiseId(int id)
563 int fromIndex = indexForId(id);
574 int fromIndex = indexForId(id);
575+ // can't raise something that doesn't exist or that it's already on top
564− if (fromIndex != -1) {
576+ if (fromIndex != -1 && fromIndex != 0) {
565 auto surface = m_windowModel[fromIndex].window->surface();
577 auto surface = m_windowModel[fromIndex].window->surface();
566 if (surface) {
578 if (surface) {
567 m_surfaceManager->raise(surface);
579 m_surfaceManager->raise(surface);
@@ -642,3 +654,15 @@ void TopLevelWindowModel::onModificationsEnded()
642 m_focusedWindowChanged = false;
654 m_focusedWindowChanged = false;
643 m_newlyFocusedWindow = nullptr;
655 m_newlyFocusedWindow = nullptr;
658+void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
660+ DEBUG_MSG << "(" << forbiddenId << ")";
662+ for (int i = 0; i < m_windowModel.count(); ++i) {
663+ Window *window = m_windowModel[i].window;
664+ if (window->id() != forbiddenId) {