Refactor: Simplify #overwritePanelAddToPanelBox heavily

Simplify `#overwritePanelAddToPanelBox` heavily by just calling the
original function in the overwrite and ordering the top bar and handling
new items afterwards.
Note that AppIndicator/KStatusNotifierItems are still not supported with
this refactor.
This commit is contained in:
Julian 2023-01-28 00:20:43 +01:00
parent d51703eb4e
commit 59d5665661
Signed by: julian
GPG Key ID: 094C2AC34192FA11
2 changed files with 16 additions and 214 deletions

View File

@ -76,182 +76,33 @@ class Extension {
*/ */
/** /**
* Overwrite `Panel._addToPanelBox` with a custom method, which handles top * Overwrite `Panel._addToPanelBox` with a custom method, which simply calls
* bar item additions to make sure that they are added in the correct * the original one and orders the top bar and handles new items afterwards.
* position and box.
*/ */
#overwritePanelAddToPanelBox() { #overwritePanelAddToPanelBox() {
// Add the original `Panel._addToPanelBox` method as // Add the original `Panel._addToPanelBox` method as
// `Panel._originalAddToPanelBox`. // `Panel._originalAddToPanelBox`.
Panel.Panel.prototype._originalAddToPanelBox = Panel.Panel.prototype._addToPanelBox; Panel.Panel.prototype._originalAddToPanelBox = Panel.Panel.prototype._addToPanelBox;
// This function gets used by the `Panel._addToPanelBox` overwrite to const orderTopBarAndHandleNewItems = () => {
// determine the position and box for a new item. this.#orderTopBarItems("left");
// It also adds the new item to the relevant box order, if it isn't in this.#orderTopBarItems("center");
// it already. this.#orderTopBarItems("right");
const getPositionAndBoxOverwrite = (role, box, indicator) => { this._boxOrderManager.saveNewTopBarItems();
const boxOrders = {
left: this.settings.get_strv("left-box-order"),
center: this.settings.get_strv("center-box-order"),
right: this.settings.get_strv("right-box-order"),
};
let boxOrder;
// Handle the case where the new item is a
// AppIndicator/KStatusNotifierItem.
// Note: This code is currently broken, since the extension
// providing AppIndicator/KStatusNotifierItems
// (appindicatorsupport@rgcjonas.gmail.com) doesn't give us an id on
// addition anymore and therefore we don't know which program/id the
// AppIndicator/KStatusNotifierItem belongs to.
// So just throw an error for now.
if (role.startsWith("appindicator-")) {
throw new Error("AppIndicator/KStatusNotifierItem addition is currently broken.");
// switch (box) {
// case "left":
// boxOrder = this.settings.get_strv("left-box-order");
// this._appIndicatorKStatusNotifierItemManager.handleAppIndicatorKStatusNotifierItemItem(indicator.container, role, boxOrder, boxOrders);
// this.settings.set_strv("left-box-order", boxOrder);
// break;
// case "center":
// boxOrder = this.settings.get_strv("center-box-order");
// this._appIndicatorKStatusNotifierItemManager.handleAppIndicatorKStatusNotifierItemItem(indicator.container, role, boxOrder, boxOrders);
// this.settings.set_strv("center-box-order", boxOrder);
// break;
// case "right":
// boxOrder = this.settings.get_strv("right-box-order");
// this._appIndicatorKStatusNotifierItemManager.handleAppIndicatorKStatusNotifierItemItem(indicator.container, role, boxOrder, boxOrders, true);
// this.settings.set_strv("right-box-order", boxOrder);
// break;
// }
}
// Get the resolved box orders for all boxes.
const resolvedBoxOrders = {
left: this._appIndicatorKStatusNotifierItemManager.createResolvedBoxOrder(this.settings.get_strv("left-box-order")),
center: this._appIndicatorKStatusNotifierItemManager.createResolvedBoxOrder(this.settings.get_strv("center-box-order")),
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._boxOrderManager.createRestrictedValidBoxOrder(box);
// Get the index of the role for each box order.
const indices = {
left: resolvedBoxOrders.left.indexOf(role),
center: resolvedBoxOrders.center.indexOf(role),
right: resolvedBoxOrders.right.indexOf(role),
};
// If the role is not already configured in one of the box orders,
// just add it to the target box order at the end/beginning, save
// the updated box order and return the relevant position and box.
if (indices.left === -1
&& indices.center === -1
&& indices.right === -1) {
switch (box) {
// For the left and center box, insert the role at the end,
// since they're LTR.
case "left":
boxOrders["left"].push(role);
this.settings.set_strv("left-box-order", boxOrders["left"]);
return {
position: restrictedValidBoxOrderOfTargetBox.length - 1,
box: box
};
case "center":
boxOrders["center"].push(role);
this.settings.set_strv("center-box-order", boxOrders["center"]);
return {
position: restrictedValidBoxOrderOfTargetBox.length - 1,
box: box
};
// For the right box, insert the role at the beginning,
// since it's RTL.
case "right":
boxOrders["right"].unshift(role);
this.settings.set_strv("right-box-order", boxOrders["right"]);
return {
position: 0,
box: box
};
}
}
/// Since the role is already configured in one of the box orders,
/// determine the correct insertion index for the position.
const determineInsertionIndex = (index, restrictedValidBoxOrder, boxOrder) => {
// Set the insertion index initially to 0, so that if no closest
// item can be found, the new item just gets inserted at the
// beginning.
let insertionIndex = 0;
// Find the index of the closest item, which is also in the
// valid box order and before the new item.
// This way, we can insert the new item just after the index of
// this closest item.
for (let i = index - 1; i >= 0; i--) {
let potentialClosestItemIndex = restrictedValidBoxOrder.indexOf(boxOrder[i]);
if (potentialClosestItemIndex !== -1) {
insertionIndex = potentialClosestItemIndex + 1;
break;
}
}
return insertionIndex;
};
if (indices.left !== -1) {
return {
position: determineInsertionIndex(indices.left, this._boxOrderManager.createRestrictedValidBoxOrder("left"), resolvedBoxOrders.left),
box: "left"
};
}
if (indices.center !== -1) {
return {
position: determineInsertionIndex(indices.center, this._boxOrderManager.createRestrictedValidBoxOrder("center"), resolvedBoxOrders.center),
box: "center"
};
}
if (indices.right !== -1) {
return {
position: determineInsertionIndex(indices.right, this._boxOrderManager.createRestrictedValidBoxOrder("right"), resolvedBoxOrders.right),
box: "right"
};
}
}; };
// Overwrite `Panel._addToPanelBox`. // Overwrite `Panel._addToPanelBox`.
Panel.Panel.prototype._addToPanelBox = function (role, indicator, position, box) { Panel.Panel.prototype._addToPanelBox = function (role, indicator, position, box) {
// Get the position and box overwrite. // Handle the case where the new item is a
let positionBoxOverwrite; // AppIndicator/KStatusNotifierItem.
switch (box) { if (role.startsWith("appindicator-")) {
case this._leftBox: // Just throw an error for now.
positionBoxOverwrite = getPositionAndBoxOverwrite(role, "left", indicator); throw new Error("AppIndicator/KStatusNotifierItem addition is currently broken/not implemented.");
break;
case this._centerBox:
positionBoxOverwrite = getPositionAndBoxOverwrite(role, "center", indicator);
break;
case this._rightBox:
positionBoxOverwrite = getPositionAndBoxOverwrite(role, "right", indicator);
break;
}
// Call the original `Panel._addToPanelBox` with the position
// overwrite as the position argument and the box determined by the
// box overwrite as the box argument.
switch (positionBoxOverwrite.box) {
case "left":
this._originalAddToPanelBox(role, indicator, positionBoxOverwrite.position, Main.panel._leftBox);
break;
case "center":
this._originalAddToPanelBox(role, indicator, positionBoxOverwrite.position, Main.panel._centerBox);
break;
case "right":
this._originalAddToPanelBox(role, indicator, positionBoxOverwrite.position, Main.panel._rightBox);
break;
} }
// Simply call the original `_addToPanelBox` and order the top bar
// and handle new items afterwards.
this._originalAddToPanelBox(role, indicator, position, box);
orderTopBarAndHandleNewItems();
}; };
} }

View File

@ -76,55 +76,6 @@ var BoxOrderManager = class BoxOrderManager {
return validBoxOrder; return validBoxOrder;
} }
/**
* This method returns a restricted valid box order for the given top bar
* box.
* This means it returns a box order, 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.
let boxOrder = this._appIndicatorKStatusNotifierItemManager.createResolvedBoxOrder(this._settings.get_strv(`${box}-box-order`));
// Get the indicator containers (of the items), which are currently
// present the in specified Gnome Shell top bar box.
let indicatorContainers;
switch (box) {
case "left":
indicatorContainers = Main.panel._leftBox.get_children();
break;
case "center":
indicatorContainers = Main.panel._centerBox.get_children();
break;
case "right":
indicatorContainers = Main.panel._rightBox.get_children();
break;
}
// Create an indicator container set from the indicator containers for
// fast easy access.
const indicatorContainerSet = new Set(indicatorContainers);
// Go through the box order and only add items to the restricted valid
// box order, where their indicator is present in the specified Gnome
// Shell top bar box.
let restrictedValidBoxOrder = [ ];
for (const role of boxOrder) {
// Get the indicator container associated with the current role.
const associatedIndicatorContainer = Main.panel.statusArea[role]?.container;
if (indicatorContainerSet.has(associatedIndicatorContainer)) restrictedValidBoxOrder.push(role);
}
return restrictedValidBoxOrder;
}
/** /**
* This method saves all new items currently present in the Gnome Shell top * This method saves all new items currently present in the Gnome Shell top
* bar to the correct box orders. * bar to the correct box orders.