-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Refactor existing Clipboard code #10495
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
Changes from all commits
0c526bf
38ec09a
7ddf2ae
9c03199
5a9ed3e
fea6e41
8b2e2a2
a75bfc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,72 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
// | ||
// | ||
#nullable enable | ||
|
||
// Description: Helper methods for code that uses types from System.Drawing. | ||
// | ||
|
||
using System.IO; | ||
using Windows.Win32.Graphics.Gdi; | ||
|
||
namespace MS.Internal | ||
{ | ||
internal static class SystemDrawingHelper | ||
{ | ||
// return true if the data is a bitmap | ||
internal static bool IsBitmap(object data) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(); | ||
return (extensions != null) ? extensions.IsBitmap(data) : false; | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.IsBitmap(object?)"/> | ||
internal static bool IsBitmap(object? data) => | ||
AssemblyHelper.ExtensionsForSystemDrawing()?.IsBitmap(data) ?? false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the benefit of these changes? Only makes it more difficult to debug to not have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More class context on the screen at the same time. The impact to debugging doesn't seem to outweigh that benefit for something this simple. |
||
|
||
// return true if the data is an Image | ||
internal static bool IsImage(object data) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(); | ||
return (extensions != null) ? extensions.IsImage(data) : false; | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.IsImage(object?)"/> | ||
internal static bool IsImage(object? data) => | ||
AssemblyHelper.ExtensionsForSystemDrawing()?.IsImage(data) ?? false; | ||
|
||
// return true if the data is a graphics metafile | ||
internal static bool IsMetafile(object data) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(); | ||
return (extensions != null) ? extensions.IsMetafile(data) : false; | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.IsMetafile(object?)"/> | ||
internal static bool IsMetafile(object? data) => | ||
AssemblyHelper.ExtensionsForSystemDrawing()?.IsMetafile(data) ?? false; | ||
|
||
// return the handle from a metafile | ||
internal static IntPtr GetHandleFromMetafile(Object data) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(); | ||
return (extensions != null) ? extensions.GetHandleFromMetafile(data) : IntPtr.Zero; | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetHandleFromMetafile(object?)"/> | ||
internal static HENHMETAFILE GetHandleFromMetafile(object? data) => | ||
(HENHMETAFILE)(AssemblyHelper.ExtensionsForSystemDrawing()?.GetHandleFromMetafile(data) ?? 0); | ||
|
||
// Get the metafile from the handle of the enhanced metafile. | ||
internal static Object GetMetafileFromHemf(IntPtr hMetafile) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true); | ||
return extensions?.GetMetafileFromHemf(hMetafile); | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetMetafileFromHemf(nint)"/> | ||
internal static object? GetMetafileFromHemf(HENHMETAFILE hMetafile) => | ||
AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.GetMetafileFromHemf(hMetafile); | ||
|
||
// Get a bitmap from the given data (either BitmapSource or Bitmap) | ||
internal static object GetBitmap(object data) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true); | ||
return extensions?.GetBitmap(data); | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetBitmap(object?)"/> | ||
internal static object? GetBitmap(object? data) => | ||
AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.GetBitmap(data); | ||
|
||
// Get a bitmap handle from the given data (either BitmapSource or Bitmap) | ||
// Also return its width and height. | ||
internal static IntPtr GetHBitmap(object data, out int width, out int height) | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetHBitmap(object?, out int, out int)"/> | ||
internal static HBITMAP GetHBitmap(object? data, out int width, out int height) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true); | ||
if (extensions != null) | ||
var extensions = AssemblyHelper.ExtensionsForSystemDrawing(force: true); | ||
if (extensions is not null) | ||
{ | ||
return extensions.GetHBitmap(data, out width, out height); | ||
return (HBITMAP)extensions.GetHBitmap(data, out width, out height); | ||
} | ||
|
||
width = height = 0; | ||
return IntPtr.Zero; | ||
return HBITMAP.Null; | ||
} | ||
|
||
// Get a bitmap handle from a Bitmap | ||
internal static IntPtr GetHBitmapFromBitmap(object data) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(); | ||
return (extensions != null) ? extensions.GetHBitmapFromBitmap(data) : IntPtr.Zero; | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetHBitmapFromBitmap(object?)"/> | ||
internal static HBITMAP GetHBitmapFromBitmap(object? data) => | ||
(HBITMAP)(AssemblyHelper.ExtensionsForSystemDrawing()?.GetHBitmapFromBitmap(data) ?? HBITMAP.Null); | ||
|
||
// Convert a metafile to HBitmap | ||
internal static IntPtr ConvertMetafileToHBitmap(IntPtr handle) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true); | ||
return (extensions != null) ? extensions.ConvertMetafileToHBitmap(handle) : IntPtr.Zero; | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.ConvertMetafileToHBitmap(nint)"/> | ||
internal static HBITMAP ConvertMetafileToHBitmap(HENHMETAFILE handle) => | ||
(HBITMAP)(AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.ConvertMetafileToHBitmap(handle) ?? HBITMAP.Null); | ||
|
||
// return a stream for the ExifUserComment in the given Gif | ||
internal static Stream GetCommentFromGifStream(Stream stream) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true); | ||
return extensions?.GetCommentFromGifStream(stream); | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetCommentFromGifStream(Stream)"/> | ||
internal static Stream? GetCommentFromGifStream(Stream stream) => | ||
AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.GetCommentFromGifStream(stream); | ||
|
||
// write a metafile stream to the output stream in PNG format | ||
internal static void SaveMetafileToImageStream(MemoryStream metafileStream, Stream imageStream) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true); | ||
extensions?.SaveMetafileToImageStream(metafileStream, imageStream); | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.SaveMetafileToImageStream(MemoryStream, Stream)"/> | ||
internal static void SaveMetafileToImageStream(MemoryStream metafileStream, Stream imageStream) => | ||
AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.SaveMetafileToImageStream(metafileStream, imageStream); | ||
|
||
//returns bitmap snapshot of selected area | ||
//this code takes a BitmapImage and converts it to a Bitmap so it can be put on the clipboard | ||
internal static object GetBitmapFromBitmapSource(object source) | ||
{ | ||
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true); | ||
return extensions?.GetBitmapFromBitmapSource(source); | ||
} | ||
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetBitmapFromBitmapSource(object)"/> | ||
internal static object? GetBitmapFromBitmapSource(object source) => | ||
AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.GetBitmapFromBitmapSource(source); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
namespace System.Windows; | ||
|
||
public sealed partial class DataObject | ||
{ | ||
private partial class DataStore | ||
{ | ||
private class DataStoreEntry | ||
{ | ||
public DataStoreEntry(object? data, bool autoConvert) | ||
{ | ||
Data = data; | ||
AutoConvert = autoConvert; | ||
} | ||
|
||
// Data object property. | ||
public object? Data { get; set; } | ||
|
||
// Auto convert proeprty. | ||
public bool AutoConvert { get; } | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd expect a comment explaining the suppressions given that
GetPropertyItem
can actually returnnull
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can totally throw. The answers here aren't great as modifying the throw is more likely to create grief than to be useful. We can put a comment in that says letting it throw as it would have for compat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not public, we can make it return null. The caller already catches
NullReferenceException
which it will cause.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll catch it in the other PR @miloush. I'm closing this one out in favor of the other after chatting with @harshit7962