From 4faf9761555912cddeb948befcb9e9277ded0a1a Mon Sep 17 00:00:00 2001 From: Julian Schacher Date: Mon, 17 May 2021 19:44:00 +0200 Subject: [PATCH] Update: Handle permutations of outdated box orders getting saved better --- src/extension.js | 57 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/src/extension.js b/src/extension.js index 40cdf81..a70256e 100644 --- a/src/extension.js +++ b/src/extension.js @@ -45,6 +45,17 @@ class Extension { const addConfiguredBoxOrderChangeHandler = (box) => { let handlerId = this.settings.connect(`changed::${box}-box-order`, () => { this._orderTopBarItems(box); + + /// For the case, where the currently saved box order is based + /// on a permutation of an outdated box order, get an updated + /// box order and save it, if needed. + const updatedBoxOrder = this._createUpdatedBoxOrder(box); + // Only save the updated box order to settings, if it is + // different, to avoid looping. + const currentBoxOrder = this.settings.get_strv(`${box}-box-order`); + if (JSON.stringify(currentBoxOrder) !== JSON.stringify(updatedBoxOrder)) { + this.settings.set_strv(`${box}-box-order`, updatedBoxOrder); + } }); this._settingsHandlerIds.push(handlerId); }; @@ -394,16 +405,46 @@ class Extension { break; } - // Go through the items (or rather their roles) of the validBoxOrder and - // order the panelBox accordingly. - for (let i = 0; i < validBoxOrder.length; i++) { - const role = validBoxOrder[i]; - // Get the indicator container associated with the current role. - const associatedIndicatorContainer = Main.panel.statusArea[role].container; + /// Go through the items (or rather their roles) of the validBoxOrder + /// and order the panelBox accordingly. + // Declare panelBoxChildCount here, because we might need it later. + let panelBoxChildCount; + switch (box) { + // If the left or center box is the target box, order form left to + // right. + case "left": + case "center": + for (let i = 0; i < validBoxOrder.length; i++) { + const role = validBoxOrder[i]; + // Get the indicator container associated with the current + // role. + const associatedIndicatorContainer = Main.panel.statusArea[role].container; - associatedIndicatorContainer.get_parent().remove_child(associatedIndicatorContainer); - panelBox.insert_child_at_index(associatedIndicatorContainer, i); + associatedIndicatorContainer.get_parent().remove_child(associatedIndicatorContainer); + panelBox.insert_child_at_index(associatedIndicatorContainer, i); + } + break; + // If the right box is the target box, order from right to left. + // The order direction is important for the case, where the box + // order got set to a box order, which doesn't include all the roles + // to cover all items of the respective box. + // This could happen, when the box order gets set to a permutation + // of an outdated box order. + case "right": + panelBoxChildCount = panelBox.get_children().length; + for (let i = 0; i < validBoxOrder.length; i++) { + const role = validBoxOrder[validBoxOrder.length - 1 - i]; + // Get the indicator container associated with the current role. + const associatedIndicatorContainer = Main.panel.statusArea[role].container; + + associatedIndicatorContainer.get_parent().remove_child(associatedIndicatorContainer); + panelBox.insert_child_at_index(associatedIndicatorContainer, panelBoxChildCount - 1 -i); + } + break; } + // To handle the case, where the box order got set to a permutation + // of an outdated box order, it would be wise, if the caller updated the + // box order now to include the items present in the top bar. } /**