appDisplay: Add moveItem()

This is a handy function to implement changing an icon's position
in the grid.

WIP: better commit message

https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/603
This commit is contained in:
Georges Basile Stavracas Neto 2019-07-02 17:41:15 -03:00
parent ca01b0287e
commit 0dd430f2f4
No known key found for this signature in database
GPG Key ID: 886C17EE170D1385
2 changed files with 38 additions and 0 deletions

View File

@ -163,6 +163,28 @@ class BaseAppView {
this.emit('view-loaded');
}
moveItem(item, newPosition) {
let itemIndex = this._allItems.indexOf(item);
if (itemIndex == -1) {
log('Trying to move item %s that is not in this app view'.format(item.id));
return;
}
let visibleItems = this._allItems.filter(item => item.actor.visible);
let visibleIndex = visibleItems.indexOf(item);
if (newPosition > visibleIndex)
newPosition -= 1;
// Remove from the old position
this._allItems.splice(itemIndex, 1);
let realPosition = this._grid.moveItem(item, newPosition);
this._allItems.splice(realPosition, 0, item);
return realPosition;
}
_selectAppInternal(id) {
if (this._items[id])
this._items[id].actor.navigate_focus(null, St.DirectionType.TAB_FORWARD, false);

View File

@ -695,6 +695,22 @@ var IconGrid = GObject.registerClass({
this.add_actor(item.actor);
}
moveItem(item, newPosition) {
if (!this.contains(item.actor)) {
log('Cannot move item not contained by the IconGrid');
return;
}
let children = this.get_children();
let visibleChildren = children.filter(c => c.is_visible());
let visibleChildAtPosition = visibleChildren[newPosition];
let realPosition = children.indexOf(visibleChildAtPosition);
this.set_child_at_index(item.actor, realPosition);
return realPosition;
}
removeItem(item) {
this.remove_child(item.actor);
}