Skip to content

Commit

Permalink
bugfix designer
Browse files Browse the repository at this point in the history
  • Loading branch information
jogibear9988 committed Feb 3, 2022
1 parent 7b5251b commit a16b4c0
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 103 deletions.
58 changes: 15 additions & 43 deletions WpfDesign.Designer/Project/DesignSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public DesignSurface()

this.AddCommandHandler(ApplicationCommands.Undo, Undo, CanUndo);
this.AddCommandHandler(ApplicationCommands.Redo, Redo, CanRedo);
this.AddCommandHandler(ApplicationCommands.Copy, Copy, CanCopyOrCut);
this.AddCommandHandler(ApplicationCommands.Cut, Cut, CanCopyOrCut);
this.AddCommandHandler(ApplicationCommands.Copy, Copy, CanCopy);
this.AddCommandHandler(ApplicationCommands.Cut, Cut, CanCut);
this.AddCommandHandler(ApplicationCommands.Delete, Delete, CanDelete);
this.AddCommandHandler(ApplicationCommands.Paste, Paste, CanPaste);
this.AddCommandHandler(ApplicationCommands.SelectAll, SelectAll, CanSelectAll);
Expand Down Expand Up @@ -237,72 +237,44 @@ public void Redo()
_designContext.Services.Selection.SetSelectedComponents(GetLiveElements(action.AffectedElements));
}

public bool CanCopyOrCut()
public bool CanCopy()
{
ISelectionService selectionService = GetService<ISelectionService>();
if(selectionService!=null){
if (selectionService.SelectedItems.Count == 0)
return false;
if (selectionService.SelectedItems.Count == 1 && selectionService.PrimarySelection == DesignContext.RootItem)
return false;
}
return true;
return _designContext?.Services?.CopyPasteService?.CanCopy(_designContext) == true;
}

public void Copy()
{
XamlDesignContext xamlContext = _designContext as XamlDesignContext;
ISelectionService selectionService = GetService<ISelectionService>();
if(xamlContext != null && selectionService != null){
xamlContext.XamlEditAction.Copy(selectionService.SelectedItems);
}
_designContext?.Services?.CopyPasteService?.Copy(_designContext);
}

public bool CanCut()
{
return _designContext?.Services?.CopyPasteService?.CanCut(_designContext) == true;
}

public void Cut()
{
XamlDesignContext xamlContext = _designContext as XamlDesignContext;
ISelectionService selectionService = GetService<ISelectionService>();
if(xamlContext != null && selectionService != null){
xamlContext.XamlEditAction.Cut(selectionService.SelectedItems);
}
_designContext?.Services?.CopyPasteService?.Cut(_designContext);
}

public bool CanDelete()
{
if (_designContext != null) {
return ModelTools.CanDeleteComponents(_designContext.Services.Selection.SelectedItems);
}
return false;
return _designContext?.Services?.CopyPasteService?.CanDelete(_designContext) == true;
}

public void Delete()
{
if (_designContext != null) {
ModelTools.DeleteComponents(_designContext.Services.Selection.SelectedItems);
}
_designContext?.Services?.CopyPasteService?.Delete(_designContext);
}

public bool CanPaste()
{
ISelectionService selectionService = GetService<ISelectionService>();
if (selectionService != null && selectionService.SelectedItems.Count != 0) {
try {
string xaml = Clipboard.GetText(TextDataFormat.Xaml);
if (xaml != "" && xaml != " ")
return true;
}
catch (Exception) {
}
}
return false;
return _designContext?.Services?.CopyPasteService?.CanPaste(_designContext) == true;
}

public void Paste()
{
XamlDesignContext xamlContext = _designContext as XamlDesignContext;
if(xamlContext != null){
xamlContext.XamlEditAction.Paste();
}
_designContext?.Services?.CopyPasteService?.Paste(_designContext);
}

public bool CanSelectAll()
Expand Down
4 changes: 2 additions & 2 deletions WpfDesign.Designer/Project/OutlineView/Outline.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public Outline()
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanRedo());
this.AddCommandHandler(ApplicationCommands.Copy,
() => ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.Copy(),
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanCopyOrCut());
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanCopy());
this.AddCommandHandler(ApplicationCommands.Cut,
() => ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.Cut(),
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanCopyOrCut());
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanCut());
this.AddCommandHandler(ApplicationCommands.Delete,
() => ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.Delete(),
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanDelete());
Expand Down
94 changes: 94 additions & 0 deletions WpfDesign.Designer/Project/Services/CopyPasteService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using ICSharpCode.WpfDesign;
using ICSharpCode.WpfDesign.Designer;
using ICSharpCode.WpfDesign.Designer.Xaml;
using ICSharpCode.WpfDesign.Services;
using System;
using System.Windows;

namespace WpfDesign.Designer.Services
{
public class CopyPasteService : ICopyPasteService
{
public virtual bool CanCopy(DesignContext designContext)
{
ISelectionService selectionService = designContext.Services.GetService<ISelectionService>();
if (selectionService != null)
{
if (selectionService.SelectedItems.Count == 0)
return false;
if (selectionService.SelectedItems.Count == 1 && selectionService.PrimarySelection == designContext.RootItem)
return false;
}
return true;
}

public virtual void Copy(DesignContext designContext)
{
XamlDesignContext xamlContext = designContext as XamlDesignContext;
ISelectionService selectionService = designContext.Services.GetService<ISelectionService>();
if (xamlContext != null && selectionService != null)
{
xamlContext.XamlEditAction.Copy(selectionService.SelectedItems);
}
}

public virtual bool CanCut(DesignContext designContext)
{
return CanCopy(designContext);
}

public virtual void Cut(DesignContext designContext)
{
XamlDesignContext xamlContext = designContext as XamlDesignContext;
ISelectionService selectionService = designContext.Services.GetService<ISelectionService>();
if (xamlContext != null && selectionService != null)
{
xamlContext.XamlEditAction.Cut(selectionService.SelectedItems);
}
}

public virtual bool CanDelete(DesignContext designContext)
{
if (designContext != null)
{
return ModelTools.CanDeleteComponents(designContext.Services.Selection.SelectedItems);
}
return false;
}

public virtual void Delete(DesignContext designContext)
{
if (designContext != null)
{
ModelTools.DeleteComponents(designContext.Services.Selection.SelectedItems);
}
}

public virtual bool CanPaste(DesignContext designContext)
{
ISelectionService selectionService = designContext.Services.GetService<ISelectionService>();
if (selectionService != null && selectionService.SelectedItems.Count != 0)
{
try
{
string xaml = Clipboard.GetText(TextDataFormat.Xaml);
if (xaml != "" && xaml != " ")
return true;
}
catch (Exception)
{
}
}
return false;
}

public virtual void Paste(DesignContext designContext)
{
XamlDesignContext xamlContext = designContext as XamlDesignContext;
if (xamlContext != null)
{
xamlContext.XamlEditAction.Paste();
}
}
}
}
5 changes: 4 additions & 1 deletion WpfDesign.Designer/Project/Xaml/XamlDesignContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
using ICSharpCode.WpfDesign.Designer.Services;
using ICSharpCode.WpfDesign.PropertyGrid;
using ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors;

using ICSharpCode.WpfDesign.Services;
using WpfDesign.Designer.Services;

namespace ICSharpCode.WpfDesign.Designer.Xaml
{
/// <summary>
Expand Down Expand Up @@ -70,6 +72,7 @@ public XamlDesignContext(XmlReader xamlReader, XamlLoadSettings loadSettings)
this.Services.AddService(typeof(IComponentPropertyService), new ComponentPropertyService());
this.Services.AddService(typeof(IToolService), new DefaultToolService(this));
this.Services.AddService(typeof(UndoService), new UndoService());
this.Services.AddService(typeof(ICopyPasteService), new CopyPasteService());
this.Services.AddService(typeof(IErrorService), new DefaultErrorService(this));
this.Services.AddService(typeof(IOutlineNodeNameService), new OutlineNodeNameService());
this.Services.AddService(typeof(ViewService), new DefaultViewService(this));
Expand Down
107 changes: 70 additions & 37 deletions WpfDesign.Designer/Project/Xaml/XamlEditOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class XamlEditOperations
readonly XamlParserSettings _settings;


readonly char _delimeter = Convert.ToChar(0x7F);
static readonly char _delimeter = Convert.ToChar(0x7F);

/// <summary>
/// Delimet character to seperate different piece of Xaml's
Expand Down Expand Up @@ -105,91 +105,124 @@ public void Copy(ICollection<DesignItem> designItems)
}

/// <summary>
/// Paste items from clipboard into the designer.
/// Paste items from clipboard into the PrimarySelection.
/// </summary>
public void Paste()
{
this.Paste(_context.Services.Selection.PrimarySelection);
}

/// <summary>
/// Paste items from clipboard into the container.
/// </summary>
public void Paste(DesignItem container)
{
var parent = container;
var child = container;

bool pasted = false;
string combinedXaml = Clipboard.GetText(TextDataFormat.Xaml);
IEnumerable<string> xamls = combinedXaml.Split(_delimeter);
xamls = xamls.Where(xaml => xaml != "");

DesignItem parent = _context.Services.Selection.PrimarySelection;
DesignItem child = _context.Services.Selection.PrimarySelection;

XamlDesignItem rootItem = _context.RootItem as XamlDesignItem;

XamlDesignItem rootItem = parent.Services.DesignPanel.Context.RootItem as XamlDesignItem;
var pastedItems = new Collection<DesignItem>();
foreach(var xaml in xamls) {
foreach (var xaml in xamls)
{
var obj = XamlParser.ParseSnippet(rootItem.XamlObject, xaml, _settings);
if(obj!=null) {
DesignItem item = _context._componentService.RegisterXamlComponentRecursive(obj);
if (obj != null)
{
DesignItem item = ((XamlComponentService)parent.Services.Component).RegisterXamlComponentRecursive(obj);
if (item != null)
pastedItems.Add(item);
}
}

if (pastedItems.Count != 0) {
var changeGroup = _context.OpenGroup("Paste " + pastedItems.Count + " elements", pastedItems);
while (parent != null && pasted == false) {
if (parent.ContentProperty != null) {
if (parent.ContentProperty.IsCollection) {
if (CollectionSupport.CanCollectionAdd(parent.ContentProperty.ReturnType, pastedItems.Select(item => item.Component)) && parent.GetBehavior<IPlacementBehavior>()!=null) {
}

if (pastedItems.Count != 0)
{
var changeGroup = parent.Services.DesignPanel.Context.OpenGroup("Paste " + pastedItems.Count + " elements", pastedItems);
while (parent != null && pasted == false)
{
if (parent.ContentProperty != null)
{
if (parent.ContentProperty.IsCollection)
{
if (CollectionSupport.CanCollectionAdd(parent.ContentProperty.ReturnType, pastedItems.Select(item => item.Component)) && parent.GetBehavior<IPlacementBehavior>() != null)
{
AddInParent(parent, pastedItems);
pasted = true;
}
} else if (pastedItems.Count == 1 && parent.ContentProperty.Value == null && parent.ContentProperty.ValueOnInstance == null && parent.View is ContentControl) {
}
else if (pastedItems.Count == 1 && parent.ContentProperty.Value == null && parent.ContentProperty.ValueOnInstance == null && parent.View is ContentControl)
{
AddInParent(parent, pastedItems);
pasted = true;
}
if(!pasted)
parent=parent.Parent;
} else {
if (!pasted)
parent = parent.Parent;
}
else
{
parent = parent.Parent;
}
}

while (pasted == false) {
if (child.ContentProperty != null) {
if (child.ContentProperty.IsCollection) {
foreach (var col in child.ContentProperty.CollectionElements) {
if (col.ContentProperty != null && col.ContentProperty.IsCollection) {
if (CollectionSupport.CanCollectionAdd(col.ContentProperty.ReturnType, pastedItems.Select(item => item.Component))) {
while (pasted == false)
{
if (child.ContentProperty != null)
{
if (child.ContentProperty.IsCollection)
{
foreach (var col in child.ContentProperty.CollectionElements)
{
if (col.ContentProperty != null && col.ContentProperty.IsCollection)
{
if (CollectionSupport.CanCollectionAdd(col.ContentProperty.ReturnType, pastedItems.Select(item => item.Component)))
{
pasted = true;
}
}
}
break;
} else if (child.ContentProperty.Value != null) {
}
else if (child.ContentProperty.Value != null)
{
child = child.ContentProperty.Value;
} else if (pastedItems.Count == 1) {
}
else if (pastedItems.Count == 1)
{
child.ContentProperty.SetValue(pastedItems.First().Component);
pasted = true;
break;
} else
}
else
break;
} else
}
else
break;
}

foreach (var pastedItem in pastedItems) {
_context._componentService.RaiseComponentRegisteredAndAddedToContainer(pastedItem);
foreach (var pastedItem in pastedItems)
{
((XamlComponentService)parent.Services.Component).RaiseComponentRegisteredAndAddedToContainer(pastedItem);
}


changeGroup.Commit();
}
}
}

/// <summary>
/// Adds Items under a parent given that the content property is collection and can add types of <paramref name="pastedItems"/>
/// </summary>
/// <param name="parent">The Parent element</param>
/// <param name="pastedItems">The list of elements to be added</param>
void AddInParent(DesignItem parent,IList<DesignItem> pastedItems)
static void AddInParent(DesignItem parent,IList<DesignItem> pastedItems)
{
IEnumerable<Rect> rects = pastedItems.Select(i => new Rect(new Point(0, 0), new Point((double)i.Properties["Width"].ValueOnInstance, (double)i.Properties["Height"].ValueOnInstance)));
var operation = PlacementOperation.TryStartInsertNewComponents(parent, pastedItems, rects.ToList(), PlacementType.PasteItem);
ISelectionService selection = _context.Services.Selection;
ISelectionService selection = parent.Services.DesignPanel.Context.Services.Selection;
selection.SetSelectedComponents(pastedItems);
if(operation != null)
operation.Commit();
Expand Down
Loading

0 comments on commit a16b4c0

Please sign in to comment.