From 612ed6353c909da3d1099222795d8a3abf49a2d0 Mon Sep 17 00:00:00 2001
From: Salman Ahmed <frenzibyte@gmail.com>
Date: Fri, 2 Jul 2021 22:30:26 +0300
Subject: [PATCH] Resolve `RestoreDefaultValueButton` issue by internal
 management

---
 .../Overlays/RestoreDefaultValueButton.cs     | 41 +++++++++++--------
 1 file changed, 25 insertions(+), 16 deletions(-)

diff --git a/osu.Game/Overlays/RestoreDefaultValueButton.cs b/osu.Game/Overlays/RestoreDefaultValueButton.cs
index fe36f6ba6d..cd4490d452 100644
--- a/osu.Game/Overlays/RestoreDefaultValueButton.cs
+++ b/osu.Game/Overlays/RestoreDefaultValueButton.cs
@@ -20,15 +20,30 @@ namespace osu.Game.Overlays
     {
         public override bool IsPresent => base.IsPresent || Scheduler.HasPendingTasks;
 
-        private readonly BindableWithCurrent<T> current = new BindableWithCurrent<T>();
-
         // this is done to ensure a click on this button doesn't trigger focus on a parent element which contains the button.
         public override bool AcceptsFocus => true;
 
+        private Bindable<T> current;
+
         public Bindable<T> Current
         {
-            get => current.Current;
-            set => current.Current = value;
+            get => current;
+            set
+            {
+                if (current != null)
+                {
+                    current.ValueChanged -= onValueChanged;
+                    current.DefaultChanged -= onDefaultChanged;
+                    current.DisabledChanged -= onDisabledChanged;
+                }
+
+                current = value;
+
+                current.ValueChanged += onValueChanged;
+                current.DefaultChanged += onDefaultChanged;
+                current.DisabledChanged += onDisabledChanged;
+                UpdateState();
+            }
         }
 
         private Color4 buttonColour;
@@ -62,21 +77,11 @@ namespace osu.Game.Overlays
 
             Action += () =>
             {
-                if (!current.Disabled) current.SetDefault();
+                if (!current.Disabled)
+                    current.SetDefault();
             };
         }
 
-        protected override void LoadComplete()
-        {
-            base.LoadComplete();
-
-            Current.ValueChanged += _ => UpdateState();
-            Current.DisabledChanged += _ => UpdateState();
-            Current.DefaultChanged += _ => UpdateState();
-
-            UpdateState();
-        }
-
         public LocalisableString TooltipText => "revert to default";
 
         protected override bool OnHover(HoverEvent e)
@@ -92,6 +97,10 @@ namespace osu.Game.Overlays
             UpdateState();
         }
 
+        private void onValueChanged(ValueChangedEvent<T> _) => UpdateState();
+        private void onDefaultChanged(ValueChangedEvent<T> _) => UpdateState();
+        private void onDisabledChanged(bool _) => UpdateState();
+
         public void UpdateState() => Scheduler.AddOnce(updateState);
 
         private void updateState()