From bb3faa44195f69c632409e5641717c260b28e230 Mon Sep 17 00:00:00 2001 From: hjqcan Date: Fri, 31 May 2024 17:28:07 +0800 Subject: [PATCH 1/4] Update CreateComponentTool.cs 1.Allow for the creation of components with parameters 2.Add event when component creation is complete --- .../Project/Services/CreateComponentTool.cs | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/WpfDesign.Designer/Project/Services/CreateComponentTool.cs b/WpfDesign.Designer/Project/Services/CreateComponentTool.cs index a410d17..7c138c3 100644 --- a/WpfDesign.Designer/Project/Services/CreateComponentTool.cs +++ b/WpfDesign.Designer/Project/Services/CreateComponentTool.cs @@ -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 @@ -26,26 +26,31 @@ namespace ICSharpCode.WpfDesign.Designer.Services { - /// - /// A tool that creates a component. - /// - public class CreateComponentTool : ITool + /// + /// A tool that creates a component. + /// + public class CreateComponentTool : ITool { protected ChangeGroup ChangeGroup; readonly Type componentType; - MoveLogic moveLogic; + object[] arguments; + + MoveLogic moveLogic; Point createPoint; - - /// - /// Creates a new CreateComponentTool instance. - /// - public CreateComponentTool(Type componentType) + + public event EventHandler CreateComponentCompleted; + + /// + /// Creates a new CreateComponentTool instance. + /// + public CreateComponentTool(Type componentType, object[] arguments) { if (componentType == null) throw new ArgumentNullException("componentType"); this.componentType = componentType; - } + this.arguments = arguments; + } /// /// Gets the type of the component to be created. @@ -115,7 +120,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) { @@ -196,7 +204,7 @@ 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; } @@ -220,9 +228,9 @@ protected virtual DesignItem[] CreateItems(DesignContext context) /// /// Is called to create the item used by the CreateComponentTool. /// - public static DesignItem CreateItem(DesignContext context, Type type) + public static DesignItem CreateItem(DesignContext context, Type type, object[] arguments) { - 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); @@ -235,15 +243,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) { - 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) { - 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) }); } @@ -306,8 +314,10 @@ void OnMouseDown(object sender, MouseButtonEventArgs e) var placementBehavior = result.ModelHit.GetBehavior(); 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; } From 3249bd7bfc6e43d4983714d78b1df48330fcda28 Mon Sep 17 00:00:00 2001 From: hjqcan Date: Sat, 1 Jun 2024 15:09:15 +0800 Subject: [PATCH 2/4] Update CreateComponentTool.cs fix for breaking change,create a additional constructor without the perameter --- .../Project/Services/CreateComponentTool.cs | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/WpfDesign.Designer/Project/Services/CreateComponentTool.cs b/WpfDesign.Designer/Project/Services/CreateComponentTool.cs index 7c138c3..03dc7fa 100644 --- a/WpfDesign.Designer/Project/Services/CreateComponentTool.cs +++ b/WpfDesign.Designer/Project/Services/CreateComponentTool.cs @@ -16,35 +16,44 @@ // 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 { - /// - /// A tool that creates a component. - /// - public class CreateComponentTool : ITool + /// + /// A tool that creates a component. + /// + public class CreateComponentTool : ITool { protected ChangeGroup ChangeGroup; readonly Type componentType; - object[] arguments; + readonly object[] arguments=null; MoveLogic moveLogic; Point createPoint; public event EventHandler CreateComponentCompleted; - /// - /// Creates a new CreateComponentTool instance. - /// - public CreateComponentTool(Type componentType, object[] arguments) + /// + /// Creates a new CreateComponentTool instance. + /// + public CreateComponentTool(Type componentType) + { + if (componentType == null) + throw new ArgumentNullException("componentType"); + this.componentType = componentType; + } + + /// + /// Creates a new CreateComponentTool instance. + /// + public CreateComponentTool(Type componentType, object[] arguments) { if (componentType == null) throw new ArgumentNullException("componentType"); @@ -204,7 +213,9 @@ protected virtual DesignItem CreateItem(DesignContext context) { if (ChangeGroup == null) ChangeGroup = context.RootItem.OpenGroup("Add Control"); - var item = CreateItem(context, componentType,arguments); + + var item = CreateItem(context, componentType, arguments); + return item; } @@ -228,7 +239,7 @@ protected virtual DesignItem[] CreateItems(DesignContext context) /// /// Is called to create the item used by the CreateComponentTool. /// - public static DesignItem CreateItem(DesignContext context, Type type, object[] arguments) + public static DesignItem CreateItem(DesignContext context, Type type, object[] arguments = null) { object newInstance = context.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory(type, arguments); DesignItem item = context.Services.Component.RegisterComponentForDesigner(newInstance); From ffb1d87eec72c379476005c274692cce8c7159b3 Mon Sep 17 00:00:00 2001 From: hjqcan Date: Mon, 3 Jun 2024 09:54:50 +0800 Subject: [PATCH 3/4] Update CreateComponentTool.cs fix for breaking change --- .../Project/Services/CreateComponentTool.cs | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/WpfDesign.Designer/Project/Services/CreateComponentTool.cs b/WpfDesign.Designer/Project/Services/CreateComponentTool.cs index 03dc7fa..0ebb5e4 100644 --- a/WpfDesign.Designer/Project/Services/CreateComponentTool.cs +++ b/WpfDesign.Designer/Project/Services/CreateComponentTool.cs @@ -43,11 +43,8 @@ public class CreateComponentTool : ITool /// /// Creates a new CreateComponentTool instance. /// - public CreateComponentTool(Type componentType) + public CreateComponentTool(Type componentType):this(componentType, null) { - if (componentType == null) - throw new ArgumentNullException("componentType"); - this.componentType = componentType; } /// @@ -56,10 +53,10 @@ public CreateComponentTool(Type componentType) public CreateComponentTool(Type componentType, object[] arguments) { if (componentType == null) - throw new ArgumentNullException("componentType"); + throw new ArgumentNullException(nameof(componentType)); this.componentType = componentType; - this.arguments = arguments; - } + this.arguments = arguments; + } /// /// Gets the type of the component to be created. @@ -239,7 +236,15 @@ protected virtual DesignItem[] CreateItems(DesignContext context) /// /// Is called to create the item used by the CreateComponentTool. /// - public static DesignItem CreateItem(DesignContext context, Type type, object[] arguments = null) + public static DesignItem CreateItem(DesignContext context, Type type) + { + return CreateItem(context,type,null); + } + + /// + /// Is called to create the item used by the CreateComponentTool. + /// + public static DesignItem CreateItem(DesignContext context, Type type, object[] arguments) { object newInstance = context.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory(type, arguments); DesignItem item = context.Services.Component.RegisterComponentForDesigner(newInstance); @@ -254,12 +259,22 @@ public static DesignItem CreateItem(DesignContext context, Type type, object[] a protected virtual void SetPropertiesForDrawnItem(DesignItem designItem) { } + public static bool AddItemWithCustomSizePosition(DesignItem container, Type createdItem, Size size, Point position) + { + return AddItemWithCustomSizePosition(container, createdItem, null, size, position); + } + public static bool AddItemWithCustomSizePosition(DesignItem container, Type createdItem, object[] arguments, Size size, Point position) { 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) + { + return AddItemWithDefaultSize(container, createdItem, null, size); + } + public static bool AddItemWithDefaultSize(DesignItem container, Type createdItem, object[] arguments, Size size) { CreateComponentTool cct = new CreateComponentTool(createdItem, arguments); From 7de537612620b64cd07450439d995784064fff6f Mon Sep 17 00:00:00 2001 From: hjqcan Date: Thu, 20 Jun 2024 20:13:21 +0800 Subject: [PATCH 4/4] Update PropertyGrid.cs bug fix for BasicMetadata.Register(); never used. --- WpfDesign.Designer/Project/PropertyGrid/PropertyGrid.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/WpfDesign.Designer/Project/PropertyGrid/PropertyGrid.cs b/WpfDesign.Designer/Project/PropertyGrid/PropertyGrid.cs index b4aaf40..61691a2 100644 --- a/WpfDesign.Designer/Project/PropertyGrid/PropertyGrid.cs +++ b/WpfDesign.Designer/Project/PropertyGrid/PropertyGrid.cs @@ -48,6 +48,8 @@ public PropertyGrid() Categories.Add(otherCategory); Categories.Add(attachedCategory); + BasicMetadata.Register(); + Events = new PropertyNodeCollection(); }