Skip to content

Commit d01d8de

Browse files
committed
Add shortCut copy paste In DemoConsole
1 parent 1c091ca commit d01d8de

File tree

1 file changed

+21
-85
lines changed
  • src/System.Windows.Forms.Design/src/System/Windows/Forms/Design

1 file changed

+21
-85
lines changed

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/CommandSet.cs

Lines changed: 21 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Runtime.Serialization.Formatters.Binary;
4+
using System.Collections;
5+
using System.Collections.ObjectModel;
56
using System.ComponentModel;
67
using System.ComponentModel.Design;
78
using System.ComponentModel.Design.Serialization;
8-
using System.Windows.Forms.Design.Behavior;
99
using System.Drawing;
1010
using System.Drawing.Design;
11-
using System.Collections;
11+
using System.Runtime.Serialization.Formatters.Binary;
12+
using System.Windows.Forms.Design.Behavior;
1213

1314
namespace System.Windows.Forms.Design;
1415

@@ -1382,6 +1383,7 @@ protected void OnMenuCenterSelection(object? sender, EventArgs e)
13821383
}
13831384
}
13841385

1386+
private ICollection _toBeCopiedComponents;
13851387
/// <summary>
13861388
/// Called when the copy menu item is selected.
13871389
/// </summary>
@@ -1397,29 +1399,8 @@ protected void OnMenuCopy(object? sender, EventArgs e)
13971399
{
13981400
Cursor.Current = Cursors.WaitCursor;
13991401

1400-
ICollection selectedComponents = GetCopySelection();
1401-
1402-
selectedComponents = PrependComponentNames(selectedComponents);
1403-
1404-
IDesignerSerializationService? ds = GetService<IDesignerSerializationService>();
1405-
Debug.Assert(ds is not null, "No designer serialization service -- we cannot copy to clipboard");
1406-
if (ds is not null)
1407-
{
1408-
object serializationData = ds.Serialize(selectedComponents);
1409-
using MemoryStream stream = new();
1410-
#pragma warning disable SYSLIB0011 // Type or member is obsolete
1411-
new BinaryFormatter().Serialize(stream, serializationData);
1412-
#pragma warning restore SYSLIB0011
1413-
stream.Seek(0, SeekOrigin.Begin);
1414-
byte[] bytes = stream.GetBuffer();
1415-
IDataObject dataObj = new DataObject(CF_DESIGNER, bytes);
1416-
if (!ExecuteSafely(() => Clipboard.SetDataObject(dataObj), throwOnException: false))
1417-
{
1418-
_uiService?.ShowError(SR.ClipboardError);
1419-
}
1420-
}
1421-
1422-
UpdateClipboardItems(null, null);
1402+
_toBeCopiedComponents = SelectionService!.GetSelectedComponents();
1403+
_commandSet.First(command => command.CommandID == StandardCommands.Paste)?.UpdateStatus();
14231404
}
14241405
finally
14251406
{
@@ -1843,52 +1824,31 @@ protected void OnMenuPaste(object? sender, EventArgs e)
18431824
return;
18441825
}
18451826

1846-
bool clipboardOperationSuccessful = ExecuteSafely(Clipboard.GetDataObject, false, out IDataObject? dataObj);
1847-
1848-
if (clipboardOperationSuccessful)
1827+
if (_toBeCopiedComponents is not null && _toBeCopiedComponents.Count != 0)
18491828
{
1850-
ICollection? components = null;
1829+
List<IComponent>? components = null;
18511830
bool createdItems = false;
18521831

18531832
// Get the current number of controls in the Component Tray in the target
18541833
ComponentTray? tray = GetService<ComponentTray>();
18551834
int numberOfOriginalTrayControls = tray is not null ? tray.Controls.Count : 0;
18561835

1857-
// We understand two things: CF_DESIGNER, and toolbox items.
1858-
object? data = dataObj?.GetData(CF_DESIGNER);
1859-
18601836
using DesignerTransaction trans = host.CreateTransaction(SR.CommandSetPaste);
1861-
if (data is byte[] bytes)
1862-
{
1863-
MemoryStream s = new(bytes);
18641837

1865-
// CF_DESIGNER was put on the clipboard by us using the designer serialization service.
1866-
if (TryGetService(out IDesignerSerializationService? ds))
1867-
{
1868-
s.Seek(0, SeekOrigin.Begin);
1869-
#pragma warning disable SYSLIB0011 // Type or member is obsolete
1870-
#pragma warning disable CA2300 // Do not use insecure deserializer BinaryFormatter
1871-
object serializationData = new BinaryFormatter().Deserialize(s); // CodeQL[SM03722, SM04191] : The operation is essential for the design experience when users are running their own designers they have created. This cannot be achieved without BinaryFormatter
1872-
#pragma warning restore CA2300
1873-
#pragma warning restore SYSLIB0011
1874-
using (ScaleHelper.EnterDpiAwarenessScope(DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE))
1875-
{
1876-
components = ds.Deserialize(serializationData);
1877-
}
1878-
}
1879-
}
1880-
else if (TryGetService(out IToolboxService? ts) && ts.IsSupported(dataObj, host))
1838+
components = DesignerUtils.CopyDragObjects(new ReadOnlyCollection<IComponent>([.. _toBeCopiedComponents.Cast<IComponent>()]), GetService<IDesignerHost>());
1839+
1840+
if (_toBeCopiedComponents.Count == components.Count)
18811841
{
1882-
// Now check for a toolbox item.
1883-
ToolboxItem? ti = ts.DeserializeToolboxItem(dataObj, host);
1884-
if (ti is not null)
1842+
int i = 0;
1843+
foreach (IComponent component in _toBeCopiedComponents)
18851844
{
1886-
using (ScaleHelper.EnterDpiAwarenessScope(DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE))
1845+
if (component is Control original && components[i] is Control duplicate)
18871846
{
1888-
components = ti.CreateComponents(host);
1847+
duplicate.Parent = original.Parent;
1848+
duplicate.Location = new Point(original.Location.X + 15, original.Location.Y + 15);
18891849
}
18901850

1891-
createdItems = true;
1851+
i++;
18921852
}
18931853
}
18941854

@@ -1897,7 +1857,7 @@ protected void OnMenuPaste(object? sender, EventArgs e)
18971857
if (components is not null && components.Count > 0)
18981858
{
18991859
// Make copy of Items in Array..
1900-
object[] allComponents = new object[components.Count];
1860+
IComponent[] allComponents = new IComponent[components.Count];
19011861
components.CopyTo(allComponents, 0);
19021862

19031863
List<IComponent> selectComps = [];
@@ -2170,12 +2130,9 @@ protected void OnMenuPaste(object? sender, EventArgs e)
21702130
}
21712131

21722132
trans.Commit();
2133+
SelectionService?.SetSelectedComponents(components);
21732134
}
21742135
}
2175-
else
2176-
{
2177-
_uiService?.ShowError(SR.ClipboardError);
2178-
}
21792136
}
21802137
finally
21812138
{
@@ -3091,28 +3048,7 @@ protected void OnStatusPaste(object? sender, EventArgs e)
30913048
}
30923049
}
30933050

3094-
// Not being inherited. Now look at the contents of the data
3095-
bool clipboardOperationSuccessful = ExecuteSafely(Clipboard.GetDataObject, false, out IDataObject? dataObj);
3096-
3097-
bool enable = false;
3098-
3099-
if (clipboardOperationSuccessful && dataObj is not null)
3100-
{
3101-
if (dataObj.GetDataPresent(CF_DESIGNER))
3102-
{
3103-
enable = true;
3104-
}
3105-
else
3106-
{
3107-
// Not ours, check to see if the toolbox service understands this
3108-
if (TryGetService(out IToolboxService? ts))
3109-
{
3110-
enable = host is not null ? ts.IsSupported(dataObj, host) : ts.IsToolboxItem(dataObj);
3111-
}
3112-
}
3113-
}
3114-
3115-
cmd.Enabled = enable;
3051+
cmd.Enabled = _toBeCopiedComponents is not null;
31163052
}
31173053

31183054
private void OnStatusPrimarySelection(object? sender, EventArgs e)

0 commit comments

Comments
 (0)