Refactor: Let PrefsBoxOrderItemRow handle item association

Let `PrefsBoxOrderItemRow` handle the association of itself with an item
itself.
Doing so makes the code cleaner, since the association isn't done
externally anymore.
This commit is contained in:
Julian 2021-07-05 07:58:01 +02:00
parent bd69f816eb
commit 309e2c07b9
Signed by: julian
GPG Key ID: 094C2AC34192FA11
2 changed files with 19 additions and 15 deletions

View File

@ -99,19 +99,7 @@ var PrefsWidget = GObject.registerClass({
// Add the items of the given configured box order as
// GtkListBoxRows.
for (const item of boxOrder) {
const listBoxRow = new PrefsBoxOrderItemRow.PrefsBoxOrderItemRow({}, this._scrollManager);
listBoxRow.item = item;
if (item.startsWith("appindicator-kstatusnotifieritem-")) {
// Set `item_name_display_label` of the `listBoxRow` to
// something nicer, if the associated item is an
// AppIndicator/KStatusNotifierItem item.
listBoxRow.item_name_display_label.set_label(item.replace("appindicator-kstatusnotifieritem-", ""));
} else {
// Otherwise just set the `item_name_display_label` of the
// `listBoxRow` to `item`.
listBoxRow.item_name_display_label.set_label(item);
}
const listBoxRow = new PrefsBoxOrderItemRow.PrefsBoxOrderItemRow({}, this._scrollManager, item);
gtkListBox.append(listBoxRow);
}

View File

@ -29,11 +29,13 @@ const Me = ExtensionUtils.getCurrentExtension();
var PrefsBoxOrderItemRow = GObject.registerClass({
GTypeName: "PrefsBoxOrderItemRow",
Template: Me.dir.get_child("prefs-box-order-item-row.ui").get_uri(),
Children: ["item-name-display-label"]
InternalChildren: ["item-name-display-label"]
}, class PrefsBoxOrderItemRow extends Gtk.ListBoxRow {
_init(params = {}, scrollManager) {
_init(params = {}, scrollManager, item) {
super._init(params);
this._associateItem(item);
// Make `this` draggable by creating a drag source and adding it to
// `this`.
let dragSource = new Gtk.DragSource();
@ -108,4 +110,18 @@ var PrefsBoxOrderItemRow = GObject.registerClass({
});
this.add_controller(dropTarget);
}
/**
* Associate `this` with an item.
* @param {String} item
*/
_associateItem(item) {
this.item = item;
// Set `this._item_name_display_label` to something nicer, if the
// associated item is an AppIndicator/KStatusNotifierItem item.
if (item.startsWith("appindicator-kstatusnotifieritem-")) this._item_name_display_label.set_label(item.replace("appindicator-kstatusnotifieritem-", ""));
// Otherwise just set it to `item`.
else this._item_name_display_label.set_label(item);
}
});