-
Notifications
You must be signed in to change notification settings - Fork 420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make RearrangeableListContainer<>
only replace differences
#6425
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a176073
Add more tests, one failing with new expectations
smoogipoo abd8aab
Make `RearrangeableListContainer<>` only replace differences
smoogipoo c383b67
Enable NRT for `RearrangeableListContainer<>`
smoogipoo ecf9dd0
Merge branch 'master' into rearrangeable-dedupe-replace
bdach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only qualm I have about this is how this implicitly relies on equality semantics, i.e. on
TModel
having a sane equality implementation. But I guess maybe fine to try...?