Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
Expand All @@ -14,47 +12,34 @@ namespace System.Windows.Forms.Design.Behavior;
/// </summary>
internal sealed class DesignerActionBehavior : Behavior
{
private readonly IComponent _relatedComponent; //The component we are bound to
private readonly DesignerActionUI _parentUI; //ptr to the parenting UI, used for showing menus and setting selection
private DesignerActionListCollection _actionLists; //all the shortcuts!
private readonly IServiceProvider _serviceProvider; // we need to cache the service provider here to be able to create the panel with the proper arguments
private readonly IServiceProvider? _serviceProvider; // we need to cache the service provider here to be able to create the panel with the proper arguments
private bool _ignoreNextMouseUp;

/// <summary>
/// Constructor that calls base and caches off the action lists.
/// </summary>
internal DesignerActionBehavior(IServiceProvider serviceProvider, IComponent relatedComponent, DesignerActionListCollection actionLists, DesignerActionUI parentUI)
internal DesignerActionBehavior(IServiceProvider? serviceProvider, IComponent relatedComponent, DesignerActionListCollection actionLists, DesignerActionUI parentUI)
{
_actionLists = actionLists;
ActionLists = actionLists;
_serviceProvider = serviceProvider;
_relatedComponent = relatedComponent;
_parentUI = parentUI;
RelatedComponent = relatedComponent;
ParentUI = parentUI;
}

/// <summary>
/// Returns the collection of DesignerActionLists this Behavior is managing. These will be dynamically updated (some can be removed, new ones can be added, etc...).
/// </summary>
internal DesignerActionListCollection ActionLists
{
get => _actionLists;
set => _actionLists = value;
}
internal DesignerActionListCollection ActionLists { get; set; }

/// <summary>
/// Returns the parenting UI (a DesignerActionUI)
/// </summary>
internal DesignerActionUI ParentUI
{
get => _parentUI;
}
internal DesignerActionUI ParentUI { get; }

/// <summary>
/// Returns the Component that this glyph is attached to.
/// </summary>
internal IComponent RelatedComponent
{
get => _relatedComponent;
}
internal IComponent RelatedComponent { get; }

/// <summary>
/// Hides the designer action panel UI.
Expand All @@ -79,7 +64,7 @@ internal DesignerActionPanel CreateDesignerActionPanel(IComponent relatedCompone
/// </summary>
internal void ShowUI(Glyph g)
{
if (!(g is DesignerActionGlyph glyph))
if (g is not DesignerActionGlyph glyph)
{
Debug.Fail("Why are we trying to 'showui' on a glyph that's not a DesignerActionGlyph?");
return;
Expand All @@ -97,23 +82,24 @@ internal bool IgnoreNextMouseUp
}
}

public override bool OnMouseDoubleClick(Glyph g, MouseButtons button, Point mouseLoc)
public override bool OnMouseDoubleClick(Glyph? g, MouseButtons button, Point mouseLoc)
{
_ignoreNextMouseUp = true;
return true;
}

public override bool OnMouseDown(Glyph g, MouseButtons button, Point mouseLoc)
{ // we take the msg
public override bool OnMouseDown(Glyph? g, MouseButtons button, Point mouseLoc)
{
// we take the msg
return (!ParentUI.IsDesignerActionPanelVisible);
}

/// <summary>
/// In response to a MouseUp, we will either 1) select the Glyph and control if not selected, or 2) Build up our context menu representing our DesignerActions and show it.
/// </summary>
public override bool OnMouseUp(Glyph g, MouseButtons button)
public override bool OnMouseUp(Glyph? g, MouseButtons button)
{
if (button != MouseButtons.Left || ParentUI is null)
if (button != MouseButtons.Left)
{
return true;
}
Expand All @@ -125,23 +111,17 @@ public override bool OnMouseUp(Glyph g, MouseButtons button)
}
else if (!_ignoreNextMouseUp)
{
if (_serviceProvider is not null)
if (_serviceProvider.TryGetService(out ISelectionService? selectionService) &&
selectionService.PrimarySelection != RelatedComponent)
{
ISelectionService selectionService = (ISelectionService)_serviceProvider.GetService(typeof(ISelectionService));
if (selectionService is not null)
{
if (selectionService.PrimarySelection != RelatedComponent)
{
List<IComponent> componentList = new List<IComponent>
{
RelatedComponent
};
selectionService.SetSelectedComponents(componentList, SelectionTypes.Primary);
}
}
List<IComponent> componentList = [RelatedComponent];
selectionService.SetSelectedComponents(componentList, SelectionTypes.Primary);
}

ShowUI(g);
if (g is not null)
{
ShowUI(g);
}
}
else
{
Expand Down