1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 08:32:57 +08:00

Avoid using a bindable in KeyBindingConflictPopover where data is never mutated

This commit is contained in:
Dean Herbert 2023-10-13 18:38:49 +09:00
parent 9b0c4acdef
commit 17b6ebbfbf
No known key found for this signature in database
3 changed files with 35 additions and 32 deletions

View File

@ -52,15 +52,12 @@ namespace osu.Game.Tests.Visual.Settings
Action = this.ShowPopover;
}
public Popover GetPopover() => new KeyBindingConflictPopover
{
ConflictInfo =
{
Value = new KeyBindingRow.KeyBindingConflictInfo(
new KeyBindingRow.ConflictingKeyBinding(Guid.NewGuid(), OsuAction.LeftButton, KeyCombination.FromKey(Key.X), new KeyCombination(InputKey.None)),
new KeyBindingRow.ConflictingKeyBinding(Guid.NewGuid(), OsuAction.RightButton, KeyCombination.FromKey(Key.Z), KeyCombination.FromKey(Key.X)))
}
};
public Popover GetPopover() => new KeyBindingConflictPopover(
new KeyBindingRow.KeyBindingConflictInfo(
new KeyBindingRow.ConflictingKeyBinding(Guid.NewGuid(), OsuAction.LeftButton, KeyCombination.FromKey(Key.X), new KeyCombination(InputKey.None)),
new KeyBindingRow.ConflictingKeyBinding(Guid.NewGuid(), OsuAction.RightButton, KeyCombination.FromKey(Key.Z), KeyCombination.FromKey(Key.X))
)
);
}
}
}

View File

@ -18,15 +18,13 @@ using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Input.Bindings;
using osuTK;
using osu.Game.Localisation;
using osuTK;
namespace osu.Game.Overlays.Settings.Sections.Input
{
public partial class KeyBindingConflictPopover : OsuPopover
{
public Bindable<KeyBindingRow.KeyBindingConflictInfo> ConflictInfo { get; } = new Bindable<KeyBindingRow.KeyBindingConflictInfo>();
public Action? BindingConflictResolved { get; init; }
private ConflictingKeyBindingPreview newPreview = null!;
@ -40,10 +38,15 @@ namespace osu.Game.Overlays.Settings.Sections.Input
[Resolved]
private OsuColour colours { get; set; } = null!;
[BackgroundDependencyLoader]
private void load() => recreateDisplay();
private readonly KeyBindingRow.KeyBindingConflictInfo conflictInfo;
private void recreateDisplay()
public KeyBindingConflictPopover(KeyBindingRow.KeyBindingConflictInfo conflictInfo)
{
this.conflictInfo = conflictInfo;
}
[BackgroundDependencyLoader]
private void load()
{
Child = new FillFlowContainer
{
@ -61,13 +64,13 @@ namespace osu.Game.Overlays.Settings.Sections.Input
Margin = new MarginPadding { Bottom = 10 }
},
existingPreview = new ConflictingKeyBindingPreview(
ConflictInfo.Value.Existing.Action,
ConflictInfo.Value.Existing.CombinationWhenChosen,
ConflictInfo.Value.Existing.CombinationWhenNotChosen),
conflictInfo.Existing.Action,
conflictInfo.Existing.CombinationWhenChosen,
conflictInfo.Existing.CombinationWhenNotChosen),
newPreview = new ConflictingKeyBindingPreview(
ConflictInfo.Value.New.Action,
ConflictInfo.Value.New.CombinationWhenChosen,
ConflictInfo.Value.New.CombinationWhenNotChosen),
conflictInfo.New.Action,
conflictInfo.New.CombinationWhenChosen,
conflictInfo.New.CombinationWhenNotChosen),
new Container
{
RelativeSizeAxes = Axes.X,
@ -107,11 +110,11 @@ namespace osu.Game.Overlays.Settings.Sections.Input
// the temporary visual changes will be reverted by calling `Hide()` / `BindingConflictResolved`.
realm.Write(r =>
{
var existingBinding = r.Find<RealmKeyBinding>(ConflictInfo.Value.Existing.ID);
existingBinding!.KeyCombinationString = ConflictInfo.Value.Existing.CombinationWhenNotChosen.ToString();
var existingBinding = r.Find<RealmKeyBinding>(conflictInfo.Existing.ID);
existingBinding!.KeyCombinationString = conflictInfo.Existing.CombinationWhenNotChosen.ToString();
var newBinding = r.Find<RealmKeyBinding>(ConflictInfo.Value.New.ID);
newBinding!.KeyCombinationString = ConflictInfo.Value.Existing.CombinationWhenChosen.ToString();
var newBinding = r.Find<RealmKeyBinding>(conflictInfo.New.ID);
newBinding!.KeyCombinationString = conflictInfo.Existing.CombinationWhenChosen.ToString();
});
Hide();

View File

@ -2,7 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Bindables;
using System.Diagnostics;
using osu.Framework.Extensions;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.UserInterface;
@ -12,17 +12,20 @@ namespace osu.Game.Overlays.Settings.Sections.Input
{
public partial class KeyBindingRow : IHasPopover
{
private readonly Bindable<KeyBindingConflictInfo> keyBindingConflictInfo = new Bindable<KeyBindingConflictInfo>();
private KeyBindingConflictInfo? pendingKeyBindingConflict;
public Popover GetPopover() => new KeyBindingConflictPopover
public Popover GetPopover()
{
ConflictInfo = { BindTarget = keyBindingConflictInfo },
BindingConflictResolved = () => BindingUpdated?.Invoke(this, new KeyBindingUpdatedEventArgs(bindingConflictResolved: true, canAdvanceToNextBinding: false))
};
Debug.Assert(pendingKeyBindingConflict != null);
return new KeyBindingConflictPopover(pendingKeyBindingConflict)
{
BindingConflictResolved = () => BindingUpdated?.Invoke(this, new KeyBindingUpdatedEventArgs(bindingConflictResolved: true, canAdvanceToNextBinding: false))
};
}
private void showBindingConflictPopover(KeyBindingConflictInfo conflictInfo)
{
keyBindingConflictInfo.Value = conflictInfo;
pendingKeyBindingConflict = conflictInfo;
this.ShowPopover();
}