Open AraHaan opened 9 months ago
My objection to having this in TerraFX is that RECT
and Rectangle
do not have the same representation, and cannot always be converted correctly.
RECT
is { int left, top, right, bottom; }
while Rectangle
is { int x, y, width, height; }
Consider a RECT
equal to { int.MinValue, int.MinValue, int.MaxValue, int.MaxValue }
. This can't be represented as a Rectangle
because the width/height is int.MaxValue - int.MinValue
and that overflows. So should this throw an exception? Should it just silently overflow? Should it be clamped, and if so to what range? IMO this should be left to applications to implement on their own, because the answer to these questions is application-specific.
Also, FWIW, WICRect
is bit-compatible with Rectangle
.
My objection to having this in TerraFX is that
RECT
andRectangle
do not have the same representation, and cannot always be converted correctly.
RECT
is{ int left, top, right, bottom; }
whileRectangle
is{ int x, y, width, height; }
Consider a
RECT
equal to{ int.MinValue, int.MinValue, int.MaxValue, int.MaxValue }
. This can't be represented as aRectangle
because the width/height isint.MaxValue - int.MinValue
and that overflows. So should this throw an exception? Should it just silently overflow? Should it be clamped, and if so to what range? IMO this should be left to applications to implement on their own, because the answer to these questions is application-specific.Also, FWIW,
WICRect
is bit-compatible withRectangle
.
I am sure in the case of TITLEBARINFOEX
the conversion from RECT
-> Rectangle
is fine because in my code it never seems to overflow (that I know of so far).
That code, I plan to eventually use to have a manual drawn title bar (non-client area) that can be themed to any colors even non-system wide ones similar to how office 2010 for example did with a fake non-client area using the information like the Size of the specific parts of it and then calling windows apis to get the images rendered inside of say the buttons themselves. With that the real ones would need to be "disabled" (aka turned off to never render) for it to work then.
I'd tend to agree with Rick here, but additionally, I don't really want to unnecessarily bring in new dependencies, particularly not ones that could require me to expose my TFM as net*-windows
. -- I know Rectangle is currently in System.Drawing.Primitives and so doesn't require this, but most of System.Drawing is not in that camp and this issue would open up a precedent that then has to be discussed for other similar cases where an "in-box" type exists.
This seems like a good place for a user to provide some static Rectangle ToRectangle(this RECT rect)
(and the inverse) extension API where they can ensure that the conversion works for their needs (including overflow behavior) -- noting that because the conversion is potentially lossy, the conversion APIs would have to be explicit and so you don't really get anything from it being an operator on RECT
at that point.
Having an implicit conversion from RECT
to Rectangle
would be a bug hazard -- way too easy to accidentally mix them together and end up with silent overflows that cause bugs or worse.
Description (optional)
I think an helpful implicit conversion to
Rectangle
would help with everything with APIs that usesRECT
or returnsRECT
.Rationale
Why to System.Drawing.Rectangle? Because the type exists in System.Drawing.Primitives which is part of the base shared framework which means it is not locked down to
Microsoft.WindowDesktop.App
as it exists in theMicrosoft.NETCore.App
shared framework which means it is available everywhere for every operating system. With it existing as a reference to every project that references the framework without it disabled that means it is safe to use in TerraFX.Interop.Windows as it is a primitive type. Also having operators between the 2 will help withRECT
<->Rectangle
conversions without things going wrong when the developer messes up conversions as the operators will take all the guess work out of it all.Proposed API
Drawbacks
I think an implicit conversion like this would have minimal drawbacks with an explicit cast from
Rectangle
->RECT
as well so people can also pass those to APIs without doing it the long way as well (it becomes problematic to remember what should go into theLeft
,Right
,Top
,Bottom
so operators that handles it for us could also help prevent bugs in people's code as well as convenience as well.Alternatives
I have not though of any alternatives yet.
Other thoughts
Implementation would look something like this of the new operators:
Discussions (optional)
I have not discussed about this much, this idea just came into mind when I was looking at some of my old code that processes
TitleBarInfo
related things.