From 43bd48b7a26cca1803d1c1c0ffac240a9f561784 Mon Sep 17 00:00:00 2001 From: Markus Stange Date: Tue, 20 Oct 2020 03:57:25 +0000 Subject: [PATCH] Bug 1672044 - Convert DrawScrollbarTrack to use DrawTarget. r=haik Differential Revision: https://phabricator.services.mozilla.com/D93841 --- widget/cocoa/nsNativeThemeCocoa.h | 3 ++- widget/cocoa/nsNativeThemeCocoa.mm | 43 +++++++++++++----------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/widget/cocoa/nsNativeThemeCocoa.h b/widget/cocoa/nsNativeThemeCocoa.h index f970e23b4518..0da013b52496 100644 --- a/widget/cocoa/nsNativeThemeCocoa.h +++ b/widget/cocoa/nsNativeThemeCocoa.h @@ -502,7 +502,8 @@ class nsNativeThemeCocoa : private nsNativeTheme, public nsITheme { void DrawStatusBar(CGContextRef cgContext, const HIRect& inBoxRect, bool aIsMain); void DrawResizer(CGContextRef cgContext, const HIRect& aRect, bool aIsRTL); void DrawScrollbarThumb(CGContextRef cgContext, const CGRect& inBoxRect, ScrollbarParams aParams); - void DrawScrollbarTrack(CGContextRef cgContext, const CGRect& inBoxRect, ScrollbarParams aParams); + void DrawScrollbarTrack(mozilla::gfx::DrawTarget& aDT, const mozilla::gfx::Rect& aRect, + ScrollbarParams aParams); void DrawScrollCorner(CGContextRef cgContext, const CGRect& inBoxRect, ScrollbarParams aParams); void DrawMultilineTextField(CGContextRef cgContext, const CGRect& inBoxRect, bool aIsFocused); void DrawSourceList(CGContextRef cgContext, const CGRect& inBoxRect, bool aIsActive); diff --git a/widget/cocoa/nsNativeThemeCocoa.mm b/widget/cocoa/nsNativeThemeCocoa.mm index ac9639b0a0f9..1d35fb9c6849 100644 --- a/widget/cocoa/nsNativeThemeCocoa.mm +++ b/widget/cocoa/nsNativeThemeCocoa.mm @@ -2559,7 +2559,7 @@ static ScrollbarTrackDecorationColors ComputeScrollbarTrackDecorationColors(nsco return result; } -void nsNativeThemeCocoa::DrawScrollbarTrack(CGContextRef cgContext, const CGRect& inBoxRect, +void nsNativeThemeCocoa::DrawScrollbarTrack(DrawTarget& aDT, const gfx::Rect& aRect, ScrollbarParams aParams) { if (aParams.overlay && !aParams.rolledOver) { // Non-hovered overlay scrollbars don't have a track. Draw nothing. @@ -2578,14 +2578,14 @@ void nsNativeThemeCocoa::DrawScrollbarTrack(CGContextRef cgContext, const CGRect } } - CGFloat thickness = aParams.horizontal ? inBoxRect.size.height : inBoxRect.size.width; + float thickness = aParams.horizontal ? aRect.height : aRect.width; // The scrollbar track is drawn as multiple non-overlapping segments, which make up lines of // different widths and with slightly different shading. ScrollbarTrackDecorationColors colors = ComputeScrollbarTrackDecorationColors(trackColor); struct { nscolor color; - CGFloat thickness; + float thickness; } segments[] = { {colors.mInnerColor, 1.0f}, {colors.mShadowColor, 1.0f}, @@ -2596,28 +2596,22 @@ void nsNativeThemeCocoa::DrawScrollbarTrack(CGContextRef cgContext, const CGRect // Iterate over the segments "from inside to outside" and fill each segment. // For horizontal scrollbars, iterate top to bottom. // For vertical scrollbars, iterate left to right or right to left based on aParams.rtl. - CGRect segmentRect = inBoxRect; + float accumulatedThickness = 0.0f; for (const auto& segment : segments) { + gfx::Rect segmentRect = aRect; + float startThickness = accumulatedThickness; + float endThickness = startThickness + segment.thickness; if (aParams.horizontal) { - segmentRect.size.height = segment.thickness; + segmentRect.SetBoxY(aRect.Y() + startThickness, aRect.Y() + endThickness); } else { if (aParams.rtl) { - CGFloat rightEdge = segmentRect.origin.x + segmentRect.size.width; - segmentRect.origin.x = rightEdge - segment.thickness; - } - segmentRect.size.width = segment.thickness; - } - SetCGContextFillColor(cgContext, segment.color); - CGContextFillRect(cgContext, segmentRect); - if (aParams.horizontal) { - segmentRect.origin.y += segment.thickness; - } else { - if (aParams.rtl) { - segmentRect.origin.x -= segment.thickness; + segmentRect.SetBoxX(aRect.XMost() - endThickness, aRect.XMost() - startThickness); } else { - segmentRect.origin.x += segment.thickness; + segmentRect.SetBoxX(aRect.X() + startThickness, aRect.X() + endThickness); } } + aDT.FillRect(segmentRect, ColorPattern(ToDeviceColor(segment.color))); + accumulatedThickness = endThickness; } } @@ -3183,6 +3177,11 @@ void nsNativeThemeCocoa::RenderWidget(const WidgetInfo& aWidgetInfo, DrawTarget& aDrawTarget.FillRect(widgetRect, ColorPattern(ToDeviceColor(color))); break; } + case Widget::eScrollbarTrack: { + ScrollbarParams params = aWidgetInfo.Params(); + DrawScrollbarTrack(aDrawTarget, widgetRect, params); + break; + } default: { // The remaining widgets require a CGContext. CGRect macRect = @@ -3203,7 +3202,8 @@ void nsNativeThemeCocoa::RenderWidget(const WidgetInfo& aWidgetInfo, DrawTarget& CGContextSetBaseCTM(cgContext, CGAffineTransformMakeScale(aScale, aScale)); switch (widget) { - case Widget::eColorFill: { + case Widget::eColorFill: + case Widget::eScrollbarTrack: { MOZ_CRASH("already handled in outer switch"); break; } @@ -3341,11 +3341,6 @@ void nsNativeThemeCocoa::RenderWidget(const WidgetInfo& aWidgetInfo, DrawTarget& DrawScrollbarThumb(cgContext, macRect, params); break; } - case Widget::eScrollbarTrack: { - ScrollbarParams params = aWidgetInfo.Params(); - DrawScrollbarTrack(cgContext, macRect, params); - break; - } case Widget::eScrollCorner: { ScrollbarParams params = aWidgetInfo.Params(); DrawScrollCorner(cgContext, macRect, params);