From f8488a97f4c1c2c4a4752e40ecdbf65fc82abf39 Mon Sep 17 00:00:00 2001 From: Julian Schacher Date: Thu, 13 May 2021 14:16:35 +0200 Subject: [PATCH] New: Add new top bar items to box orders on enable Introduce box orders, which allow this extension to save the order top bar items should be in. On extension enable, save new top bar items to those box orders. --- src/extension.js | 65 +++++++++++++++++++ src/metadata.json | 3 +- ...l.extensions.top-bar-organizer.gschema.xml | 17 +++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/schemas/org.gnome.shell.extensions.top-bar-organizer.gschema.xml diff --git a/src/extension.js b/src/extension.js index 37b175d..f64409d 100644 --- a/src/extension.js +++ b/src/extension.js @@ -1,14 +1,79 @@ "use strict"; +const ExtensionUtils = imports.misc.extensionUtils; + +const Main = imports.ui.main; + class Extension { constructor() { + this.settings = ExtensionUtils.getSettings(); } enable() { + this._addNewItemsToBoxOrders(); } disable() { } + + /** + * This method adds all new items currently present in the Gnome Shell top + * 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); + + // 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); + } } function init() { diff --git a/src/metadata.json b/src/metadata.json index 824c3cd..2e94822 100644 --- a/src/metadata.json +++ b/src/metadata.json @@ -3,5 +3,6 @@ "name": "Top Bar Organizer", "description": "Organize the items of the top (menu)bar.", "version": 1, - "shell-version": [ "40" ] + "shell-version": [ "40" ], + "settings-schema": "org.gnome.shell.extensions.top-bar-organizer" } diff --git a/src/schemas/org.gnome.shell.extensions.top-bar-organizer.gschema.xml b/src/schemas/org.gnome.shell.extensions.top-bar-organizer.gschema.xml new file mode 100644 index 0000000..24e17fe --- /dev/null +++ b/src/schemas/org.gnome.shell.extensions.top-bar-organizer.gschema.xml @@ -0,0 +1,17 @@ + + + + + Order of items in the left box of the top bar. + [] + + + Order of items in the center box of the top bar. + [] + + + Order of items in the right box of the top bar. + [] + + +