浏览代码

Merge pull request #1439 from DmitryZhelnin/RepeatButton-InitialDelay

RepeatButton: start repeating process after delay for better user experience.
Steven Kirk 7 年之前
父节点
当前提交
4c88879287
共有 1 个文件被更改,包括 27 次插入2 次删除
  1. 27 2
      src/Avalonia.Controls/RepeatButton.cs

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

@@ -6,17 +6,32 @@ namespace Avalonia.Controls
 {
 {
     public class RepeatButton : Button
     public class RepeatButton : Button
     {
     {
+        /// <summary>
+        /// Defines the <see cref="Interval"/> property.
+        /// </summary>
+        public static readonly StyledProperty<int> IntervalProperty =
+            AvaloniaProperty.Register<Button, int>(nameof(Interval), 100);
+
         /// <summary>
         /// <summary>
         /// Defines the <see cref="Delay"/> property.
         /// Defines the <see cref="Delay"/> property.
         /// </summary>
         /// </summary>
         public static readonly StyledProperty<int> DelayProperty =
         public static readonly StyledProperty<int> DelayProperty =
-            AvaloniaProperty.Register<Button, int>(nameof(Delay), 100);
+            AvaloniaProperty.Register<Button, int>(nameof(Delay), 300);
 
 
         private DispatcherTimer _repeatTimer;
         private DispatcherTimer _repeatTimer;
 
 
         /// <summary>
         /// <summary>
         /// Gets or sets the amount of time, in milliseconds, of repeating clicks.
         /// Gets or sets the amount of time, in milliseconds, of repeating clicks.
         /// </summary>
         /// </summary>
+        public int Interval
+        {
+            get { return GetValue(IntervalProperty); }
+            set { SetValue(IntervalProperty, value); }
+        }
+
+        /// <summary>
+        /// Gets or sets the amount of time, in milliseconds, to wait before repeating begins.
+        /// </summary>
         public int Delay
         public int Delay
         {
         {
             get { return GetValue(DelayProperty); }
             get { return GetValue(DelayProperty); }
@@ -28,7 +43,7 @@ namespace Avalonia.Controls
             if (_repeatTimer == null)
             if (_repeatTimer == null)
             {
             {
                 _repeatTimer = new DispatcherTimer();
                 _repeatTimer = new DispatcherTimer();
-                _repeatTimer.Tick += (o, e) => OnClick();
+                _repeatTimer.Tick += RepeatTimerOnTick;
             }
             }
 
 
             if (_repeatTimer.IsEnabled) return;
             if (_repeatTimer.IsEnabled) return;
@@ -37,6 +52,16 @@ namespace Avalonia.Controls
             _repeatTimer.Start();
             _repeatTimer.Start();
         }
         }
 
 
+        private void RepeatTimerOnTick(object sender, EventArgs e)
+        {
+            var interval = TimeSpan.FromMilliseconds(Interval);
+            if (_repeatTimer.Interval != interval)
+            {
+                _repeatTimer.Interval = interval;
+            }
+            OnClick();
+        }
+
         private void StopTimer()
         private void StopTimer()
         {
         {
             _repeatTimer?.Stop();
             _repeatTimer?.Stop();