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

Issus 4908 port save file dialog designer and folder browser dialog designer #9682

Merged
2 changes: 2 additions & 0 deletions src/System.Design/src/System.Design.Forwards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.PrintDialogDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.RadioButtonDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.RichTextBoxDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.SaveFileDialogDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.SplitContainerDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.SplitterPanelDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.SplitterDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.FlowLayoutPanelDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.FolderBrowserDialogDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.TabControlDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.TableLayoutPanelDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.TabPageDesigner))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virtual System.ComponentModel.Design.ComponentDesigner.SetTextualDefaultProperty.get -> bool
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ public partial class ComponentDesigner : ITreeDesigner, IDesignerFilter, ICompon
/// </summary>
public virtual ICollection AssociatedComponents => Array.Empty<IComponent>();

private protected virtual void UpdateTextualDefaultProperty()
{
var component = Component;
if (component?.Site is { } site)
{
PropertyDescriptor defaultProperty = TypeDescriptor.GetDefaultProperty(component);
if (!(defaultProperty is not null && defaultProperty.PropertyType.Equals(typeof(string))))
{
return;
}

if (defaultProperty.GetValue(component) is string currentValue && string.IsNullOrEmpty(currentValue))
{
defaultProperty.SetValue(component, site.Name);
}
}
}

internal virtual bool CanBeAssociatedWith(IDesigner parentDesigner) => true;

/// <summary>
Expand Down Expand Up @@ -125,6 +143,15 @@ public virtual void InitializeNewComponent(IDictionary defaultValues)
{
// execute legacy code
InitializeNonDefault();

// Note: This was originally an obsoleted API called OnSetComponentDefaults(). The
// default behavior of this API was to set the the default property to the component's
// site name, if the property was a string and null or empty. We've removed the API
// but preserved the same behavior, now controlled by SetTextualDefaultProperty.
if (SetTextualDefaultProperty)
LeafShi1 marked this conversation as resolved.
Show resolved Hide resolved
{
UpdateTextualDefaultProperty();
}
}

void IDesignerFilter.PostFilterAttributes(IDictionary attributes) => PostFilterAttributes(attributes);
Expand All @@ -149,6 +176,12 @@ public virtual void InitializeNewComponent(IDictionary defaultValues)
/// </summary>
public virtual DesignerVerbCollection Verbs => _verbs ??= new DesignerVerbCollection();

/// <summary>
/// Controls whether the default property of <see cref="Component"/> is automatically set
/// to <see cref="ISite.Name"/> on creation. The default is <see langword="true"/>.
/// </summary>
protected virtual bool SetTextualDefaultProperty => true;

ICollection ITreeDesigner.Children
{
get
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.Design;

namespace System.Windows.Forms.Design;

internal class FolderBrowserDialogDesigner : ComponentDesigner
{
// Overridden to avoid setting the default property ("SelectedPath")
// to the Site.Name (i.e. folderBrowserDialog1).
protected override bool SetTextualDefaultProperty => false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public unsafe DialogResult ShowDialog(IWin32Window? owner)
}

// Retrieve the path from the IDList.
PWSTR selectedPath = default;
PWSTR selectedPath = pDisplayName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this change for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why the value was set to "default" before, but the default value always points to "0", and when I set the property value, such as FolderBrowerDialog's SelectedPath, an exception occurs.
image

Compare with the code with NexFx, the selectedPath should use pDisplayName pointer.

PInvoke.SHGetPathFromIDList(browseHandle, selectedPath);
DirectoryPath = new string((char*)selectedPath);
return DialogResult.OK;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.Design;

namespace System.Windows.Forms.Design;

internal class SaveFileDialogDesigner : ComponentDesigner
{
// Overridden to avoid setting the default property ("FileName")
// to the Site.Name (i.e. saveFileDialog1).
protected override bool SetTextualDefaultProperty => false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ private void CreateDesignSurface(int n)
FlowLayoutPanel layoutPanel = surface.CreateControl<FlowLayoutPanel>(new Size(430, 200), new Point(250, 10));
layoutPanel.Controls.Add(subButton1OfLayoutPanel);
layoutPanel.Controls.Add(subButton2OfLayoutPanel);

FolderBrowserDialog folderBrowserDialog = surface.CreateComponent<FolderBrowserDialog>();
SaveFileDialog saveFileDialog = surface.CreateComponent<SaveFileDialog>();
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ public class DesignerAttributeTests
"System.Windows.Forms.Design.DataGridViewColumnDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.DataGridViewComboBoxColumnDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.DataGridViewDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.FolderBrowserDialogDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.NotifyIconDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.PropertyGridDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.SaveFileDialogDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.StatusBarDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.ToolStripContainerDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.ToolStripContentPanelDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
Expand Down
Loading