Bug 507947 - Use CGContextFillRect instead of NSRectFill when drawing toolbars and other grey boxes in native theming. r=josh
authorMarkus Stange <mstange@themasta.com>
Tue, 04 Aug 2009 10:27:31 +1200
changeset 31094 9d37b381e2ae991ccfe08e68791bbf0e07926440
parent 31093 a285b8d3c18e5894a45e54544caaea7b69491592
child 31095 0cfd302c9d581474d4d6c8df2ec1d78f8c4b1418
push id8361
push usermstange@themasta.com
push dateMon, 03 Aug 2009 22:29:41 +0000
treeherdermozilla-central@9d37b381e2ae [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjosh
bugs507947
milestone1.9.2a1pre
Bug 507947 - Use CGContextFillRect instead of NSRectFill when drawing toolbars and other grey boxes in native theming. r=josh
widget/src/cocoa/nsCocoaWindow.mm
widget/src/cocoa/nsNativeThemeCocoa.mm
widget/src/cocoa/nsNativeThemeColors.h
--- a/widget/src/cocoa/nsCocoaWindow.mm
+++ b/widget/src/cocoa/nsCocoaWindow.mm
@@ -2072,18 +2072,18 @@ void patternDraw(void* aInfo, CGContextR
                                                 function, NO, NO);
     CGColorSpaceRelease(colorSpace);
     CGFunctionRelease(function);
     CGContextDrawShading(aContext, shading);
     CGShadingRelease(shading);
 
     // Draw the one pixel border at the bottom of the titlebar.
     if ([window unifiedToolbarHeight] == 0) {
-      [NativeGreyColorAsNSColor(headerBorderGrey, isMain) set];
-      NSRectFill(NSMakeRect(0.0f, titlebarOrigin, sPatternWidth, 1.0f));
+      CGRect borderRect = CGRectMake(0.0f, titlebarOrigin, sPatternWidth, 1.0f);
+      DrawNativeGreyColorInRect(aContext, headerBorderGrey, borderRect, isMain);
     }
   } else {
     // if the titlebar color is not nil, just set and draw it normally.
     [titlebarColor set];
     NSRectFill(NSMakeRect(0.0f, titlebarOrigin, sPatternWidth, titlebarHeight));
   }
 
   // Draw the background color of the window everywhere but where the titlebar is.
--- a/widget/src/cocoa/nsNativeThemeCocoa.mm
+++ b/widget/src/cocoa/nsNativeThemeCocoa.mm
@@ -1349,17 +1349,17 @@ nsNativeThemeCocoa::DrawUnifiedToolbar(C
       DrawingAtWindowTop(cgContext, [[win contentView] bounds].size.height, inBoxRect.origin.y)) {
     // Consider the titlebar height when calculating the gradient.
     titlebarHeight = [(ToolbarWindow*)win titlebarHeight];
     // Notify the window about the toolbar's height so that it can draw the
     // correct gradient in the titlebar.
     [(ToolbarWindow*)win setUnifiedToolbarHeight:inBoxRect.size.height];
   }
   
-  BOOL isMain = win ? [win isMainWindow] : YES;
+  BOOL isMain = [win isMainWindow] || ![NSView focusView];
 
   // Draw the gradient
   UnifiedGradientInfo info = { titlebarHeight, inBoxRect.size.height, isMain, NO };
   struct CGFunctionCallbacks callbacks = { 0, nsCocoaWindow::UnifiedShading, NULL };
   CGFunctionRef function = CGFunctionCreate(&info, 1,  NULL, 4, NULL, &callbacks);
   float srcY = inBoxRect.origin.y;
   float dstY = srcY + inBoxRect.size.height - 1;
   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
@@ -1369,19 +1369,20 @@ nsNativeThemeCocoa::DrawUnifiedToolbar(C
                                               NO, NO);
   CGColorSpaceRelease(colorSpace);
   CGFunctionRelease(function);
   CGContextClipToRect(cgContext, inBoxRect);
   CGContextDrawShading(cgContext, shading);
   CGShadingRelease(shading);
 
   // Draw the border at the bottom of the toolbar.
-  [NativeGreyColorAsNSColor(headerBorderGrey, isMain) set];
-  NSRectFill(NSMakeRect(inBoxRect.origin.x, inBoxRect.origin.y +
-                        inBoxRect.size.height - 1.0f, inBoxRect.size.width, 1.0f));
+  CGRect borderRect = CGRectMake(inBoxRect.origin.x, inBoxRect.origin.y +
+                                 inBoxRect.size.height - 1.0f,
+                                 inBoxRect.size.width, 1.0f);
+  DrawNativeGreyColorInRect(cgContext, headerBorderGrey, borderRect, isMain);
 
   NS_OBJC_END_TRY_ABORT_BLOCK;
 }
 
 struct GreyGradientInfo {
   float startGrey;
   float endGrey;
 };
@@ -1423,25 +1424,24 @@ void
 nsNativeThemeCocoa::DrawStatusBar(CGContextRef cgContext, const HIRect& inBoxRect,
                                   nsIFrame *aFrame)
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
   if (inBoxRect.size.height < 2.0f)
     return;
 
-  BOOL isMain = [NativeWindowForFrame(aFrame) isMainWindow];
+  BOOL isMain = [NativeWindowForFrame(aFrame) isMainWindow] || ![NSView focusView];
 
   // Draw the borders at the top of the statusbar.
-  [NativeGreyColorAsNSColor(statusbarFirstTopBorderGrey, isMain) set];
-  NSRectFill(NSMakeRect(inBoxRect.origin.x, inBoxRect.origin.y,
-                        inBoxRect.size.width, 1.0f));
-  [NativeGreyColorAsNSColor(statusbarSecondTopBorderGrey, isMain) set];
-  NSRectFill(NSMakeRect(inBoxRect.origin.x, inBoxRect.origin.y + 1.0f,
-                        inBoxRect.size.width, 1.0f));
+  CGRect rect = CGRectMake(inBoxRect.origin.x, inBoxRect.origin.y,
+                           inBoxRect.size.width, 1.0f);
+  DrawNativeGreyColorInRect(cgContext, statusbarFirstTopBorderGrey, rect, isMain);
+  rect.origin.y += 1.0f;
+  DrawNativeGreyColorInRect(cgContext, statusbarSecondTopBorderGrey, rect, isMain);
 
   // Draw the gradient.
   DrawGreyGradient(cgContext, CGRectMake(inBoxRect.origin.x, inBoxRect.origin.y + 2.0f,
                                          inBoxRect.size.width, inBoxRect.size.height - 2.0f),
                    NativeGreyColorAsFloat(statusbarGradientStartGrey, isMain),
                    NativeGreyColorAsFloat(statusbarGradientEndGrey, isMain));
 
   NS_OBJC_END_TRY_ABORT_BLOCK;
@@ -1628,32 +1628,32 @@ nsNativeThemeCocoa::DrawWidgetBackground
     }
       break;
 
     case NS_THEME_MOZ_MAC_UNIFIED_TOOLBAR:
       DrawUnifiedToolbar(cgContext, macRect, aFrame);
       break;
 
     case NS_THEME_TOOLBAR: {
-      BOOL isMain = [NativeWindowForFrame(aFrame) isMainWindow];
+      BOOL isMain = [NativeWindowForFrame(aFrame) isMainWindow] || ![NSView focusView];
+      CGRect drawRect = macRect;
 
       // top border
-      [NativeGreyColorAsNSColor(toolbarTopBorderGrey, isMain) set];
-      NSRectFill(NSMakeRect(macRect.origin.x, macRect.origin.y,
-                            macRect.size.width, 1.0f));
+      drawRect.size.height = 1.0f;
+      DrawNativeGreyColorInRect(cgContext, toolbarTopBorderGrey, drawRect, isMain);
 
       // background
-      [NativeGreyColorAsNSColor(headerEndGrey, isMain) set];
-      NSRectFill(NSMakeRect(macRect.origin.x, macRect.origin.y + 1.0f,
-                            macRect.size.width, macRect.size.height - 2.0f));
+      drawRect.origin.y += drawRect.size.height;
+      drawRect.size.height = macRect.size.height - 2.0f;
+      DrawNativeGreyColorInRect(cgContext, headerEndGrey, drawRect, isMain);
 
       // bottom border
-      [NativeGreyColorAsNSColor(headerBorderGrey, isMain) set];
-      NSRectFill(NSMakeRect(macRect.origin.x, macRect.origin.y +
-                            macRect.size.height - 1.0f, macRect.size.width, 1.0f));
+      drawRect.origin.y += drawRect.size.height;
+      drawRect.size.height = 1.0f;
+      DrawNativeGreyColorInRect(cgContext, headerBorderGrey, drawRect, isMain);
     }
       break;
 
     case NS_THEME_TOOLBOX: {
       HIThemeHeaderDrawInfo hdi = { 0, kThemeStateActive, kHIThemeHeaderKindWindow };
       HIThemeDrawHeader(&macRect, &hdi, cgContext, HITHEME_ORIENTATION);
     }
       break;
--- a/widget/src/cocoa/nsNativeThemeColors.h
+++ b/widget/src/cocoa/nsNativeThemeColors.h
@@ -70,14 +70,17 @@ static int NativeGreyColorAsInt(ColorNam
   return sLeopardThemeColors[name][isMain ? 0 : 1];
 }
 
 static float NativeGreyColorAsFloat(ColorName name, BOOL isMain)
 {
   return NativeGreyColorAsInt(name, isMain) / 255.0f;
 }
 
-static NSColor* NativeGreyColorAsNSColor(ColorName name, BOOL isMain)
+static void DrawNativeGreyColorInRect(CGContextRef context, ColorName name,
+                                      CGRect rect, BOOL isMain)
 {
-  return [NSColor colorWithDeviceWhite:NativeGreyColorAsFloat(name, isMain) alpha:1.0f];
+  float grey = NativeGreyColorAsFloat(name, isMain);
+  CGContextSetRGBFillColor(context, grey, grey, grey, 1.0f);
+  CGContextFillRect(context, rect);
 }
 
 #endif // nsNativeThemeColors_h_