on xenial help sign in
Move code into a separate object to reduce clutter in Stage.qml
commit2ad40618cac0afc41ac18e8528a60a221ea350a7 authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateWed 23 Nov 2016, 10:28 (10 years ago) parent1d037c5
2 files changed+55−31
qml/Stage/Stage.qml+9−31 qml/Stage/WindowStateSaver.qml+46
qml/Stage/Stage.qml+9−31
@@ -848,35 +848,13 @@ FocusScope {
848848 }
849849 }
850850
851− function loadWindowState() {
852− var defaultWidth = units.gu(60);
853− var defaultHeight = units.gu(50);
854− var windowGeometry = WindowStateStorage.getGeometry(appDelegate.appId,
855− Qt.rect(appDelegate.windowedX, appDelegate.windowedY, defaultWidth, defaultHeight));
856−
857− appDelegate.windowedWidth = Qt.binding(function() { return Math.min(Math.max(windowGeometry.width, appDelegate.minimumWidth), appContainer.width - root.leftMargin); });
858− appDelegate.windowedHeight = Qt.binding(function() { return Math.min(Math.max(windowGeometry.height, appDelegate.minimumHeight),
859− appContainer.height - (appDelegate.fullscreen ? 0 : PanelState.panelHeight)); });
860− appDelegate.windowedX = Qt.binding(function() { return Math.max(Math.min(windowGeometry.x, appContainer.width - root.leftMargin - appDelegate.windowedWidth),
861− (appDelegate.fullscreen ? 0 : root.leftMargin)); });
862− appDelegate.windowedY = Qt.binding(function() { return Math.max(Math.min(windowGeometry.y, appContainer.height - appDelegate.windowedHeight), PanelState.panelHeight); });
863−
864− var windowState = WindowStateStorage.getState(appDelegate.appId, WindowStateStorage.WindowStateNormal)
865− appDelegate.restore(false /* animated */, windowState);
866−
867− // initialize the x/y to restore to
868− appDelegate.restoredX = windowGeometry.x;
869− appDelegate.restoredY = windowGeometry.y;
870− }
871− function saveWindowState() {
872− var state = appDelegate.windowState;
873− if (state === WindowStateStorage.WindowStateRestored) {
874− state = WindowStateStorage.WindowStateNormal;
875− }
876−
877− WindowStateStorage.saveState(appDelegate.appId, state & ~WindowStateStorage.WindowStateMinimized); // clear the minimized bit when saving
878− WindowStateStorage.saveGeometry(appDelegate.appId, Qt.rect(appDelegate.windowedX, appDelegate.windowedY,
879− appDelegate.windowedWidth, appDelegate.windowedHeight));
851+ WindowStateSaver {
852+ id: windowStateSaver
853+ target: appDelegate
854+ screenWidth: appContainer.width
855+ screenHeight: appContainer.height
856+ leftMargin: root.leftMargin
857+ minimumY: PanelState.panelHeight
880858 }
881859
882860 Component.onCompleted: {
@@ -890,7 +868,7 @@ FocusScope {
890868 windowedX = priv.focusedAppDelegate ? priv.focusedAppDelegate.windowedX + units.gu(3) : (normalZ - 1) * units.gu(3)
891869 windowedY = priv.focusedAppDelegate ? priv.focusedAppDelegate.windowedY + units.gu(3) : normalZ * units.gu(3)
892870 // Now load any saved state. This needs to happen *after* the cascading!
893− loadWindowState();
871+ windowStateSaver.load();
894872 resizeArea.init();
895873
896874 // NB: We're differentiating if this delegate was created in response to a new entry in the model
@@ -908,7 +886,7 @@ FocusScope {
908886 _constructing = false;
909887 }
910888 Component.onDestruction: {
911− saveWindowState();
889+ windowStateSaver.save();
912890
913891 if (!root.parent) {
914892 // This stage is about to be destroyed. Don't mess up with the model at this point
qml/Stage/WindowStateSaver.qmlnew file+46
@@ -0,0 +1,46 @@
1+import QtQuick 2.4
2+import Ubuntu.Components 1.3
3+import Utils 0.1
4+
5+QtObject {
6+ id: root
7+
8+ // set from outside
9+ property var target
10+ property int screenWidth: 0
11+ property int screenHeight: 0
12+ property int leftMargin: 0
13+ property int minimumY: 0
14+
15+ function load() {
16+ var defaultWidth = units.gu(60);
17+ var defaultHeight = units.gu(50);
18+ var windowGeometry = WindowStateStorage.getGeometry(target.appId,
19+ Qt.rect(target.windowedX, target.windowedY, defaultWidth, defaultHeight));
20+
21+ target.windowedWidth = Qt.binding(function() { return Math.min(Math.max(windowGeometry.width, target.minimumWidth), screenWidth - root.leftMargin); });
22+ target.windowedHeight = Qt.binding(function() { return Math.min(Math.max(windowGeometry.height, target.minimumHeight),
23+ screenHeight - (target.fullscreen ? 0 : minimumY)); });
24+ target.windowedX = Qt.binding(function() { return Math.max(Math.min(windowGeometry.x, screenWidth - root.leftMargin - target.windowedWidth),
25+ (target.fullscreen ? 0 : root.leftMargin)); });
26+ target.windowedY = Qt.binding(function() { return Math.max(Math.min(windowGeometry.y, screenHeight - target.windowedHeight), minimumY); });
27+
28+ var windowState = WindowStateStorage.getState(target.appId, WindowStateStorage.WindowStateNormal)
29+ target.restore(false /* animated */, windowState);
30+
31+ // initialize the x/y to restore to
32+ target.restoredX = windowGeometry.x;
33+ target.restoredY = windowGeometry.y;
34+ }
35+
36+ function save() {
37+ var state = target.windowState;
38+ if (state === WindowStateStorage.WindowStateRestored) {
39+ state = WindowStateStorage.WindowStateNormal;
40+ }
41+
42+ WindowStateStorage.saveState(target.appId, state & ~WindowStateStorage.WindowStateMinimized); // clear the minimized bit when saving
43+ WindowStateStorage.saveGeometry(target.appId, Qt.rect(target.windowedX, target.windowedY,
44+ target.windowedWidth, target.windowedHeight));
45+ }
46+}