Better layouting behavior when window is removed.
authorSamuel Rødal <samuel.rodal@nokia.com>
Wed, 16 Mar 2011 08:35:54 +0000 (09:35 +0100)
committerSamuel Rødal <samuel.rodal@nokia.com>
Wed, 16 Mar 2011 08:35:54 +0000 (09:35 +0100)
examples/qml-compositor/qml/QmlCompositor/Window.qml
examples/qml-compositor/qml/QmlCompositor/compositor.js
examples/qml-compositor/qml/QmlCompositor/main.qml

index 3068836..81cffcc 100644 (file)
@@ -49,6 +49,7 @@ Rectangle {
 
     property variant child: null;
     property bool animationsEnabled: false;
+    property int index;
 
     Behavior on x {
         enabled: container.animationsEnabled;
index d74d790..456527e 100644 (file)
@@ -39,6 +39,7 @@
 ****************************************************************************/
 
 var windowList = null;
+var indexes = null;
 
 function relayout() {
     if (windowList.length == 0)
@@ -55,6 +56,8 @@ function relayout() {
     var iy = 0;
     var lastDim = 1;
 
+    indexes = new Array(dim * dim);
+
     for (i = 0; i < windowList.length; ++i) {
         if (i > 0) {
             var currentDim = Math.ceil(Math.sqrt(i + 1));
@@ -73,6 +76,9 @@ function relayout() {
             lastDim = currentDim;
         }
 
+        indexes[iy * dim + ix] = i;
+        windowList[i].index = iy * dim + ix;
+
         var cx = (ix + 0.5) * w;
         var cy = (iy + 0.5) * h;
 
@@ -83,3 +89,67 @@ function relayout() {
     }
 }
 
+function addWindow(window)
+{
+    if (windowList == null)
+        windowList = new Array(0);
+
+    windowList.push(window);
+    relayout();
+}
+
+function removeWindow(window)
+{
+    var i;
+    for (i = 0; i < windowList.length; ++i) {
+        if (windowList[i] == window)
+            break;
+    }
+
+    var index = windowList[i].index;
+    var dim = Math.ceil(Math.sqrt(windowList.length));
+    var maxY = Math.floor((windowList.length-1) / dim);
+
+    var shrinking = Math.ceil(Math.sqrt(windowList.length - 1)) != dim;
+
+    while (true) {
+        var ix = index % dim;
+        var iy = Math.floor(index / dim);
+
+        console.log("index: " + ix + " " + iy);
+
+        if (shrinking) {
+            if (iy > 0)
+                --iy;
+            else if (++ix == dim)
+                break;
+        } else {
+            if (iy < maxY) {
+                if (ix > 0)
+                    --ix;
+                else
+                    ++iy;
+            } else {
+                ++ix;
+            }
+        }
+
+        var next = iy * dim + ix;
+
+        var currentIndex = indexes[index];
+        var nextIndex = indexes[next];
+
+        if (nextIndex == null)
+            break;
+
+        var temp = windowList[currentIndex];
+        windowList[currentIndex] = windowList[nextIndex];
+        windowList[currentIndex].index = currentIndex;
+        windowList[nextIndex] = temp;
+
+        index = next;
+    }
+
+    windowList.splice(indexes[index], 1);
+    relayout();
+}
index ece3e50..3a3f9e5 100644 (file)
@@ -64,28 +64,15 @@ Rectangle {
         windowContainer.height = window.height;
         windowContainer.child = window;
 
-        if (CompositorLogic.windowList == null)
-            CompositorLogic.windowList = new Array(0);
-
-        CompositorLogic.windowList.push(windowContainer);
-        CompositorLogic.relayout();
+        CompositorLogic.addWindow(windowContainer);
 
         windowContainer.opacity = 1
         windowContainer.animationsEnabled = true;
     }
 
     function windowDestroyed(window) {
-        var i;
-        for (i = 0; i < CompositorLogic.windowList.length; ++i) {
-            if (CompositorLogic.windowList[i].child == window)
-                break;
-        }
-
-        var container = CompositorLogic.windowList[i];
-
-        CompositorLogic.windowList.splice(i, 1);
-        CompositorLogic.relayout();
+        CompositorLogic.removeWindow(window.parent);
 
-        container.destroy();
+        window.parent.destroy();
     }
 }