Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Resizer.cs228 linesDownload Raw Back to CommonControls
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.ComponentModel;6using System.Diagnostics;7using System.Windows;8using System.Windows.Controls;9using System.Windows.Controls.Primitives;10using System.Windows.Documents;11 12namespace Microsoft.Management.UI.Internal13{14    /// <summary>15    /// The resize grip possibilities.16    /// </summary>17    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes")]18    public enum ResizeGripLocation19    {20        /// <summary>21        /// One grip is shown, on the right side.22        /// </summary>23        Right,24 25        /// <summary>26        /// One grip is shown, on the left side.27        /// </summary>28        Left,29    }30 31    /// <content>32    /// Partial class implementation for Resizer control.33    /// </content>34    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes")]35    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")]36    public partial class Resizer : ContentControl37    {38        private AdornerLayer adornerLayer;39        private UIElementAdorner adorner;40        private ContentControl adornerContent;41 42        /// <summary>43        /// Creates an instance of Resizer.44        /// </summary>45        public Resizer()46        {47            // nothing48        }49 50        internal static Thickness CreateGripThickness(double visibleGripWidth, ResizeGripLocation gripLocation)51        {52            Thickness thickness;53 54            if (visibleGripWidth < 0.0 || double.IsNaN(visibleGripWidth))55            {56                throw new ArgumentOutOfRangeException("visibleGripWidth", "The value must be greater than or equal to 0.");57            }58 59            if (double.IsInfinity(visibleGripWidth))60            {61                throw new ArgumentOutOfRangeException("visibleGripWidth", "The value must be less than infinity.");62            }63 64            switch (gripLocation)65            {66                case ResizeGripLocation.Right:67                    thickness = new Thickness(0, 0, visibleGripWidth, 0);68                    break;69                case ResizeGripLocation.Left:70                    thickness = new Thickness(visibleGripWidth, 0, 0, 0);71                    break;72                default:73                    throw new InvalidEnumArgumentException("gripLocation", (int)gripLocation, typeof(ResizeGripLocation));74            }75 76            return thickness;77        }78 79        partial void PreOnApplyTemplate()80        {81            if (this.rightGrip != null)82            {83                this.rightGrip.DragDelta -= this.OnRightGripDragDelta;84                this.rightGrip.DragStarted -= this.OnRightGripDragStarted;85                this.rightGrip.DragCompleted -= this.OnRightGripDragCompleted;86            }87 88            if (this.leftGrip != null)89            {90                this.leftGrip.DragDelta -= this.OnLeftGripDragDelta;91                this.leftGrip.DragStarted -= this.OnLeftGripDragStarted;92                this.leftGrip.DragCompleted -= this.OnLeftGripDragCompleted;93            }94        }95 96        partial void PostOnApplyTemplate()97        {98            this.rightGrip.DragDelta += this.OnRightGripDragDelta;99            this.rightGrip.DragStarted += this.OnRightGripDragStarted;100            this.rightGrip.DragCompleted += this.OnRightGripDragCompleted;101 102            this.leftGrip.DragDelta += this.OnLeftGripDragDelta;103            this.leftGrip.DragStarted += this.OnLeftGripDragStarted;104            this.leftGrip.DragCompleted += this.OnLeftGripDragCompleted;105        }106 107        private void CreateAdorner()108        {109            this.adornerLayer = AdornerLayer.GetAdornerLayer(this);110 111            this.adorner = new UIElementAdorner(this);112 113            this.adornerContent = new ContentControl();114            this.adornerContent.Name = "ResizerAdornerContent";115            this.adornerContent.HorizontalContentAlignment = HorizontalAlignment.Stretch;116            this.adornerContent.VerticalContentAlignment = VerticalAlignment.Stretch;117            this.adornerContent.ContentTemplate = this.DraggingTemplate;118            this.adorner.Child = this.adornerContent;119        }120 121        private void RemoveAdorner()122        {123            this.adornerLayer.Remove(this.adorner);124        }125 126        private void OnLeftGripDragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)127        {128            this.StopDragging(ResizeGripLocation.Left, e);129        }130 131        private void OnLeftGripDragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)132        {133            this.StartDragging(ResizeGripLocation.Left);134        }135 136        private void OnLeftGripDragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)137        {138            this.PerformDrag(ResizeGripLocation.Left, e);139        }140 141        private void OnRightGripDragCompleted(object sender, DragCompletedEventArgs e)142        {143            this.StopDragging(ResizeGripLocation.Right, e);144        }145 146        private void OnRightGripDragStarted(object sender, DragStartedEventArgs e)147        {148            this.StartDragging(ResizeGripLocation.Right);149        }150 151        private void OnRightGripDragDelta(object sender, DragDeltaEventArgs e)152        {153            this.PerformDrag(ResizeGripLocation.Right, e);154        }155 156        private void PerformDrag(ResizeGripLocation location, DragDeltaEventArgs e)157        {158            double newWidth = this.GetNewWidth(location, e.HorizontalChange);159 160            if (this.ResizeWhileDragging)161            {162                this.Width = newWidth;163            }164            else165            {166                this.adorner.Width = newWidth;167            }168        }169 170        private void StartDragging(ResizeGripLocation location)171        {172            if (this.ResizeWhileDragging == false)173            {174                if (this.adornerLayer == null)175                {176                    this.CreateAdorner();177                }178 179                this.adornerContent.Content = location;180                this.adornerLayer.Add(this.adorner);181                this.adorner.Height = this.ActualHeight;182                this.adorner.Width = this.ActualWidth;183            }184        }185 186        private void StopDragging(ResizeGripLocation location, DragCompletedEventArgs e)187        {188            if (this.ResizeWhileDragging == false)189            {190                this.RemoveAdorner();191                double newWidth = this.GetNewWidth(location, e.HorizontalChange);192                this.Width = newWidth;193            }194        }195 196        private double GetNewWidth(ResizeGripLocation location, double horzDelta)197        {198            var realDelta = this.GetHorizontalDelta(location, horzDelta);199 200            double newWidth = this.ActualWidth + realDelta;201 202            return this.GetConstrainedValue(newWidth, this.MaxWidth, this.MinWidth);203        }204 205        private double GetHorizontalDelta(ResizeGripLocation location, double horzDelta)206        {207            double realDelta;208 209            if (location == ResizeGripLocation.Right)210            {211                realDelta = horzDelta;212            }213            else214            {215                Debug.Assert(location == ResizeGripLocation.Left, "location is left");216                realDelta = -horzDelta;217            }218 219            return realDelta;220        }221 222        private double GetConstrainedValue(double value, double max, double min)223        {224            return Math.Min(max, Math.Max(value, min));225        }226    }227}228