Better choose new selection when multiple items are removed including current

This commit is contained in:
Dean Herbert 2017-12-16 17:32:21 +09:00
parent 59dbca2612
commit bd9056c709
2 changed files with 38 additions and 2 deletions

View File

@ -257,9 +257,15 @@ namespace osu.Game.Tests.Visual
private void testRemoveAll()
{
setSelected(2, 1);
AddAssert("Selection is non-null", () => currentSelection != null);
AddStep("Remove selected", () => carousel.RemoveBeatmapSet(carousel.SelectedBeatmapSet));
checkSelected(2);
AddStep("Remove first", () => carousel.RemoveBeatmapSet(carousel.BeatmapSets.First()));
AddStep("Remove first", () => carousel.RemoveBeatmapSet(carousel.BeatmapSets.First()));
checkSelected(1);
AddUntilStep(() =>
{
carousel.RemoveBeatmapSet(carousel.BeatmapSets.Last());

View File

@ -1,6 +1,7 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Linq;
namespace osu.Game.Screens.Select.Carousel
@ -19,14 +20,33 @@ namespace osu.Game.Screens.Select.Carousel
};
}
/// <summary>
/// We need to keep track of the index for cases where the selection is removed but we want to select a new item based on its old location.
/// </summary>
private int lastSelectedIndex;
private CarouselItem lastSelected;
public override void Filter(FilterCriteria criteria)
{
base.Filter(criteria);
attemptSelection();
}
public override void RemoveChild(CarouselItem i)
{
base.RemoveChild(i);
if (i != lastSelected)
updateSelectedIndex();
}
public override void AddChild(CarouselItem i)
{
base.AddChild(i);
updateSelectedIndex();
}
protected override void ChildItemStateChanged(CarouselItem item, CarouselItemState value)
{
base.ChildItemStateChanged(item, value);
@ -34,7 +54,7 @@ namespace osu.Game.Screens.Select.Carousel
switch (value)
{
case CarouselItemState.Selected:
lastSelectedIndex = InternalChildren.IndexOf(item);
updateSelected(item);
break;
case CarouselItemState.NotSelected:
attemptSelection();
@ -56,6 +76,16 @@ namespace osu.Game.Screens.Select.Carousel
if (nextToSelect != null)
nextToSelect.State.Value = CarouselItemState.Selected;
else
updateSelected(null);
}
private void updateSelected(CarouselItem newSelection)
{
lastSelected = newSelection;
updateSelectedIndex();
}
private void updateSelectedIndex() => lastSelectedIndex = Math.Max(0, InternalChildren.IndexOf(lastSelected));
}
}