Skip to content

Commit

Permalink
Replace ArrayList with List<T> in ParentControlDesigner (#10289)
Browse files Browse the repository at this point in the history
* Refactor ParentControlDesigner to use List<T> instead of ArrayList

* refactor GetComponentsInRect to avoid allocations
  • Loading branch information
halgab authored Nov 14, 2023
1 parent 90a2b51 commit add92c1
Showing 1 changed file with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -851,12 +851,12 @@ private static SnapLine[] GenerateNewToolSnapLines(Rectangle r)
/// <summary>
/// Finds the array of components within the given rectangle. This uses the rectangle to
/// find controls within our control, and then uses those controls to find the actual
/// components. It returns an object array so the output can be directly fed into
/// components. It returns an object list so the output can be directly fed into
/// the selection service.
/// </summary>
internal object[] GetComponentsInRect(Rectangle value, bool screenCoords, bool containRect)
internal List<Control> GetComponentsInRect(Rectangle value, bool screenCoords, bool containRect)
{
ArrayList list = new ArrayList();
List<Control> list = new();
Rectangle rect = screenCoords ? Control.RectangleToClient(value) : value;

IContainer container = Component.Site.Container;
Expand All @@ -878,7 +878,7 @@ internal object[] GetComponentsInRect(Rectangle value, bool screenCoords, bool c
}
}

return list.ToArray();
return list;
}

/// <summary>
Expand Down Expand Up @@ -1255,16 +1255,14 @@ public override void InitializeNewComponent(IDictionary defaultValues)
return;
}

object[] comps = parentDesigner.GetComponentsInRect(bounds, true, true /* component should be fully contained*/);
List<Control> selectedControls = parentDesigner.GetComponentsInRect(bounds, true, true /* component should be fully contained*/);

if (comps is null || comps.Length == 0)
if (selectedControls is null || selectedControls.Count == 0)
{
//no comps to re-parent
return;
}

ArrayList selectedControls = new ArrayList(comps);

//remove this
if (selectedControls.Contains(Control))
{
Expand Down Expand Up @@ -1889,8 +1887,8 @@ protected override void OnMouseDragEnd(bool cancel)
var selSvc = (ISelectionService)GetService(typeof(ISelectionService));
if (selSvc is not null)
{
object[] selection = GetComponentsInRect(offset, true, false /*component does not need to be fully contained*/);
if (selection.Length > 0)
List<Control> selection = GetComponentsInRect(offset, true, false /*component does not need to be fully contained*/);
if (selection.Count > 0)
{
selSvc.SetSelectedComponents(selection);
}
Expand Down Expand Up @@ -2135,7 +2133,7 @@ protected override void PreFilterProperties(IDictionary properties)
/// Control in the toolbox then drags a rectangle around four Buttons on the Form's surface. We'll attempt
/// to re-parent those four Buttons to the newly created Panel.
/// </summary>
private void ReParentControls(Control newParent, ArrayList controls, string transactionName, IDesignerHost host)
private void ReParentControls(Control newParent, List<Control> controls, string transactionName, IDesignerHost host)
{
using DesignerTransaction dt = host.CreateTransaction(transactionName);
var changeService = GetService<IComponentChangeService>();
Expand All @@ -2154,9 +2152,8 @@ private void ReParentControls(Control newParent, ArrayList controls, string tran
changeService?.OnComponentChanging(newParent, controlsProp);

//enumerate the lasso'd controls relocate and re-parent...
foreach (object comp in controls)
foreach (Control control in controls)
{
Control control = comp as Control;
Control oldParent = control.Parent;
Point controlLoc = Point.Empty;

Expand Down

0 comments on commit add92c1

Please sign in to comment.