@@ -208,6 +208,8 @@ AbstractStage {
208208
209209 print("*** updated! MainStage:", priv.mainStageAppId, "SideStage:", priv.sideStageAppId)
210210 }
211+
212+ property int nextInStack: 1
211213 }
212214
213215 Connections {
@@ -302,6 +304,7 @@ AbstractStage {
302304 State {
303305 name: "stagedWithSideStage"; when: root.mode === "stagedWithSideStage"
304306 PropertyChanges { target: triGestureArea; enabled: true }
307+ PropertyChanges { target: priv; nextInStack: priv.sideStageDelegate ? 3 : 2 }
305308 },
306309 State {
307310 name: "windowed"; when: root.mode === "windowed"
@@ -353,6 +356,21 @@ AbstractStage {
353356 shown: false
354357 height: appContainer.height
355358 x: appContainer.width - width
359+ z: {
360+ if (!priv.mainStageItemId) return 0;
361+
362+ if (priv.sideStageItemId && priv.nextInStack > 0) {
363+ var nextDelegateInStack = appRepeater.itemAt(priv.nextInStack);
364+
365+ if (nextDelegateInStack.stage === ApplicationInfoInterface.MainStage) {
366+ // if the next app in stack is a main stage app, put the sidestage on top of it.
367+ return 2;
368+ }
369+ return 1;
370+ }
371+
372+ return 2;
373+ }
356374
357375 DropArea {
358376 id: sideStageDropArea
@@ -469,9 +487,10 @@ AbstractStage {
469487 property bool visuallyMinimized: false
470488
471489 property int stage: ApplicationInfoInterface.MainStage
472− function saveStage(stage) {
473− appDelegate.stage = stage;
490+ function saveStage(newStage) {
491+ appDelegate.stage = newStage;
474492 WindowStateStorage.saveStage(application.appId, newStage);
493+ priv.updateMainAndSideStageIndexes()
475494 }
476495
477496 readonly property var surface: model.surface
@@ -568,6 +587,12 @@ AbstractStage {
568587
569588 onVisuallyMaximizedChanged: priv.updateForegroundMaximizedApp()
570589
590+ onStageChanged: {
591+ if (!_constructing) {
592+ priv.updateMainAndSideStageIndexes();
593+ }
594+ }
595+
571596// visible: (
572597// !visuallyMinimized
573598// && !greeter.fullyShown
@@ -670,6 +695,8 @@ AbstractStage {
670695 thisDelegate: appDelegate
671696 mainStageDelegate: priv.mainStageDelegate
672697 sideStageDelegate: priv.sideStageDelegate
698+ sideStageWidth: sideStage.panelWidth
699+ sideStageX: sideStage.x
673700 }
674701
675702 StagedRightEdgeMaths {
@@ -758,6 +785,7 @@ AbstractStage {
758785 target: appDelegate
759786 x: stageMaths.itemX
760787 y: appDelegate.fullscreen ? 0 : PanelState.panelHeight
788+ z: stageMaths.itemZ
761789 requestedWidth: stageMaths.itemWidth
762790 requestedHeight: appContainer.height - PanelState.panelHeight
763791 visuallyMaximized: true
@@ -1251,4 +1279,9 @@ AbstractStage {
12511279 }
12521280 }
12531281 }
1282+
1283+ Label {
1284+ anchors { left: parent.left; top: parent.top; margins: units.gu(4) }
1285+ text: "sidestage z: " + sideStage.z
1286+ }
12541287}