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 3 // found in the LICENSE file. 4 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_toolbar_view.h" 6 7 #include "chrome/browser/search/search.h" 8 #include "chrome/browser/themes/theme_properties.h" 9 #include "chrome/browser/themes/theme_service.h" 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_constants.h" 11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" 12 #import "chrome/browser/ui/cocoa/browser_window_controller.h" 13 #import "chrome/browser/ui/cocoa/nsview_additions.h" 14 #import "chrome/browser/ui/cocoa/themed_window.h" 15 #include "chrome/browser/ui/ntp_background_util.h" 16 #include "chrome/browser/ui/search/search_ui.h" 17 #include "grit/theme_resources.h" 18 #include "skia/ext/skia_utils_mac.h" 19 #import "ui/base/cocoa/nsgraphics_context_additions.h" 20 #include "ui/base/theme_provider.h" 21 #include "ui/gfx/canvas_skia_paint.h" 22 #include "ui/gfx/rect.h" 23 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" 24 25 @interface BookmarkBarToolbarView (Private) 26 - (void)drawAsDetachedBubble; 27 @end 28 29 @implementation BookmarkBarToolbarView 30 31 - (BOOL)isOpaque { 32 return [controller_ isInState:BookmarkBar::DETACHED]; 33 } 34 35 - (void)resetCursorRects { 36 NSCursor *arrow = [NSCursor arrowCursor]; 37 [self addCursorRect:[self visibleRect] cursor:arrow]; 38 [arrow setOnMouseEntered:YES]; 39 } 40 41 - (void)drawRect:(NSRect)rect { 42 if ([controller_ isInState:BookmarkBar::DETACHED] || 43 [controller_ isAnimatingToState:BookmarkBar::DETACHED] || 44 [controller_ isAnimatingFromState:BookmarkBar::DETACHED]) { 45 [self drawAsDetachedBubble]; 46 } else { 47 NSPoint position = [[self window] 48 themeImagePositionForAlignment:THEME_IMAGE_ALIGN_WITH_TAB_STRIP]; 49 [[NSGraphicsContext currentContext] 50 cr_setPatternPhase:position 51 forView:[self cr_viewBeingDrawnTo]]; 52 [self drawBackgroundWithOpaque:YES]; 53 } 54 } 55 56 - (void)drawAsDetachedBubble { 57 CGFloat morph = [controller_ detachedMorphProgress]; 58 NSRect bounds = [self bounds]; 59 ThemeService* themeService = [controller_ themeService]; 60 if (!themeService) 61 return; 62 63 [[NSColor whiteColor] set]; 64 NSRectFill([self bounds]); 65 66 // Overlay with a lighter background color. 67 NSColor* toolbarColor = gfx::SkColorToCalibratedNSColor( 68 chrome::GetDetachedBookmarkBarBackgroundColor(themeService)); 69 CGFloat alpha = morph * [toolbarColor alphaComponent]; 70 [[toolbarColor colorWithAlphaComponent:alpha] set]; 71 NSRectFillUsingOperation(bounds, NSCompositeSourceOver); 72 73 // Fade in/out the background. 74 { 75 gfx::ScopedNSGraphicsContextSaveGState bgScopedState; 76 NSGraphicsContext* context = [NSGraphicsContext currentContext]; 77 CGContextRef cgContext = static_cast<CGContextRef>([context graphicsPort]); 78 CGContextSetAlpha(cgContext, 1 - morph); 79 CGContextBeginTransparencyLayer(cgContext, NULL); 80 NSPoint position = [[self window] 81 themeImagePositionForAlignment:THEME_IMAGE_ALIGN_WITH_TAB_STRIP]; 82 [context cr_setPatternPhase:position forView:[self cr_viewBeingDrawnTo]]; 83 [self drawBackgroundWithOpaque:YES]; 84 CGContextEndTransparencyLayer(cgContext); 85 } 86 87 // Bottom stroke. 88 NSColor* strokeColor = gfx::SkColorToCalibratedNSColor( 89 chrome::GetDetachedBookmarkBarSeparatorColor(themeService)); 90 strokeColor = [[self strokeColor] blendedColorWithFraction:morph 91 ofColor:strokeColor]; 92 strokeColor = [strokeColor colorWithAlphaComponent:0.5]; 93 [strokeColor set]; 94 NSRect strokeRect = bounds; 95 strokeRect.size.height = [self cr_lineWidth]; 96 NSRectFillUsingOperation(strokeRect, NSCompositeSourceOver); 97 } 98 99 @end // @implementation BookmarkBarToolbarView 100