Skip to content

Commit a16b4c0

Browse files
committed
bugfix designer
1 parent 7b5251b commit a16b4c0

File tree

7 files changed

+239
-103
lines changed

7 files changed

+239
-103
lines changed

WpfDesign.Designer/Project/DesignSurface.cs

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public DesignSurface()
5353

5454
this.AddCommandHandler(ApplicationCommands.Undo, Undo, CanUndo);
5555
this.AddCommandHandler(ApplicationCommands.Redo, Redo, CanRedo);
56-
this.AddCommandHandler(ApplicationCommands.Copy, Copy, CanCopyOrCut);
57-
this.AddCommandHandler(ApplicationCommands.Cut, Cut, CanCopyOrCut);
56+
this.AddCommandHandler(ApplicationCommands.Copy, Copy, CanCopy);
57+
this.AddCommandHandler(ApplicationCommands.Cut, Cut, CanCut);
5858
this.AddCommandHandler(ApplicationCommands.Delete, Delete, CanDelete);
5959
this.AddCommandHandler(ApplicationCommands.Paste, Paste, CanPaste);
6060
this.AddCommandHandler(ApplicationCommands.SelectAll, SelectAll, CanSelectAll);
@@ -237,72 +237,44 @@ public void Redo()
237237
_designContext.Services.Selection.SetSelectedComponents(GetLiveElements(action.AffectedElements));
238238
}
239239

240-
public bool CanCopyOrCut()
240+
public bool CanCopy()
241241
{
242-
ISelectionService selectionService = GetService<ISelectionService>();
243-
if(selectionService!=null){
244-
if (selectionService.SelectedItems.Count == 0)
245-
return false;
246-
if (selectionService.SelectedItems.Count == 1 && selectionService.PrimarySelection == DesignContext.RootItem)
247-
return false;
248-
}
249-
return true;
242+
return _designContext?.Services?.CopyPasteService?.CanCopy(_designContext) == true;
250243
}
251244

252245
public void Copy()
253246
{
254-
XamlDesignContext xamlContext = _designContext as XamlDesignContext;
255-
ISelectionService selectionService = GetService<ISelectionService>();
256-
if(xamlContext != null && selectionService != null){
257-
xamlContext.XamlEditAction.Copy(selectionService.SelectedItems);
258-
}
247+
_designContext?.Services?.CopyPasteService?.Copy(_designContext);
248+
}
249+
250+
public bool CanCut()
251+
{
252+
return _designContext?.Services?.CopyPasteService?.CanCut(_designContext) == true;
259253
}
260254

261255
public void Cut()
262256
{
263-
XamlDesignContext xamlContext = _designContext as XamlDesignContext;
264-
ISelectionService selectionService = GetService<ISelectionService>();
265-
if(xamlContext != null && selectionService != null){
266-
xamlContext.XamlEditAction.Cut(selectionService.SelectedItems);
267-
}
257+
_designContext?.Services?.CopyPasteService?.Cut(_designContext);
268258
}
269259

270260
public bool CanDelete()
271261
{
272-
if (_designContext != null) {
273-
return ModelTools.CanDeleteComponents(_designContext.Services.Selection.SelectedItems);
274-
}
275-
return false;
262+
return _designContext?.Services?.CopyPasteService?.CanDelete(_designContext) == true;
276263
}
277264

278265
public void Delete()
279266
{
280-
if (_designContext != null) {
281-
ModelTools.DeleteComponents(_designContext.Services.Selection.SelectedItems);
282-
}
267+
_designContext?.Services?.CopyPasteService?.Delete(_designContext);
283268
}
284269

285270
public bool CanPaste()
286271
{
287-
ISelectionService selectionService = GetService<ISelectionService>();
288-
if (selectionService != null && selectionService.SelectedItems.Count != 0) {
289-
try {
290-
string xaml = Clipboard.GetText(TextDataFormat.Xaml);
291-
if (xaml != "" && xaml != " ")
292-
return true;
293-
}
294-
catch (Exception) {
295-
}
296-
}
297-
return false;
272+
return _designContext?.Services?.CopyPasteService?.CanPaste(_designContext) == true;
298273
}
299274

300275
public void Paste()
301276
{
302-
XamlDesignContext xamlContext = _designContext as XamlDesignContext;
303-
if(xamlContext != null){
304-
xamlContext.XamlEditAction.Paste();
305-
}
277+
_designContext?.Services?.CopyPasteService?.Paste(_designContext);
306278
}
307279

308280
public bool CanSelectAll()

WpfDesign.Designer/Project/OutlineView/Outline.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public Outline()
3737
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanRedo());
3838
this.AddCommandHandler(ApplicationCommands.Copy,
3939
() => ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.Copy(),
40-
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanCopyOrCut());
40+
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanCopy());
4141
this.AddCommandHandler(ApplicationCommands.Cut,
4242
() => ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.Cut(),
43-
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanCopyOrCut());
43+
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanCut());
4444
this.AddCommandHandler(ApplicationCommands.Delete,
4545
() => ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.Delete(),
4646
() => Root == null ? false : ((DesignPanel) Root.DesignItem.Services.DesignPanel).DesignSurface.CanDelete());
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using ICSharpCode.WpfDesign;
2+
using ICSharpCode.WpfDesign.Designer;
3+
using ICSharpCode.WpfDesign.Designer.Xaml;
4+
using ICSharpCode.WpfDesign.Services;
5+
using System;
6+
using System.Windows;
7+
8+
namespace WpfDesign.Designer.Services
9+
{
10+
public class CopyPasteService : ICopyPasteService
11+
{
12+
public virtual bool CanCopy(DesignContext designContext)
13+
{
14+
ISelectionService selectionService = designContext.Services.GetService<ISelectionService>();
15+
if (selectionService != null)
16+
{
17+
if (selectionService.SelectedItems.Count == 0)
18+
return false;
19+
if (selectionService.SelectedItems.Count == 1 && selectionService.PrimarySelection == designContext.RootItem)
20+
return false;
21+
}
22+
return true;
23+
}
24+
25+
public virtual void Copy(DesignContext designContext)
26+
{
27+
XamlDesignContext xamlContext = designContext as XamlDesignContext;
28+
ISelectionService selectionService = designContext.Services.GetService<ISelectionService>();
29+
if (xamlContext != null && selectionService != null)
30+
{
31+
xamlContext.XamlEditAction.Copy(selectionService.SelectedItems);
32+
}
33+
}
34+
35+
public virtual bool CanCut(DesignContext designContext)
36+
{
37+
return CanCopy(designContext);
38+
}
39+
40+
public virtual void Cut(DesignContext designContext)
41+
{
42+
XamlDesignContext xamlContext = designContext as XamlDesignContext;
43+
ISelectionService selectionService = designContext.Services.GetService<ISelectionService>();
44+
if (xamlContext != null && selectionService != null)
45+
{
46+
xamlContext.XamlEditAction.Cut(selectionService.SelectedItems);
47+
}
48+
}
49+
50+
public virtual bool CanDelete(DesignContext designContext)
51+
{
52+
if (designContext != null)
53+
{
54+
return ModelTools.CanDeleteComponents(designContext.Services.Selection.SelectedItems);
55+
}
56+
return false;
57+
}
58+
59+
public virtual void Delete(DesignContext designContext)
60+
{
61+
if (designContext != null)
62+
{
63+
ModelTools.DeleteComponents(designContext.Services.Selection.SelectedItems);
64+
}
65+
}
66+
67+
public virtual bool CanPaste(DesignContext designContext)
68+
{
69+
ISelectionService selectionService = designContext.Services.GetService<ISelectionService>();
70+
if (selectionService != null && selectionService.SelectedItems.Count != 0)
71+
{
72+
try
73+
{
74+
string xaml = Clipboard.GetText(TextDataFormat.Xaml);
75+
if (xaml != "" && xaml != " ")
76+
return true;
77+
}
78+
catch (Exception)
79+
{
80+
}
81+
}
82+
return false;
83+
}
84+
85+
public virtual void Paste(DesignContext designContext)
86+
{
87+
XamlDesignContext xamlContext = designContext as XamlDesignContext;
88+
if (xamlContext != null)
89+
{
90+
xamlContext.XamlEditAction.Paste();
91+
}
92+
}
93+
}
94+
}

WpfDesign.Designer/Project/Xaml/XamlDesignContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
using ICSharpCode.WpfDesign.Designer.Services;
2626
using ICSharpCode.WpfDesign.PropertyGrid;
2727
using ICSharpCode.WpfDesign.Designer.PropertyGrid.Editors;
28-
28+
using ICSharpCode.WpfDesign.Services;
29+
using WpfDesign.Designer.Services;
30+
2931
namespace ICSharpCode.WpfDesign.Designer.Xaml
3032
{
3133
/// <summary>
@@ -70,6 +72,7 @@ public XamlDesignContext(XmlReader xamlReader, XamlLoadSettings loadSettings)
7072
this.Services.AddService(typeof(IComponentPropertyService), new ComponentPropertyService());
7173
this.Services.AddService(typeof(IToolService), new DefaultToolService(this));
7274
this.Services.AddService(typeof(UndoService), new UndoService());
75+
this.Services.AddService(typeof(ICopyPasteService), new CopyPasteService());
7376
this.Services.AddService(typeof(IErrorService), new DefaultErrorService(this));
7477
this.Services.AddService(typeof(IOutlineNodeNameService), new OutlineNodeNameService());
7578
this.Services.AddService(typeof(ViewService), new DefaultViewService(this));

WpfDesign.Designer/Project/Xaml/XamlEditOperations.cs

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class XamlEditOperations
3636
readonly XamlParserSettings _settings;
3737

3838

39-
readonly char _delimeter = Convert.ToChar(0x7F);
39+
static readonly char _delimeter = Convert.ToChar(0x7F);
4040

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

107107
/// <summary>
108-
/// Paste items from clipboard into the designer.
108+
/// Paste items from clipboard into the PrimarySelection.
109109
/// </summary>
110110
public void Paste()
111111
{
112+
this.Paste(_context.Services.Selection.PrimarySelection);
113+
}
114+
115+
/// <summary>
116+
/// Paste items from clipboard into the container.
117+
/// </summary>
118+
public void Paste(DesignItem container)
119+
{
120+
var parent = container;
121+
var child = container;
122+
112123
bool pasted = false;
113124
string combinedXaml = Clipboard.GetText(TextDataFormat.Xaml);
114125
IEnumerable<string> xamls = combinedXaml.Split(_delimeter);
115126
xamls = xamls.Where(xaml => xaml != "");
116127

117-
DesignItem parent = _context.Services.Selection.PrimarySelection;
118-
DesignItem child = _context.Services.Selection.PrimarySelection;
119-
120-
XamlDesignItem rootItem = _context.RootItem as XamlDesignItem;
128+
129+
XamlDesignItem rootItem = parent.Services.DesignPanel.Context.RootItem as XamlDesignItem;
121130
var pastedItems = new Collection<DesignItem>();
122-
foreach(var xaml in xamls) {
131+
foreach (var xaml in xamls)
132+
{
123133
var obj = XamlParser.ParseSnippet(rootItem.XamlObject, xaml, _settings);
124-
if(obj!=null) {
125-
DesignItem item = _context._componentService.RegisterXamlComponentRecursive(obj);
134+
if (obj != null)
135+
{
136+
DesignItem item = ((XamlComponentService)parent.Services.Component).RegisterXamlComponentRecursive(obj);
126137
if (item != null)
127138
pastedItems.Add(item);
128139
}
129-
}
130-
131-
if (pastedItems.Count != 0) {
132-
var changeGroup = _context.OpenGroup("Paste " + pastedItems.Count + " elements", pastedItems);
133-
while (parent != null && pasted == false) {
134-
if (parent.ContentProperty != null) {
135-
if (parent.ContentProperty.IsCollection) {
136-
if (CollectionSupport.CanCollectionAdd(parent.ContentProperty.ReturnType, pastedItems.Select(item => item.Component)) && parent.GetBehavior<IPlacementBehavior>()!=null) {
140+
}
141+
142+
if (pastedItems.Count != 0)
143+
{
144+
var changeGroup = parent.Services.DesignPanel.Context.OpenGroup("Paste " + pastedItems.Count + " elements", pastedItems);
145+
while (parent != null && pasted == false)
146+
{
147+
if (parent.ContentProperty != null)
148+
{
149+
if (parent.ContentProperty.IsCollection)
150+
{
151+
if (CollectionSupport.CanCollectionAdd(parent.ContentProperty.ReturnType, pastedItems.Select(item => item.Component)) && parent.GetBehavior<IPlacementBehavior>() != null)
152+
{
137153
AddInParent(parent, pastedItems);
138154
pasted = true;
139155
}
140-
} else if (pastedItems.Count == 1 && parent.ContentProperty.Value == null && parent.ContentProperty.ValueOnInstance == null && parent.View is ContentControl) {
156+
}
157+
else if (pastedItems.Count == 1 && parent.ContentProperty.Value == null && parent.ContentProperty.ValueOnInstance == null && parent.View is ContentControl)
158+
{
141159
AddInParent(parent, pastedItems);
142160
pasted = true;
143161
}
144-
if(!pasted)
145-
parent=parent.Parent;
146-
} else {
162+
if (!pasted)
163+
parent = parent.Parent;
164+
}
165+
else
166+
{
147167
parent = parent.Parent;
148168
}
149169
}
150170

151-
while (pasted == false) {
152-
if (child.ContentProperty != null) {
153-
if (child.ContentProperty.IsCollection) {
154-
foreach (var col in child.ContentProperty.CollectionElements) {
155-
if (col.ContentProperty != null && col.ContentProperty.IsCollection) {
156-
if (CollectionSupport.CanCollectionAdd(col.ContentProperty.ReturnType, pastedItems.Select(item => item.Component))) {
171+
while (pasted == false)
172+
{
173+
if (child.ContentProperty != null)
174+
{
175+
if (child.ContentProperty.IsCollection)
176+
{
177+
foreach (var col in child.ContentProperty.CollectionElements)
178+
{
179+
if (col.ContentProperty != null && col.ContentProperty.IsCollection)
180+
{
181+
if (CollectionSupport.CanCollectionAdd(col.ContentProperty.ReturnType, pastedItems.Select(item => item.Component)))
182+
{
157183
pasted = true;
158184
}
159185
}
160186
}
161187
break;
162-
} else if (child.ContentProperty.Value != null) {
188+
}
189+
else if (child.ContentProperty.Value != null)
190+
{
163191
child = child.ContentProperty.Value;
164-
} else if (pastedItems.Count == 1) {
192+
}
193+
else if (pastedItems.Count == 1)
194+
{
165195
child.ContentProperty.SetValue(pastedItems.First().Component);
166196
pasted = true;
167197
break;
168-
} else
198+
}
199+
else
169200
break;
170-
} else
201+
}
202+
else
171203
break;
172204
}
173205

174-
foreach (var pastedItem in pastedItems) {
175-
_context._componentService.RaiseComponentRegisteredAndAddedToContainer(pastedItem);
206+
foreach (var pastedItem in pastedItems)
207+
{
208+
((XamlComponentService)parent.Services.Component).RaiseComponentRegisteredAndAddedToContainer(pastedItem);
176209
}
177210

178211

179212
changeGroup.Commit();
180213
}
181-
}
182-
214+
}
215+
183216
/// <summary>
184217
/// Adds Items under a parent given that the content property is collection and can add types of <paramref name="pastedItems"/>
185218
/// </summary>
186219
/// <param name="parent">The Parent element</param>
187220
/// <param name="pastedItems">The list of elements to be added</param>
188-
void AddInParent(DesignItem parent,IList<DesignItem> pastedItems)
221+
static void AddInParent(DesignItem parent,IList<DesignItem> pastedItems)
189222
{
190223
IEnumerable<Rect> rects = pastedItems.Select(i => new Rect(new Point(0, 0), new Point((double)i.Properties["Width"].ValueOnInstance, (double)i.Properties["Height"].ValueOnInstance)));
191224
var operation = PlacementOperation.TryStartInsertNewComponents(parent, pastedItems, rects.ToList(), PlacementType.PasteItem);
192-
ISelectionService selection = _context.Services.Selection;
225+
ISelectionService selection = parent.Services.DesignPanel.Context.Services.Selection;
193226
selection.SetSelectedComponents(pastedItems);
194227
if(operation != null)
195228
operation.Commit();

0 commit comments

Comments
 (0)