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.
This commit is contained in:
Julian 2021-05-13 14:16:35 +02:00
parent 2851fe3aaa
commit f8488a97f4
Signed by: julian
GPG Key ID: 094C2AC34192FA11
3 changed files with 84 additions and 1 deletions

View File

@ -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() {

View File

@ -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"
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema id="org.gnome.shell.extensions.top-bar-organizer" path="/org/gnome/shell/extensions/top-bar-organizer/">
<key name="left-box-order" type="as">
<description>Order of items in the left box of the top bar.</description>
<default>[]</default>
</key>
<key name="center-box-order" type="as">
<description>Order of items in the center box of the top bar.</description>
<default>[]</default>
</key>
<key name="right-box-order" type="as">
<description>Order of items in the right box of the top bar.</description>
<default>[]</default>
</key>
</schema>
</schemalist>