Bug 1843954 - Have Coord * Primitive and Coord / Primitive return Coord. r=botond

While at it, improve the readability of the enable_if
IntCoordTyped constructor.

Differential Revision: https://phabricator.services.mozilla.com/D184573
This commit is contained in:
Razvan Cojocaru 2023-09-14 00:37:04 +00:00
parent 11c85b6b6d
commit 7904413197
4 changed files with 68 additions and 15 deletions

View File

@ -4152,8 +4152,9 @@ static bool ShouldBlockCustomCursor(nsPresContext* aPresContext,
nsSize size(CSSPixel::ToAppUnits(width / zoom),
CSSPixel::ToAppUnits(height / zoom));
nsPoint hotspot(CSSPixel::ToAppUnits(aCursor.mHotspot.x / zoom),
CSSPixel::ToAppUnits(aCursor.mHotspot.y / zoom));
nsPoint hotspot(
CSSPixel::ToAppUnits(ViewAs<CSSPixel>(aCursor.mHotspot.x / zoom)),
CSSPixel::ToAppUnits(ViewAs<CSSPixel>(aCursor.mHotspot.y / zoom)));
const nsRect cursorRect(point - hotspot, size);
auto output = DOMIntersectionObserver::Intersect(input, cursorRect);

View File

@ -68,7 +68,7 @@ int32_t WheelEvent::WheelDeltaX(CallerType aCallerType) {
// We always return pixels regardless of the checking-state.
double pixelDelta =
ev->mDeltaMode == WheelEvent_Binding::DOM_DELTA_PIXEL
? DevToCssPixels(ev->OverriddenDeltaX())
? CSSCoord(DevToCssPixels(ev->OverriddenDeltaX()))
: ev->OverriddenDeltaX() *
CSSPixel::FromAppUnits(ev->mScrollAmount.width).Rounded();
return int32_t(-std::round(pixelDelta * kTrustedDeltaToWheelDelta));
@ -85,7 +85,7 @@ int32_t WheelEvent::WheelDeltaY(CallerType aCallerType) {
if (IsTrusted()) {
double pixelDelta =
ev->mDeltaMode == WheelEvent_Binding::DOM_DELTA_PIXEL
? DevToCssPixels(ev->OverriddenDeltaY())
? CSSCoord(DevToCssPixels(ev->OverriddenDeltaY()))
: ev->OverriddenDeltaY() *
CSSPixel::FromAppUnits(ev->mScrollAmount.height).Rounded();
return int32_t(-std::round(pixelDelta * kTrustedDeltaToWheelDelta));

View File

@ -17,6 +17,58 @@
namespace mozilla {
namespace gfx {
template <class Units, class Rep = int32_t>
struct IntCoordTyped;
template <class Units, class F = Float>
struct CoordTyped;
} // namespace gfx
} // namespace mozilla
namespace std {
template <class Units, class Rep>
struct common_type<mozilla::gfx::IntCoordTyped<Units, Rep>, float> {
using type = mozilla::gfx::CoordTyped<Units, common_type_t<Rep, float>>;
};
template <class Units, class Rep>
struct common_type<mozilla::gfx::IntCoordTyped<Units, Rep>, double> {
using type = mozilla::gfx::CoordTyped<Units, common_type_t<Rep, double>>;
};
template <class Units, class Rep>
struct common_type<mozilla::gfx::IntCoordTyped<Units, Rep>, int32_t> {
using type = mozilla::gfx::IntCoordTyped<Units, common_type_t<Rep, int32_t>>;
};
template <class Units, class Rep>
struct common_type<mozilla::gfx::IntCoordTyped<Units, Rep>, uint32_t> {
using type = mozilla::gfx::IntCoordTyped<Units, common_type_t<Rep, uint32_t>>;
};
template <class Units, class F, class T>
struct common_type<mozilla::gfx::CoordTyped<Units, F>, T> {
using type = mozilla::gfx::CoordTyped<Units, common_type_t<F, T>>;
};
// With a few exceptions, we use CoordTyped values with a float representation.
// These are the types for which we have short typedefs like
// CSSCoord, and the types expected in most interfaces.
// So, for float inputs, keep the results as float even if the other
// operand is a double, accepting a slight loss of precision.
template <class Units, class T>
struct common_type<mozilla::gfx::CoordTyped<Units, float>, T> {
using type = mozilla::gfx::CoordTyped<Units, float>;
};
} // namespace std
namespace mozilla {
template <typename>
struct IsPixel;
@ -25,11 +77,6 @@ namespace gfx {
// Should only be used to define generic typedefs like Coord, Point, etc.
struct UnknownUnits {};
template <class Units, class Rep = int32_t>
struct IntCoordTyped;
template <class Units, class F = Float>
struct CoordTyped;
// This is a base class that provides mixed-type operator overloads between
// a strongly-typed Coord and a Primitive value. It is needed to avoid
// ambiguities at mixed-type call sites, because Coord classes are implicitly
@ -55,13 +102,13 @@ struct CoordOperatorsHelper<true, Coord, Primitive> {
friend auto operator-(Coord aA, Primitive aB) { return aA.value - aB; }
friend auto operator-(Primitive aA, Coord aB) { return aA - aB.value; }
friend auto operator*(Coord aCoord, Primitive aScale) {
return aCoord.value * aScale;
return std::common_type_t<Coord, Primitive>(aCoord.value * aScale);
}
friend auto operator*(Primitive aScale, Coord aCoord) {
return aScale * aCoord.value;
return aCoord * aScale;
}
friend auto operator/(Coord aCoord, Primitive aScale) {
return aCoord.value / aScale;
return std::common_type_t<Coord, Primitive>(aCoord.value / aScale);
}
// 'scale / coord' is intentionally omitted because it doesn't make sense.
};
@ -80,9 +127,8 @@ struct MOZ_EMPTY_BASES IntCoordTyped
static_assert(sizeof(IntCoordTyped) == sizeof(Rep),
"Would be unfortunate otherwise!");
}
template <class T,
typename = typename std::enable_if<std::is_integral<T>::value ||
std::is_enum<T>::value>::type>
template <class T, typename = typename std::enable_if_t<
std::is_integral_v<T> || std::is_enum_v<T>>>
constexpr MOZ_IMPLICIT IntCoordTyped(T aValue) : Super(aValue) {
static_assert(sizeof(IntCoordTyped) == sizeof(Rep),
"Would be unfortunate otherwise!");

View File

@ -18,6 +18,7 @@
# include <windows.h>
#endif
using mozilla::CSSCoord;
using mozilla::CSSIntCoord;
using mozilla::CSSIntSize;
using mozilla::ScreenIntCoord;
@ -30,6 +31,11 @@ static_assert(
static_assert(std::is_constructible_v<CSSIntSize, int, int>);
static_assert(!std::is_constructible_v<CSSIntSize, float, float>);
static_assert(std::is_same_v<CSSIntCoord, decltype(CSSIntCoord() * 42)>);
static_assert(std::is_same_v<CSSCoord, decltype(CSSCoord() * 42)>);
static_assert(std::is_same_v<CSSCoord, decltype(CSSIntCoord() * 42.f)>);
static_assert(std::is_same_v<CSSCoord, decltype(CSSCoord() * 42.f)>);
template <class RectType>
static bool TestConstructors() {
// Create a rectangle