You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
7.4 KiB

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Data;
  22. using System.Windows.Input;
  23. using System.Windows.Media.Animation;
  24. using System.Windows.Threading;
  25. namespace ICSharpCode.ILSpy.Controls
  26. {
  27. /// <summary>
  28. /// Allows animated collapsing of the content of this panel.
  29. /// </summary>
  30. public class CollapsiblePanel : ContentControl
  31. {
  32. static CollapsiblePanel()
  33. {
  34. DefaultStyleKeyProperty.OverrideMetadata(typeof(CollapsiblePanel),
  35. new FrameworkPropertyMetadata(typeof(CollapsiblePanel)));
  36. FocusableProperty.OverrideMetadata(typeof(CollapsiblePanel),
  37. new FrameworkPropertyMetadata(false));
  38. }
  39. public static readonly DependencyProperty IsCollapsedProperty = DependencyProperty.Register(
  40. "IsCollapsed", typeof(bool), typeof(CollapsiblePanel),
  41. new UIPropertyMetadata(false, new PropertyChangedCallback(OnIsCollapsedChanged)));
  42. public bool IsCollapsed {
  43. get { return (bool)GetValue(IsCollapsedProperty); }
  44. set { SetValue(IsCollapsedProperty, value); }
  45. }
  46. public static readonly DependencyProperty CollapseOrientationProperty =
  47. DependencyProperty.Register("CollapseOrientation", typeof(Orientation), typeof(CollapsiblePanel),
  48. new FrameworkPropertyMetadata(Orientation.Vertical));
  49. public Orientation CollapseOrientation {
  50. get { return (Orientation)GetValue(CollapseOrientationProperty); }
  51. set { SetValue(CollapseOrientationProperty, value); }
  52. }
  53. public static readonly DependencyProperty DurationProperty = DependencyProperty.Register(
  54. "Duration", typeof(TimeSpan), typeof(CollapsiblePanel),
  55. new UIPropertyMetadata(TimeSpan.FromMilliseconds(250)));
  56. /// <summary>
  57. /// The duration in milliseconds of the animation.
  58. /// </summary>
  59. public TimeSpan Duration {
  60. get { return (TimeSpan)GetValue(DurationProperty); }
  61. set { SetValue(DurationProperty, value); }
  62. }
  63. protected internal static readonly DependencyProperty AnimationProgressProperty = DependencyProperty.Register(
  64. "AnimationProgress", typeof(double), typeof(CollapsiblePanel),
  65. new FrameworkPropertyMetadata(1.0));
  66. /// <summary>
  67. /// Value between 0 and 1 specifying how far the animation currently is.
  68. /// </summary>
  69. protected internal double AnimationProgress {
  70. get { return (double)GetValue(AnimationProgressProperty); }
  71. set { SetValue(AnimationProgressProperty, value); }
  72. }
  73. protected internal static readonly DependencyProperty AnimationProgressXProperty = DependencyProperty.Register(
  74. "AnimationProgressX", typeof(double), typeof(CollapsiblePanel),
  75. new FrameworkPropertyMetadata(1.0));
  76. /// <summary>
  77. /// Value between 0 and 1 specifying how far the animation currently is.
  78. /// </summary>
  79. protected internal double AnimationProgressX {
  80. get { return (double)GetValue(AnimationProgressXProperty); }
  81. set { SetValue(AnimationProgressXProperty, value); }
  82. }
  83. protected internal static readonly DependencyProperty AnimationProgressYProperty = DependencyProperty.Register(
  84. "AnimationProgressY", typeof(double), typeof(CollapsiblePanel),
  85. new FrameworkPropertyMetadata(1.0));
  86. /// <summary>
  87. /// Value between 0 and 1 specifying how far the animation currently is.
  88. /// </summary>
  89. protected internal double AnimationProgressY {
  90. get { return (double)GetValue(AnimationProgressYProperty); }
  91. set { SetValue(AnimationProgressYProperty, value); }
  92. }
  93. static void OnIsCollapsedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  94. {
  95. ((CollapsiblePanel)d).SetupAnimation((bool)e.NewValue);
  96. }
  97. void SetupAnimation(bool isCollapsed)
  98. {
  99. if (this.IsLoaded)
  100. {
  101. // If the animation is already running, calculate remaining portion of the time
  102. double currentProgress = AnimationProgress;
  103. if (!isCollapsed)
  104. {
  105. currentProgress = 1.0 - currentProgress;
  106. }
  107. DoubleAnimation animation = new DoubleAnimation();
  108. animation.To = isCollapsed ? 0.0 : 1.0;
  109. animation.Duration = TimeSpan.FromSeconds(Duration.TotalSeconds * currentProgress);
  110. animation.FillBehavior = FillBehavior.HoldEnd;
  111. this.BeginAnimation(AnimationProgressProperty, animation);
  112. if (CollapseOrientation == Orientation.Horizontal)
  113. {
  114. this.BeginAnimation(AnimationProgressXProperty, animation);
  115. this.AnimationProgressY = 1.0;
  116. }
  117. else
  118. {
  119. this.AnimationProgressX = 1.0;
  120. this.BeginAnimation(AnimationProgressYProperty, animation);
  121. }
  122. }
  123. else
  124. {
  125. this.AnimationProgress = isCollapsed ? 0.0 : 1.0;
  126. this.AnimationProgressX = (CollapseOrientation == Orientation.Horizontal) ? this.AnimationProgress : 1.0;
  127. this.AnimationProgressY = (CollapseOrientation == Orientation.Vertical) ? this.AnimationProgress : 1.0;
  128. }
  129. }
  130. }
  131. sealed class CollapsiblePanelProgressToVisibilityConverter : IValueConverter
  132. {
  133. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  134. {
  135. if (value is double)
  136. return (double)value > 0 ? Visibility.Visible : Visibility.Collapsed;
  137. else
  138. return Visibility.Visible;
  139. }
  140. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  141. {
  142. throw new NotImplementedException();
  143. }
  144. }
  145. public class SelfCollapsingPanel : CollapsiblePanel
  146. {
  147. public static readonly DependencyProperty CanCollapseProperty =
  148. DependencyProperty.Register("CanCollapse", typeof(bool), typeof(SelfCollapsingPanel),
  149. new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnCanCollapseChanged)));
  150. public bool CanCollapse {
  151. get { return (bool)GetValue(CanCollapseProperty); }
  152. set { SetValue(CanCollapseProperty, value); }
  153. }
  154. static void OnCanCollapseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  155. {
  156. SelfCollapsingPanel panel = (SelfCollapsingPanel)d;
  157. if ((bool)e.NewValue)
  158. {
  159. if (!panel.HeldOpenByMouse)
  160. panel.IsCollapsed = true;
  161. }
  162. else
  163. {
  164. panel.IsCollapsed = false;
  165. }
  166. }
  167. bool HeldOpenByMouse {
  168. get { return IsMouseOver || IsMouseCaptureWithin; }
  169. }
  170. protected override void OnMouseLeave(MouseEventArgs e)
  171. {
  172. base.OnMouseLeave(e);
  173. if (CanCollapse && !HeldOpenByMouse)
  174. IsCollapsed = true;
  175. }
  176. protected override void OnLostMouseCapture(MouseEventArgs e)
  177. {
  178. base.OnLostMouseCapture(e);
  179. if (CanCollapse && !HeldOpenByMouse)
  180. IsCollapsed = true;
  181. }
  182. }
  183. }