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
Changes from 2 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
55 changes: 38 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,9 +33,13 @@ 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>
Expand All @@ -46,6 +49,17 @@ public CreateComponentTool(Type componentType)
throw new ArgumentNullException("componentType");
this.componentType = componentType;
}

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

/// <summary>
/// Gets the type of the component to be created.
Expand Down Expand Up @@ -115,7 +129,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 +213,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 @@ -220,9 +239,9 @@ protected virtual DesignItem[] CreateItems(DesignContext context)
/// <summary>
/// Is called to create the item used by the CreateComponentTool.
/// </summary>
public static DesignItem CreateItem(DesignContext context, Type type)
public static DesignItem CreateItem(DesignContext context, Type type, object[] arguments = null)
hjqcan marked this conversation as resolved.
Show resolved Hide resolved
{
object newInstance = context.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory(type, null);
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 @@ -235,15 +254,15 @@ public static DesignItem CreateItem(DesignContext context, Type type)
protected virtual void SetPropertiesForDrawnItem(DesignItem designItem)
{ }

public static bool AddItemWithCustomSizePosition(DesignItem container, Type createdItem, Size size, Point 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);
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)
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);
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 +325,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