File: Stage.qml

package info (click to toggle)
lomiri 0.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (2522 lines) | stat: -rw-r--r-- 119,669 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
/*
 * Copyright (C) 2014-2017 Canonical Ltd.
 * Copyright (C) 2021 UBports Foundation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.15
import QtQml 2.15
import QtQuick.Window 2.2
import Lomiri.Components 1.3
import QtMir.Application 0.1
import "../Components/PanelState"
import "../Components"
import Utils 0.1
import Lomiri.Gestures 0.1
import GlobalShortcut 1.0
import GSettings 1.0
import "Spread"
import "Spread/MathUtils.js" as MathUtils
import ProcessControl 0.1
import WindowManager 1.0

FocusScope {
    id: root
    anchors.fill: parent

    property QtObject applicationManager
    property QtObject topLevelSurfaceList
    property bool altTabPressed
    property url background
    property alias backgroundSourceSize: wallpaper.sourceSize
    property int dragAreaWidth
    property real nativeHeight
    property real nativeWidth
    property QtObject orientations
    property int shellOrientation
    property int shellOrientationAngle
    property bool spreadEnabled: true // If false, animations and right edge will be disabled
    property bool suspended
    property bool oskEnabled: false
    property bool lightMode: false
    property rect inputMethodRect
    property real rightEdgePushProgress: 0
    property Item availableDesktopArea
    property PanelState panelState

    // Whether outside forces say that the Stage may have focus
    property bool allowInteractivity

    readonly property bool interactive: (state === "staged" || state === "stagedWithSideStage" || state === "windowed") && allowInteractivity

    // Configuration
    property string mode: "staged"

    readonly property var temporarySelectedWorkspace: state == "spread" ? screensAndWorkspaces.activeWorkspace : null
    property bool workspaceEnabled: (mode == "windowed" && settings.enableWorkspace) || settings.forceEnableWorkspace

    // Used by the tutorial code
    readonly property real rightEdgeDragProgress: rightEdgeDragArea.dragging ? rightEdgeDragArea.progress : 0 // How far left the stage has been dragged

    // used by the snap windows (edge maximize) feature
    readonly property alias previewRectangle: fakeRectangle

    readonly property bool spreadShown: state == "spread"
    readonly property var mainApp: priv.focusedAppDelegate ? priv.focusedAppDelegate.application : null

    // application windows never rotate independently
    property int mainAppWindowOrientationAngle: shellOrientationAngle

    property bool orientationChangesEnabled: !priv.focusedAppDelegate || priv.focusedAppDelegate.orientationChangesEnabled

    property int supportedOrientations: {
        if (mainApp) {
            switch (mode) {
            case "staged":
                return mainApp.supportedOrientations;
            case "stagedWithSideStage":
                var orientations = mainApp.supportedOrientations;
                orientations |= Qt.LandscapeOrientation | Qt.InvertedLandscapeOrientation;
                if (priv.sideStageItemId) {
                    // If we have a sidestage app, support Portrait orientation
                    // so that it will switch the sidestage app to mainstage on rotate to portrait
                    orientations |= Qt.PortraitOrientation|Qt.InvertedPortraitOrientation;
                }
                return orientations;
            }
        }

        return Qt.PortraitOrientation |
                Qt.LandscapeOrientation |
                Qt.InvertedPortraitOrientation |
                Qt.InvertedLandscapeOrientation;
    }

    GSettings {
        id: settings
        schema.id: "com.lomiri.Shell"
    }

    property int launcherLeftMargin : 0

    Binding {
        target: topLevelSurfaceList
        restoreMode: Binding.RestoreBinding
        property: "rootFocus"
        value: interactive
    }

    onInteractiveChanged: {
        // Stage must have focus before activating windows, including null
        if (interactive) {
            focus = true;
        }
    }

    onAltTabPressedChanged: {
        root.focus = true;
        if (altTabPressed) {
            if (root.spreadEnabled) {
                altTabDelayTimer.start();
            }
        } else {
            // Alt Tab has been released, did we already go to spread?
            if (priv.goneToSpread) {
                priv.goneToSpread = false;
            } else {
                // No we didn't, do a quick alt-tab
                if (appRepeater.count > 1) {
                    appRepeater.itemAt(1).activate();
                } else if (appRepeater.count > 0) {
                    appRepeater.itemAt(0).activate(); // quick alt-tab to the only (minimized) window should still activate it
                }
            }
        }
    }

    Timer {
        id: altTabDelayTimer
        interval: 140
        repeat: false
        onTriggered: {
            if (root.altTabPressed) {
                priv.goneToSpread = true;
            }
        }
    }

    // For MirAL window management
    WindowMargins {
        normal: Qt.rect(0, root.mode === "windowed" ? priv.windowDecorationHeight : 0, 0, 0)
        dialog: normal
    }

    property Item itemConfiningMouseCursor: !spreadShown && priv.focusedAppDelegate && priv.focusedAppDelegate.window && priv.focusedAppDelegate.window.confinesMousePointer ?
                              priv.focusedAppDelegate.clientAreaItem : null;

    signal itemSnapshotRequested(Item item)

    // functions to be called from outside
    function updateFocusedAppOrientation() { /* TODO */ }
    function updateFocusedAppOrientationAnimated() { /* TODO */}

    function closeSpread() {
        spreadItem.highlightedIndex = -1;
        priv.goneToSpread = false;
    }

    onSpreadEnabledChanged: {
        if (!spreadEnabled && spreadShown) {
            closeSpread();
        }
    }

    onRightEdgePushProgressChanged: {
        if (spreadEnabled && rightEdgePushProgress >= 1) {
            priv.goneToSpread = true
        }
    }

    GSettings {
        id: lifecycleExceptions
        schema.id: "com.canonical.qtmir"
    }

    function isExemptFromLifecycle(appId) {
        var shortAppId = appId.split('_')[0];
        for (var i = 0; i < lifecycleExceptions.lifecycleExemptAppids.length; i++) {
            if (shortAppId === lifecycleExceptions.lifecycleExemptAppids[i]) {
                return true;
            }
        }
        return false;
    }

    GlobalShortcut {
        id: closeFocusedShortcut
        shortcut: Qt.AltModifier|Qt.Key_F4
        onTriggered: {
            if (priv.focusedAppDelegate) {
                priv.focusedAppDelegate.close();
            }
        }
    }

    GlobalShortcut {
        id: showSpreadShortcut
        shortcut: Qt.MetaModifier|Qt.Key_W
        active: root.spreadEnabled
        onTriggered: priv.goneToSpread = true
    }

    GlobalShortcut {
        id: toggleSideStageShortcut
        shortcut: Qt.MetaModifier|Qt.Key_S
        active: priv.sideStageEnabled
        onTriggered: {
           priv.toggleSideStage()
        }
    }

    GlobalShortcut {
        id: minimizeAllShortcut
        shortcut: Qt.MetaModifier|Qt.ControlModifier|Qt.Key_D
        onTriggered: priv.minimizeAllWindows()
        active: root.state == "windowed"
    }

    GlobalShortcut {
        id: maximizeWindowShortcut
        shortcut: Qt.MetaModifier|Qt.ControlModifier|Qt.Key_Up
        onTriggered: priv.focusedAppDelegate.requestMaximize()
        active: root.state == "windowed" && priv.focusedAppDelegate && priv.focusedAppDelegate.canBeMaximized
    }

    GlobalShortcut {
        id: maximizeWindowLeftShortcut
        shortcut: Qt.MetaModifier|Qt.ControlModifier|Qt.Key_Left
        onTriggered: {
            switch (root.mode) {
                case "stagedWithSideStage":
                    if (priv.focusedAppDelegate.stage == ApplicationInfoInterface.SideStage) {
                        priv.focusedAppDelegate.saveStage(ApplicationInfoInterface.MainStage);
                        priv.focusedAppDelegate.focus = true;
                    }
                    break;
                case "windowed":
                    priv.focusedAppDelegate.requestMaximizeLeft()
                    break;
            }
        }
        active: (root.state == "windowed" && priv.focusedAppDelegate && priv.focusedAppDelegate.canBeMaximizedLeftRight)
                    ||  (root.state == "stagedWithSideStage" && priv.focusedAppDelegate.stage == ApplicationInfoInterface.SideStage)
    }

    GlobalShortcut {
        id: maximizeWindowRightShortcut
        shortcut: Qt.MetaModifier|Qt.ControlModifier|Qt.Key_Right
        onTriggered: {
            switch (root.mode) {
                case "stagedWithSideStage":
                    if (priv.focusedAppDelegate.stage == ApplicationInfoInterface.MainStage) {
                        priv.focusedAppDelegate.saveStage(ApplicationInfoInterface.SideStage);
                        priv.focusedAppDelegate.focus = true;
                        sideStage.show();
                        priv.updateMainAndSideStageIndexes()
                    }
                    break;
                case "windowed":
                    priv.focusedAppDelegate.requestMaximizeRight()
                    break;
            }
        }
        active: (root.state == "windowed" && priv.focusedAppDelegate && priv.focusedAppDelegate.canBeMaximizedLeftRight)
                    ||  (root.state == "stagedWithSideStage" && priv.focusedAppDelegate.stage == ApplicationInfoInterface.MainStage)
    }

    GlobalShortcut {
        id: minimizeRestoreShortcut
        shortcut: Qt.MetaModifier|Qt.ControlModifier|Qt.Key_Down
        onTriggered: {
            if (priv.focusedAppDelegate.anyMaximized) {
                priv.focusedAppDelegate.requestRestore();
            } else {
                priv.focusedAppDelegate.requestMinimize();
            }
        }
        active: root.state == "windowed" && priv.focusedAppDelegate
    }

    GlobalShortcut {
        shortcut: Qt.AltModifier|Qt.Key_Print
        onTriggered: root.itemSnapshotRequested(priv.focusedAppDelegate)
        active: priv.focusedAppDelegate !== null
    }

    GlobalShortcut {
        shortcut: Qt.ControlModifier|Qt.AltModifier|Qt.Key_T
        onTriggered: {
            // try in this order: snap pkg, new deb name, old deb name
            var candidates = ["lomiri-terminal-app_lomiri-terminal-app", "lomiri-terminal-app", "com.lomiri.terminal_terminal"];
            for (var i = 0; i < candidates.length; i++) {
                if (priv.startApp(candidates[i]))
                    break;
            }
        }
    }

    GlobalShortcut {
        id: showWorkspaceSwitcherShortcutLeft
        shortcut: Qt.AltModifier|Qt.ControlModifier|Qt.Key_Left
        active: !workspaceSwitcher.active && root.workspaceEnabled
        onTriggered: {
            root.focus = true;
            workspaceSwitcher.showLeft()
        }
    }
    GlobalShortcut {
        id: showWorkspaceSwitcherShortcutRight
        shortcut: Qt.AltModifier|Qt.ControlModifier|Qt.Key_Right
        active: !workspaceSwitcher.active && root.workspaceEnabled
        onTriggered: {
            root.focus = true;
            workspaceSwitcher.showRight()
        }
    }
    GlobalShortcut {
        id: showWorkspaceSwitcherShortcutUp
        shortcut: Qt.AltModifier|Qt.ControlModifier|Qt.Key_Up
        active: !workspaceSwitcher.active && root.workspaceEnabled
        onTriggered: {
            root.focus = true;
            workspaceSwitcher.showUp()
        }
    }
    GlobalShortcut {
        id: showWorkspaceSwitcherShortcutDown
        shortcut: Qt.AltModifier|Qt.ControlModifier|Qt.Key_Down
        active: !workspaceSwitcher.active && root.workspaceEnabled
        onTriggered: {
            root.focus = true;
            workspaceSwitcher.showDown()
        }
    }

    GlobalShortcut {
        id: moveAppShowWorkspaceSwitcherShortcutLeft
        shortcut: Qt.AltModifier|Qt.ControlModifier|Qt.ShiftModifier|Qt.Key_Left
        active: !workspaceSwitcher.active && root.workspaceEnabled && root.focusedAppDelegate
        onTriggered: {
            root.focus = true;
            workspaceSwitcher.showLeftMoveApp(root.focusedAppDelegate.surface)
        }
    }
    GlobalShortcut {
        id: moveAppShowWorkspaceSwitcherShortcutRight
        shortcut: Qt.AltModifier|Qt.ControlModifier|Qt.ShiftModifier|Qt.Key_Right
        active: !workspaceSwitcher.active && root.workspaceEnabled && root.focusedAppDelegate
        onTriggered: {
            root.focus = true;
            workspaceSwitcher.showRightMoveApp(root.focusedAppDelegate.surface)
        }
    }
    GlobalShortcut {
        id: moveAppShowWorkspaceSwitcherShortcutUp
        shortcut: Qt.AltModifier|Qt.ControlModifier|Qt.ShiftModifier|Qt.Key_Up
        active: !workspaceSwitcher.active && root.workspaceEnabled && root.focusedAppDelegate
        onTriggered: {
            root.focus = true;
            workspaceSwitcher.showUpMoveApp(root.focusedAppDelegate.surface)
        }
    }
    GlobalShortcut {
        id: moveAppShowWorkspaceSwitcherShortcutDown
        shortcut: Qt.AltModifier|Qt.ControlModifier|Qt.ShiftModifier|Qt.Key_Down
        active: !workspaceSwitcher.active && root.workspaceEnabled && root.focusedAppDelegate
        onTriggered: {
            root.focus = true;
            workspaceSwitcher.showDownMoveApp(root.focusedAppDelegate.surface)
        }
    }

    QtObject {
        id: priv
        objectName: "DesktopStagePrivate"

        function startApp(appId) {
            if (root.applicationManager.findApplication(appId)) {
                return root.applicationManager.requestFocusApplication(appId);
            } else {
                return root.applicationManager.startApplication(appId) !== null;
            }
        }

        property var focusedAppDelegate: null
        property var foregroundMaximizedAppDelegate: null // for stuff like drop shadow and focusing maximized app by clicking panel

        property bool goneToSpread: false
        property int closingIndex: -1
        property int animationDuration: LomiriAnimation.FastDuration

        function updateForegroundMaximizedApp() {
            var found = false;
            for (var i = 0; i < appRepeater.count && !found; i++) {
                var item = appRepeater.itemAt(i);
                if (item && item.visuallyMaximized) {
                    foregroundMaximizedAppDelegate = item;
                    found = true;
                }
            }
            if (!found) {
                foregroundMaximizedAppDelegate = null;
            }
        }

        function minimizeAllWindows() {
            for (var i = appRepeater.count - 1; i >= 0; i--) {
                var appDelegate = appRepeater.itemAt(i);
                if (appDelegate && !appDelegate.minimized) {
                    appDelegate.requestMinimize();
                }
            }
        }

        readonly property bool sideStageEnabled: root.mode === "stagedWithSideStage" &&
                                                 (root.shellOrientation == Qt.LandscapeOrientation ||
                                                 root.shellOrientation == Qt.InvertedLandscapeOrientation)
        onSideStageEnabledChanged: {
            for (var i = 0; i < appRepeater.count; i++) {
                appRepeater.itemAt(i).refreshStage();
            }
            priv.updateMainAndSideStageIndexes();
        }

        property var mainStageDelegate: null
        property var sideStageDelegate: null
        property int mainStageItemId: 0
        property int sideStageItemId: 0
        property string mainStageAppId: ""
        property string sideStageAppId: ""

        onSideStageDelegateChanged: {
            if (!sideStageDelegate) {
                sideStage.hide();
            }
        }

        function toggleSideStage() {
            if (sideStage.shown) {
                sideStage.hide();
            } else  {
                sideStage.show();
                updateMainAndSideStageIndexes()
            }
        }

        function updateMainAndSideStageIndexes() {
            if (root.mode != "stagedWithSideStage") {
                priv.sideStageDelegate = null;
                priv.sideStageItemId = 0;
                priv.sideStageAppId = "";
                priv.mainStageDelegate = appRepeater.itemAt(0);
                priv.mainStageItemId = topLevelSurfaceList.idAt(0);
                priv.mainStageAppId = topLevelSurfaceList.applicationAt(0) ? topLevelSurfaceList.applicationAt(0).appId : ""
                return;
            }

            var choseMainStage = false;
            var choseSideStage = false;

            if (!root.topLevelSurfaceList)
                return;

            for (var i = 0; i < appRepeater.count && (!choseMainStage || !choseSideStage); ++i) {
                var appDelegate = appRepeater.itemAt(i);
                if (!appDelegate) {
                    // This might happen during startup phase... If the delegate appears and claims focus
                    // things are updated and appRepeater.itemAt(x) still returns null while appRepeater.count >= x
                    // Lets just skip it, on startup it will be generated at a later point too...
                    continue;
                }
                if (sideStage.shown && appDelegate.stage == ApplicationInfoInterface.SideStage
                        && !choseSideStage) {
                    priv.sideStageDelegate = appDelegate
                    priv.sideStageItemId = root.topLevelSurfaceList.idAt(i);
                    priv.sideStageAppId = root.topLevelSurfaceList.applicationAt(i).appId;
                    choseSideStage = true;
                } else if (!choseMainStage && appDelegate.stage == ApplicationInfoInterface.MainStage) {
                    priv.mainStageDelegate = appDelegate;
                    priv.mainStageItemId = root.topLevelSurfaceList.idAt(i);
                    priv.mainStageAppId = root.topLevelSurfaceList.applicationAt(i).appId;
                    choseMainStage = true;
                }
            }
            if (!choseMainStage && priv.mainStageDelegate) {
                priv.mainStageDelegate = null;
                priv.mainStageItemId = 0;
                priv.mainStageAppId = "";
            }
            if (!choseSideStage && priv.sideStageDelegate) {
                priv.sideStageDelegate = null;
                priv.sideStageItemId = 0;
                priv.sideStageAppId = "";
            }
        }

        property int nextInStack: {
            var mainStageIndex = priv.mainStageDelegate ? priv.mainStageDelegate.itemIndex : -1;
            var sideStageIndex = priv.sideStageDelegate ? priv.sideStageDelegate.itemIndex : -1;
            if (sideStageIndex == -1) {
                return topLevelSurfaceList.count > 1 ? 1 : -1;
            }
            if (mainStageIndex == 0 || sideStageIndex == 0) {
                if (mainStageIndex == 1 || sideStageIndex == 1) {
                    return topLevelSurfaceList.count > 2 ? 2 : -1;
                }
                return 1;
            }
            return -1;
        }

        readonly property real virtualKeyboardHeight: root.inputMethodRect.height

        readonly property real windowDecorationHeight: units.gu(3)
    }

    Component.onCompleted: priv.updateMainAndSideStageIndexes()

    Connections {
        target: panelState
        function onCloseClicked() { if (priv.focusedAppDelegate) { priv.focusedAppDelegate.close(); } }
        function onMinimizeClicked() { if (priv.focusedAppDelegate) { priv.focusedAppDelegate.requestMinimize(); } }
        function onRestoreClicked() { if (priv.focusedAppDelegate) { priv.focusedAppDelegate.requestRestore(); } }
    }

    Binding {
        target: panelState
        restoreMode: Binding.RestoreBinding
        property: "decorationsVisible"
        value: mode == "windowed" && priv.focusedAppDelegate !== null && priv.focusedAppDelegate.maximized && !root.spreadShown
    }

    Binding {
        target: panelState
        restoreMode: Binding.RestoreBinding
        property: "title"
        value: {
            if (priv.focusedAppDelegate !== null) {
                if (priv.focusedAppDelegate.maximized)
                    return priv.focusedAppDelegate.title
                else
                    return priv.focusedAppDelegate.appName
            }
            return ""
        }
        when: priv.focusedAppDelegate
    }

    Binding {
        target: panelState
        restoreMode: Binding.RestoreBinding
        property: "focusedPersistentSurfaceId"
        value: {
            if (priv.focusedAppDelegate !== null) {
                if (priv.focusedAppDelegate.surface) {
                    return priv.focusedAppDelegate.surface.persistentId;
                }
            }
            return "";
        }
        when: priv.focusedAppDelegate
    }

    Binding {
        target: panelState
        restoreMode: Binding.RestoreBinding
        property: "dropShadow"
        value: priv.focusedAppDelegate && !priv.focusedAppDelegate.maximized && priv.foregroundMaximizedAppDelegate !== null && mode == "windowed"
    }

    Binding {
        target: panelState
        restoreMode: Binding.RestoreBinding
        property: "closeButtonShown"
        value: priv.focusedAppDelegate && priv.focusedAppDelegate.maximized
    }

    Component.onDestruction: {
        panelState.title = "";
        panelState.decorationsVisible = false;
        panelState.dropShadow = false;
    }

    Instantiator {
        model: root.applicationManager
        delegate: QtObject {
            id: applicationDelegate
            // TODO: figure out some lifecycle policy, like suspending minimized apps
            //       or something if running windowed.
            // TODO: If the device has a dozen suspended apps because it was running
            //       in staged mode, when it switches to Windowed mode it will suddenly
            //       resume all those apps at once. We might want to avoid that.
            property var requestedState: ApplicationInfoInterface.RequestedRunning
            property bool temporaryAwaken: ProcessControl.awakenProcesses.indexOf(model.application.appId) >= 0

            property var stateBinding: Binding {
                target: model.application
                property: "requestedState"
                value: applicationDelegate.requestedState
                restoreMode: Binding.RestoreBinding
            }

            property var lifecycleBinding: Binding {
                target: model.application
                property: "exemptFromLifecycle"
                restoreMode: Binding.RestoreBinding
                value: model.application
                            ? (!model.application.isTouchApp ||
                               isExemptFromLifecycle(model.application.appId) ||
                               applicationDelegate.temporaryAwaken)
                            : false

            }

            property var focusRequestedConnection: Connections {
                target: model.application

                function onFocusRequested() {
                    // Application emits focusRequested when it has no surface (i.e. their processes died).
                    // Find the topmost window for this application and activate it, after which the app
                    // will be requested to be running.

                    for (var i = 0; i < appRepeater.count; i++) {
                        var appDelegate = appRepeater.itemAt(i);
                        if (appDelegate.application.appId === model.application.appId) {
                            appDelegate.activate();
                            return;
                        }
                    }

                    console.warn("Application requested te be focused but no window for it. What should we do?");
                }
            }
        }
    }

    states: [
        State {
            name: "spread"; when: priv.goneToSpread
            PropertyChanges { target: floatingFlickable; enabled: true }
            PropertyChanges { target: root; focus: true }
            PropertyChanges { target: spreadItem; focus: true }
            PropertyChanges { target: hoverMouseArea; enabled: true }
            PropertyChanges { target: rightEdgeDragArea; enabled: false }
            PropertyChanges { target: cancelSpreadMouseArea; enabled: true }
            PropertyChanges { target: noAppsRunningHint; visible: (root.topLevelSurfaceList.count < 1) }
            PropertyChanges { target: blurLayer; visible: true; blurRadius: 32; brightness: .65; opacity: 1 }
            PropertyChanges { target: wallpaper; visible: false }
            PropertyChanges { target: screensAndWorkspaces.showTimer; running: true }
        },
        State {
            name: "stagedRightEdge"; when: root.spreadEnabled && (rightEdgeDragArea.dragging || rightEdgePushProgress > 0) && root.mode == "staged"
            PropertyChanges {
                target: blurLayer;
                visible: true;
                blurRadius: 32
                brightness: .65
                opacity: 1
            }
            PropertyChanges { target: noAppsRunningHint; visible: (root.topLevelSurfaceList.count < 1) }
        },
        State {
            name: "sideStagedRightEdge"; when: root.spreadEnabled && (rightEdgeDragArea.dragging || rightEdgePushProgress > 0) && root.mode == "stagedWithSideStage"
            extend: "stagedRightEdge"
            PropertyChanges {
                target: sideStage
                opacity: priv.sideStageDelegate && priv.sideStageDelegate.x === sideStage.x ? 1 : 0
                visible: true
            }
        },
        State {
            name: "windowedRightEdge"; when: root.spreadEnabled && (rightEdgeDragArea.dragging || rightEdgePushProgress > 0) && root.mode == "windowed"
            PropertyChanges {
                target: blurLayer;
                visible: true
                blurRadius: 32
                brightness: .65
                opacity: MathUtils.linearAnimation(spreadItem.rightEdgeBreakPoint, 1, 0, 1, Math.max(rightEdgeDragArea.dragging ? rightEdgeDragArea.progress : 0, rightEdgePushProgress))
            }
        },
        State {
            name: "staged"; when: root.mode === "staged"
            PropertyChanges { target: root; focus: true }
            PropertyChanges { target: appContainer; focus: true }
        },
        State {
            name: "stagedWithSideStage"; when: root.mode === "stagedWithSideStage"
            PropertyChanges { target: triGestureArea; enabled: priv.sideStageEnabled }
            PropertyChanges { target: sideStage; visible: true }
            PropertyChanges { target: root; focus: true }
            PropertyChanges { target: appContainer; focus: true }
        },
        State {
            name: "windowed"; when: root.mode === "windowed"
            PropertyChanges { target: root; focus: true }
            PropertyChanges { target: appContainer; focus: true }
        }
    ]
    transitions: [
        Transition {
            from: "stagedRightEdge,sideStagedRightEdge,windowedRightEdge"; to: "spread"
            PropertyAction { target: spreadItem; property: "highlightedIndex"; value: -1 }
            PropertyAction { target: screensAndWorkspaces; property: "activeWorkspace"; value: WMScreen.currentWorkspace }
            PropertyAnimation { target: blurLayer; properties: "brightness,blurRadius"; duration: priv.animationDuration }
        },
        Transition {
            to: "spread"
            PropertyAction { target: screensAndWorkspaces; property: "activeWorkspace"; value: WMScreen.currentWorkspace }
            PropertyAction { target: spreadItem; property: "highlightedIndex"; value: appRepeater.count > 1 ? 1 : 0 }
            PropertyAction { target: floatingFlickable; property: "contentX"; value: 0 }
        },
        Transition {
            from: "spread"
            SequentialAnimation {
                ScriptAction {
                    script: {
                        var item = appRepeater.itemAt(Math.max(0, spreadItem.highlightedIndex));
                        if (item) {
                            if (item.stage == ApplicationInfoInterface.SideStage && !sideStage.shown) {
                                sideStage.show();
                            }
                            item.playFocusAnimation();
                        }
                    }
                }
                PropertyAction { target: spreadItem; property: "highlightedIndex"; value: -1 }
                PropertyAction { target: floatingFlickable; property: "contentX"; value: 0 }
            }
        },
        Transition {
            to: "stagedRightEdge,sideStagedRightEdge"
            PropertyAction { target: floatingFlickable; property: "contentX"; value: 0 }
        },
        Transition {
            to: "stagedWithSideStage"
            ScriptAction { script: priv.updateMainAndSideStageIndexes(); }
        }

    ]

    MouseArea {
        id: cancelSpreadMouseArea
        anchors.fill: parent
        enabled: false
        onClicked: priv.goneToSpread = false
    }

    FocusScope {
        id: appContainer
        objectName: "appContainer"
        anchors.fill: parent
        focus: true

        Wallpaper {
            id: wallpaper
            objectName: "stageBackground"
            anchors.fill: parent
            source: root.background
            // Make sure it's the lowest item. Due to the left edge drag we sometimes need
            // to put the dash at -1 and we don't want it behind the Wallpaper
            z: -2
        }

        BlurLayer {
            id: blurLayer
            anchors.fill: parent
            source: wallpaper
            visible: false
        }

        ScreensAndWorkspaces {
            id: screensAndWorkspaces
            anchors { left: parent.left; top: parent.top; right: parent.right; leftMargin: root.launcherLeftMargin }
            height: Math.max(units.gu(30), parent.height * .3)
            background: root.background
            visible: showAllowed
            enabled: workspaceEnabled
            mode: root.mode
            availableDesktopArea: root.availableDesktopArea
            onCloseSpread: priv.goneToSpread = false;
            // Clicking a workspace should put it front and center
            onActiveWorkspaceChanged: activeWorkspace.activate()
            opacity: visible ? 1.0 : 0.0
            Behavior on opacity {
                NumberAnimation { duration: priv.animationDuration }
            }

            property bool showAllowed : false
            property var showTimer: Timer {
                running: false
                repeat: false
                interval: priv.animationDuration
                onTriggered: {
                    if (!priv.goneToSpread)
                        return;
                    screensAndWorkspaces.showAllowed = root.workspaceEnabled;
                }
            }
            Connections {
                target: priv
                onGoneToSpreadChanged: if (!priv.goneToSpread) screensAndWorkspaces.showAllowed = false
            }
        }

        Spread {
            id: spreadItem
            objectName: "spreadItem"
            anchors {
                left: parent.left;
                bottom: parent.bottom;
                right: parent.right;
                top: workspaceEnabled ? screensAndWorkspaces.bottom : parent.top;
            }
            leftMargin: root.availableDesktopArea.x
            model: root.topLevelSurfaceList
            spreadFlickable: floatingFlickable
            z: root.topLevelSurfaceList.count

            onLeaveSpread: {
                priv.goneToSpread = false;
            }

            onCloseCurrentApp: {
                appRepeater.itemAt(highlightedIndex).close();
            }

            FloatingFlickable {
                id: floatingFlickable
                objectName: "spreadFlickable"
                anchors.fill: parent
                enabled: false
                contentWidth: spreadItem.spreadTotalWidth

                function snap(toIndex) {
                    var delegate = appRepeater.itemAt(toIndex)
                    var targetContentX = floatingFlickable.contentWidth / spreadItem.totalItemCount * toIndex;
                    if (targetContentX - floatingFlickable.contentX > spreadItem.rightStackXPos - (spreadItem.spreadItemWidth / 2)) {
                        var offset = (spreadItem.rightStackXPos - (spreadItem.spreadItemWidth / 2)) - (targetContentX - floatingFlickable.contentX)
                        snapAnimation.to = floatingFlickable.contentX - offset;
                        snapAnimation.start();
                    } else if (targetContentX - floatingFlickable.contentX < spreadItem.leftStackXPos + units.gu(1)) {
                        var offset = (spreadItem.leftStackXPos + units.gu(1)) - (targetContentX - floatingFlickable.contentX);
                        snapAnimation.to = floatingFlickable.contentX - offset;
                        snapAnimation.start();
                    }
                }
                LomiriNumberAnimation {id: snapAnimation; target: floatingFlickable; property: "contentX"}
            }

            MouseArea {
                id: hoverMouseArea
                objectName: "hoverMouseArea"
                anchors.fill: parent
                propagateComposedEvents: true
                hoverEnabled: true
                enabled: false
                visible: enabled
                property bool wasTouchPress: false

                property int scrollAreaWidth: width / 3
                property bool progressiveScrollingEnabled: false

                onMouseXChanged: {
                    mouse.accepted = false

                    if (hoverMouseArea.pressed || wasTouchPress) {
                        return;
                    }

                    // Find the hovered item and mark it active
                    for (var i = appRepeater.count - 1; i >= 0; i--) {
                        var appDelegate = appRepeater.itemAt(i);
                        var mapped = mapToItem(appDelegate, hoverMouseArea.mouseX, hoverMouseArea.mouseY)
                        var itemUnder = appDelegate.childAt(mapped.x, mapped.y);
                        if (itemUnder && (itemUnder.objectName === "dragArea" || itemUnder.objectName === "windowInfoItem" || itemUnder.objectName == "closeMouseArea")) {
                            spreadItem.highlightedIndex = i;
                            break;
                        }
                    }

                    if (floatingFlickable.contentWidth > floatingFlickable.width) {
                        var margins = floatingFlickable.width * 0.05;

                        if (!progressiveScrollingEnabled && mouseX < floatingFlickable.width - scrollAreaWidth) {
                            progressiveScrollingEnabled = true
                        }

                        // do we need to scroll?
                        if (mouseX < scrollAreaWidth + margins) {
                            var progress = Math.min(1, (scrollAreaWidth + margins - mouseX) / (scrollAreaWidth - margins));
                            var contentX = (1 - progress) * (floatingFlickable.contentWidth - floatingFlickable.width)
                            floatingFlickable.contentX = Math.max(0, Math.min(floatingFlickable.contentX, contentX))
                        }
                        if (mouseX > floatingFlickable.width - scrollAreaWidth && progressiveScrollingEnabled) {
                            var progress = Math.min(1, (mouseX - (floatingFlickable.width - scrollAreaWidth)) / (scrollAreaWidth - margins))
                            var contentX = progress * (floatingFlickable.contentWidth - floatingFlickable.width)
                            floatingFlickable.contentX = Math.min(floatingFlickable.contentWidth - floatingFlickable.width, Math.max(floatingFlickable.contentX, contentX))
                        }
                    }
                }

                onPressed: {
                    mouse.accepted = false;
                    wasTouchPress = mouse.source === Qt.MouseEventSynthesizedByQt;
                }

                onExited: wasTouchPress = false;
            }
        }

        Label {
            id: noAppsRunningHint
            visible: false
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            anchors.fill: parent
            horizontalAlignment: Qt.AlignHCenter
            verticalAlignment: Qt.AlignVCenter
            anchors.leftMargin: root.launcherLeftMargin
            wrapMode: Label.WordWrap
            fontSize: "large"
            text: i18n.tr("No running apps")
            color: "#FFFFFF"
        }

        Connections {
            target: root.topLevelSurfaceList
            function onListChanged() { priv.updateMainAndSideStageIndexes() }
        }


        DropArea {
            objectName: "MainStageDropArea"
            anchors {
                left: parent.left
                top: parent.top
                bottom: parent.bottom
            }
            width: appContainer.width - sideStage.width
            enabled: priv.sideStageEnabled

            onDropped: {
                drop.source.appDelegate.saveStage(ApplicationInfoInterface.MainStage);
                drop.source.appDelegate.activate();
            }
            keys: "SideStage"
        }

        SideStage {
            id: sideStage
            objectName: "sideStage"
            shown: false
            height: appContainer.height
            x: appContainer.width - width
            visible: false
            showHint: !priv.sideStageDelegate
            Behavior on opacity { LomiriNumberAnimation {} }
            z: {
                if (!priv.mainStageItemId) return 0;

                if (priv.sideStageItemId && priv.nextInStack > 0) {

                    // Due the order in which bindings are evaluated, this might be triggered while shuffling
                    // the list and index doesn't yet match with itemIndex (even though itemIndex: index)
                    // Let's walk the list and compare itemIndex to make sure we have the correct one.
                    var nextDelegateInStack = -1;
                    for (var i = 0; i < appRepeater.count; i++) {
                        if (appRepeater.itemAt(i).itemIndex == priv.nextInStack) {
                            nextDelegateInStack = appRepeater.itemAt(i);
                            break;
                        }
                    }

                    if (nextDelegateInStack.stage ===  ApplicationInfoInterface.MainStage) {
                        // if the next app in stack is a main stage app, put the sidestage on top of it.
                        return 2;
                    }
                    return 1;
                }

                return 1;
            }

            onShownChanged: {
                if (!shown && priv.mainStageDelegate && !root.spreadShown) {
                    priv.mainStageDelegate.activate();
                }
            }

            DropArea {
                id: sideStageDropArea
                objectName: "SideStageDropArea"
                anchors.fill: parent

                property bool dropAllowed: true

                onEntered: {
                    dropAllowed = drag.keys != "Disabled";
                }
                onExited: {
                    dropAllowed = true;
                }
                onDropped: {
                    if (drop.keys == "MainStage") {
                        drop.source.appDelegate.saveStage(ApplicationInfoInterface.SideStage);
                        drop.source.appDelegate.activate();
                    }
                }
                drag {
                    onSourceChanged: {
                        if (!sideStageDropArea.drag.source) {
                            dropAllowed = true;
                        }
                    }
                }
            }
        }

        MirSurfaceItem {
            id: fakeDragItem
            property real previewScale: .5
            height: (screensAndWorkspaces.height - units.gu(8)) / 2
            // w : h = iw : ih
            width: implicitWidth * height / implicitHeight
            surfaceWidth: -1
            surfaceHeight: -1
            opacity: surface != null ? 1 : 0
            Behavior on opacity { LomiriNumberAnimation {} }
            visible: opacity > 0
            enabled: workspaceSwitcher

            Drag.active: surface != null
            Drag.keys: ["application"]

            z: 1000
        }

        Repeater {
            id: appRepeater
            model: topLevelSurfaceList
            objectName: "appRepeater"

            function indexOf(delegateItem) {
                for (var i = 0; i < count; i++) {
                    if (itemAt(i) === delegateItem) {
                        return i;
                    }
                }
                return -1;
            }

            delegate: FocusScope {
                id: appDelegate
                objectName: "appDelegate_" + model.window.id
                property int itemIndex: index // We need this from outside the repeater
                // z might be overriden in some cases by effects, but we need z ordering
                // to calculate occlusion detection
                property int normalZ: topLevelSurfaceList.count - index
                onNormalZChanged: {
                    if (visuallyMaximized) {
                        priv.updateForegroundMaximizedApp();
                    }
                }
                z: normalZ

                opacity: fakeDragItem.surface == model.window.surface && fakeDragItem.Drag.active ? 0 : 1
                Behavior on opacity { LomiriNumberAnimation {} }

                // Set these as propertyes as they wont update otherwise
                property real screenOffsetX: Screen.virtualX
                property real screenOffsetY: Screen.virtualY

                // Normally we want x/y where the surface thinks it is. Width/height of our delegate will
                // match what the actual surface size is.
                // Don't write to those, they will be set by states
                // --
                // Here we will also need to remove the screen offset from miral's results
                // as lomiri x,y will be relative to the current screen only
                // FIXME: when proper multiscreen lands
                x: model.window.position.x - clientAreaItem.x - screenOffsetX
                y: model.window.position.y - clientAreaItem.y - screenOffsetY
                width: decoratedWindow.implicitWidth
                height: decoratedWindow.implicitHeight

                // requestedX/Y/width/height is what we ask the actual surface to be.
                // Do not write to those, they will be set by states
                property real requestedX: windowedX
                property real requestedY: windowedY
                property real requestedWidth: windowedWidth
                property real requestedHeight: windowedHeight

                // For both windowed and staged need to tell miral what screen we are on,
                // so we need to add the screen offset to the position we tell miral
                // FIXME: when proper multiscreen lands
                Binding {
                    target: model.window; property: "requestedPosition"
                    // miral doesn't know about our window decorations. So we have to deduct them
                    value: Qt.point(appDelegate.requestedX + appDelegate.clientAreaItem.x + screenOffsetX,
                                    appDelegate.requestedY + appDelegate.clientAreaItem.y + screenOffsetY)
                    when: root.mode == "windowed"
                    restoreMode: Binding.RestoreBinding
                }
                Binding {
                    target: model.window; property: "requestedPosition"
                    value: Qt.point(screenOffsetX, screenOffsetY)
                    when: root.mode != "windowed"
                    restoreMode: Binding.RestoreBinding
                }

                // In those are for windowed mode. Those values basically store the window's properties
                // when having a floating window. If you want to move/resize a window in normal mode, this is what you want to write to.
                property real windowedX
                property real windowedY
                property real windowedWidth
                property real windowedHeight

                // unlike windowedX/Y, this is the last known grab position before being pushed against edges/corners
                // when restoring, the window should return to these, not to the place where it was dropped near the edge
                property real restoredX
                property real restoredY

                // Keeps track of the window geometry while in normal or restored state
                // Useful when returning from some maxmized state or when saving the geometry while maximized
                // FIXME: find a better solution
                property real normalX: 0
                property real normalY: 0
                property real normalWidth: 0
                property real normalHeight: 0
                function updateNormalGeometry() {
                    if (appDelegate.state == "normal" || appDelegate.state == "restored") {
                        normalX = appDelegate.requestedX;
                        normalY = appDelegate.requestedY;
                        normalWidth = appDelegate.width;
                        normalHeight = appDelegate.height;
                    }
                }
                function updateRestoredGeometry() {
                    if (appDelegate.state == "normal" || appDelegate.state == "restored") {
                        // save the x/y to restore to
                        restoredX = appDelegate.x;
                        restoredY = appDelegate.y;
                    }
                }

                Connections {
                    target: appDelegate
                    function onXChanged() { appDelegate.updateNormalGeometry(); }
                    function onYChanged() {  appDelegate.updateNormalGeometry(); }
                    function onWidthChanged() {  appDelegate.updateNormalGeometry(); }
                    function onHeightChanged() {  appDelegate.updateNormalGeometry(); }
                }

                // True when the Stage is focusing this app and playing its own animation.
                // Stays true until the app is unfocused.
                // If it is, we don't want to play the slide in/out transition from StageMaths.
                // Setting it imperatively is not great, but any declarative solution hits
                // race conditions, causing two animations to play for one focus event.
                property bool inhibitSlideAnimation: false

                Binding {
                    target: appDelegate
                    property: "y"
                    value: appDelegate.requestedY -
                           Math.min(appDelegate.requestedY - root.availableDesktopArea.y,
                                    Math.max(0, priv.virtualKeyboardHeight - (appContainer.height - (appDelegate.requestedY + appDelegate.height))))
                    when: root.oskEnabled && appDelegate.focus && (appDelegate.state == "normal" || appDelegate.state == "restored")
                          && root.inputMethodRect.height > 0
                    restoreMode: Binding.RestoreBinding
                }

                Behavior on x { id: xBehavior; enabled: priv.closingIndex >= 0; LomiriNumberAnimation { onRunningChanged: if (!running) priv.closingIndex = -1} }

                Connections {
                    target: root
                    function onShellOrientationAngleChanged() {
                        // at this point decoratedWindow.surfaceOrientationAngle is the old shellOrientationAngle
                        if (appDelegate.application && appDelegate.application.rotatesWindowContents) {
                            if (root.state == "windowed") {
                                var angleDiff = decoratedWindow.surfaceOrientationAngle - shellOrientationAngle;
                                angleDiff = (360 + angleDiff) % 360;
                                if (angleDiff === 90 || angleDiff === 270) {
                                    var aux = decoratedWindow.requestedHeight;
                                    decoratedWindow.requestedHeight = decoratedWindow.requestedWidth + decoratedWindow.actualDecorationHeight;
                                    decoratedWindow.requestedWidth = aux - decoratedWindow.actualDecorationHeight;
                                }
                            }
                            decoratedWindow.surfaceOrientationAngle = shellOrientationAngle;
                        } else {
                            decoratedWindow.surfaceOrientationAngle = 0;
                        }
                    }
                }

                readonly property alias application: decoratedWindow.application
                readonly property alias minimumWidth: decoratedWindow.minimumWidth
                readonly property alias minimumHeight: decoratedWindow.minimumHeight
                readonly property alias maximumWidth: decoratedWindow.maximumWidth
                readonly property alias maximumHeight: decoratedWindow.maximumHeight
                readonly property alias widthIncrement: decoratedWindow.widthIncrement
                readonly property alias heightIncrement: decoratedWindow.heightIncrement

                readonly property bool maximized: windowState === WindowStateStorage.WindowStateMaximized
                readonly property bool maximizedLeft: windowState === WindowStateStorage.WindowStateMaximizedLeft
                readonly property bool maximizedRight: windowState === WindowStateStorage.WindowStateMaximizedRight
                readonly property bool maximizedHorizontally: windowState === WindowStateStorage.WindowStateMaximizedHorizontally
                readonly property bool maximizedVertically: windowState === WindowStateStorage.WindowStateMaximizedVertically
                readonly property bool maximizedTopLeft: windowState === WindowStateStorage.WindowStateMaximizedTopLeft
                readonly property bool maximizedTopRight: windowState === WindowStateStorage.WindowStateMaximizedTopRight
                readonly property bool maximizedBottomLeft: windowState === WindowStateStorage.WindowStateMaximizedBottomLeft
                readonly property bool maximizedBottomRight: windowState === WindowStateStorage.WindowStateMaximizedBottomRight
                readonly property bool anyMaximized: maximized || maximizedLeft || maximizedRight || maximizedHorizontally || maximizedVertically ||
                                                     maximizedTopLeft || maximizedTopRight || maximizedBottomLeft || maximizedBottomRight

                readonly property bool minimized: windowState & WindowStateStorage.WindowStateMinimized
                readonly property bool fullscreen: windowState === WindowStateStorage.WindowStateFullscreen

                readonly property bool canBeMaximized: canBeMaximizedHorizontally && canBeMaximizedVertically
                readonly property bool canBeMaximizedLeftRight: (maximumWidth == 0 || maximumWidth >= appContainer.width/2) &&
                                                                (maximumHeight == 0 || maximumHeight >= appContainer.height)
                readonly property bool canBeCornerMaximized: (maximumWidth == 0 || maximumWidth >= appContainer.width/2) &&
                                                             (maximumHeight == 0 || maximumHeight >= appContainer.height/2)
                readonly property bool canBeMaximizedHorizontally: maximumWidth == 0 || maximumWidth >= appContainer.width
                readonly property bool canBeMaximizedVertically: maximumHeight == 0 || maximumHeight >= appContainer.height
                readonly property alias orientationChangesEnabled: decoratedWindow.orientationChangesEnabled

                // TODO drop our own windowType once Mir/Miral/Qtmir gets in sync with ours
                property int windowState: WindowStateStorage.WindowStateNormal
                property int prevWindowState: WindowStateStorage.WindowStateRestored

                property bool animationsEnabled: true
                property alias title: decoratedWindow.title
                readonly property string appName: model.application ? model.application.name : ""
                property bool visuallyMaximized: false
                property bool visuallyMinimized: false
                readonly property alias windowedTransitionRunning: windowedTransition.running

                property int stage: ApplicationInfoInterface.MainStage
                function saveStage(newStage) {
                    appDelegate.stage = newStage;
                    WindowStateStorage.saveStage(appId, newStage);
                    priv.updateMainAndSideStageIndexes()
                }

                readonly property var surface: model.window.surface
                readonly property var window: model.window

                readonly property alias focusedSurface: decoratedWindow.focusedSurface
                readonly property bool dragging: touchControls.overlayShown ? touchControls.dragging : decoratedWindow.dragging

                readonly property string appId: model.application.appId
                readonly property alias clientAreaItem: decoratedWindow.clientAreaItem

                // It is Lomiri policy to close any window but the last one during OOM teardown
/*
                Connections {
                    target: model.window.surface
                    onLiveChanged: {
                        if ((!surface.live && application && application.surfaceCount > 1) || !application)
                            topLevelSurfaceList.removeAt(appRepeater.indexOf(appDelegate));
                    }
                }
*/


                function activate() {
                    if (model.window.focused) {
                        updateQmlFocusFromMirSurfaceFocus();
                    } else {
                        if (surface.live) {
                            // Activate the window since it has a surface (with a running app) backing it
                            model.window.activate();
                        } else {
                            // Otherwise, cause a respawn of the app, and trigger it's refocusing as the last window
                            topLevelSurfaceList.raiseId(model.window.id);
                        }
                    }
                }
                function requestMaximize() { model.window.requestState(Mir.MaximizedState); }
                function requestMaximizeVertically() { model.window.requestState(Mir.VertMaximizedState); }
                function requestMaximizeHorizontally() { model.window.requestState(Mir.HorizMaximizedState); }
                function requestMaximizeLeft() { model.window.requestState(Mir.MaximizedLeftState); }
                function requestMaximizeRight() { model.window.requestState(Mir.MaximizedRightState); }
                function requestMaximizeTopLeft() { model.window.requestState(Mir.MaximizedTopLeftState); }
                function requestMaximizeTopRight() { model.window.requestState(Mir.MaximizedTopRightState); }
                function requestMaximizeBottomLeft() { model.window.requestState(Mir.MaximizedBottomLeftState); }
                function requestMaximizeBottomRight() { model.window.requestState(Mir.MaximizedBottomRightState); }
                function requestMinimize() { model.window.requestState(Mir.MinimizedState); }
                function requestRestore() { model.window.requestState(Mir.RestoredState); }

                function claimFocus() {
                    if (root.state == "spread") {
                        spreadItem.highlightedIndex = index
                        // force pendingActivation so that when switching to staged mode, topLevelSurfaceList focus won't got to previous app ( case when apps are launched from outside )
                        topLevelSurfaceList.pendingActivation();
                        priv.goneToSpread = false;
                    }
                    if (root.mode == "stagedWithSideStage") {
                        if (appDelegate.stage == ApplicationInfoInterface.SideStage && !sideStage.shown) {
                            sideStage.show();
                        }
                        priv.updateMainAndSideStageIndexes();
                    }
                    appDelegate.focus = true;

                    // Don't set focusedAppDelegate (and signal mainAppChanged) unnecessarily
                    // which can happen after getting interactive again.
                    if (priv.focusedAppDelegate !== appDelegate)
                        priv.focusedAppDelegate = appDelegate;
                }

                function updateQmlFocusFromMirSurfaceFocus() {
                    if (model.window.focused) {
                        claimFocus();
                        decoratedWindow.focus = true;
                    }
                }

                WindowStateSaver {
                    id: windowStateSaver
                    target: appDelegate
                    screenWidth: appContainer.width
                    screenHeight: appContainer.height
                    leftMargin: root.availableDesktopArea.x
                    minimumY: root.availableDesktopArea.y
                }

                Connections {
                    target: model.window
                    function onFocusedChanged() {
                        updateQmlFocusFromMirSurfaceFocus();
                        if (!model.window.focused) {
                            inhibitSlideAnimation = false;
                        }
                    }
                    function onFocusRequested() {
                        appDelegate.activate();
                    }
                    function onStateChanged(value) {
                        if (value == Mir.MinimizedState) {
                            appDelegate.minimize();
                        } else if (value == Mir.MaximizedState) {
                            appDelegate.maximize();
                        } else if (value == Mir.VertMaximizedState) {
                            appDelegate.maximizeVertically();
                        } else if (value == Mir.HorizMaximizedState) {
                            appDelegate.maximizeHorizontally();
                        } else if (value == Mir.MaximizedLeftState) {
                            appDelegate.maximizeLeft();
                        } else if (value == Mir.MaximizedRightState) {
                            appDelegate.maximizeRight();
                        } else if (value == Mir.MaximizedTopLeftState) {
                            appDelegate.maximizeTopLeft();
                        } else if (value == Mir.MaximizedTopRightState) {
                            appDelegate.maximizeTopRight();
                        } else if (value == Mir.MaximizedBottomLeftState) {
                            appDelegate.maximizeBottomLeft();
                        } else if (value == Mir.MaximizedBottomRightState) {
                            appDelegate.maximizeBottomRight();
                        } else if (value == Mir.RestoredState) {
                            if (appDelegate.fullscreen && appDelegate.prevWindowState != WindowStateStorage.WindowStateRestored
                                    && appDelegate.prevWindowState != WindowStateStorage.WindowStateNormal) {
                                model.window.requestState(WindowStateStorage.toMirState(appDelegate.prevWindowState));
                            } else {
                                appDelegate.restore();
                            }
                        } else if (value == Mir.FullscreenState) {
                            appDelegate.prevWindowState = appDelegate.windowState;
                            appDelegate.windowState = WindowStateStorage.WindowStateFullscreen;
                        }
                    }
                }

                readonly property bool windowReady: clientAreaItem.surfaceInitialized
                onWindowReadyChanged: {
                    if (windowReady) {
                        var loadedMirState = WindowStateStorage.toMirState(windowStateSaver.loadedState);
                        var state = loadedMirState;

                        if (window.state == Mir.FullscreenState) {
                            // If the app is fullscreen at startup, we should not use saved state
                            // Example of why: if you open game that only requests fullscreen at
                            // Statup, this will automaticly be set to "restored state" since
                            // thats the default value of stateStorage, this will result in the app
                            // having the "restored state" as it will not make a fullscreen
                            // call after the app has started.
                            console.log("Initial window state is fullscreen, not using saved state.");
                            state = window.state;
                        } else if (loadedMirState == Mir.FullscreenState) {
                            // If saved state is fullscreen, we should use app initial state
                            // Example of why: if you open browser with youtube video at fullscreen
                            // and close this app, it will be fullscreen next time you open the app.
                            console.log("Saved window state is fullscreen, using initial window state");
                            state = window.state;
                        }

                        // need to apply the shell chrome policy on top the saved window state
                        var policy;
                        if (root.mode == "windowed") {
                            policy = windowedFullscreenPolicy;
                        } else {
                            policy = stagedFullscreenPolicy
                        }
                        window.requestState(policy.applyPolicy(state, surface.shellChrome));
                    }
                }

                Component.onCompleted: {
                    if (application && application.rotatesWindowContents) {
                        decoratedWindow.surfaceOrientationAngle = shellOrientationAngle;
                    } else {
                        decoratedWindow.surfaceOrientationAngle = 0;
                    }

                    // First, cascade the newly created window, relative to the currently/old focused window.
                    windowedX = priv.focusedAppDelegate ? priv.focusedAppDelegate.windowedX + units.gu(3) : (normalZ - 1) * units.gu(3)
                    windowedY = priv.focusedAppDelegate ? priv.focusedAppDelegate.windowedY + units.gu(3) : normalZ * units.gu(3)
                    // Now load any saved state. This needs to happen *after* the cascading!
                    windowStateSaver.load();

                    updateQmlFocusFromMirSurfaceFocus();

                    // Make apps maximized on phones & tablets
                    if (root.mode == "staged" || root.mode == "stagedWithSideStage")
                        appDelegate.maximize()

                    refreshStage();
                    _constructing = false;
                }
                Component.onDestruction: {
                    windowStateSaver.save();

                    if (!root.parent) {
                        // This stage is about to be destroyed. Don't mess up with the model at this point
                        return;
                    }

                    if (visuallyMaximized) {
                        priv.updateForegroundMaximizedApp();
                    }
                }

                onVisuallyMaximizedChanged: priv.updateForegroundMaximizedApp()

                property bool _constructing: true;
                onStageChanged: {
                    if (!_constructing) {
                        priv.updateMainAndSideStageIndexes();
                    }
                }

                visible: (
                          !visuallyMinimized
                          && !greeter.fullyShown
                          && (priv.foregroundMaximizedAppDelegate === null || priv.foregroundMaximizedAppDelegate.normalZ <= z)
                         )
                         || appDelegate.fullscreen
                         || focusAnimation.running || rightEdgeFocusAnimation.running || hidingAnimation.running

                function close() {
                    model.window.close();
                }

                function maximize(animated) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState = WindowStateStorage.WindowStateMaximized;
                }
                function maximizeLeft(animated) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState = WindowStateStorage.WindowStateMaximizedLeft;
                }
                function maximizeRight(animated) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState = WindowStateStorage.WindowStateMaximizedRight;
                }
                function maximizeHorizontally(animated) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState = WindowStateStorage.WindowStateMaximizedHorizontally;
                }
                function maximizeVertically(animated) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState = WindowStateStorage.WindowStateMaximizedVertically;
                }
                function maximizeTopLeft(animated) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState = WindowStateStorage.WindowStateMaximizedTopLeft;
                }
                function maximizeTopRight(animated) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState = WindowStateStorage.WindowStateMaximizedTopRight;
                }
                function maximizeBottomLeft(animated) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState = WindowStateStorage.WindowStateMaximizedBottomLeft;
                }
                function maximizeBottomRight(animated) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState = WindowStateStorage.WindowStateMaximizedBottomRight;
                }
                function minimize(animated) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState |= WindowStateStorage.WindowStateMinimized; // add the minimized bit
                }
                function restore(animated,state) {
                    animationsEnabled = (animated === undefined) || animated;
                    windowState = state || WindowStateStorage.WindowStateRestored;
                    windowState &= ~WindowStateStorage.WindowStateMinimized; // clear the minimized bit
                    prevWindowState = windowState;
                }

                function playFocusAnimation() {
                    if (state == "stagedRightEdge") {
                        // TODO: Can we drop this if and find something that always works?
                        if (root.mode == "staged") {
                            rightEdgeFocusAnimation.targetX = 0
                            rightEdgeFocusAnimation.start()
                        } else if (root.mode == "stagedWithSideStage") {
                            rightEdgeFocusAnimation.targetX = appDelegate.stage == ApplicationInfoInterface.SideStage ? sideStage.x : 0
                            rightEdgeFocusAnimation.start()
                        }
                    } else {
                        focusAnimation.start()
                    }
                }
                function playHidingAnimation() {
                    if (state != "windowedRightEdge") {
                        hidingAnimation.start()
                    }
                }

                function refreshStage() {
                    var newStage = ApplicationInfoInterface.MainStage;
                    if (priv.sideStageEnabled) { // we're in lanscape rotation.
                        if (application && application.supportedOrientations & (Qt.PortraitOrientation|Qt.InvertedPortraitOrientation)) {
                            var defaultStage = ApplicationInfoInterface.SideStage; // if application supports portrait, it defaults to sidestage.
                            if (application.supportedOrientations & (Qt.LandscapeOrientation|Qt.InvertedLandscapeOrientation)) {
                                // if it supports lanscape, it defaults to mainstage.
                                defaultStage = ApplicationInfoInterface.MainStage;
                            }
                            newStage = WindowStateStorage.getStage(application.appId, defaultStage);
                        }
                    }

                    stage = newStage;
                    if (focus && stage == ApplicationInfoInterface.SideStage && !sideStage.shown) {
                        sideStage.show();
                    }
                }

                LomiriNumberAnimation {
                    id: focusAnimation
                    target: appDelegate
                    property: "scale"
                    from: 0.98
                    to: 1
                    duration: LomiriAnimation.SnapDuration
                    onStarted: {
                        topLevelSurfaceList.pendingActivation();
                        topLevelSurfaceList.raiseId(model.window.id);
                    }
                    onStopped: {
                        appDelegate.activate();
                    }
                }
                ParallelAnimation {
                    id: rightEdgeFocusAnimation
                    property int targetX: 0
                    LomiriNumberAnimation { target: appDelegate; properties: "x"; to: rightEdgeFocusAnimation.targetX; duration: priv.animationDuration }
                    LomiriNumberAnimation { target: decoratedWindow; properties: "angle"; to: 0; duration: priv.animationDuration }
                    LomiriNumberAnimation { target: decoratedWindow; properties: "itemScale"; to: 1; duration: priv.animationDuration }
                    onStarted: {
                        topLevelSurfaceList.pendingActivation();
                        inhibitSlideAnimation = true;
                    }
                    onStopped: {
                        appDelegate.activate();
                    }
                }
                ParallelAnimation {
                    id: hidingAnimation
                    LomiriNumberAnimation { target: appDelegate; property: "opacity"; to: 0; duration: priv.animationDuration }
                    onStopped: appDelegate.opacity = 1
                }

                SpreadMaths {
                    id: spreadMaths
                    spread: spreadItem
                    itemIndex: index
                    flickable: floatingFlickable
                }
                StageMaths {
                    id: stageMaths
                    sceneWidth: root.width
                    stage: appDelegate.stage
                    thisDelegate: appDelegate
                    mainStageDelegate: priv.mainStageDelegate
                    sideStageDelegate: priv.sideStageDelegate
                    sideStageWidth: sideStage.panelWidth
                    sideStageHandleWidth: sideStage.handleWidth
                    sideStageX: sideStage.x
                    itemIndex: appDelegate.itemIndex
                    nextInStack: priv.nextInStack
                    animationDuration: priv.animationDuration
                }

                StagedRightEdgeMaths {
                    id: stagedRightEdgeMaths
                    sceneWidth: root.availableDesktopArea.width
                    sceneHeight: appContainer.height
                    isMainStageApp: priv.mainStageDelegate == appDelegate
                    isSideStageApp: priv.sideStageDelegate == appDelegate
                    sideStageWidth: sideStage.width
                    sideStageOpen: sideStage.shown
                    itemIndex: index
                    nextInStack: priv.nextInStack
                    progress: 0
                    targetHeight: spreadItem.stackHeight
                    targetX: spreadMaths.targetX
                    startY: appDelegate.fullscreen ? 0 : root.availableDesktopArea.y
                    targetY: spreadMaths.targetY
                    targetAngle: spreadMaths.targetAngle
                    targetScale: spreadMaths.targetScale
                    shuffledZ: stageMaths.itemZ
                    breakPoint: spreadItem.rightEdgeBreakPoint
                }

                WindowedRightEdgeMaths {
                    id: windowedRightEdgeMaths
                    itemIndex: index
                    startWidth: appDelegate.requestedWidth
                    startHeight: appDelegate.requestedHeight
                    targetHeight: spreadItem.stackHeight
                    targetX: spreadMaths.targetX
                    targetY: spreadMaths.targetY
                    normalZ: appDelegate.normalZ
                    targetAngle: spreadMaths.targetAngle
                    targetScale: spreadMaths.targetScale
                    breakPoint: spreadItem.rightEdgeBreakPoint
                }

                states: [
                    State {
                        name: "spread"; when: root.state == "spread"
                        StateChangeScript { script: { decoratedWindow.cancelDrag(); } }
                        PropertyChanges {
                            target: decoratedWindow;
                            showDecoration: false;
                            angle: spreadMaths.targetAngle
                            itemScale: spreadMaths.targetScale
                            scaleToPreviewSize: spreadItem.stackHeight
                            scaleToPreviewProgress: 1
                            hasDecoration: root.mode === "windowed"
                            shadowOpacity: spreadMaths.shadowOpacity
                            showHighlight: spreadItem.highlightedIndex === index
                            darkening: spreadItem.highlightedIndex >= 0
                            anchors.topMargin: dragArea.distance
                        }
                        PropertyChanges {
                            target: appDelegate
                            x: spreadMaths.targetX
                            y: spreadMaths.targetY
                            z: index
                            height: spreadItem.spreadItemHeight
                            visible: spreadMaths.itemVisible
                        }
                        PropertyChanges { target: dragArea; enabled: true }
                        PropertyChanges { target: windowInfoItem; opacity: spreadMaths.tileInfoOpacity; visible: spreadMaths.itemVisible }
                        PropertyChanges { target: touchControls; enabled: false }
                    },
                    State {
                        name: "stagedRightEdge"
                        when: (root.mode == "staged" || root.mode == "stagedWithSideStage") && (root.state == "sideStagedRightEdge" || root.state == "stagedRightEdge" || rightEdgeFocusAnimation.running || hidingAnimation.running)
                        PropertyChanges {
                            target: stagedRightEdgeMaths
                            progress: Math.max(rightEdgePushProgress, rightEdgeDragArea.draggedProgress)
                        }
                        PropertyChanges {
                            target: appDelegate
                            x: stagedRightEdgeMaths.animatedX
                            y: stagedRightEdgeMaths.animatedY
                            z: stagedRightEdgeMaths.animatedZ
                            height: stagedRightEdgeMaths.animatedHeight
                            visible: appDelegate.x < root.width
                        }
                        PropertyChanges {
                            target: decoratedWindow
                            hasDecoration: false
                            angle: stagedRightEdgeMaths.animatedAngle
                            itemScale: stagedRightEdgeMaths.animatedScale
                            scaleToPreviewSize: spreadItem.stackHeight
                            scaleToPreviewProgress: stagedRightEdgeMaths.scaleToPreviewProgress
                            shadowOpacity: .3
                        }
                        // make sure it's visible but transparent so it fades in when we transition to spread
                        PropertyChanges { target: windowInfoItem; opacity: 0; visible: true }
                    },
                    State {
                        name: "windowedRightEdge"
                        when: root.mode == "windowed" && (root.state == "windowedRightEdge" || rightEdgeFocusAnimation.running || hidingAnimation.running || rightEdgePushProgress > 0)
                        PropertyChanges {
                            target: windowedRightEdgeMaths
                            swipeProgress: rightEdgeDragArea.dragging ? rightEdgeDragArea.progress : 0
                            pushProgress: rightEdgePushProgress
                        }
                        PropertyChanges {
                            target: appDelegate
                            x: windowedRightEdgeMaths.animatedX
                            y: windowedRightEdgeMaths.animatedY
                            z: windowedRightEdgeMaths.animatedZ
                            height: stagedRightEdgeMaths.animatedHeight
                        }
                        PropertyChanges {
                            target: decoratedWindow
                            showDecoration: windowedRightEdgeMaths.decorationHeight
                            angle: windowedRightEdgeMaths.animatedAngle
                            itemScale: windowedRightEdgeMaths.animatedScale
                            scaleToPreviewSize: spreadItem.stackHeight
                            scaleToPreviewProgress: windowedRightEdgeMaths.scaleToPreviewProgress
                            shadowOpacity: .3
                        }
                        PropertyChanges {
                            target: opacityEffect;
                            opacityValue: windowedRightEdgeMaths.opacityMask
                            sourceItem: windowedRightEdgeMaths.opacityMask < 1 ? decoratedWindow : null
                        }
                    },
                    State {
                        name: "staged"; when: root.state == "staged"
                        PropertyChanges {
                            target: appDelegate
                            x: stageMaths.itemX
                            y: root.availableDesktopArea.y
                            visuallyMaximized: true
                            visible: appDelegate.x < root.width
                        }
                        PropertyChanges {
                            target: appDelegate
                            requestedWidth: appContainer.width
                            requestedHeight: root.availableDesktopArea.height
                            restoreEntryValues: false
                        }
                        PropertyChanges {
                            target: decoratedWindow
                            hasDecoration: false
                        }
                        PropertyChanges {
                            target: resizeArea
                            enabled: false
                        }
                        PropertyChanges {
                            target: stageMaths
                            animateX: !focusAnimation.running && !rightEdgeFocusAnimation.running && itemIndex !== spreadItem.highlightedIndex && !inhibitSlideAnimation
                        }
                        PropertyChanges {
                            target: appDelegate.window
                            allowClientResize: false
                        }
                    },
                    State {
                        name: "stagedWithSideStage"; when: root.state == "stagedWithSideStage"
                        PropertyChanges {
                            target: stageMaths
                            itemIndex: index
                        }
                        PropertyChanges {
                            target: appDelegate
                            x: stageMaths.itemX
                            y: root.availableDesktopArea.y
                            z: stageMaths.itemZ
                            visuallyMaximized: true
                            visible: appDelegate.x < root.width
                        }
                        PropertyChanges {
                            target: appDelegate
                            requestedWidth: stageMaths.itemWidth
                            requestedHeight: root.availableDesktopArea.height
                            restoreEntryValues: false
                        }
                        PropertyChanges {
                            target: decoratedWindow
                            hasDecoration: false
                        }
                        PropertyChanges {
                            target: resizeArea
                            enabled: false
                        }
                        PropertyChanges {
                            target: appDelegate.window
                            allowClientResize: false
                        }
                    },
                    State {
                        name: "maximized"; when: appDelegate.maximized && !appDelegate.minimized
                        PropertyChanges {
                            target: appDelegate;
                            requestedX: root.availableDesktopArea.x;
                            requestedY: 0;
                            visuallyMinimized: false;
                            visuallyMaximized: true
                        }
                        PropertyChanges {
                            target: appDelegate
                            requestedWidth: root.availableDesktopArea.width;
                            requestedHeight: appContainer.height;
                            restoreEntryValues: false
                        }
                        PropertyChanges { target: touchControls; enabled: true }
                        PropertyChanges { target: decoratedWindow; windowControlButtonsVisible: false }
                    },
                    State {
                        name: "fullscreen"; when: appDelegate.fullscreen && !appDelegate.minimized
                        PropertyChanges {
                            target: appDelegate;
                            requestedX: 0
                            requestedY: 0
                        }
                        PropertyChanges {
                            target: appDelegate
                            requestedWidth: appContainer.width
                            requestedHeight: appContainer.height
                            restoreEntryValues: false
                        }
                        PropertyChanges { target: decoratedWindow; hasDecoration: false }
                    },
                    State {
                        name: "normal";
                        when: appDelegate.windowState == WindowStateStorage.WindowStateNormal
                        PropertyChanges {
                            target: appDelegate
                            visuallyMinimized: false
                        }
                        PropertyChanges { target: touchControls; enabled: true }
                        PropertyChanges { target: resizeArea; enabled: true }
                        PropertyChanges { target: decoratedWindow; shadowOpacity: .3; windowControlButtonsVisible: true}
                        PropertyChanges {
                            target: appDelegate
                            requestedWidth: windowedWidth
                            requestedHeight: windowedHeight
                            restoreEntryValues: false
                        }
                    },
                    State {
                        name: "restored";
                        when: appDelegate.windowState == WindowStateStorage.WindowStateRestored
                        extend: "normal"
                        PropertyChanges {
                            restoreEntryValues: false
                            target: appDelegate;
                            windowedX: restoredX;
                            windowedY: restoredY;
                        }
                    },
                    State {
                        name: "maximizedLeft"; when: appDelegate.maximizedLeft && !appDelegate.minimized
                        extend: "normal"
                        PropertyChanges {
                            target: appDelegate
                            windowedX: root.availableDesktopArea.x
                            windowedY: root.availableDesktopArea.y
                            windowedWidth: root.availableDesktopArea.width / 2
                            windowedHeight: root.availableDesktopArea.height
                        }
                    },
                    State {
                        name: "maximizedRight"; when: appDelegate.maximizedRight && !appDelegate.minimized
                        extend: "maximizedLeft"
                        PropertyChanges {
                            target: appDelegate;
                            windowedX: root.availableDesktopArea.x + (root.availableDesktopArea.width / 2)
                        }
                    },
                    State {
                        name: "maximizedTopLeft"; when: appDelegate.maximizedTopLeft && !appDelegate.minimized
                        extend: "normal"
                        PropertyChanges {
                            target: appDelegate
                            windowedX: root.availableDesktopArea.x
                            windowedY: root.availableDesktopArea.y
                            windowedWidth: root.availableDesktopArea.width / 2
                            windowedHeight: root.availableDesktopArea.height / 2
                        }
                    },
                    State {
                        name: "maximizedTopRight"; when: appDelegate.maximizedTopRight && !appDelegate.minimized
                        extend: "maximizedTopLeft"
                        PropertyChanges {
                            target: appDelegate
                            windowedX: root.availableDesktopArea.x + (root.availableDesktopArea.width / 2)
                        }
                    },
                    State {
                        name: "maximizedBottomLeft"; when: appDelegate.maximizedBottomLeft && !appDelegate.minimized
                        extend: "normal"
                        PropertyChanges {
                            target: appDelegate
                            windowedX: root.availableDesktopArea.x
                            windowedY: root.availableDesktopArea.y + (root.availableDesktopArea.height / 2)
                            windowedWidth: root.availableDesktopArea.width / 2
                            windowedHeight: root.availableDesktopArea.height / 2
                        }
                    },
                    State {
                        name: "maximizedBottomRight"; when: appDelegate.maximizedBottomRight && !appDelegate.minimized
                        extend: "maximizedBottomLeft"
                        PropertyChanges {
                            target: appDelegate
                            windowedX: root.availableDesktopArea.x + (root.availableDesktopArea.width / 2)
                        }
                    },
                    State {
                        name: "maximizedHorizontally"; when: appDelegate.maximizedHorizontally && !appDelegate.minimized
                        extend: "normal"
                        PropertyChanges {
                            target: appDelegate
                            windowedX: root.availableDesktopArea.x; windowedY: windowedY
                            windowedWidth: root.availableDesktopArea.width; windowedHeight: windowedHeight
                        }
                    },
                    State {
                        name: "maximizedVertically"; when: appDelegate.maximizedVertically && !appDelegate.minimized
                        extend: "normal"
                        PropertyChanges {
                            target: appDelegate
                            windowedX: windowedX; windowedY: root.availableDesktopArea.y
                            windowedWidth: windowedWidth; windowedHeight: root.availableDesktopArea.height
                        }
                    },
                    State {
                        name: "minimized"; when: appDelegate.minimized
                        PropertyChanges {
                            target: appDelegate
                            scale: units.gu(5) / appDelegate.width
                            opacity: 0;
                            visuallyMinimized: true
                            visuallyMaximized: false
                            x: -appDelegate.width / 2
                            y: root.height / 2
                        }
                    }
                ]

                transitions: [

                    // These two animate applications into position from Staged to Desktop and back
                    Transition {
                        from: "staged,stagedWithSideStage"
                        to: "normal,restored,maximized,maximizedHorizontally,maximizedVertically,maximizedLeft,maximizedRight,maximizedTopLeft,maximizedBottomLeft,maximizedTopRight,maximizedBottomRight"
                        enabled: appDelegate.animationsEnabled
                        PropertyAction { target: appDelegate; properties: "visuallyMinimized,visuallyMaximized" }
                        LomiriNumberAnimation { target: appDelegate; properties: "x,y,requestedX,requestedY,opacity,requestedWidth,requestedHeight,scale"; duration: priv.animationDuration }
                    },
                    Transition {
                        from: "normal,restored,maximized,maximizedHorizontally,maximizedVertically,maximizedLeft,maximizedRight,maximizedTopLeft,maximizedBottomLeft,maximizedTopRight,maximizedBottomRight"
                        to: "staged,stagedWithSideStage"
                        LomiriNumberAnimation { target: appDelegate; properties: "x,y,requestedX,requestedY,requestedWidth,requestedHeight"; duration: priv.animationDuration}
                    },

                    Transition {
                        from: "normal,restored,maximized,maximizedHorizontally,maximizedVertically,maximizedLeft,maximizedRight,maximizedTopLeft,maximizedBottomLeft,maximizedTopRight,maximizedBottomRight,staged,stagedWithSideStage,windowedRightEdge,stagedRightEdge";
                        to: "spread"
                        // DecoratedWindow wants the scaleToPreviewSize set before enabling scaleToPreview
                        PropertyAction { target: appDelegate; properties: "z,visible" }
                        PropertyAction { target: decoratedWindow; property: "scaleToPreviewSize" }
                        LomiriNumberAnimation { target: appDelegate; properties: "x,y,height"; duration: priv.animationDuration }
                        LomiriNumberAnimation { target: decoratedWindow; properties: "width,height,itemScale,angle,scaleToPreviewProgress"; duration: priv.animationDuration }
                        LomiriNumberAnimation { target: windowInfoItem; properties: "opacity"; duration: priv.animationDuration }
                    },
                    Transition {
                        from: "normal,staged"; to: "stagedWithSideStage"
                        LomiriNumberAnimation { target: appDelegate; properties: "x,y,requestedWidth,requestedHeight"; duration: priv.animationDuration }
                    },
                    Transition {
                        to: "windowedRightEdge"
                        ScriptAction {
                            script: {
                                windowedRightEdgeMaths.startX = appDelegate.requestedX
                                windowedRightEdgeMaths.startY = appDelegate.requestedY

                                if (index == 1) {
                                    var thisRect = { x: appDelegate.windowedX, y: appDelegate.windowedY, width: appDelegate.requestedWidth, height: appDelegate.requestedHeight }
                                    var otherDelegate = appRepeater.itemAt(0);
                                    var otherRect = { x: otherDelegate.windowedX, y: otherDelegate.windowedY, width: otherDelegate.requestedWidth, height: otherDelegate.requestedHeight }
                                    var intersectionRect = MathUtils.intersectionRect(thisRect, otherRect)
                                    var mappedInterSectionRect = appDelegate.mapFromItem(root, intersectionRect.x, intersectionRect.y)
                                    opacityEffect.maskX = mappedInterSectionRect.x
                                    opacityEffect.maskY = mappedInterSectionRect.y
                                    opacityEffect.maskWidth = intersectionRect.width
                                    opacityEffect.maskHeight = intersectionRect.height
                                }
                            }
                        }
                    },
                    Transition {
                        from: "stagedRightEdge"; to: "staged"
                        enabled: rightEdgeDragArea.cancelled // only transition back to state if the gesture was cancelled, in the other cases we play the focusAnimations.
                        SequentialAnimation {
                            ParallelAnimation {
                                LomiriNumberAnimation { target: appDelegate; properties: "x,y,height,width,scale"; duration: priv.animationDuration }
                                LomiriNumberAnimation { target: decoratedWindow; properties: "width,height,itemScale,angle,scaleToPreviewProgress"; duration: priv.animationDuration }
                            }
                            // We need to release scaleToPreviewSize at last
                            PropertyAction { target: decoratedWindow; property: "scaleToPreviewSize" }
                            PropertyAction { target: appDelegate; property: "visible" }
                        }
                    },
                    Transition {
                        from: ",normal,restored,maximized,maximizedLeft,maximizedRight,maximizedTopLeft,maximizedTopRight,maximizedBottomLeft,maximizedBottomRight,maximizedHorizontally,maximizedVertically,fullscreen"
                        to: "minimized"
                        SequentialAnimation {
                            ScriptAction { script: { fakeRectangle.stop(); } }
                            PropertyAction { target: appDelegate; property: "visuallyMaximized" }
                            PropertyAction { target: appDelegate; property: "visuallyMinimized" }
                            LomiriNumberAnimation { target: appDelegate; properties: "x,y,scale,opacity"; duration: priv.animationDuration }
                            PropertyAction { target: appDelegate; property: "visuallyMinimized" }
                        }
                    },
                    Transition {
                        from: "minimized"
                        to: ",normal,restored,maximized,maximizedLeft,maximizedRight,maximizedTopLeft,maximizedTopRight,maximizedBottomLeft,maximizedBottomRight,maximizedHorizontally,maximizedVertically,fullscreen"
                        SequentialAnimation {
                            PropertyAction { target: appDelegate; property: "visuallyMinimized,z" }
                            ParallelAnimation {
                                LomiriNumberAnimation { target: appDelegate; properties: "x"; from: -appDelegate.width / 2; duration: priv.animationDuration }
                                LomiriNumberAnimation { target: appDelegate; properties: "y,opacity"; duration: priv.animationDuration }
                                LomiriNumberAnimation { target: appDelegate; properties: "scale"; from: 0; duration: priv.animationDuration }
                            }
                            PropertyAction { target: appDelegate; property: "visuallyMaximized" }
                        }
                    },
                    Transition {
                        id: windowedTransition
                        from: ",normal,restored,maximized,maximizedLeft,maximizedRight,maximizedTopLeft,maximizedTopRight,maximizedBottomLeft,maximizedBottomRight,maximizedHorizontally,maximizedVertically,fullscreen,minimized"
                        to: ",normal,restored,maximized,maximizedLeft,maximizedRight,maximizedTopLeft,maximizedTopRight,maximizedBottomLeft,maximizedBottomRight,maximizedHorizontally,maximizedVertically,fullscreen"
                        enabled: appDelegate.animationsEnabled
                        SequentialAnimation {
                            ScriptAction { script: {
                                    if (appDelegate.visuallyMaximized) visuallyMaximized = false; // maximized before -> going to restored
                                }
                            }
                            PropertyAction { target: appDelegate; property: "visuallyMinimized" }
                            LomiriNumberAnimation { target: appDelegate; properties: "requestedX,requestedY,windowedX,windowedY,opacity,scale,requestedWidth,requestedHeight,windowedWidth,windowedHeight";
                                duration: priv.animationDuration }
                            ScriptAction { script: {
                                    fakeRectangle.stop();
                                    appDelegate.visuallyMaximized = appDelegate.maximized; // reflect the target state
                                }
                            }
                        }
                    }
                ]

                Binding {
                    target: panelState
                    property: "decorationsAlwaysVisible"
                    value: appDelegate && appDelegate.maximized && touchControls.overlayShown
                    restoreMode: Binding.RestoreBinding
                }

                WindowResizeArea {
                    id: resizeArea
                    objectName: "windowResizeArea"

                    anchors.fill: appDelegate

                    // workaround so that it chooses the correct resize borders when you drag from a corner ResizeGrip
                    anchors.margins: touchControls.overlayShown ? borderThickness/2 : -borderThickness

                    target: appDelegate
                    boundsItem: root.availableDesktopArea
                    minWidth: units.gu(10)
                    minHeight: units.gu(10)
                    borderThickness: units.gu(2)
                    enabled: false
                    visible: enabled
                    readyToAssesBounds: !appDelegate._constructing

                    onPressed: {
                        appDelegate.activate();
                    }
                }

                DecoratedWindow {
                    id: decoratedWindow
                    objectName: "decoratedWindow"
                    anchors.left: appDelegate.left
                    anchors.top: appDelegate.top
                    stage: root
                    application: model.application
                    surface: model.window.surface
                    active: model.window.focused
                    focus: true
                    interactive: root.interactive
                    showDecoration: 1
                    decorationHeight: priv.windowDecorationHeight
                    maximizeButtonShown: appDelegate.canBeMaximized
                    overlayShown: touchControls.overlayShown
                    width: implicitWidth
                    height: implicitHeight
                    highlightSize: windowInfoItem.iconMargin / 2
                    boundsItem: root.availableDesktopArea
                    panelState: root.panelState
                    altDragEnabled: root.mode == "windowed"
                    lightMode: root.lightMode
                    clipSurface: root.mode === "windowed"

                    requestedWidth: appDelegate.requestedWidth
                    requestedHeight: appDelegate.requestedHeight

                    onCloseClicked: { appDelegate.close(); }
                    onMaximizeClicked: {
                        if (appDelegate.canBeMaximized) {
                            appDelegate.anyMaximized ? appDelegate.requestRestore() : appDelegate.requestMaximize();
                        }
                    }
                    onMaximizeHorizontallyClicked: {
                        if (appDelegate.canBeMaximizedHorizontally) {
                            appDelegate.maximizedHorizontally ? appDelegate.requestRestore() : appDelegate.requestMaximizeHorizontally()
                        }
                    }
                    onMaximizeVerticallyClicked: {
                        if (appDelegate.canBeMaximizedVertically) {
                            appDelegate.maximizedVertically ? appDelegate.requestRestore() : appDelegate.requestMaximizeVertically()
                        }
                    }
                    onMinimizeClicked: { appDelegate.requestMinimize(); }
                    onDecorationPressed: { appDelegate.activate(); }
                    onDecorationReleased: fakeRectangle.visible ? fakeRectangle.commit() : appDelegate.updateRestoredGeometry()

                    property real angle: 0
                    Behavior on angle { enabled: priv.closingIndex >= 0; LomiriNumberAnimation {} }
                    property real itemScale: 1
                    Behavior on itemScale { enabled: priv.closingIndex >= 0; LomiriNumberAnimation {} }

                    transform: [
                        Scale {
                            origin.x: 0
                            origin.y: decoratedWindow.implicitHeight / 2
                            xScale: decoratedWindow.itemScale
                            yScale: decoratedWindow.itemScale
                        },
                        Rotation {
                            origin { x: 0; y: (decoratedWindow.height / 2) }
                            axis { x: 0; y: 1; z: 0 }
                            angle: decoratedWindow.angle
                        }
                    ]
                }

                OpacityMask {
                    id: opacityEffect
                    anchors.fill: decoratedWindow
                }

                WindowControlsOverlay {
                    id: touchControls
                    anchors.fill: appDelegate
                    target: appDelegate
                    resizeArea: resizeArea
                    enabled: false
                    visible: enabled
                    boundsItem: root.availableDesktopArea

                    onFakeMaximizeAnimationRequested: if (!appDelegate.maximized) fakeRectangle.maximize(amount, true)
                    onFakeMaximizeLeftAnimationRequested: if (!appDelegate.maximizedLeft) fakeRectangle.maximizeLeft(amount, true)
                    onFakeMaximizeRightAnimationRequested: if (!appDelegate.maximizedRight) fakeRectangle.maximizeRight(amount, true)
                    onFakeMaximizeTopLeftAnimationRequested: if (!appDelegate.maximizedTopLeft) fakeRectangle.maximizeTopLeft(amount, true);
                    onFakeMaximizeTopRightAnimationRequested: if (!appDelegate.maximizedTopRight) fakeRectangle.maximizeTopRight(amount, true);
                    onFakeMaximizeBottomLeftAnimationRequested: if (!appDelegate.maximizedBottomLeft) fakeRectangle.maximizeBottomLeft(amount, true);
                    onFakeMaximizeBottomRightAnimationRequested: if (!appDelegate.maximizedBottomRight) fakeRectangle.maximizeBottomRight(amount, true);
                    onStopFakeAnimation: fakeRectangle.stop();
                    onDragReleased: fakeRectangle.visible ? fakeRectangle.commit() : appDelegate.updateRestoredGeometry()
                }

                WindowedFullscreenPolicy {
                    id: windowedFullscreenPolicy
                }
                StagedFullscreenPolicy {
                    id: stagedFullscreenPolicy
                    active: root.mode == "staged" || root.mode == "stagedWithSideStage"
                    surface: model.window.surface
                }

                SpreadDelegateInputArea {
                    id: dragArea
                    objectName: "dragArea"
                    anchors.fill: decoratedWindow
                    enabled: false
                    closeable: true
                    stage: root
                    dragDelegate: fakeDragItem

                    onClicked: {
                        spreadItem.highlightedIndex = index;
                        if (distance == 0) {
                            priv.goneToSpread = false;
                        }
                    }
                    onClose: {
                        priv.closingIndex = index
                        appDelegate.close();
                    }
                }

                WindowInfoItem {
                    id: windowInfoItem
                    objectName: "windowInfoItem"
                    anchors { left: parent.left; top: decoratedWindow.bottom; topMargin: units.gu(1) }
                    title: model.application.name
                    iconSource: model.application.icon
                    height: spreadItem.appInfoHeight
                    opacity: 0
                    z: 1
                    visible: opacity > 0
                    maxWidth: {
                        var nextApp = appRepeater.itemAt(index + 1);
                        if (nextApp) {
                            return Math.max(iconHeight, nextApp.x - appDelegate.x - units.gu(1))
                        }
                        return appDelegate.width;
                    }

                    onClicked: {
                        spreadItem.highlightedIndex = index;
                        priv.goneToSpread = false;
                    }
                }

                MouseArea {
                    id: closeMouseArea
                    objectName: "closeMouseArea"
                    anchors { left: parent.left; top: parent.top; leftMargin: -height / 2; topMargin: -height / 2 + spreadMaths.closeIconOffset }
                    readonly property var mousePos: hoverMouseArea.mapToItem(appDelegate, hoverMouseArea.mouseX, hoverMouseArea.mouseY)
                    readonly property bool shown: dragArea.distance == 0
                             && index == spreadItem.highlightedIndex
                             && mousePos.y < (decoratedWindow.height / 3)
                             && mousePos.y > -units.gu(4)
                             && mousePos.x > -units.gu(4)
                             && mousePos.x < (decoratedWindow.width * 2 / 3)
                    opacity: shown ? 1 : 0
                    visible: opacity > 0
                    Behavior on opacity { LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration } }
                    height: units.gu(6)
                    width: height

                    onClicked: {
                        priv.closingIndex = index;
                        appDelegate.close();
                    }
                    Image {
                        id: closeImage
                        source: "graphics/window-close.svg"
                        anchors.fill: closeMouseArea
                        anchors.margins: units.gu(2)
                        sourceSize.width: width
                        sourceSize.height: height
                    }
                }

                Item {
                    // Group all child windows in this item so that we can fade them out together when going to the spread
                    // (and fade them in back again when returning from it)
                    readonly property bool stageOnProperState: root.state === "windowed"
                                                            || root.state === "staged"
                                                            || root.state === "stagedWithSideStage"

                    // TODO: Is it worth the extra cost of layering to avoid the opacity artifacts of intersecting children?
                    //       Btw, will involve more than uncommenting the line below as children won't necessarily fit this item's
                    //       geometry. This is just a reference.
                    //layer.enabled: opacity !== 0.0 && opacity !== 1.0

                    opacity: stageOnProperState ? 1.0 : 0.0
                    visible: opacity !== 0.0 // make it transparent to input as well
                    Behavior on opacity { LomiriNumberAnimation {} }

                    Repeater {
                        id: childWindowRepeater
                        model: appDelegate.surface ? appDelegate.surface.childSurfaceList : null

                        delegate: ChildWindowTree {
                            surface: model.surface

                            // Account for the displacement caused by window decoration in the top-level surface
                            // Ie, the top-level surface is not positioned at (0,0) of this ChildWindow's parent (appDelegate)
                            displacementX: appDelegate.clientAreaItem.x
                            displacementY: appDelegate.clientAreaItem.y

                            boundsItem: root.availableDesktopArea
                            decorationHeight: priv.windowDecorationHeight

                            z: childWindowRepeater.count - model.index

                            onFocusChanged: {
                                if (focus) {
                                    // some child surface in this tree got focus.
                                    // Ensure we also have it at the top-level hierarchy
                                    appDelegate.claimFocus();
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    FakeMaximizeDelegate {
        id: fakeRectangle
        target: priv.focusedAppDelegate
        leftMargin: root.availableDesktopArea.x
        appContainerWidth: appContainer.width
        appContainerHeight: appContainer.height
        panelState: root.panelState
    }

    WorkspaceSwitcher {
        id: workspaceSwitcher
        enabled: workspaceEnabled
        anchors.centerIn: parent
        height: units.gu(20)
        width: root.width - units.gu(8)
        background: root.background
        availableDesktopArea: root.availableDesktopArea
        onActiveChanged: {
            if (!active) {
                appContainer.focus = true;
            }
        }
    }

    PropertyAnimation {
        id: shortRightEdgeSwipeAnimation
        property: "x"
        to: 0
        duration: priv.animationDuration
    }

    SwipeArea {
        id: rightEdgeDragArea
        objectName: "rightEdgeDragArea"
        direction: Direction.Leftwards
        anchors { top: parent.top; right: parent.right; bottom: parent.bottom }
        width: root.dragAreaWidth
        enabled: root.spreadEnabled

        property var gesturePoints: []
        property bool cancelled: false

        property real progress: -touchPosition.x / root.width
        onProgressChanged: {
            if (dragging) {
                draggedProgress = progress;
            }
        }

        property real draggedProgress: 0

        onTouchPositionChanged: {
            gesturePoints.push(touchPosition.x);
            if (gesturePoints.length > 10) {
                gesturePoints.splice(0, gesturePoints.length - 10)
            }
        }

        onDraggingChanged: {
            if (dragging) {
                // A potential edge-drag gesture has started. Start recording it
                gesturePoints = [];
                cancelled = false;
                draggedProgress = 0;
            } else {
                // Ok. The user released. Did he drag far enough to go to full spread?
                if (gesturePoints[gesturePoints.length - 1] < -spreadItem.rightEdgeBreakPoint * spreadItem.width ) {

                    // He dragged far enough, but if the last movement was a flick to the right again, he wants to cancel the spread again.
                    var oneWayFlickToRight = true;
                    var smallestX = gesturePoints[0]-1;
                    for (var i = 0; i < gesturePoints.length; i++) {
                        if (gesturePoints[i] <= smallestX) {
                            oneWayFlickToRight = false;
                            break;
                        }
                        smallestX = gesturePoints[i];
                    }

                    if (!oneWayFlickToRight) {
                        // Ok, the user made it, let's go to spread!
                        priv.goneToSpread = true;
                    } else {
                        cancelled = true;
                    }
                } else {
                    // Ok, the user didn't drag far enough to cross the breakPoint
                    // Find out if it was a one-way movement to the left, in which case we just switch directly to next app.
                    var oneWayFlick = true;
                    var smallestX = rightEdgeDragArea.width;
                    for (var i = 0; i < gesturePoints.length; i++) {
                        if (gesturePoints[i] >= smallestX) {
                            oneWayFlick = false;
                            break;
                        }
                        smallestX = gesturePoints[i];
                    }

                    if (appRepeater.count > 1 &&
                            (oneWayFlick && rightEdgeDragArea.distance > units.gu(2) || rightEdgeDragArea.distance > spreadItem.rightEdgeBreakPoint * spreadItem.width)) {
                        var nextStage = appRepeater.itemAt(priv.nextInStack).stage
                        for (var i = 0; i < appRepeater.count; i++) {
                            if (i != priv.nextInStack && appRepeater.itemAt(i).stage == nextStage) {
                                appRepeater.itemAt(i).playHidingAnimation()
                                break;
                            }
                        }
                        appRepeater.itemAt(priv.nextInStack).playFocusAnimation()
                        if (appRepeater.itemAt(priv.nextInStack).stage == ApplicationInfoInterface.SideStage && !sideStage.shown) {
                            sideStage.show();
                        }

                    } else {
                        cancelled = true;
                    }

                    gesturePoints = [];
                }
            }
        }

        GestureAreaSizeHint {
            anchors.fill: parent
        }
    }

    TabletSideStageTouchGesture {
        id: triGestureArea
        objectName: "triGestureArea"
        anchors.fill: parent
        enabled: false
        property Item appDelegate

        dragComponent: dragComponent
        dragComponentProperties: { "appDelegate": appDelegate }

        onPressed: {
            function matchDelegate(obj) { return String(obj.objectName).indexOf("appDelegate") >= 0; }

            var delegateAtCenter = Functions.itemAt(appContainer, x, y, matchDelegate);
            if (!delegateAtCenter) return;

            appDelegate = delegateAtCenter;
        }

        onClicked: {
            priv.toggleSideStage()
        }

        onDragStarted: {
            // If we're dragging to the sidestage.
            if (!sideStage.shown) {
                sideStage.show();
            }
        }

        onDropped: {
            // Hide side stage if the app drag was cancelled
            if (!priv.sideStageDelegate) {
                sideStage.hide();
            }
        }

        Component {
            id: dragComponent
            SurfaceContainer {
                property Item appDelegate

                surface: appDelegate ? appDelegate.surface : null

                consumesInput: false
                interactive: false
                focus: false
                requestedWidth: appDelegate ? appDelegate.requestedWidth : 0
                requestedHeight: appDelegate ? appDelegate.requestedHeight : 0

                width: units.gu(40)
                height: units.gu(40)

                Drag.hotSpot.x: width/2
                Drag.hotSpot.y: height/2
                // only accept opposite stage.
                Drag.keys: {
                    if (!surface) return "Disabled";

                    if (appDelegate.stage === ApplicationInfo.MainStage) {
                        if (appDelegate.application.supportedOrientations
                                & (Qt.PortraitOrientation|Qt.InvertedPortraitOrientation)) {
                            return "MainStage";
                        }
                        return "Disabled";
                    }
                    return "SideStage";
                }
            }
        }
    }
}