ButtonSpinner.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using Avalonia.Controls.Primitives;
  3. using Avalonia.Input;
  4. using Avalonia.Interactivity;
  5. namespace Avalonia.Controls
  6. {
  7. public enum Location
  8. {
  9. Left,
  10. Right
  11. }
  12. /// <summary>
  13. /// Represents a spinner control that includes two Buttons.
  14. /// </summary>
  15. public class ButtonSpinner : Spinner
  16. {
  17. /// <summary>
  18. /// Defines the <see cref="AllowSpin"/> property.
  19. /// </summary>
  20. public static readonly StyledProperty<bool> AllowSpinProperty =
  21. AvaloniaProperty.Register<ButtonSpinner, bool>(nameof(AllowSpin), true);
  22. /// <summary>
  23. /// Defines the <see cref="ShowButtonSpinner"/> property.
  24. /// </summary>
  25. public static readonly StyledProperty<bool> ShowButtonSpinnerProperty =
  26. AvaloniaProperty.Register<ButtonSpinner, bool>(nameof(ShowButtonSpinner), true);
  27. /// <summary>
  28. /// Defines the <see cref="ButtonSpinnerLocation"/> property.
  29. /// </summary>
  30. public static readonly StyledProperty<Location> ButtonSpinnerLocationProperty =
  31. AvaloniaProperty.Register<ButtonSpinner, Location>(nameof(ButtonSpinnerLocation), Location.Right);
  32. private Button _decreaseButton;
  33. /// <summary>
  34. /// Gets or sets the DecreaseButton template part.
  35. /// </summary>
  36. private Button DecreaseButton
  37. {
  38. get { return _decreaseButton; }
  39. set
  40. {
  41. if (_decreaseButton != null)
  42. {
  43. _decreaseButton.Click -= OnButtonClick;
  44. }
  45. _decreaseButton = value;
  46. if (_decreaseButton != null)
  47. {
  48. _decreaseButton.Click += OnButtonClick;
  49. }
  50. }
  51. }
  52. private Button _increaseButton;
  53. /// <summary>
  54. /// Gets or sets the IncreaseButton template part.
  55. /// </summary>
  56. private Button IncreaseButton
  57. {
  58. get
  59. {
  60. return _increaseButton;
  61. }
  62. set
  63. {
  64. if (_increaseButton != null)
  65. {
  66. _increaseButton.Click -= OnButtonClick;
  67. }
  68. _increaseButton = value;
  69. if (_increaseButton != null)
  70. {
  71. _increaseButton.Click += OnButtonClick;
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// Initializes static members of the <see cref="ButtonSpinner"/> class.
  77. /// </summary>
  78. static ButtonSpinner()
  79. {
  80. AllowSpinProperty.Changed.Subscribe(AllowSpinChanged);
  81. PseudoClass(ButtonSpinnerLocationProperty, location => location == Location.Left, ":left");
  82. PseudoClass(ButtonSpinnerLocationProperty, location => location == Location.Right, ":right");
  83. }
  84. /// <summary>
  85. /// Gets or sets a value indicating whether the <see cref="ButtonSpinner"/> should allow to spin.
  86. /// </summary>
  87. public bool AllowSpin
  88. {
  89. get { return GetValue(AllowSpinProperty); }
  90. set { SetValue(AllowSpinProperty, value); }
  91. }
  92. /// <summary>
  93. /// Gets or sets a value indicating whether the spin buttons should be shown.
  94. /// </summary>
  95. public bool ShowButtonSpinner
  96. {
  97. get { return GetValue(ShowButtonSpinnerProperty); }
  98. set { SetValue(ShowButtonSpinnerProperty, value); }
  99. }
  100. /// <summary>
  101. /// Gets or sets current location of the <see cref="ButtonSpinner"/>.
  102. /// </summary>
  103. public Location ButtonSpinnerLocation
  104. {
  105. get { return GetValue(ButtonSpinnerLocationProperty); }
  106. set { SetValue(ButtonSpinnerLocationProperty, value); }
  107. }
  108. /// <inheritdoc />
  109. protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
  110. {
  111. IncreaseButton = e.NameScope.Find<Button>("PART_IncreaseButton");
  112. DecreaseButton = e.NameScope.Find<Button>("PART_DecreaseButton");
  113. SetButtonUsage();
  114. }
  115. /// <inheritdoc />
  116. protected override void OnPointerReleased(PointerReleasedEventArgs e)
  117. {
  118. base.OnPointerReleased(e);
  119. Point mousePosition;
  120. if (IncreaseButton != null && IncreaseButton.IsEnabled == false)
  121. {
  122. mousePosition = e.GetPosition(IncreaseButton);
  123. if (mousePosition.X > 0 && mousePosition.X < IncreaseButton.Width &&
  124. mousePosition.Y > 0 && mousePosition.Y < IncreaseButton.Height)
  125. {
  126. e.Handled = true;
  127. }
  128. }
  129. if (DecreaseButton != null && DecreaseButton.IsEnabled == false)
  130. {
  131. mousePosition = e.GetPosition(DecreaseButton);
  132. if (mousePosition.X > 0 && mousePosition.X < DecreaseButton.Width &&
  133. mousePosition.Y > 0 && mousePosition.Y < DecreaseButton.Height)
  134. {
  135. e.Handled = true;
  136. }
  137. }
  138. }
  139. /// <inheritdoc />
  140. protected override void OnKeyDown(KeyEventArgs e)
  141. {
  142. switch (e.Key)
  143. {
  144. case Key.Up:
  145. {
  146. if (AllowSpin)
  147. {
  148. OnSpin(new SpinEventArgs(SpinEvent, SpinDirection.Increase));
  149. e.Handled = true;
  150. }
  151. break;
  152. }
  153. case Key.Down:
  154. {
  155. if (AllowSpin)
  156. {
  157. OnSpin(new SpinEventArgs(SpinEvent, SpinDirection.Decrease));
  158. e.Handled = true;
  159. }
  160. break;
  161. }
  162. case Key.Enter:
  163. {
  164. //Do not Spin on enter Key when spinners have focus
  165. if (((IncreaseButton != null) && (IncreaseButton.IsFocused))
  166. || ((DecreaseButton != null) && DecreaseButton.IsFocused))
  167. {
  168. e.Handled = true;
  169. }
  170. break;
  171. }
  172. }
  173. }
  174. /// <inheritdoc />
  175. protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
  176. {
  177. base.OnPointerWheelChanged(e);
  178. if (!e.Handled && AllowSpin)
  179. {
  180. if (e.Delta.Y != 0)
  181. {
  182. var spinnerEventArgs = new SpinEventArgs(SpinEvent, (e.Delta.Y < 0) ? SpinDirection.Decrease : SpinDirection.Increase, true);
  183. OnSpin(spinnerEventArgs);
  184. e.Handled = spinnerEventArgs.Handled;
  185. }
  186. }
  187. }
  188. protected override void OnValidSpinDirectionChanged(ValidSpinDirections oldValue, ValidSpinDirections newValue)
  189. {
  190. SetButtonUsage();
  191. }
  192. /// <summary>
  193. /// Called when the <see cref="AllowSpin"/> property value changed.
  194. /// </summary>
  195. /// <param name="oldValue">The old value.</param>
  196. /// <param name="newValue">The new value.</param>
  197. protected virtual void OnAllowSpinChanged(bool oldValue, bool newValue)
  198. {
  199. SetButtonUsage();
  200. }
  201. /// <summary>
  202. /// Called when the <see cref="AllowSpin"/> property value changed.
  203. /// </summary>
  204. /// <param name="e">The event args.</param>
  205. private static void AllowSpinChanged(AvaloniaPropertyChangedEventArgs e)
  206. {
  207. if (e.Sender is ButtonSpinner spinner)
  208. {
  209. var oldValue = (bool)e.OldValue;
  210. var newValue = (bool)e.NewValue;
  211. spinner.OnAllowSpinChanged(oldValue, newValue);
  212. }
  213. }
  214. /// <summary>
  215. /// Disables or enables the buttons based on the valid spin direction.
  216. /// </summary>
  217. private void SetButtonUsage()
  218. {
  219. if (IncreaseButton != null)
  220. {
  221. IncreaseButton.IsEnabled = AllowSpin && ((ValidSpinDirection & ValidSpinDirections.Increase) == ValidSpinDirections.Increase);
  222. }
  223. if (DecreaseButton != null)
  224. {
  225. DecreaseButton.IsEnabled = AllowSpin && ((ValidSpinDirection & ValidSpinDirections.Decrease) == ValidSpinDirections.Decrease);
  226. }
  227. }
  228. /// <summary>
  229. /// Called when user clicks one of the spin buttons.
  230. /// </summary>
  231. /// <param name="sender">The event sender.</param>
  232. /// <param name="e">The event args.</param>
  233. private void OnButtonClick(object sender, RoutedEventArgs e)
  234. {
  235. if (AllowSpin)
  236. {
  237. var direction = sender == IncreaseButton ? SpinDirection.Increase : SpinDirection.Decrease;
  238. OnSpin(new SpinEventArgs(SpinEvent, direction));
  239. }
  240. }
  241. }
  242. }