Browse Source

Added InitialDelay property for better user experience.

pull/1439/head
dzhelnin 8 years ago
parent
commit
21e627873b
  1. 29
      src/Avalonia.Controls/RepeatButton.cs

29
src/Avalonia.Controls/RepeatButton.cs

@ -12,6 +12,12 @@ namespace Avalonia.Controls
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), 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; private DispatcherTimer _repeatTimer;
/// <summary> /// <summary>
@ -23,20 +29,39 @@ namespace Avalonia.Controls
set { SetValue(DelayProperty, value); } 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() private void StartTimer()
{ {
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;
_repeatTimer.Interval = TimeSpan.FromMilliseconds(Delay); _repeatTimer.Interval = TimeSpan.FromMilliseconds(InitialDelay);
_repeatTimer.Start(); _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() private void StopTimer()
{ {
_repeatTimer?.Stop(); _repeatTimer?.Stop();

Loading…
Cancel
Save