From efba5372568325d6e7a512d11058c6ca15260986 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Samuel=20R=C3=B8dal?= Date: Wed, 16 Mar 2011 09:35:54 +0100 Subject: [PATCH] Better layouting behavior when window is removed. --- .../qml-compositor/qml/QmlCompositor/Window.qml | 1 + .../qml-compositor/qml/QmlCompositor/compositor.js | 70 ++++++++++++++++++++++ examples/qml-compositor/qml/QmlCompositor/main.qml | 19 +----- 3 files changed, 74 insertions(+), 16 deletions(-) diff --git a/examples/qml-compositor/qml/QmlCompositor/Window.qml b/examples/qml-compositor/qml/QmlCompositor/Window.qml index 3068836..81cffcc 100644 --- a/examples/qml-compositor/qml/QmlCompositor/Window.qml +++ b/examples/qml-compositor/qml/QmlCompositor/Window.qml @@ -49,6 +49,7 @@ Rectangle { property variant child: null; property bool animationsEnabled: false; + property int index; Behavior on x { enabled: container.animationsEnabled; diff --git a/examples/qml-compositor/qml/QmlCompositor/compositor.js b/examples/qml-compositor/qml/QmlCompositor/compositor.js index d74d790..456527e 100644 --- a/examples/qml-compositor/qml/QmlCompositor/compositor.js +++ b/examples/qml-compositor/qml/QmlCompositor/compositor.js @@ -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(); +} diff --git a/examples/qml-compositor/qml/QmlCompositor/main.qml b/examples/qml-compositor/qml/QmlCompositor/main.qml index ece3e50..3a3f9e5 100644 --- a/examples/qml-compositor/qml/QmlCompositor/main.qml +++ b/examples/qml-compositor/qml/QmlCompositor/main.qml @@ -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(); } } -- 2.7.4