refactor: move logic for loading and saving box orders to priv. methods

Move logic for loading and saving box orders from/to settings to private
methods to have more readable code.
This commit is contained in:
June 2024-09-26 02:52:15 +02:00
parent cc088f443c
commit 80f394d2d4
No known key found for this signature in database

View File

@ -40,6 +40,41 @@ export default class BoxOrderManager extends GObject.Object {
this.#settings = settings; this.#settings = settings;
} }
/**
* Gets a box order for the given top bar box from settings.
* @param {string} box - The top bar box for which to get the box order.
* Must be one of the following values:
* - "left"
* - "center"
* - "right"
* @returns {string[]} - The box order consisting of an array of item
* settings identifiers.
*/
#getBoxOrder(box) {
return this.#settings.get_strv(`${box}-box-order`);
}
/**
* Save the given box order to settings, making sure to only save a changed
* box order, to avoid loops when listening on settings changes.
* @param {string} box - The top bar box for which to save the box order.
* Must be one of the following values:
* - "left"
* - "center"
* - "right"
* @param {string[]} boxOrder - The box order to save. Must be an array of
* item settings identifiers.
*/
#saveBoxOrder(box, boxOrder) {
const currentBoxOrder = this.#getBoxOrder(box);
// Only save the given box order to settings, if it is different, to
// avoid loops when listening on settings changes.
if (JSON.stringify(boxOrder) !== JSON.stringify(currentBoxOrder)) {
this.#settings.set_strv(`${box}-box-order`, boxOrder);
}
}
/** /**
* Handles an AppIndicator/KStatusNotifierItem item by deriving a settings * Handles an AppIndicator/KStatusNotifierItem item by deriving a settings
* identifier and then associating the role of the given item to the items * identifier and then associating the role of the given item to the items
@ -108,8 +143,7 @@ export default class BoxOrderManager extends GObject.Object {
* @returns {ResolvedBoxOrderItem[]} - The resolved box order. * @returns {ResolvedBoxOrderItem[]} - The resolved box order.
*/ */
#getResolvedBoxOrder(box) { #getResolvedBoxOrder(box) {
// Get the box order from settings. let boxOrder = this.#getBoxOrder(box);
let boxOrder = this.#settings.get_strv(`${box}-box-order`);
let resolvedBoxOrder = []; let resolvedBoxOrder = [];
for (const itemSettingsId of boxOrder) { for (const itemSettingsId of boxOrder) {
@ -219,11 +253,11 @@ export default class BoxOrderManager extends GObject.Object {
return; return;
} }
// Load the configured box orders from settings. // Get the box orders.
const boxOrders = { const boxOrders = {
left: this.#settings.get_strv("left-box-order"), left: this.#getBoxOrder("left"),
center: this.#settings.get_strv("center-box-order"), center: this.#getBoxOrder("center"),
right: this.#settings.get_strv("right-box-order"), right: this.#getBoxOrder("right"),
}; };
// Get roles (of items) currently present in the GNOME Shell top bar and // Get roles (of items) currently present in the GNOME Shell top bar and
@ -292,18 +326,8 @@ export default class BoxOrderManager extends GObject.Object {
addNewItemSettingsIdsToBoxOrder(boxIndicatorContainers.center, boxOrders.center, "center"); addNewItemSettingsIdsToBoxOrder(boxIndicatorContainers.center, boxOrders.center, "center");
addNewItemSettingsIdsToBoxOrder(boxIndicatorContainers.right, boxOrders.right, "right"); addNewItemSettingsIdsToBoxOrder(boxIndicatorContainers.right, boxOrders.right, "right");
// This function saves the given box order to settings. this.#saveBoxOrder("left", boxOrders.left);
const saveBoxOrderToSettings = (boxOrder, box) => { this.#saveBoxOrder("center", boxOrders.center);
const currentBoxOrder = this.#settings.get_strv(`${box}-box-order`); this.#saveBoxOrder("right", boxOrders.right);
// Only save the updated box order to settings, if it is different,
// to avoid loops, when listening on settings changes.
if (JSON.stringify(currentBoxOrder) !== JSON.stringify(boxOrder)) {
this.#settings.set_strv(`${box}-box-order`, boxOrder);
}
};
saveBoxOrderToSettings(boxOrders.left, "left");
saveBoxOrderToSettings(boxOrders.center, "center");
saveBoxOrderToSettings(boxOrders.right, "right");
} }
} }