Refactor: Move logic for creation of special box orders into own class

Move the logic for the creation of special box orders
(`createValidBoxOrder` and `createRestrictedValidBoxOrder`) into an own
class.
Moving this logic into an own class makes the `Extension` class lighter.
This commit is contained in:
Julian 2021-05-27 06:44:36 +02:00
parent 158a4b584e
commit e69997b487
Signed by: julian
GPG Key ID: 094C2AC34192FA11
2 changed files with 147 additions and 101 deletions

View File

@ -26,6 +26,7 @@ const Main = imports.ui.main;
const Panel = imports.ui.panel;
const AppIndicatorKStatusNotifierItemManager = Me.imports.extensionModules.AppIndicatorKStatusNotifierItemManager;
const BoxOrderCreator = Me.imports.extensionModules.BoxOrderCreator;
class Extension {
constructor() {
@ -36,6 +37,10 @@ class Extension {
// Create an instance of AppIndicatorKStatusNotifierItemManager to handle AppIndicator/KStatusNotifierItem items.
this._appIndicatorKStatusNotifierItemManager = new AppIndicatorKStatusNotifierItemManager.AppIndicatorKStatusNotifierItemManager();
// Create an instance of BoxOrderCreator for the creation of special box
// orders.
this._boxOrderCreator = new BoxOrderCreator.BoxOrderCreator(this._appIndicatorKStatusNotifierItemManager);
this._addNewItemsToBoxOrders();
this._orderTopBarItemsOfAllBoxes();
this._overwritePanelAddToPanelBox();
@ -170,7 +175,7 @@ class Extension {
right: this._appIndicatorKStatusNotifierItemManager.createResolvedBoxOrder(this.settings.get_strv("right-box-order")),
};
// Also get the restricted valid box order of the target box.
const restrictedValidBoxOrderOfTargetBox = this._createRestrictedValidBoxOrder(box);
const restrictedValidBoxOrderOfTargetBox = this._boxOrderCreator.createRestrictedValidBoxOrder(box);
// Get the index of the role for each box order.
const indices = {
@ -239,21 +244,21 @@ class Extension {
if (indices.left !== -1) {
return {
position: determineInsertionIndex(indices.left, this._createRestrictedValidBoxOrder("left"), boxOrders.left),
position: determineInsertionIndex(indices.left, this._boxOrderCreator.createRestrictedValidBoxOrder("left"), boxOrders.left),
box: "left"
};
}
if (indices.center !== -1) {
return {
position: determineInsertionIndex(indices.center, this._createRestrictedValidBoxOrder("center"), boxOrders.center),
position: determineInsertionIndex(indices.center, this._boxOrderCreator.createRestrictedValidBoxOrder("center"), boxOrders.center),
box: "center"
};
}
if (indices.right !== -1) {
return {
position: determineInsertionIndex(indices.right, this._createRestrictedValidBoxOrder("right"), boxOrders.right),
position: determineInsertionIndex(indices.right, this._boxOrderCreator.createRestrictedValidBoxOrder("right"), boxOrders.right),
box: "right"
};
}
@ -373,102 +378,6 @@ class Extension {
return boxOrders;
}
/**
* 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
* included, which have their associated indicator container already in some
* box of the Gnome Shell top bar.
* @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) {
// Get a resolved box order.
let boxOrder = this._appIndicatorKStatusNotifierItemManager.createResolvedBoxOrder(this.settings.get_strv(`${box}-box-order`));
// Get the indicator containers (of the items) currently present in the
// Gnome Shell top bar.
const boxIndicatorContainers = [ ];
const addIndicatorContainersOfBox = (panelBox) => {
for (const indicatorContainer of panelBox.get_children()) {
boxIndicatorContainers.push(indicatorContainer);
}
};
addIndicatorContainersOfBox(Main.panel._leftBox);
addIndicatorContainersOfBox(Main.panel._centerBox);
addIndicatorContainersOfBox(Main.panel._rightBox);
// 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 function creates a restricted valid box order for the given box.
* This means it returns a box order for the box, where only roles are
* included, which have their associated indicator container already in the
* specified 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 restricted valid box order.
*/
_createRestrictedValidBoxOrder(box) {
// Get a resolved box order and get the indicator containers (of the
// items) which are currently present in the Gnome Shell top bar in the
// specified box.
let boxOrder = this._appIndicatorKStatusNotifierItemManager.createResolvedBoxOrder(this.settings.get_strv(`${box}-box-order`));
let boxIndicatorContainers;
switch (box) {
case "left":
boxIndicatorContainers = Main.panel._leftBox.get_children();
break;
case "center":
boxIndicatorContainers = Main.panel._centerBox.get_children();
break;
case "right":
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 restricted valid
// box order, where their indicator is present in the Gnome Shell top
// bar in the specified box currently.
let restrictedValidBoxOrder = [ ];
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)) restrictedValidBoxOrder.push(role);
}
return restrictedValidBoxOrder;
}
/**
* This method orders the top bar items of the specified box according to
* the configured box orders.
@ -476,7 +385,7 @@ class Extension {
*/
_orderTopBarItems(box) {
// Get the valid box order.
const validBoxOrder = this._createValidBoxOrder(box);
const validBoxOrder = this._boxOrderCreator.createValidBoxOrder(box);
// Get the relevant box of `Main.panel`.
let panelBox;

View File

@ -0,0 +1,137 @@
/*
* This file is part of Top-Bar-Organizer (a Gnome Shell Extension for
* organizing your Gnome Shell top bar).
* Copyright (C) 2021 Julian Schacher
*
* Top-Bar-Organizer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* exported BoxOrderCreator */
"use strict";
const ExtensionUtils = imports.misc.extensionUtils;
const Main = imports.ui.main;
/**
* A class exposing methods, which create special box orders.
*/
var BoxOrderCreator = class BoxOrderCreator {
/**
* @param {AppIndicatorKStatusNotifierItemManager}
* appIndicatorKStatusNotifierItemManager - An instance of
* AppIndicatorKStatusNotifierItemManager to be used in the methods of
* `this`.
*/
constructor(appIndicatorKStatusNotifierItemManager) {
this._appIndicatorKStatusNotifierItemManager = appIndicatorKStatusNotifierItemManager;
this._settings = ExtensionUtils.getSettings();
}
/**
* 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
* included, which have their associated indicator container already in some
* box of the Gnome Shell top bar.
* @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) {
// Get a resolved box order.
let boxOrder = this._appIndicatorKStatusNotifierItemManager.createResolvedBoxOrder(this._settings.get_strv(`${box}-box-order`));
// Get the indicator containers (of the items) currently present in the
// Gnome Shell top bar.
const boxIndicatorContainers = [ ];
const addIndicatorContainersOfBox = (panelBox) => {
for (const indicatorContainer of panelBox.get_children()) {
boxIndicatorContainers.push(indicatorContainer);
}
};
addIndicatorContainersOfBox(Main.panel._leftBox);
addIndicatorContainersOfBox(Main.panel._centerBox);
addIndicatorContainersOfBox(Main.panel._rightBox);
// 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 function creates a restricted valid box order for the given box.
* This means it returns a box order for the box, where only roles are
* included, which have their associated indicator container already in the
* specified 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 restricted valid box order.
*/
createRestrictedValidBoxOrder(box) {
// Get a resolved box order and get the indicator containers (of the
// items) which are currently present in the Gnome Shell top bar in the
// specified box.
let boxOrder = this._appIndicatorKStatusNotifierItemManager.createResolvedBoxOrder(this._settings.get_strv(`${box}-box-order`));
let boxIndicatorContainers;
switch (box) {
case "left":
boxIndicatorContainers = Main.panel._leftBox.get_children();
break;
case "center":
boxIndicatorContainers = Main.panel._centerBox.get_children();
break;
case "right":
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 restricted valid
// box order, where their indicator is present in the Gnome Shell top
// bar in the specified box currently.
let restrictedValidBoxOrder = [ ];
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)) restrictedValidBoxOrder.push(role);
}
return restrictedValidBoxOrder;
}
};