Refactor: Move valid box order creation into own method

Move the valid box order creation into an own method.
This makes the `_orderTopBarItems` method cleaner and allows other
methods to easily create valid box orders as well in the future.
This commit is contained in:
Julian 2021-05-14 13:19:20 +02:00
parent 0f3e1608a9
commit 0fc62ae465
Signed by: julian
GPG Key ID: 094C2AC34192FA11

View File

@ -81,39 +81,10 @@ class Extension {
* orders. * orders.
*/ */
_orderTopBarItems() { _orderTopBarItems() {
// Load the configured box orders from settings.
const leftBoxOrder = this.settings.get_strv("left-box-order");
const centerBoxOrder = this.settings.get_strv("center-box-order");
const rightBoxOrder = this.settings.get_strv("right-box-order");
// Get the indicator containers (of the items) currently present in the
// Gnome Shell top bar.
const leftBoxIndicatorContainers = Main.panel._leftBox.get_children();
const centerBoxIndicatorContainers = Main.panel._centerBox.get_children();
const rightBoxIndicatorContainers = Main.panel._rightBox.get_children();
// Go through the box order and remove all items (or rather their
// roles), which aren't present in the top bar currently.
const getValidBoxOrder = (boxIndicatorContainers, boxOrder) => {
// Create a indicator containers set from the indicator containers
// for fast easy access.
const boxIndicatorContainersSet = new Set(boxIndicatorContainers);
let validBoxOrder = [ ];
for (const role of boxOrder) {
// Get the indicator container associated with the current role.
const associatedIndicatorContainer = Main.panel.statusArea[role]?.container;
if (boxIndicatorContainersSet.has(associatedIndicatorContainer)) validBoxOrder.push(role);
}
return validBoxOrder;
}
// Get valid box orders. // Get valid box orders.
const validLeftBoxOrder = getValidBoxOrder(leftBoxIndicatorContainers, leftBoxOrder); const validLeftBoxOrder = this._createValidBoxOrder("left");
const validCenterBoxOrder = getValidBoxOrder(centerBoxIndicatorContainers, centerBoxOrder); const validCenterBoxOrder = this._createValidBoxOrder("center");
const validRightBoxOrder = getValidBoxOrder(rightBoxIndicatorContainers, rightBoxOrder); const validRightBoxOrder = this._createValidBoxOrder("right");
// Go through the items (or rather their roles) of a box and order the // Go through the items (or rather their roles) of a box and order the
// box accordingly. // box accordingly.
@ -132,6 +103,54 @@ class Extension {
orderBox(validCenterBoxOrder, Main.panel._centerBox); orderBox(validCenterBoxOrder, Main.panel._centerBox);
orderBox(validRightBoxOrder, Main.panel._rightBox); orderBox(validRightBoxOrder, Main.panel._rightBox);
} }
/**
* This function creates a valid box order for the given box.
* @param {string} box - The box to return the valid box order for.
* Must be one of the following values:
* - "left"
* - "center"
* - "right"
* @returns {string[]} - The valid box order.
*/
_createValidBoxOrder(box) {
// Load the configured box order from settings and get the indicator
// containers (of the items) currently present in the Gnome Shell top
// bar.
let boxOrder;
let boxIndicatorContainers;
switch (box) {
case "left":
boxOrder = this.settings.get_strv("left-box-order");
boxIndicatorContainers = Main.panel._leftBox.get_children();
break;
case "center":
boxOrder = this.settings.get_strv("center-box-order");
boxIndicatorContainers = Main.panel._centerBox.get_children();
break;
case "right":
boxOrder = this.settings.get_strv("right-box-order");
boxIndicatorContainers = Main.panel._rightBox.get_children();
break;
}
// Create an indicator containers set from the indicator containers for
// fast easy access.
const boxIndicatorContainersSet = new Set(boxIndicatorContainers);
// Go through the box order and only add items to the valid box order,
// where their indicator is present in the Gnome Shell top bar
// currently.
let validBoxOrder = [ ];
for (const role of boxOrder) {
// Get the indicator container associated with the current role.
const associatedIndicatorContainer = Main.panel.statusArea[role]?.container;
if (boxIndicatorContainersSet.has(associatedIndicatorContainer)) validBoxOrder.push(role);
}
return validBoxOrder;
}
} }
function init() { function init() {