@@ -119,21 +119,17 @@ AbstractStage {
119119 var index = indexOf(focusedAppId);
120120 return index >= 0 && index < appRepeater.count ? appRepeater.itemAt(index) : null
121121 }
122− property int foregroundMaximizedAppIdIndex: -1
122+ property int foregroundMaximizedAppZ: -1
123123
124124 function updateForegroundMaximizedApp() {
125− for (var i = 0; i < appRepeater.count; i++) {
125+ var tmp = -1;
126+ for (var i = appRepeater.count - 1; i >= 0; i--) {
126127 var item = appRepeater.itemAt(i);
127−
128128 if (item && item.visuallyMaximized) {
129− var app = ApplicationManager.get(i);
130− if (app) {
131− foregroundMaximizedAppIdIndex = i;
132− return;
133− }
129+ tmp = Math.max(tmp, item.normalZ);
134130 }
135131 }
136− foregroundMaximizedAppIdIndex = -1;
132+ foregroundMaximizedAppZ = tmp;
137133 }
138134
139135 function indexOf(appId) {
@@ -217,7 +213,10 @@ AbstractStage {
217213 delegate: FocusScope {
218214 id: appDelegate
219215 objectName: "appDelegate_" + appId
220− z: ApplicationManager.count - index
216+ // z might be overriden in some cases by effects, but we need z ordering
217+ // to calculate occlusion detection
218+ property int normalZ: ApplicationManager.count - index
219+ z: normalZ
221220 y: units.gu(3)
222221 width: units.gu(60)
223222 height: units.gu(50)
@@ -252,7 +251,7 @@ AbstractStage {
252251
253252 visible: !visuallyMinimized &&
254253 !greeter.fullyShown &&
255− (priv.foregroundMaximizedAppIdIndex === -1 || priv.foregroundMaximizedAppIdIndex >= index) ||
254+ (priv.foregroundMaximizedAppZ === -1 || priv.foregroundMaximizedAppZ <= z) ||
256255 (spread.state == "altTab" && index === spread.highlightedIndex)
257256
258257 Binding {