Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: chrome/browser/ui/cocoa/tabs/tab_view.mm

Issue 7572031: Always call the class methods to save/restore contexts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ensure saving before context access Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "chrome/browser/ui/cocoa/tabs/tab_view.h" 5 #import "chrome/browser/ui/cocoa/tabs/tab_view.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "chrome/browser/themes/theme_service.h" 8 #include "chrome/browser/themes/theme_service.h"
9 #import "chrome/browser/ui/cocoa/nsview_additions.h" 9 #import "chrome/browser/ui/cocoa/nsview_additions.h"
10 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h" 10 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h"
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 // If the mouse up occurred in our view or over the close button, then 244 // If the mouse up occurred in our view or over the close button, then
245 // close. 245 // close.
246 if ([self hitTest:upLocation]) 246 if ([self hitTest:upLocation])
247 [controller_ closeTab:self]; 247 [controller_ closeTab:self];
248 } 248 }
249 } 249 }
250 250
251 - (void)drawRect:(NSRect)dirtyRect { 251 - (void)drawRect:(NSRect)dirtyRect {
252 const CGFloat lineWidth = [self cr_lineWidth]; 252 const CGFloat lineWidth = [self cr_lineWidth];
253 253
254 gfx::ScopedNSGraphicsContextSaveGState scopedGState;
254 NSGraphicsContext* context = [NSGraphicsContext currentContext]; 255 NSGraphicsContext* context = [NSGraphicsContext currentContext];
255 gfx::ScopedNSGraphicsContextSaveGState scopedGState(context);
256 256
257 ThemeService* themeProvider = 257 ThemeService* themeProvider =
258 static_cast<ThemeService*>([[self window] themeProvider]); 258 static_cast<ThemeService*>([[self window] themeProvider]);
259 [context setPatternPhase:[[self window] themePatternPhase]]; 259 [context setPatternPhase:[[self window] themePatternPhase]];
260 260
261 NSRect rect = [self bounds]; 261 NSRect rect = [self bounds];
262 NSBezierPath* path = [self bezierPathForRect:rect]; 262 NSBezierPath* path = [self bezierPathForRect:rect];
263 263
264 BOOL selected = [self state]; 264 BOOL selected = [self state];
265 // Don't draw the window/tab bar background when selected, since the tab 265 // Don't draw the window/tab bar background when selected, since the tab
(...skipping 15 matching lines...) Expand all
281 if (backgroundImageColor) { 281 if (backgroundImageColor) {
282 [backgroundImageColor set]; 282 [backgroundImageColor set];
283 [path fill]; 283 [path fill];
284 } else { 284 } else {
285 // Use the window's background color rather than |[NSColor 285 // Use the window's background color rather than |[NSColor
286 // windowBackgroundColor]|, which gets confused by the fullscreen window. 286 // windowBackgroundColor]|, which gets confused by the fullscreen window.
287 // (The result is the same for normal, non-fullscreen windows.) 287 // (The result is the same for normal, non-fullscreen windows.)
288 [[[self window] backgroundColor] set]; 288 [[[self window] backgroundColor] set];
289 [path fill]; 289 [path fill];
290 290
291 gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState(context); 291 gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState;
292 CGContextRef cgContext = 292 CGContextRef cgContext =
293 static_cast<CGContextRef>([context graphicsPort]); 293 static_cast<CGContextRef>([context graphicsPort]);
Mark Mentovai 2011/08/04 23:20:17 This is probably fine because the port should have
Avi (use Gerrit) 2011/08/05 00:18:50 Done.
294 CGContextBeginTransparencyLayer(cgContext, 0); 294 CGContextBeginTransparencyLayer(cgContext, 0);
295 CGContextSetAlpha(cgContext, 0.5); 295 CGContextSetAlpha(cgContext, 0.5);
296 [path addClip]; 296 [path addClip];
297 [super drawBackgroundWithOpaque:NO]; 297 [super drawBackgroundWithOpaque:NO];
298 CGContextEndTransparencyLayer(cgContext); 298 CGContextEndTransparencyLayer(cgContext);
299 } 299 }
300 } 300 }
301 301
302 [context saveGraphicsState];
303 [path addClip];
304
305 // Use the same overlay for the selected state and for hover and alert glows;
306 // for the selected state, it's fully opaque.
307 CGFloat hoverAlpha = [self hoverAlpha];
308 CGFloat alertAlpha = [self alertAlpha];
309 if (selected || hoverAlpha > 0 || alertAlpha > 0) {
310 // Draw the selected background / glow overlay.
311 gfx::ScopedNSGraphicsContextSaveGState drawHoverState(context);
312 CGContextRef cgContext = static_cast<CGContextRef>([context graphicsPort]);
313 CGContextBeginTransparencyLayer(cgContext, 0);
314 if (!selected) {
315 // The alert glow overlay is like the selected state but at most at most
316 // 80% opaque. The hover glow brings up the overlay's opacity at most 50%.
317 CGFloat backgroundAlpha = 0.8 * alertAlpha;
318 backgroundAlpha += (1 - backgroundAlpha) * 0.5 * hoverAlpha;
319 CGContextSetAlpha(cgContext, backgroundAlpha);
320 }
321 [path addClip];
322 {
323 gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState(context);
324 [super drawBackgroundWithOpaque:NO];
325 }
326
327 // Draw a mouse hover gradient for the default themes.
328 if (!selected && hoverAlpha > 0) {
329 if (themeProvider && !hasBackgroundImage) {
330 scoped_nsobject<NSGradient> glow([NSGradient alloc]);
331 [glow initWithStartingColor:[NSColor colorWithCalibratedWhite:1.0
332 alpha:1.0 * hoverAlpha]
333 endingColor:[NSColor colorWithCalibratedWhite:1.0
334 alpha:0.0]];
335
336 NSPoint point = hoverPoint_;
337 point.y = NSHeight(rect);
338 [glow drawFromCenter:point
339 radius:0.0
340 toCenter:point
341 radius:NSWidth(rect) / 3.0
342 options:NSGradientDrawsBeforeStartingLocation];
343
344 [glow drawInBezierPath:path relativeCenterPosition:hoverPoint_];
345 }
346 }
347
348 CGContextEndTransparencyLayer(cgContext);
349 }
350
351 BOOL active = [[self window] isKeyWindow] || [[self window] isMainWindow]; 302 BOOL active = [[self window] isKeyWindow] || [[self window] isMainWindow];
352 CGFloat borderAlpha = selected ? (active ? 0.3 : 0.2) : 0.2; 303 CGFloat borderAlpha = selected ? (active ? 0.3 : 0.2) : 0.2;
353 NSColor* borderColor = [NSColor colorWithDeviceWhite:0.0 alpha:borderAlpha]; 304 NSColor* borderColor = [NSColor colorWithDeviceWhite:0.0 alpha:borderAlpha];
354 NSColor* highlightColor = themeProvider ? themeProvider->GetNSColor( 305 NSColor* highlightColor = themeProvider ? themeProvider->GetNSColor(
355 themeProvider->UsingDefaultTheme() ? 306 themeProvider->UsingDefaultTheme() ?
356 ThemeService::COLOR_TOOLBAR_BEZEL : 307 ThemeService::COLOR_TOOLBAR_BEZEL :
357 ThemeService::COLOR_TOOLBAR, true) : nil; 308 ThemeService::COLOR_TOOLBAR, true) : nil;
358 309
359 // Draw the top inner highlight within the tab if using the default theme. 310 {
360 if (themeProvider && themeProvider->UsingDefaultTheme()) { 311 gfx::ScopedNSGraphicsContextSaveGState contextSave;
Mark Mentovai 2011/08/04 23:20:17 I’m not going to do a line-by-line comparison here
361 NSAffineTransform* highlightTransform = [NSAffineTransform transform]; 312 [path addClip];
362 [highlightTransform translateXBy:lineWidth yBy:-lineWidth]; 313
363 if (selected) { 314 // Use the same overlay for the selected state and for hover and alert
364 scoped_nsobject<NSBezierPath> highlightPath([path copy]); 315 // glows; for the selected state, it's fully opaque.
365 [highlightPath transformUsingAffineTransform:highlightTransform]; 316 CGFloat hoverAlpha = [self hoverAlpha];
366 [highlightColor setStroke]; 317 CGFloat alertAlpha = [self alertAlpha];
367 [highlightPath setLineWidth:lineWidth]; 318 if (selected || hoverAlpha > 0 || alertAlpha > 0) {
368 [highlightPath stroke]; 319 // Draw the selected background / glow overlay.
369 highlightTransform = [NSAffineTransform transform]; 320 gfx::ScopedNSGraphicsContextSaveGState drawHoverState;
370 [highlightTransform translateXBy:-2 * lineWidth yBy:0.0]; 321 CGContextRef cgContext =
371 [highlightPath transformUsingAffineTransform:highlightTransform]; 322 static_cast<CGContextRef>([context graphicsPort]);
Mark Mentovai 2011/08/04 23:20:17 Ditto.
Avi (use Gerrit) 2011/08/05 00:18:50 Mr Paranoid!
372 [highlightPath stroke]; 323 CGContextBeginTransparencyLayer(cgContext, 0);
373 } else { 324 if (!selected) {
374 NSBezierPath* topHighlightPath = 325 // The alert glow overlay is like the selected state but at most at most
375 [self topHighlightBezierPathForRect:[self bounds]]; 326 // 80% opaque. The hover glow brings up the overlay's opacity at most
376 [topHighlightPath transformUsingAffineTransform:highlightTransform]; 327 // 50%.
377 [highlightColor setStroke]; 328 CGFloat backgroundAlpha = 0.8 * alertAlpha;
378 [topHighlightPath setLineWidth:lineWidth]; 329 backgroundAlpha += (1 - backgroundAlpha) * 0.5 * hoverAlpha;
379 [topHighlightPath stroke]; 330 CGContextSetAlpha(cgContext, backgroundAlpha);
331 }
332 [path addClip];
333 {
334 gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState;
335 [super drawBackgroundWithOpaque:NO];
336 }
337
338 // Draw a mouse hover gradient for the default themes.
339 if (!selected && hoverAlpha > 0) {
340 if (themeProvider && !hasBackgroundImage) {
341 scoped_nsobject<NSGradient> glow([NSGradient alloc]);
342 [glow initWithStartingColor:[NSColor colorWithCalibratedWhite:1.0
343 alpha:1.0 * hoverAlpha]
344 endingColor:[NSColor colorWithCalibratedWhite:1.0
345 alpha:0.0]];
346
347 NSPoint point = hoverPoint_;
348 point.y = NSHeight(rect);
349 [glow drawFromCenter:point
350 radius:0.0
351 toCenter:point
352 radius:NSWidth(rect) / 3.0
353 options:NSGradientDrawsBeforeStartingLocation];
354
355 [glow drawInBezierPath:path relativeCenterPosition:hoverPoint_];
356 }
357 }
358
359 CGContextEndTransparencyLayer(cgContext);
360 }
361
362 // Draw the top inner highlight within the tab if using the default theme.
363 if (themeProvider && themeProvider->UsingDefaultTheme()) {
364 NSAffineTransform* highlightTransform = [NSAffineTransform transform];
365 [highlightTransform translateXBy:lineWidth yBy:-lineWidth];
366 if (selected) {
367 scoped_nsobject<NSBezierPath> highlightPath([path copy]);
368 [highlightPath transformUsingAffineTransform:highlightTransform];
369 [highlightColor setStroke];
370 [highlightPath setLineWidth:lineWidth];
371 [highlightPath stroke];
372 highlightTransform = [NSAffineTransform transform];
373 [highlightTransform translateXBy:-2 * lineWidth yBy:0.0];
374 [highlightPath transformUsingAffineTransform:highlightTransform];
375 [highlightPath stroke];
376 } else {
377 NSBezierPath* topHighlightPath =
378 [self topHighlightBezierPathForRect:[self bounds]];
379 [topHighlightPath transformUsingAffineTransform:highlightTransform];
380 [highlightColor setStroke];
381 [topHighlightPath setLineWidth:lineWidth];
382 [topHighlightPath stroke];
383 }
380 } 384 }
381 } 385 }
382 386
383 [context restoreGraphicsState];
384
385 // Draw the top stroke. 387 // Draw the top stroke.
386 { 388 {
387 gfx::ScopedNSGraphicsContextSaveGState drawBorderState(context); 389 gfx::ScopedNSGraphicsContextSaveGState drawBorderState;
388 [borderColor set]; 390 [borderColor set];
389 [path setLineWidth:lineWidth]; 391 [path setLineWidth:lineWidth];
390 [path stroke]; 392 [path stroke];
391 } 393 }
392 394
393 // Mimic the tab strip's bottom border, which consists of a dark border 395 // Mimic the tab strip's bottom border, which consists of a dark border
394 // and light highlight. 396 // and light highlight.
395 if (![controller_ active]) { 397 if (![controller_ active]) {
396 [path addClip]; 398 [path addClip];
397 NSRect borderRect = rect; 399 NSRect borderRect = rect;
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 NSMakePoint(NSMinX(rect) + kInsetMultiplier * NSHeight(rect), 649 NSMakePoint(NSMinX(rect) + kInsetMultiplier * NSHeight(rect),
648 NSMaxY(rect)); 650 NSMaxY(rect));
649 651
650 NSBezierPath* path = [NSBezierPath bezierPath]; 652 NSBezierPath* path = [NSBezierPath bezierPath];
651 [path moveToPoint:topLeft]; 653 [path moveToPoint:topLeft];
652 [path lineToPoint:topRight]; 654 [path lineToPoint:topRight];
653 return path; 655 return path;
654 } 656 }
655 657
656 @end // @implementation TabView(Private) 658 @end // @implementation TabView(Private)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698