From 572c6b69c1bf698d4302cb31923fa36fa9c2f473 Mon Sep 17 00:00:00 2001 From: Julian Schacher Date: Mon, 17 May 2021 11:38:17 +0200 Subject: [PATCH] Refactor: Group "private" methods Group "private" methods to make their purpose clearer. --- src/extension.js | 172 +++++++++++++++++++++++++---------------------- 1 file changed, 90 insertions(+), 82 deletions(-) diff --git a/src/extension.js b/src/extension.js index 242a32e..6840bd0 100644 --- a/src/extension.js +++ b/src/extension.js @@ -40,6 +40,10 @@ class Extension { } } + //////////////////////////////////////////////////////////////////////////// + /// Methods used on extension enable. /// + //////////////////////////////////////////////////////////////////////////// + /** * This method adds all new items currently present in the Gnome Shell top * bar to the box orders. @@ -109,88 +113,6 @@ class Extension { 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 * 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); } } + + //////////////////////////////////////////////////////////////////////////// + /// 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() {