MuchinfoPagerNavigationButton.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. using System.Windows.Threading;
  8. namespace Muchinfo.DataPager.Base
  9. {
  10. public class MuchinfoPagerNavigationButton : Button
  11. {
  12. public static readonly DependencyProperty HoverDelayProperty;
  13. public static readonly DependencyProperty CornerRadiusProperty;
  14. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  15. public static readonly DependencyProperty InnerCornerRadiusProperty;
  16. public static readonly DependencyProperty IsBackgroundVisibleProperty;
  17. public static readonly RoutedEvent ActivateEvent;
  18. public static readonly RoutedEvent HoverEvent;
  19. private DispatcherTimer hoverTimer;
  20. public event EventHandler<MuchinfoRoutedEventArgs> Activate
  21. {
  22. add
  23. {
  24. base.AddHandler(MuchinfoPagerNavigationButton.ActivateEvent, value);
  25. }
  26. remove
  27. {
  28. base.RemoveHandler(MuchinfoPagerNavigationButton.ActivateEvent, value);
  29. }
  30. }
  31. [Browsable(false)]
  32. public event EventHandler<MuchinfoRoutedEventArgs> Hover
  33. {
  34. add
  35. {
  36. base.AddHandler(MuchinfoPagerNavigationButton.HoverEvent, value);
  37. }
  38. remove
  39. {
  40. base.RemoveHandler(MuchinfoPagerNavigationButton.HoverEvent, value);
  41. }
  42. }
  43. [Browsable(false)]
  44. public TimeSpan HoverDelay
  45. {
  46. get
  47. {
  48. return (TimeSpan)base.GetValue(MuchinfoPagerNavigationButton.HoverDelayProperty);
  49. }
  50. set
  51. {
  52. base.SetValue(MuchinfoPagerNavigationButton.HoverDelayProperty, value);
  53. }
  54. }
  55. public CornerRadius CornerRadius
  56. {
  57. get
  58. {
  59. return (CornerRadius)base.GetValue(MuchinfoPagerNavigationButton.CornerRadiusProperty);
  60. }
  61. set
  62. {
  63. base.SetValue(MuchinfoPagerNavigationButton.CornerRadiusProperty, value);
  64. }
  65. }
  66. [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  67. public CornerRadius InnerCornerRadius
  68. {
  69. get
  70. {
  71. return (CornerRadius)base.GetValue(MuchinfoPagerNavigationButton.InnerCornerRadiusProperty);
  72. }
  73. set
  74. {
  75. base.SetValue(MuchinfoPagerNavigationButton.InnerCornerRadiusProperty, value);
  76. }
  77. }
  78. [Browsable(false)]
  79. public bool IsBackgroundVisible
  80. {
  81. get
  82. {
  83. return (bool)base.GetValue(MuchinfoPagerNavigationButton.IsBackgroundVisibleProperty);
  84. }
  85. set
  86. {
  87. base.SetValue(MuchinfoPagerNavigationButton.IsBackgroundVisibleProperty, value);
  88. }
  89. }
  90. static MuchinfoPagerNavigationButton()
  91. {
  92. MuchinfoPagerNavigationButton.HoverDelayProperty = DependencyProperty.Register("HoverDelay", typeof(TimeSpan), typeof(MuchinfoPagerNavigationButton), new PropertyMetadata(TimeSpan.Zero, new PropertyChangedCallback(MuchinfoPagerNavigationButton.OnHoverDelayChanged)));
  93. MuchinfoPagerNavigationButton.CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(MuchinfoPagerNavigationButton), new PropertyMetadata(new PropertyChangedCallback(MuchinfoPagerNavigationButton.OnCornerRadiusChanged)));
  94. MuchinfoPagerNavigationButton.InnerCornerRadiusProperty = DependencyProperty.Register("InnerCornerRadius", typeof(CornerRadius), typeof(MuchinfoPagerNavigationButton), new PropertyMetadata());
  95. MuchinfoPagerNavigationButton.IsBackgroundVisibleProperty = DependencyProperty.Register("IsBackgroundVisible", typeof(bool), typeof(MuchinfoPagerNavigationButton), new PropertyMetadata(true, new PropertyChangedCallback(MuchinfoPagerNavigationButton.OnIsBackgroundVisiblePropertyChanged)));
  96. MuchinfoPagerNavigationButton.ActivateEvent = EventManager.RegisterRoutedEvent("Activate", RoutingStrategy.Bubble, typeof(EventHandler<MuchinfoRoutedEventArgs>), typeof(MuchinfoPagerNavigationButton));
  97. MuchinfoPagerNavigationButton.HoverEvent = EventManager.RegisterRoutedEvent("Hover", RoutingStrategy.Bubble, typeof(EventHandler<MuchinfoRoutedEventArgs>), typeof(MuchinfoPagerNavigationButton));
  98. FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(MuchinfoPagerNavigationButton), new FrameworkPropertyMetadata(typeof(MuchinfoPagerNavigationButton)));
  99. }
  100. public MuchinfoPagerNavigationButton()
  101. {
  102. //TelerikLicense.Verify(this);
  103. }
  104. public override void OnApplyTemplate()
  105. {
  106. base.OnApplyTemplate();
  107. this.UpdateVisualStates(false);
  108. this.UpdateBackgroundVisibility();
  109. }
  110. internal static MuchinfoRoutedEventArgs RaiseRadRoutedEvent(RoutedEvent routedEvent, UIElement source)
  111. {
  112. MuchinfoRoutedEventArgs radRoutedEventArgs = new MuchinfoRoutedEventArgs(routedEvent, source);
  113. source.RaiseEvent(radRoutedEventArgs);
  114. return radRoutedEventArgs;
  115. }
  116. internal static bool HasChildOf(DependencyObject parent, DependencyObject child, bool skipParent)
  117. {
  118. if (parent == null || child == null)
  119. {
  120. return false;
  121. }
  122. if (!skipParent && parent == child)
  123. {
  124. return true;
  125. }
  126. FrameworkElement frameworkElement = child as FrameworkElement;
  127. DependencyObject parent2 = VisualTreeHelper.GetParent(child);
  128. if (parent2 == null && frameworkElement != null)
  129. {
  130. parent2 = frameworkElement.Parent;
  131. }
  132. return MuchinfoPagerNavigationButton.HasChildOf(parent, parent2, false);
  133. }
  134. internal virtual void UpdateVisualStates(bool useTransitions)
  135. {
  136. if (!base.IsEnabled)
  137. {
  138. this.GoToState("Disabled", useTransitions);
  139. }
  140. else
  141. {
  142. if (base.IsPressed)
  143. {
  144. this.GoToState("Pressed", useTransitions);
  145. }
  146. else
  147. {
  148. if (base.IsMouseOver)
  149. {
  150. this.GoToState("MouseOver", useTransitions);
  151. }
  152. else
  153. {
  154. this.GoToState("Normal", useTransitions);
  155. }
  156. }
  157. }
  158. if (base.IsFocused && base.IsEnabled)
  159. {
  160. this.GoToState("Focused", useTransitions);
  161. return;
  162. }
  163. this.GoToState("Unfocused", useTransitions);
  164. }
  165. internal virtual void UpdateBackgroundVisibility()
  166. {
  167. if (this.IsBackgroundVisible || base.IsMouseOver || base.IsPressed)
  168. {
  169. this.GoToState("BackgroundVisible", false);
  170. }
  171. else
  172. {
  173. this.GoToState("BackgroundHidden", false);
  174. }
  175. if (this.IsBackgroundVisible)
  176. {
  177. this.GoToState("BackgroundIsVisible", false);
  178. return;
  179. }
  180. this.GoToState("BackgroundIsHidden", false);
  181. }
  182. internal bool GoToState(string stateName, bool useTransitions)
  183. {
  184. return VisualStateManager.GoToState(this, stateName, useTransitions);
  185. }
  186. internal void OnClickInternal()
  187. {
  188. this.OnClick();
  189. }
  190. protected internal virtual void OnActivate()
  191. {
  192. base.RaiseEvent(new MuchinfoRoutedEventArgs(MuchinfoPagerNavigationButton.ActivateEvent, this));
  193. }
  194. protected internal virtual void OnHover()
  195. {
  196. base.RaiseEvent(new MuchinfoRoutedEventArgs(MuchinfoPagerNavigationButton.HoverEvent, this));
  197. }
  198. protected override void OnClick()
  199. {
  200. base.OnClick();
  201. this.OnActivate();
  202. //TraceMonitor.TrackAtomicFeature(this, "Click");
  203. }
  204. protected override void OnLostFocus(RoutedEventArgs e)
  205. {
  206. base.OnLostFocus(e);
  207. this.UpdateBackgroundVisibility();
  208. }
  209. protected override void OnGotFocus(RoutedEventArgs e)
  210. {
  211. base.OnGotFocus(e);
  212. this.UpdateBackgroundVisibility();
  213. }
  214. protected override void OnMouseEnter(MouseEventArgs e)
  215. {
  216. base.OnMouseEnter(e);
  217. this.HoverTimerStart();
  218. this.UpdateBackgroundVisibility();
  219. }
  220. protected override void OnMouseLeave(MouseEventArgs e)
  221. {
  222. this.HoverTimerStop();
  223. base.OnMouseLeave(e);
  224. this.UpdateBackgroundVisibility();
  225. }
  226. protected override void OnMouseMove(MouseEventArgs e)
  227. {
  228. this.HoverTimerRestart();
  229. base.OnMouseMove(e);
  230. }
  231. protected override void OnKeyDown(KeyEventArgs e)
  232. {
  233. base.OnKeyDown(e);
  234. }
  235. private static void OnHoverDelayChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  236. {
  237. MuchinfoPagerNavigationButton radButton = d as MuchinfoPagerNavigationButton;
  238. if (radButton != null)
  239. {
  240. radButton.HoverTimerApplyState(true);
  241. }
  242. }
  243. private static void OnCornerRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  244. {
  245. MuchinfoPagerNavigationButton radButton = d as MuchinfoPagerNavigationButton;
  246. CornerRadius cornerRadius = (CornerRadius)e.NewValue;
  247. if (radButton != null)
  248. {
  249. radButton.InnerCornerRadius = new CornerRadius(Math.Max(0.0, cornerRadius.TopLeft - 1.0), Math.Max(0.0, cornerRadius.TopRight - 1.0), Math.Max(0.0, cornerRadius.BottomRight - 1.0), Math.Max(0.0, cornerRadius.BottomLeft - 1.0));
  250. }
  251. }
  252. private static void OnIsBackgroundVisiblePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  253. {
  254. MuchinfoPagerNavigationButton radButton = d as MuchinfoPagerNavigationButton;
  255. if (radButton != null)
  256. {
  257. radButton.UpdateBackgroundVisibility();
  258. }
  259. }
  260. private void OnHoverTimerTick(object sender, EventArgs e)
  261. {
  262. this.HoverTimerStop();
  263. this.OnHover();
  264. }
  265. private void HoverTimerDestroy()
  266. {
  267. if (this.hoverTimer != null)
  268. {
  269. this.hoverTimer.Stop();
  270. this.hoverTimer.Tick -= new EventHandler(this.OnHoverTimerTick);
  271. this.hoverTimer = null;
  272. }
  273. }
  274. private void HoverTimerStart()
  275. {
  276. if (this.HoverTimerApplyState(false))
  277. {
  278. this.hoverTimer.Start();
  279. }
  280. }
  281. private void HoverTimerStop()
  282. {
  283. if (this.HoverTimerApplyState(false))
  284. {
  285. this.hoverTimer.Stop();
  286. }
  287. }
  288. private void HoverTimerRestart()
  289. {
  290. if (this.HoverTimerApplyState(false))
  291. {
  292. this.hoverTimer.Start();
  293. }
  294. }
  295. private bool HoverTimerApplyState(bool applyAction)
  296. {
  297. double totalMilliseconds = this.HoverDelay.TotalMilliseconds;
  298. if (totalMilliseconds > 0.0)
  299. {
  300. if (this.hoverTimer == null)
  301. {
  302. this.hoverTimer = new DispatcherTimer();
  303. this.hoverTimer.Tick += new EventHandler(this.OnHoverTimerTick);
  304. }
  305. if (totalMilliseconds != this.hoverTimer.Interval.TotalMilliseconds)
  306. {
  307. this.hoverTimer.Interval = this.HoverDelay;
  308. }
  309. if (applyAction)
  310. {
  311. if (base.IsMouseOver)
  312. {
  313. this.HoverTimerStart();
  314. }
  315. else
  316. {
  317. this.HoverTimerStop();
  318. }
  319. }
  320. }
  321. else
  322. {
  323. this.HoverTimerDestroy();
  324. }
  325. return this.hoverTimer != null;
  326. }
  327. }
  328. }