Skip to content

Commit

Permalink
Refactor SelectionManager to replace Hashtable with Dictionary (#…
Browse files Browse the repository at this point in the history
…8664)

* Refactor SelectionManager to replace Hashtable with Dictionary

* changes from review
  • Loading branch information
elachlan authored Feb 22, 2023
1 parent 46195b1 commit bba30b5
Showing 1 changed file with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal sealed class SelectionManager : IDisposable
private Adorner _bodyAdorner; //used to track all body glyphs for each control
private BehaviorService _behaviorService; //ptr back to our BehaviorService
private IServiceProvider _serviceProvider; //standard service provider
private readonly Hashtable _componentToDesigner; //used for quick look up of designers related to comps
private readonly Dictionary<IComponent, ControlDesigner> _componentToDesigner; //used for quick look up of designers related to components
private readonly Control _rootComponent; //root component being designed
private ISelectionService _selSvc; //we cache the selection service for perf.
private IDesignerHost _designerHost; //we cache the designerhost for perf.
Expand Down Expand Up @@ -65,10 +65,10 @@ public SelectionManager(IServiceProvider serviceProvider, BehaviorService behavi
_selectionAdorner = new Adorner();
_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.
behaviorService.Adorners.Add(_selectionAdorner); // adding this will cause the adorner to get setup with a ptr
// to the beh.svc.

_componentToDesigner = new Hashtable();
_componentToDesigner = new();

IComponentChangeService cs = (IComponentChangeService)serviceProvider.GetService(typeof(IComponentChangeService));
if (cs is not null)
Expand Down Expand Up @@ -152,12 +152,11 @@ private void AddAllControlGlyphs(Control parent, ArrayList selComps, object prim
/// <summary>
/// Recursive method that goes through and adds all the glyphs of every child to our global Adorner.
/// </summary>
private void AddControlGlyphs(Control c, GlyphSelectionType selType)
private void AddControlGlyphs(Control control, GlyphSelectionType selType)
{
ControlDesigner cd = (ControlDesigner)_componentToDesigner[c];
if (cd is not null)
if (_componentToDesigner.TryGetValue(control, out ControlDesigner controlDesigner) && controlDesigner is not null)
{
ControlBodyGlyph bodyGlyph = cd.GetControlGlyphInternal(selType);
ControlBodyGlyph bodyGlyph = controlDesigner.GetControlGlyphInternal(selType);
if (bodyGlyph is not null)
{
_bodyAdorner.Glyphs.Add(bodyGlyph);
Expand All @@ -175,7 +174,7 @@ private void AddControlGlyphs(Control c, GlyphSelectionType selType)
}
}

GlyphCollection glyphs = cd.GetGlyphs(selType);
GlyphCollection glyphs = controlDesigner.GetGlyphs(selType);
if (glyphs is not null)
{
_selectionAdorner.Glyphs.AddRange(glyphs);
Expand Down Expand Up @@ -271,9 +270,9 @@ private void OnComponentAdded(object source, ComponentEventArgs ce)
{
IComponent component = ce.Component;
IDesigner designer = _designerHost.GetDesigner(component);
if (designer is ControlDesigner)
if (designer is ControlDesigner controlDesigner)
{
_componentToDesigner.Add(component, designer);
_componentToDesigner.Add(component, controlDesigner);
}
}

Expand Down Expand Up @@ -331,10 +330,7 @@ private void OnComponentChanged(object source, ComponentChangedEventArgs ce)
/// </summary>
private void OnComponentRemoved(object source, ComponentEventArgs ce)
{
if (_componentToDesigner.Contains(ce.Component))
{
_componentToDesigner.Remove(ce.Component);
}
_componentToDesigner.Remove(ce.Component);

//remove the associated designeractionpanel
_designerActionUI?.RemoveActionGlyph(ce.Component);
Expand Down

0 comments on commit bba30b5

Please sign in to comment.