Refactor: Introduce #handleNewItemsAndOrderTopBar method

Introducing this method cleans up code, which did pretty much the same
thing, by replacing it with a call to this method.
This commit is contained in:
Julian 2023-01-29 00:34:44 +01:00
parent c61776d8d4
commit 6e61014331
Signed by: julian
GPG Key ID: 094C2AC34192FA11

View File

@ -19,10 +19,7 @@ class Extension {
this._boxOrderManager = new BoxOrderManager.BoxOrderManager(); this._boxOrderManager = new BoxOrderManager.BoxOrderManager();
// Stuff to do on startup(extension enable). // Stuff to do on startup(extension enable).
this._boxOrderManager.saveNewTopBarItems(); this.#handleNewItemsAndOrderTopBar();
this.#orderTopBarItems("left");
this.#orderTopBarItems("center");
this.#orderTopBarItems("right");
this.#overwritePanelAddToPanelBox(); this.#overwritePanelAddToPanelBox();
// Handle changes of configured box orders. // Handle changes of configured box orders.
@ -30,12 +27,7 @@ class Extension {
const addConfiguredBoxOrderChangeHandler = (box) => { const addConfiguredBoxOrderChangeHandler = (box) => {
let handlerId = this.settings.connect(`changed::${box}-box-order`, () => { let handlerId = this.settings.connect(`changed::${box}-box-order`, () => {
this.#orderTopBarItems(box); this.#handleNewItemsAndOrderTopBar();
// For the case, where the currently saved box order is based on
// a permutation of an outdated box order, save new top bar
// items.
this._boxOrderManager.saveNewTopBarItems();
}); });
this._settingsHandlerIds.push(handlerId); this._settingsHandlerIds.push(handlerId);
}; };
@ -46,10 +38,7 @@ class Extension {
// Handle AppIndicators getting ready. // Handle AppIndicators getting ready.
this._boxOrderManager.connect("appIndicatorReady", () => { this._boxOrderManager.connect("appIndicatorReady", () => {
this._boxOrderManager.saveNewTopBarItems(); this.#handleNewItemsAndOrderTopBar();
this.#orderTopBarItems("left");
this.#orderTopBarItems("center");
this.#orderTopBarItems("right");
}); });
} }
@ -83,18 +72,15 @@ class Extension {
/** /**
* Overwrite `Panel._addToPanelBox` with a custom method, which simply calls * Overwrite `Panel._addToPanelBox` with a custom method, which simply calls
* the original one and orders the top bar and handles new items afterwards. * the original one and handles new items and orders the top bar afterwards.
*/ */
#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;
const orderTopBarAndHandleNewItems = () => { const handleNewItemsAndOrderTopBar = () => {
this.#orderTopBarItems("left"); this.#handleNewItemsAndOrderTopBar();
this.#orderTopBarItems("center");
this.#orderTopBarItems("right");
this._boxOrderManager.saveNewTopBarItems();
}; };
// Overwrite `Panel._addToPanelBox`. // Overwrite `Panel._addToPanelBox`.
@ -102,7 +88,7 @@ class Extension {
// Simply call the original `_addToPanelBox` and order the top bar // Simply call the original `_addToPanelBox` and order the top bar
// and handle new items afterwards. // and handle new items afterwards.
this._originalAddToPanelBox(role, indicator, position, box); this._originalAddToPanelBox(role, indicator, position, box);
orderTopBarAndHandleNewItems(); handleNewItemsAndOrderTopBar();
}; };
} }
@ -163,6 +149,21 @@ class Extension {
// of an outdated box order, it would be wise, if the caller updated the // of an outdated box order, it would be wise, if the caller updated the
// box order now to include the items present in the top bar. // box order now to include the items present in the top bar.
} }
/**
* This method handles all new items currently present in the top bar and
* orders the items of all top bar boxes.
*/
#handleNewItemsAndOrderTopBar() {
this._boxOrderManager.saveNewTopBarItems();
this.#orderTopBarItems("left");
this.#orderTopBarItems("center");
this.#orderTopBarItems("right");
// In `this.#orderTopBarItems` it says to update the box orders to
// include potentially new items, since the ordering might have been
// based on an outdated box order. However, since we already handle new
// top bar items at the beginning of this method, this isn't a concern.
}
} }
function init() { function init() {