Refactor: Group "private" methods

Group "private" methods to make their purpose clearer.
This commit is contained in:
Julian 2021-05-17 11:38:17 +02:00
parent a73ed96bda
commit 572c6b69c1
Signed by: julian
GPG Key ID: 094C2AC34192FA11

View File

@ -40,6 +40,10 @@ class Extension {
} }
} }
////////////////////////////////////////////////////////////////////////////
/// Methods used on extension enable. ///
////////////////////////////////////////////////////////////////////////////
/** /**
* This method adds all new items currently present in the Gnome Shell top * This method adds all new items currently present in the Gnome Shell top
* bar to the box orders. * bar to the box orders.
@ -109,88 +113,6 @@ class Extension {
this._orderTopBarItems("right"); this._orderTopBarItems("right");
} }
/**
* This method orders the top bar items of the specified box according to
* the configured box orders.
* @param {string} box - The box to order.
*/
_orderTopBarItems(box) {
// Get the valid box order.
const validBoxOrder = this._createValidBoxOrder(box);
// Get the relevant box of `Main.panel`.
let panelBox;
switch (box) {
case "left":
panelBox = Main.panel._leftBox;
break;
case "center":
panelBox = Main.panel._centerBox;
break;
case "right":
panelBox = Main.panel._rightBox;
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;
panelBox.set_child_at_index(associatedIndicatorContainer, i);
}
}
/**
* 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;
}
/** /**
* Overwrite `Panel._addToPanelBox` with a custom method, which handles top * Overwrite `Panel._addToPanelBox` with a custom method, which handles top
* bar item additions to make sure that they are added in the correct * bar item additions to make sure that they are added in the correct
@ -295,6 +217,92 @@ class Extension {
this._originalAddToPanelBox(role, indicator, positionOverwrite, box); this._originalAddToPanelBox(role, indicator, positionOverwrite, box);
} }
} }
////////////////////////////////////////////////////////////////////////////
/// Helper methods holding logic needed by other methods. ///
////////////////////////////////////////////////////////////////////////////
/**
* 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;
}
/**
* This method orders the top bar items of the specified box according to
* the configured box orders.
* @param {string} box - The box to order.
*/
_orderTopBarItems(box) {
// Get the valid box order.
const validBoxOrder = this._createValidBoxOrder(box);
// Get the relevant box of `Main.panel`.
let panelBox;
switch (box) {
case "left":
panelBox = Main.panel._leftBox;
break;
case "center":
panelBox = Main.panel._centerBox;
break;
case "right":
panelBox = Main.panel._rightBox;
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;
panelBox.set_child_at_index(associatedIndicatorContainer, i);
}
}
} }
function init() { function init() {