|
| 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 | + |
| 19 | +using System; |
| 20 | +using System.Windows; |
| 21 | +using System.Windows.Controls; |
| 22 | +using System.Windows.Data; |
| 23 | +using System.Windows.Input; |
| 24 | +using System.Windows.Media.Animation; |
| 25 | + |
| 26 | +namespace ICSharpCode.WpfDesign.Designer.Controls |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Allows animated collapsing of the content of this panel. |
| 30 | + /// </summary> |
| 31 | + public class CollapsiblePanel : ContentControl |
| 32 | + { |
| 33 | + static CollapsiblePanel() |
| 34 | + { |
| 35 | + DefaultStyleKeyProperty.OverrideMetadata(typeof(CollapsiblePanel), |
| 36 | + new FrameworkPropertyMetadata(typeof(CollapsiblePanel))); |
| 37 | + FocusableProperty.OverrideMetadata(typeof(CollapsiblePanel), |
| 38 | + new FrameworkPropertyMetadata(false)); |
| 39 | + } |
| 40 | + |
| 41 | + public static readonly DependencyProperty IsCollapsedProperty = DependencyProperty.Register( |
| 42 | + "IsCollapsed", typeof(bool), typeof(CollapsiblePanel), |
| 43 | + new UIPropertyMetadata(false, new PropertyChangedCallback(OnIsCollapsedChanged))); |
| 44 | + |
| 45 | + public bool IsCollapsed { |
| 46 | + get { return (bool)GetValue(IsCollapsedProperty); } |
| 47 | + set { SetValue(IsCollapsedProperty, value); } |
| 48 | + } |
| 49 | + |
| 50 | + public static readonly DependencyProperty CollapseOrientationProperty = |
| 51 | + DependencyProperty.Register("CollapseOrientation", typeof(Orientation), typeof(CollapsiblePanel), |
| 52 | + new FrameworkPropertyMetadata(Orientation.Vertical)); |
| 53 | + |
| 54 | + public Orientation CollapseOrientation { |
| 55 | + get { return (Orientation)GetValue(CollapseOrientationProperty); } |
| 56 | + set { SetValue(CollapseOrientationProperty, value); } |
| 57 | + } |
| 58 | + |
| 59 | + public static readonly DependencyProperty DurationProperty = DependencyProperty.Register( |
| 60 | + "Duration", typeof(TimeSpan), typeof(CollapsiblePanel), |
| 61 | + new UIPropertyMetadata(TimeSpan.FromMilliseconds(250))); |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// The duration in milliseconds of the animation. |
| 65 | + /// </summary> |
| 66 | + public TimeSpan Duration { |
| 67 | + get { return (TimeSpan)GetValue(DurationProperty); } |
| 68 | + set { SetValue(DurationProperty, value); } |
| 69 | + } |
| 70 | + |
| 71 | + protected internal static readonly DependencyProperty AnimationProgressProperty = DependencyProperty.Register( |
| 72 | + "AnimationProgress", typeof(double), typeof(CollapsiblePanel), |
| 73 | + new FrameworkPropertyMetadata(1.0)); |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Value between 0 and 1 specifying how far the animation currently is. |
| 77 | + /// </summary> |
| 78 | + protected internal double AnimationProgress { |
| 79 | + get { return (double)GetValue(AnimationProgressProperty); } |
| 80 | + set { SetValue(AnimationProgressProperty, value); } |
| 81 | + } |
| 82 | + |
| 83 | + protected internal static readonly DependencyProperty AnimationProgressXProperty = DependencyProperty.Register( |
| 84 | + "AnimationProgressX", typeof(double), typeof(CollapsiblePanel), |
| 85 | + new FrameworkPropertyMetadata(1.0)); |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Value between 0 and 1 specifying how far the animation currently is. |
| 89 | + /// </summary> |
| 90 | + protected internal double AnimationProgressX { |
| 91 | + get { return (double)GetValue(AnimationProgressXProperty); } |
| 92 | + set { SetValue(AnimationProgressXProperty, value); } |
| 93 | + } |
| 94 | + |
| 95 | + protected internal static readonly DependencyProperty AnimationProgressYProperty = DependencyProperty.Register( |
| 96 | + "AnimationProgressY", typeof(double), typeof(CollapsiblePanel), |
| 97 | + new FrameworkPropertyMetadata(1.0)); |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Value between 0 and 1 specifying how far the animation currently is. |
| 101 | + /// </summary> |
| 102 | + protected internal double AnimationProgressY { |
| 103 | + get { return (double)GetValue(AnimationProgressYProperty); } |
| 104 | + set { SetValue(AnimationProgressYProperty, value); } |
| 105 | + } |
| 106 | + |
| 107 | + static void OnIsCollapsedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 108 | + { |
| 109 | + ((CollapsiblePanel)d).SetupAnimation((bool)e.NewValue); |
| 110 | + } |
| 111 | + |
| 112 | + void SetupAnimation(bool isCollapsed) |
| 113 | + { |
| 114 | + if (this.IsLoaded) { |
| 115 | + // If the animation is already running, calculate remaining portion of the time |
| 116 | + double currentProgress = AnimationProgress; |
| 117 | + if (!isCollapsed) { |
| 118 | + currentProgress = 1.0 - currentProgress; |
| 119 | + } |
| 120 | + |
| 121 | + DoubleAnimation animation = new DoubleAnimation(); |
| 122 | + animation.To = isCollapsed ? 0.0 : 1.0; |
| 123 | + animation.Duration = TimeSpan.FromSeconds(Duration.TotalSeconds * currentProgress); |
| 124 | + animation.FillBehavior = FillBehavior.HoldEnd; |
| 125 | + |
| 126 | + this.BeginAnimation(AnimationProgressProperty, animation); |
| 127 | + if (CollapseOrientation == Orientation.Horizontal) { |
| 128 | + this.BeginAnimation(AnimationProgressXProperty, animation); |
| 129 | + this.AnimationProgressY = 1.0; |
| 130 | + } else { |
| 131 | + this.AnimationProgressX = 1.0; |
| 132 | + this.BeginAnimation(AnimationProgressYProperty, animation); |
| 133 | + } |
| 134 | + } else { |
| 135 | + this.AnimationProgress = isCollapsed ? 0.0 : 1.0; |
| 136 | + this.AnimationProgressX = (CollapseOrientation == Orientation.Horizontal) ? this.AnimationProgress : 1.0; |
| 137 | + this.AnimationProgressY = (CollapseOrientation == Orientation.Vertical) ? this.AnimationProgress : 1.0; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + sealed class CollapsiblePanelProgressToVisibilityConverter : IValueConverter |
| 143 | + { |
| 144 | + public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) |
| 145 | + { |
| 146 | + if (value is double) |
| 147 | + return (double)value > 0 ? Visibility.Visible : Visibility.Collapsed; |
| 148 | + else |
| 149 | + return Visibility.Visible; |
| 150 | + } |
| 151 | + |
| 152 | + public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) |
| 153 | + { |
| 154 | + throw new NotImplementedException(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public class SelfCollapsingPanel : CollapsiblePanel |
| 159 | + { |
| 160 | + public static readonly DependencyProperty CanCollapseProperty = |
| 161 | + DependencyProperty.Register("CanCollapse", typeof(bool), typeof(SelfCollapsingPanel), |
| 162 | + new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnCanCollapseChanged))); |
| 163 | + |
| 164 | + public bool CanCollapse { |
| 165 | + get { return (bool)GetValue(CanCollapseProperty); } |
| 166 | + set { SetValue(CanCollapseProperty, value); } |
| 167 | + } |
| 168 | + |
| 169 | + static void OnCanCollapseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 170 | + { |
| 171 | + SelfCollapsingPanel panel = (SelfCollapsingPanel)d; |
| 172 | + if ((bool)e.NewValue) { |
| 173 | + if (!panel.HeldOpenByMouse) |
| 174 | + panel.IsCollapsed = true; |
| 175 | + } else { |
| 176 | + panel.IsCollapsed = false; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + bool HeldOpenByMouse { |
| 181 | + get { return IsMouseOver || IsMouseCaptureWithin; } |
| 182 | + } |
| 183 | + |
| 184 | + protected override void OnMouseLeave(MouseEventArgs e) |
| 185 | + { |
| 186 | + base.OnMouseLeave(e); |
| 187 | + if (CanCollapse && !HeldOpenByMouse) |
| 188 | + IsCollapsed = true; |
| 189 | + } |
| 190 | + |
| 191 | + protected override void OnLostMouseCapture(MouseEventArgs e) |
| 192 | + { |
| 193 | + base.OnLostMouseCapture(e); |
| 194 | + if (CanCollapse && !HeldOpenByMouse) |
| 195 | + IsCollapsed = true; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments