Create pages and store in global array, makes sure it will not be cleared by garbage collector whe out of scope

This commit is contained in:
vladest 2017-12-18 14:56:33 +01:00
parent 62a762b879
commit 38453afb3b

View file

@ -17,12 +17,13 @@ Item {
readonly property int buttonsColumnsOnPage: 3 readonly property int buttonsColumnsOnPage: 3
property var currentGridItems: null property var currentGridItems: null
property var gridPages: [];
focus: true focus: true
Timer { Timer {
id: gridsRecreateTimer id: gridsRecreateTimer
interval: 50 interval: 1
repeat: false repeat: false
onTriggered: { onTriggered: {
doRecreateGrids() doRecreateGrids()
@ -32,34 +33,53 @@ Item {
//fake invisible item for initial buttons creations //fake invisible item for initial buttons creations
Item { Item {
id: fakeParent id: fakeParent
visible: parent visible: false
} }
function doRecreateGrids() { function doRecreateGrids() {
var tabletProxy = Tablet.getTablet("com.highfidelity.interface.tablet.system"); var tabletProxy = Tablet.getTablet("com.highfidelity.interface.tablet.system");
var tabletButtons = tabletProxy.getButtonsList(); var tabletButtons = tabletProxy.getButtonsList();
for (var i = 0; i < swipeView.count; i++) {
var gridOuter = swipeView.itemAt(i); if (tabletButtons.length <= 0) {
gridOuter.destroy(); console.warn("Tablet buttons list is empty");
return;
}
for (var i = swipeView.count - 1; i >= 0; i--) {
swipeView.removeItem(i); swipeView.removeItem(i);
} }
gridPages = [];
swipeView.setCurrentIndex(-1);
sortButtons(tabletButtons); sortButtons(tabletButtons);
var pagesNum = Math.ceil(tabletButtons.length / 12);
var currentPage = 0;
gridPages.push(pageComponent.createObject(null));
for (var buttonIndex = 0; buttonIndex < tabletButtons.length; buttonIndex++) { for (var buttonIndex = 0; buttonIndex < tabletButtons.length; buttonIndex++) {
var button = tabletButtons[buttonIndex] var button = tabletButtons[buttonIndex]
if (button !== null) { if (button !== null) {
var grid = swipeView.itemAt(swipeView.count - 1); var grid = gridPages[currentPage];
if (grid === null || grid.children[0].children.length === buttonsOnPage) { if (grid.children[0].children.length === buttonsOnPage) {
grid = pageComponent.createObject(swipeView); gridPages.push(pageComponent.createObject(null));
currentPage = currentPage + 1;
grid = gridPages[currentPage];
} }
button.row = Math.floor(grid.children[0].children.length / buttonsColumnsOnPage); button.row = Math.floor(grid.children[0].children.length / buttonsColumnsOnPage);
button.column = grid.children[0].children.length % buttonsColumnsOnPage; button.column = grid.children[0].children.length % buttonsColumnsOnPage;
//reparent to actual grid //reparent to actual grid
button.parent = grid.children[0]; button.parent = grid.children[0];
grid.children[0].children.push(button); grid.children[0].children.push(button);
} else {
console.warn("Button is null:", buttonIndex);
} }
} }
swipeView.currentIndex = 0; for (var pageIndex = 0; pageIndex < gridPages.length; pageIndex++) {
swipeView.addItem(gridPages[pageIndex]);
}
swipeView.setCurrentIndex(0);
} }
// used to look up a button by its uuid // used to look up a button by its uuid
@ -244,9 +264,10 @@ Item {
SwipeView { SwipeView {
id: swipeView id: swipeView
clip: false clip: false
currentIndex: -1
property int previousIndex: -1 property int previousIndex: -1
onCurrentItemChanged: { onCurrentIndexChanged: {
if (swipeView.currentIndex < 0 if (swipeView.currentIndex < 0
|| swipeView.itemAt(swipeView.currentIndex) === null || swipeView.itemAt(swipeView.currentIndex) === null
|| swipeView.itemAt(swipeView.currentIndex).children[0] === null || swipeView.itemAt(swipeView.currentIndex).children[0] === null