Przeglądaj źródła

Added InitialDelay property for better user experience.

dzhelnin 7 lat temu
rodzic
commit
21e627873b
1 zmienionych plików z 27 dodań i 2 usunięć
  1. 27 2
      src/Avalonia.Controls/RepeatButton.cs

+ 27 - 2
src/Avalonia.Controls/RepeatButton.cs

@@ -12,6 +12,12 @@ namespace Avalonia.Controls
         public static readonly StyledProperty<int> DelayProperty =
             AvaloniaProperty.Register<Button, int>(nameof(Delay), 100);
 
+        /// <summary>
+        /// Defines the <see cref="InitialDelay"/> property.
+        /// </summary>
+        public static readonly StyledProperty<int> InitialDelayProperty =
+            AvaloniaProperty.Register<Button, int>(nameof(InitialDelay), 300);
+
         private DispatcherTimer _repeatTimer;
 
         /// <summary>
@@ -23,20 +29,39 @@ namespace Avalonia.Controls
             set { SetValue(DelayProperty, value); }
         }
 
+        /// <summary>
+        /// Gets or sets the amount of time, in milliseconds, to wait before repeating begins.
+        /// </summary>
+        public int InitialDelay
+        {
+            get { return GetValue(InitialDelayProperty); }
+            set { SetValue(InitialDelayProperty, value); }
+        }
+
         private void StartTimer()
         {
             if (_repeatTimer == null)
             {
                 _repeatTimer = new DispatcherTimer();
-                _repeatTimer.Tick += (o, e) => OnClick();
+                _repeatTimer.Tick += RepeatTimerOnTick;
             }
 
             if (_repeatTimer.IsEnabled) return;
 
-            _repeatTimer.Interval = TimeSpan.FromMilliseconds(Delay);
+            _repeatTimer.Interval = TimeSpan.FromMilliseconds(InitialDelay);
             _repeatTimer.Start();
         }
 
+        private void RepeatTimerOnTick(object sender, EventArgs e)
+        {
+            var interval = TimeSpan.FromMilliseconds(Delay);
+            if (_repeatTimer.Interval != interval)
+            {
+                _repeatTimer.Interval = interval;
+            }
+            OnClick();
+        }
+
         private void StopTimer()
         {
             _repeatTimer?.Stop();