From d84e660230f86c6d2d3622c861324deffe3f99fb Mon Sep 17 00:00:00 2001 From: Lachlan Ennis <2433737+elachlan@users.noreply.github.com> Date: Wed, 22 Feb 2023 10:48:24 +1000 Subject: [PATCH] Refactor DropSourceBehaviour and related code to replace ArrayList with List --- .../Behavior/ContainerSelectorBehavior.cs | 2 +- .../Design/Behavior/DropSourceBehavior.cs | 18 +++++++++--------- .../Forms/Design/Behavior/SelectionManager.cs | 14 +++++--------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/ContainerSelectorBehavior.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/ContainerSelectorBehavior.cs index bc2ccf592ff..87e07695121 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/ContainerSelectorBehavior.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/ContainerSelectorBehavior.cs @@ -207,7 +207,7 @@ private void StartDragOperation(Point initialMouseLocation) //must identify a required parent to avoid dragging mixes of children Control requiredParent = _containerControl.Parent; - ArrayList dragControls = new ArrayList(); + List dragControls = new(); ICollection selComps = selSvc.GetSelectedComponents(); //create our list of controls-to-drag foreach (IComponent comp in selComps) diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/DropSourceBehavior.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/DropSourceBehavior.cs index 8f0a30b88f6..e12848b6dbd 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/DropSourceBehavior.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/DropSourceBehavior.cs @@ -19,7 +19,7 @@ internal sealed partial class DropSourceBehavior : Behavior, IComparer { private struct DragComponent { - public object dragComponent; //the dragComponent + public IComponent dragComponent; //the dragComponent public int zorderIndex; //the dragComponent's z-order index public Point originalControlLocation; //the original control of the control in AdornerWindow coordinates public Point draggedLocation; //the location of the component after each drag - in AdornerWindow coordinates @@ -72,9 +72,9 @@ private struct DragComponent private int primaryComponentIndex = -1; // Index of the primary component (control) in dragComponents /// - /// Constuctor that caches all needed vars for perf reasons. + /// Constructor that caches all needed variables for perf reasons. /// - internal DropSourceBehavior(ICollection dragComponents, Control source, Point initialMouseLocation) + internal DropSourceBehavior(List dragComponents, Control source, Point initialMouseLocation) { serviceProviderSource = source.Site; if (serviceProviderSource is null) @@ -321,7 +321,7 @@ private void EndDragDrop(bool allowSetChildIndexOnDrop) } // We use this list when doing a Drag-Copy, so that we can correctly restore state when we are done. See Copy code below. - ArrayList originalControls = null; + List originalControls = null; bool performCopy = (lastEffect == DragDropEffects.Copy); Control dragSource = data.Source; @@ -385,21 +385,21 @@ private void EndDragDrop(bool allowSetChildIndexOnDrop) numberOfOriginalTrayControls = tray is not null ? tray.Controls.Count : 0; // Get the objects to copy - ArrayList temp = new ArrayList(); + List temp = new(); for (int i = 0; i < dragComponents.Length; i++) { temp.Add(dragComponents[i].dragComponent); } // Create a copy of them - temp = DesignerUtils.CopyDragObjects(temp, serviceProviderTarget) as ArrayList; + temp = DesignerUtils.CopyDragObjects(temp, serviceProviderTarget).Cast().ToList(); if (temp is null) { Debug.Fail("Couldn't create copies of the controls we are dragging."); return; } - originalControls = new ArrayList(); + originalControls = new(); // And stick the copied controls back into the dragComponents array for (int j = 0; j < temp.Count; j++) { @@ -885,9 +885,9 @@ private void DisableAdorners(IServiceProvider serviceProvider, BehaviorService b /// /// Called when the ControlDesigner starts a drag operation. Here, all adorners are disabled, screen shots of all related controls are taken, and the DragAssistanceManager (for SnapLines) is created. /// - private void InitiateDrag(Point initialMouseLocation, ICollection dragComps) + private void InitiateDrag(Point initialMouseLocation, ICollection dragComps) { - dragObjects = dragComps.Cast().ToList(); + dragObjects = dragComps.ToList(); DisableAdorners(serviceProviderSource, behaviorServiceSource, false); Control primaryControl = dragObjects[0] as Control; Control primaryParent = primaryControl?.Parent; diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/SelectionManager.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/SelectionManager.cs index ea939d6c2f5..86a81ca51d9 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/SelectionManager.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/SelectionManager.cs @@ -66,7 +66,7 @@ public SelectionManager(IServiceProvider serviceProvider, BehaviorService behavi _bodyAdorner = new Adorner(); behaviorService.Adorners.Add(_bodyAdorner); behaviorService.Adorners.Add(_selectionAdorner); //adding this will cause the adorner to get setup with a ptr - //to the beh.svc. + //to the beh.svc. _componentToDesigner = new Hashtable(); @@ -282,17 +282,13 @@ private void OnComponentAdded(object source, ComponentEventArgs ce) /// private void OnBeginDrag(object source, BehaviorDragDropEventArgs e) { - ArrayList dragComps = new ArrayList(e.DragComponents); - ArrayList glyphsToRemove = new ArrayList(); + List dragComps = e.DragComponents.Cast().ToList(); + List glyphsToRemove = new(); foreach (ControlBodyGlyph g in _bodyAdorner.Glyphs) { - if (g.RelatedComponent is Control) + if (g.RelatedComponent is Control control && (dragComps.Contains(g.RelatedComponent) || !control.AllowDrop)) { - if (dragComps.Contains(g.RelatedComponent) || - !((Control)g.RelatedComponent).AllowDrop) - { - glyphsToRemove.Add(g); - } + glyphsToRemove.Add(g); } }