-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6425 from smoogipoo/rearrangeable-dedupe-replace
Make `RearrangeableListContainer<>` only replace differences
- Loading branch information
Showing
2 changed files
with
122 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Collections.Specialized; | ||
using System.Linq; | ||
|
@@ -22,6 +19,7 @@ namespace osu.Framework.Graphics.Containers | |
/// </remarks> | ||
/// <typeparam name="TModel">The type of rearrangeable item.</typeparam> | ||
public abstract partial class RearrangeableListContainer<TModel> : CompositeDrawable | ||
where TModel : notnull | ||
{ | ||
private const float exp_base = 1.05f; | ||
|
||
|
@@ -51,7 +49,7 @@ public abstract partial class RearrangeableListContainer<TModel> : CompositeDraw | |
protected IReadOnlyDictionary<TModel, RearrangeableListItem<TModel>> ItemMap => itemMap; | ||
|
||
private readonly Dictionary<TModel, RearrangeableListItem<TModel>> itemMap = new Dictionary<TModel, RearrangeableListItem<TModel>>(); | ||
private RearrangeableListItem<TModel> currentlyDraggedItem; | ||
private RearrangeableListItem<TModel>? currentlyDraggedItem; | ||
private Vector2 screenSpaceDragPosition; | ||
|
||
/// <summary> | ||
|
@@ -82,16 +80,16 @@ protected virtual void OnItemsChanged() | |
{ | ||
} | ||
|
||
private void collectionChanged(object sender, NotifyCollectionChangedEventArgs e) | ||
private void collectionChanged(object? sender, NotifyCollectionChangedEventArgs e) | ||
{ | ||
switch (e.Action) | ||
{ | ||
case NotifyCollectionChangedAction.Add: | ||
addItems(e.NewItems); | ||
addItems(e.NewItems?.Cast<TModel>() ?? []); | ||
break; | ||
|
||
case NotifyCollectionChangedAction.Remove: | ||
removeItems(e.OldItems); | ||
removeItems(e.OldItems?.Cast<TModel>() ?? []); | ||
|
||
// Explicitly reset scroll position here so that ScrollContainer doesn't retain our | ||
// scroll position if we quickly add new items after calling a Clear(). | ||
|
@@ -107,8 +105,11 @@ private void collectionChanged(object sender, NotifyCollectionChangedEventArgs e | |
break; | ||
|
||
case NotifyCollectionChangedAction.Replace: | ||
removeItems(e.OldItems); | ||
addItems(e.NewItems); | ||
IEnumerable<TModel> tOldItems = e.OldItems?.Cast<TModel>() ?? []; | ||
IEnumerable<TModel> tNewItems = e.NewItems?.Cast<TModel>() ?? []; | ||
|
||
removeItems(tOldItems.Except(tNewItems)); | ||
addItems(tNewItems.Except(tOldItems)); | ||
break; | ||
|
||
case NotifyCollectionChangedAction.Move: | ||
|
@@ -118,9 +119,9 @@ private void collectionChanged(object sender, NotifyCollectionChangedEventArgs e | |
} | ||
} | ||
|
||
private void removeItems(IList items) | ||
private void removeItems(IEnumerable<TModel> items) | ||
{ | ||
foreach (var item in items.Cast<TModel>()) | ||
foreach (var item in items) | ||
{ | ||
if (currentlyDraggedItem != null && EqualityComparer<TModel>.Default.Equals(currentlyDraggedItem.Model, item)) | ||
currentlyDraggedItem = null; | ||
|
@@ -137,11 +138,11 @@ private void removeItems(IList items) | |
OnItemsChanged(); | ||
} | ||
|
||
private void addItems(IList items) | ||
private void addItems(IEnumerable<TModel> items) | ||
{ | ||
var drawablesToAdd = new List<Drawable>(); | ||
|
||
foreach (var item in items.Cast<TModel>()) | ||
foreach (var item in items) | ||
{ | ||
if (itemMap.ContainsKey(item)) | ||
{ | ||
|
@@ -191,7 +192,7 @@ private void sortItems() | |
if (drawable.Parent != ListContainer) | ||
continue; | ||
|
||
ListContainer!.SetLayoutPosition(drawable, i); | ||
ListContainer.SetLayoutPosition(drawable, i); | ||
} | ||
} | ||
|
||
|
@@ -216,9 +217,7 @@ protected override void Update() | |
protected override void UpdateAfterChildren() | ||
{ | ||
base.UpdateAfterChildren(); | ||
|
||
if (currentlyDraggedItem != null) | ||
updateArrangement(); | ||
updateArrangement(); | ||
} | ||
|
||
private void updateScrollPosition() | ||
|
@@ -243,6 +242,9 @@ private void updateScrollPosition() | |
|
||
private void updateArrangement() | ||
{ | ||
if (currentlyDraggedItem == null) | ||
return; | ||
|
||
var localPos = ListContainer.ToLocalSpace(screenSpaceDragPosition); | ||
int srcIndex = Items.IndexOf(currentlyDraggedItem.Model); | ||
|
||
|