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.
5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_toolbar_view.h"
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/themed_window.h"
14 #include "chrome/browser/ui/search/search_ui.h"
15 #include "skia/ext/skia_utils_mac.h"
16 #import "ui/base/cocoa/nsgraphics_context_additions.h"
17 #import "ui/base/cocoa/nsview_additions.h"
18 #include "ui/base/theme_provider.h"
19 #include "ui/gfx/canvas_skia_paint.h"
20 #include "ui/gfx/rect.h"
21 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
23 @interface BookmarkBarToolbarView (Private)
24 - (void)drawAsDetachedBubble;
27 @implementation BookmarkBarToolbarView
30 return [controller_ isInState:BookmarkBar::DETACHED];
33 - (void)resetCursorRects {
34 NSCursor *arrow = [NSCursor arrowCursor];
35 [self addCursorRect:[self visibleRect] cursor:arrow];
36 [arrow setOnMouseEntered:YES];
39 - (void)drawRect:(NSRect)rect {
40 if ([controller_ isInState:BookmarkBar::DETACHED] ||
41 [controller_ isAnimatingToState:BookmarkBar::DETACHED] ||
42 [controller_ isAnimatingFromState:BookmarkBar::DETACHED]) {
43 [self drawAsDetachedBubble];
45 NSPoint position = [[self window]
46 themeImagePositionForAlignment:THEME_IMAGE_ALIGN_WITH_TAB_STRIP];
47 [[NSGraphicsContext currentContext]
48 cr_setPatternPhase:position
49 forView:[self cr_viewBeingDrawnTo]];
50 [self drawBackgroundWithOpaque:YES];
54 - (void)drawAsDetachedBubble {
55 CGFloat morph = [controller_ detachedMorphProgress];
56 NSRect bounds = [self bounds];
57 ThemeService* themeService = [controller_ themeService];
61 [[NSColor whiteColor] set];
62 NSRectFill([self bounds]);
64 // Overlay with a lighter background color.
65 NSColor* toolbarColor = gfx::SkColorToCalibratedNSColor(
66 chrome::GetDetachedBookmarkBarBackgroundColor(themeService));
67 CGFloat alpha = morph * [toolbarColor alphaComponent];
68 [[toolbarColor colorWithAlphaComponent:alpha] set];
69 NSRectFillUsingOperation(bounds, NSCompositeSourceOver);
71 // Fade in/out the background.
73 gfx::ScopedNSGraphicsContextSaveGState bgScopedState;
74 NSGraphicsContext* context = [NSGraphicsContext currentContext];
75 CGContextRef cgContext = static_cast<CGContextRef>([context graphicsPort]);
76 CGContextSetAlpha(cgContext, 1 - morph);
77 CGContextBeginTransparencyLayer(cgContext, NULL);
78 NSPoint position = [[self window]
79 themeImagePositionForAlignment:THEME_IMAGE_ALIGN_WITH_TAB_STRIP];
80 [context cr_setPatternPhase:position forView:[self cr_viewBeingDrawnTo]];
81 [self drawBackgroundWithOpaque:YES];
82 CGContextEndTransparencyLayer(cgContext);
86 NSColor* strokeColor = gfx::SkColorToCalibratedNSColor(
87 chrome::GetDetachedBookmarkBarSeparatorColor(themeService));
88 strokeColor = [[self strokeColor] blendedColorWithFraction:morph
90 strokeColor = [strokeColor colorWithAlphaComponent:0.5];
92 NSRect strokeRect = bounds;
93 strokeRect.size.height = [self cr_lineWidth];
94 NSRectFillUsingOperation(strokeRect, NSCompositeSourceOver);
97 @end // @implementation BookmarkBarToolbarView