diff --git a/src/extension.js b/src/extension.js index 24dce9c..40cdf81 100644 --- a/src/extension.js +++ b/src/extension.js @@ -73,64 +73,9 @@ class Extension { * bar to the box orders. */ _addNewItemsToBoxOrders() { - // Load the configured box orders from settings. - let leftBoxOrder = this.settings.get_strv("left-box-order"); - let centerBoxOrder = this.settings.get_strv("center-box-order"); - let rightBoxOrder = this.settings.get_strv("right-box-order"); - - // Get items (or rather their roles) currently present in the Gnome - // Shell top bar and index them using their associated indicator - // container. - let indicatorContainerRoleMap = new Map(); - for (const role in Main.panel.statusArea) { - indicatorContainerRoleMap.set(Main.panel.statusArea[role].container, role); - } - - // 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(); - // Reverse this array, since the items in the left and center box are - // logically LTR, while the items in the right box are RTL. - const rightBoxIndicatorContainers = Main.panel._rightBox.get_children().reverse(); - - // Go through the items (or rather their indicator containers) of each - // box and add new items (or rather their roles) to the box orders. - const addNewRolesToBoxOrder = (boxIndicatorContainers, boxOrder, atToBeginning = false) => { - // Create a box order set from the box order for fast easy access. - const boxOrderSet = new Set(boxOrder); - - for (const indicatorContainer of boxIndicatorContainers) { - // First get the role associated with the current indicator - // container. - const associatedRole = indicatorContainerRoleMap.get(indicatorContainer); - - // Handle an AppIndicator/KStatusNotifierItem item differently. - if (associatedRole.startsWith("appindicator-")) { - this._handleAppIndicatorKStatusNotifierItemItem(indicatorContainer, associatedRole, boxOrder, atToBeginning); - continue; - } - - // Add the role to the box order, if it isn't in it already. - if (!boxOrderSet.has(associatedRole)) { - if (atToBeginning) { - boxOrder.unshift(associatedRole); - } else { - boxOrder.push(associatedRole); - } - } - } - }; - - // Add new items (or rather their roles) to the box orders and save - // them. - addNewRolesToBoxOrder(leftBoxIndicatorContainers, leftBoxOrder); - addNewRolesToBoxOrder(centerBoxIndicatorContainers, centerBoxOrder); - // Add the items to the beginning for this array, since its RTL. - addNewRolesToBoxOrder(rightBoxIndicatorContainers, rightBoxOrder, true); - this.settings.set_strv("left-box-order", leftBoxOrder); - this.settings.set_strv("center-box-order", centerBoxOrder); - this.settings.set_strv("right-box-order", rightBoxOrder); + this.settings.set_strv("left-box-order", this._createUpdatedBoxOrder("left")); + this.settings.set_strv("center-box-order", this._createUpdatedBoxOrder("center")); + this.settings.set_strv("right-box-order", this._createUpdatedBoxOrder("right")); } /** @@ -263,6 +208,73 @@ class Extension { /// Helper methods holding logic needed by other methods. /// //////////////////////////////////////////////////////////////////////////// + /** + * This method adds all new items currently present in the specified Gnome + * Shell top bar box to a box order and returns that new box order. + * @param {string} box - The box to create the updated box order for. + * @returns {string[]} - The updated box order. + */ + _createUpdatedBoxOrder(box) { + // Load the configured box order from settings. + const boxOrder = this.settings.get_strv(`${box}-box-order`); + + // Get items (or rather their roles) currently present in the Gnome + // Shell top bar and index them using their associated indicator + // container. + let indicatorContainerRoleMap = new Map(); + for (const role in Main.panel.statusArea) { + indicatorContainerRoleMap.set(Main.panel.statusArea[role].container, role); + } + + // Get the indicator containers (of the items) currently present in the + // Gnome Shell top bar box. + let boxIndicatorContainers; + switch (box) { + case "left": + boxIndicatorContainers = Main.panel._leftBox.get_children(); + break; + case "center": + boxIndicatorContainers = Main.panel._centerBox.get_children(); + break; + case "right": + // Reverse this array, since the items in the left and center + // box are logically LTR, while the items in the right box are + // RTL. + boxIndicatorContainers = Main.panel._rightBox.get_children().reverse(); + break; + } + + /// Go through the items (or rather their indicator containers) of the + /// box and add new items (or rather their roles) to the box order. + // Create a box order set from the box order for fast easy access. + const boxOrderSet = new Set(boxOrder); + + for (const indicatorContainer of boxIndicatorContainers) { + // First get the role associated with the current indicator + // container. + const associatedRole = indicatorContainerRoleMap.get(indicatorContainer); + + // Handle an AppIndicator/KStatusNotifierItem item differently. + if (associatedRole.startsWith("appindicator-")) { + this._handleAppIndicatorKStatusNotifierItemItem(indicatorContainer, associatedRole, boxOrder, box === "right"); + continue; + } + + // Add the role to the box order, if it isn't in it already. + if (!boxOrderSet.has(associatedRole)) { + if (box === "right") { + // Add the items to the beginning for this array, since + // its RTL. + boxOrder.unshift(associatedRole); + } else { + boxOrder.push(associatedRole); + } + } + } + + return boxOrder; + } + /** * This function creates a valid box order for the given box. * This means it returns a box order for the box, where only roles are @@ -423,8 +435,8 @@ class Extension { let roles = this._applicationRoleMap.get(application); if (roles) { // If the application already has an array of associated roles, just - // add the role to it. - roles.push(role); + // add the role to it, if needed. + if (!roles.includes(role)) roles.push(role); } else { // Otherwise create a new array. this._applicationRoleMap.set(application, [ role ]);