[API Proposal]: Implement IEquatable interface on DpiScale
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Background and motivation
Currently, the `DpiScale` struct does not implement `IEquatable`, while we have an internal implemented wrapper class `DpiScale2` that does implement it and wraps this struct. This is for example used as a dictionary key in `StylusLogic`, which could then be simplified to use the regular struct and avoid for example temporary heap allocs on lookup.
There were already some boxing issues solved in #6309 but the equality is done with an internal `Equals` currently, however as Stephen Toub mentions, the hope is for `DpiScale` to implement `IEquatable` in the future and I couldn't agree more.
While at it, I'd mark the struct as `readonly` since it is designed to be immutable anyways.
The hope is to have then 1 unifying type that we can use to represent `DpiScale` in different scenarios and not having to depend on internal types that are just wrappers around the struct to use it in other places.
### API Proposal
```diff
namespace System.Windows;
- public partial struct DpiScale
+ public readonly partial struct DpiScale : IEquatable
{
+ public bool Equals(DpiScale other)
+ public override bool Equals(object obj)
+ public override int GetHashCode()
+ public static bool operator ==(DpiScale left, DpiScale right)
+ public static bool operator !=(DpiScale left, DpiScale right)
}
```
### API Usage
Besides other places:
https://github.com/dotnet/wpf/blob/6c96913d693c56922e697b23b3053aa604d3bd2c/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusLogic.cs#L183
### Possible additions
We could also add a public utility factory method that's currently also on `DpiScale2`, that is `FromPixelsPerInch`, I believe it has its uses:
https://github.com/dotnet/wpf/blob/6c96913d693c56922e697b23b3053aa604d3bd2c/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/DpiScale2.cs#L202
### Risks
Adding `readonly` is not a breaking change, adding `IEquatable` can obviously produce a new behavior in terms of equality comparisons but I believe it is the right thing to do moving forward.
Contributor guide
Assessment
This issue has not been assessed yet.