Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CreateComponentTool.cs #112

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions WpfDesign.Designer/Project/PropertyGrid/PropertyGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public PropertyGrid()
Categories.Add(otherCategory);
Categories.Add(attachedCategory);

BasicMetadata.Register();

Events = new PropertyNodeCollection();
}

Expand Down
70 changes: 53 additions & 17 deletions WpfDesign.Designer/Project/Services/CreateComponentTool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 AlphaSierraPapa for the SharpDevelop Team
// Copyright (c) 2019 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
Expand All @@ -16,13 +16,12 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System.Linq;
using System.Windows;
using ICSharpCode.WpfDesign.Designer.Xaml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using ICSharpCode.WpfDesign.Designer.Xaml;

namespace ICSharpCode.WpfDesign.Designer.Services
{
Expand All @@ -34,17 +33,29 @@ public class CreateComponentTool : ITool
protected ChangeGroup ChangeGroup;

readonly Type componentType;
MoveLogic moveLogic;
readonly object[] arguments=null;

MoveLogic moveLogic;
Point createPoint;


public event EventHandler<DesignItem> CreateComponentCompleted;

/// <summary>
/// Creates a new CreateComponentTool instance.
/// </summary>
public CreateComponentTool(Type componentType)
public CreateComponentTool(Type componentType):this(componentType, null)
{
}

/// <summary>
/// Creates a new CreateComponentTool instance.
/// </summary>
public CreateComponentTool(Type componentType, object[] arguments)
{
if (componentType == null)
throw new ArgumentNullException("componentType");
throw new ArgumentNullException(nameof(componentType));
this.componentType = componentType;
this.arguments = arguments;
}

/// <summary>
Expand Down Expand Up @@ -115,7 +126,10 @@ void designPanel_DragOver(object sender, DragEventArgs e)
else
{
DesignItem createdItem = CreateItemWithPosition(designPanel.Context, e.GetPosition(result.ModelHit.View));
if (AddItemsWithDefaultSize(result.ModelHit, new[] { createdItem })) {

CreateComponentCompleted?.Invoke(this, createdItem);

if (AddItemsWithDefaultSize(result.ModelHit, new[] { createdItem })) {
moveLogic = new MoveLogic(createdItem);

if (designPanel.Context.Services.Component is XamlComponentService) {
Expand Down Expand Up @@ -196,7 +210,9 @@ protected virtual DesignItem CreateItem(DesignContext context)
{
if (ChangeGroup == null)
ChangeGroup = context.RootItem.OpenGroup("Add Control");
var item = CreateItem(context, componentType);

var item = CreateItem(context, componentType, arguments);

return item;
}

Expand All @@ -222,7 +238,15 @@ protected virtual DesignItem[] CreateItems(DesignContext context)
/// </summary>
public static DesignItem CreateItem(DesignContext context, Type type)
{
object newInstance = context.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory(type, null);
return CreateItem(context,type,null);
}

/// <summary>
/// Is called to create the item used by the CreateComponentTool.
/// </summary>
public static DesignItem CreateItem(DesignContext context, Type type, object[] arguments)
{
hjqcan marked this conversation as resolved.
Show resolved Hide resolved
object newInstance = context.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory(type, arguments);
DesignItem item = context.Services.Component.RegisterComponentForDesigner(newInstance);
context.Services.Component.SetDefaultPropertyValues(item);
context.Services.ExtensionManager.ApplyDefaultInitializers(item);
Expand All @@ -237,13 +261,23 @@ protected virtual void SetPropertiesForDrawnItem(DesignItem designItem)

public static bool AddItemWithCustomSizePosition(DesignItem container, Type createdItem, Size size, Point position)
{
CreateComponentTool cct = new CreateComponentTool(createdItem);
return AddItemWithCustomSizePosition(container, createdItem, null, size, position);
}

public static bool AddItemWithCustomSizePosition(DesignItem container, Type createdItem, object[] arguments, Size size, Point position)
{
hjqcan marked this conversation as resolved.
Show resolved Hide resolved
CreateComponentTool cct = new CreateComponentTool(createdItem, arguments);
return AddItemsWithCustomSize(container, new[] { cct.CreateItem(container.Context) }, new[] { new Rect(position, size) });
}

public static bool AddItemWithDefaultSize(DesignItem container, Type createdItem, Size size)
{
CreateComponentTool cct = new CreateComponentTool(createdItem);
return AddItemWithDefaultSize(container, createdItem, null, size);
}

public static bool AddItemWithDefaultSize(DesignItem container, Type createdItem, object[] arguments, Size size)
{
hjqcan marked this conversation as resolved.
Show resolved Hide resolved
CreateComponentTool cct = new CreateComponentTool(createdItem, arguments);
return AddItemsWithCustomSize(container, new[] { cct.CreateItem(container.Context) }, new[] { new Rect(new Point(0, 0), size) });
}

Expand Down Expand Up @@ -306,8 +340,10 @@ void OnMouseDown(object sender, MouseButtonEventArgs e)
var placementBehavior = result.ModelHit.GetBehavior<IPlacementBehavior>();
if (placementBehavior != null) {
var createdItem = CreateItem(designPanel.Context);

new CreateComponentMouseGesture(result.ModelHit, createdItem, ChangeGroup).Start(designPanel, e);

CreateComponentCompleted?.Invoke(this, createdItem);

new CreateComponentMouseGesture(result.ModelHit, createdItem, ChangeGroup).Start(designPanel, e);
// CreateComponentMouseGesture now is responsible for the changeGroup created by CreateItem()
ChangeGroup = null;
}
Expand Down
Loading