ThumbButtonInfo.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. namespace Microsoft.Windows.Shell;
  7. [DefaultEvent("Click")]
  8. public sealed class ThumbButtonInfo : Freezable, ICommandSource
  9. {
  10. protected override Freezable CreateInstanceCore()
  11. {
  12. return new ThumbButtonInfo();
  13. }
  14. public Visibility Visibility
  15. {
  16. get
  17. {
  18. return (Visibility) base.GetValue(ThumbButtonInfo.VisibilityProperty);
  19. }
  20. set
  21. {
  22. base.SetValue(ThumbButtonInfo.VisibilityProperty, value);
  23. }
  24. }
  25. public bool DismissWhenClicked
  26. {
  27. get
  28. {
  29. return (bool) base.GetValue(ThumbButtonInfo.DismissWhenClickedProperty);
  30. }
  31. set
  32. {
  33. base.SetValue(ThumbButtonInfo.DismissWhenClickedProperty, value);
  34. }
  35. }
  36. public ImageSource ImageSource
  37. {
  38. get
  39. {
  40. return (ImageSource) base.GetValue(ThumbButtonInfo.ImageSourceProperty);
  41. }
  42. set
  43. {
  44. base.SetValue(ThumbButtonInfo.ImageSourceProperty, value);
  45. }
  46. }
  47. public bool IsBackgroundVisible
  48. {
  49. get
  50. {
  51. return (bool) base.GetValue(ThumbButtonInfo.IsBackgroundVisibleProperty);
  52. }
  53. set
  54. {
  55. base.SetValue(ThumbButtonInfo.IsBackgroundVisibleProperty, value);
  56. }
  57. }
  58. public string Description
  59. {
  60. get
  61. {
  62. return (string) base.GetValue(ThumbButtonInfo.DescriptionProperty);
  63. }
  64. set
  65. {
  66. base.SetValue(ThumbButtonInfo.DescriptionProperty, value);
  67. }
  68. }
  69. private static object _CoerceDescription(DependencyObject d, object value)
  70. {
  71. string text = (string) value;
  72. if (text != null && text.Length >= 260)
  73. {
  74. text = text.Substring(0, 259);
  75. }
  76. return text;
  77. }
  78. private object _CoerceIsEnabledValue(object value)
  79. {
  80. bool flag = (bool) value;
  81. return flag && this._CanExecute;
  82. }
  83. public bool IsEnabled
  84. {
  85. get
  86. {
  87. return (bool) base.GetValue(ThumbButtonInfo.IsEnabledProperty);
  88. }
  89. set
  90. {
  91. base.SetValue(ThumbButtonInfo.IsEnabledProperty, value);
  92. }
  93. }
  94. public bool IsInteractive
  95. {
  96. get
  97. {
  98. return (bool) base.GetValue(ThumbButtonInfo.IsInteractiveProperty);
  99. }
  100. set
  101. {
  102. base.SetValue(ThumbButtonInfo.IsInteractiveProperty, value);
  103. }
  104. }
  105. private void _OnCommandChanged(DependencyPropertyChangedEventArgs e)
  106. {
  107. ICommand command = (ICommand) e.OldValue;
  108. ICommand command2 = (ICommand) e.NewValue;
  109. if (command == command2)
  110. {
  111. return;
  112. }
  113. if (command != null)
  114. {
  115. this._UnhookCommand(command);
  116. }
  117. if (command2 != null)
  118. {
  119. this._HookCommand(command2);
  120. }
  121. }
  122. private bool _CanExecute
  123. {
  124. get
  125. {
  126. return (bool) base.GetValue(ThumbButtonInfo._CanExecuteProperty);
  127. }
  128. set
  129. {
  130. base.SetValue(ThumbButtonInfo._CanExecuteProperty, value);
  131. }
  132. }
  133. public event EventHandler Click;
  134. internal void InvokeClick()
  135. {
  136. EventHandler click = this.Click;
  137. if (click != null)
  138. {
  139. click(this, EventArgs.Empty);
  140. }
  141. this._InvokeCommand();
  142. }
  143. private void _InvokeCommand()
  144. {
  145. ICommand command = this.Command;
  146. if (command != null)
  147. {
  148. object commandParameter = this.CommandParameter;
  149. IInputElement commandTarget = this.CommandTarget;
  150. RoutedCommand routedCommand = command as RoutedCommand;
  151. if (routedCommand != null)
  152. {
  153. if (routedCommand.CanExecute(commandParameter, commandTarget))
  154. {
  155. routedCommand.Execute(commandParameter, commandTarget);
  156. return;
  157. }
  158. }
  159. else if (command.CanExecute(commandParameter))
  160. {
  161. command.Execute(commandParameter);
  162. }
  163. }
  164. }
  165. private void _UnhookCommand(ICommand command)
  166. {
  167. command.CanExecuteChanged -= this._commandEvent;
  168. this._commandEvent = null;
  169. this._UpdateCanExecute();
  170. }
  171. private void _HookCommand(ICommand command)
  172. {
  173. this._commandEvent = delegate (object sender, EventArgs e)
  174. {
  175. this._UpdateCanExecute();
  176. };
  177. command.CanExecuteChanged += this._commandEvent;
  178. this._UpdateCanExecute();
  179. }
  180. private void _UpdateCanExecute()
  181. {
  182. if (this.Command == null)
  183. {
  184. this._CanExecute = true;
  185. return;
  186. }
  187. object commandParameter = this.CommandParameter;
  188. IInputElement commandTarget = this.CommandTarget;
  189. RoutedCommand routedCommand = this.Command as RoutedCommand;
  190. if (routedCommand != null)
  191. {
  192. this._CanExecute = routedCommand.CanExecute(commandParameter, commandTarget);
  193. return;
  194. }
  195. this._CanExecute = this.Command.CanExecute(commandParameter);
  196. }
  197. public ICommand Command
  198. {
  199. get
  200. {
  201. return (ICommand) base.GetValue(ThumbButtonInfo.CommandProperty);
  202. }
  203. set
  204. {
  205. base.SetValue(ThumbButtonInfo.CommandProperty, value);
  206. }
  207. }
  208. public object CommandParameter
  209. {
  210. get
  211. {
  212. return base.GetValue(ThumbButtonInfo.CommandParameterProperty);
  213. }
  214. set
  215. {
  216. base.SetValue(ThumbButtonInfo.CommandParameterProperty, value);
  217. }
  218. }
  219. public IInputElement CommandTarget
  220. {
  221. get
  222. {
  223. return (IInputElement) base.GetValue(ThumbButtonInfo.CommandTargetProperty);
  224. }
  225. set
  226. {
  227. base.SetValue(ThumbButtonInfo.CommandTargetProperty, value);
  228. }
  229. }
  230. private EventHandler _commandEvent;
  231. public static readonly DependencyProperty VisibilityProperty = DependencyProperty.Register("Visibility", typeof(Visibility), typeof(ThumbButtonInfo), new PropertyMetadata(Visibility.Visible));
  232. public static readonly DependencyProperty DismissWhenClickedProperty = DependencyProperty.Register("DismissWhenClicked", typeof(bool), typeof(ThumbButtonInfo), new PropertyMetadata(false));
  233. public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ThumbButtonInfo), new PropertyMetadata(null));
  234. public static readonly DependencyProperty IsBackgroundVisibleProperty = DependencyProperty.Register("IsBackgroundVisible", typeof(bool), typeof(ThumbButtonInfo), new PropertyMetadata(true));
  235. public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(ThumbButtonInfo), new PropertyMetadata(string.Empty, null, new CoerceValueCallback(ThumbButtonInfo._CoerceDescription)));
  236. public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register("IsEnabled", typeof(bool), typeof(ThumbButtonInfo), new PropertyMetadata(true, null, (DependencyObject d, object e) => ((ThumbButtonInfo) d)._CoerceIsEnabledValue(e)));
  237. public static readonly DependencyProperty IsInteractiveProperty = DependencyProperty.Register("IsInteractive", typeof(bool), typeof(ThumbButtonInfo), new PropertyMetadata(true));
  238. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ThumbButtonInfo), new PropertyMetadata(null, delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
  239. {
  240. ((ThumbButtonInfo) d)._OnCommandChanged(e);
  241. }));
  242. public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ThumbButtonInfo), new PropertyMetadata(null, delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
  243. {
  244. ((ThumbButtonInfo) d)._UpdateCanExecute();
  245. }));
  246. public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(ThumbButtonInfo), new PropertyMetadata(null, delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
  247. {
  248. ((ThumbButtonInfo) d)._UpdateCanExecute();
  249. }));
  250. private static readonly DependencyProperty _CanExecuteProperty = DependencyProperty.Register("_CanExecute", typeof(bool), typeof(ThumbButtonInfo), new PropertyMetadata(true, delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
  251. {
  252. d.CoerceValue(ThumbButtonInfo.IsEnabledProperty);
  253. }));
  254. }