1 // Copyright (c) 2012 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/infobars/infobar_utilities.h"
7 #include "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h"
9 #import "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h"
10 #import "chrome/browser/ui/cocoa/nsview_additions.h"
11 #import "components/infobars/core/infobar.h"
13 @interface InfobarLabelTextField : NSTextField
16 @implementation InfobarLabelTextField
18 - (void)drawRect:(NSRect)rect {
19 NSView* infobarGradientView = [self superview];
20 [self cr_drawUsingAncestor:infobarGradientView inRect:rect];
21 [super drawRect:rect];
30 namespace InfoBarUtilities {
32 // Move the |toMove| view |spacing| pixels before/after the |anchor| view.
33 // |after| signifies the side of |anchor| on which to place |toMove|.
34 void MoveControl(NSView* anchor, NSView* toMove, int spacing, bool after) {
35 NSRect anchorFrame = [anchor frame];
36 NSRect toMoveFrame = [toMove frame];
38 // At the time of this writing, OS X doesn't natively support BiDi UIs, but
39 // it doesn't hurt to be forward looking.
43 toMoveFrame.origin.x = NSMaxX(anchorFrame) + spacing;
45 // Place toMove to theleft of anchor.
46 toMoveFrame.origin.x = NSMinX(anchorFrame) -
47 spacing - NSWidth(toMoveFrame);
49 [toMove setFrame:toMoveFrame];
52 // Check that the control |before| is ordered visually before the |after|
53 // control. Also, check that there is space between them.
54 bool VerifyControlOrderAndSpacing(id before, id after) {
55 NSRect beforeFrame = [before frame];
56 NSRect afterFrame = [after frame];
57 return NSMinX(afterFrame) >= NSMaxX(beforeFrame);
60 // Vertically center |toMove| in its container.
61 void VerticallyCenterView(NSView* toMove) {
62 NSRect superViewFrame = [[toMove superview] frame];
63 NSRect viewFrame = [toMove frame];
64 // If the superview is the infobar view, then subtract out the anti-spoof
65 // height so that the content is centered in the content area of the infobar,
66 // rather than in the total height (which includes the bulge).
67 CGFloat superHeight = NSHeight(superViewFrame);
68 if ([[toMove superview] isKindOfClass:[InfoBarGradientView class]])
69 superHeight = infobars::InfoBar::kDefaultBarTargetHeight;
71 floor((superHeight - NSHeight(viewFrame)) / 2.0);
72 [toMove setFrame:viewFrame];
75 // Creates a label control in the style we need for the infobar's labels
77 NSTextField* CreateLabel(NSRect bounds) {
78 NSTextField* ret = [[InfobarLabelTextField alloc] initWithFrame:bounds];
80 [ret setDrawsBackground:NO];
85 // Adds an item with the specified properties to |menu|.
86 void AddMenuItem(NSMenu *menu, id target, SEL selector, NSString* title,
87 int tag, bool enabled, bool checked) {
89 [menu addItem:[NSMenuItem separatorItem]];
91 base::scoped_nsobject<NSMenuItem> item(
92 [[NSMenuItem alloc] initWithTitle:title
97 [item setTarget:target];
99 [item setState:NSOnState];
101 [item setEnabled:NO];
105 } // namespace InfoBarUtilities