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 #include "chrome/browser/ui/infobar_container_delegate.h"
11 #import "components/infobars/core/infobar.h"
12 #import "ui/base/cocoa/nsview_additions.h"
14 @interface InfobarLabelTextField : NSTextField
17 @implementation InfobarLabelTextField
19 - (void)drawRect:(NSRect)rect {
20 NSView* infobarGradientView = [self superview];
21 [self cr_drawUsingAncestor:infobarGradientView inRect:rect];
22 [super drawRect:rect];
31 namespace InfoBarUtilities {
33 // Move the |toMove| view |spacing| pixels before/after the |anchor| view.
34 // |after| signifies the side of |anchor| on which to place |toMove|.
35 void MoveControl(NSView* anchor, NSView* toMove, int spacing, bool after) {
36 NSRect anchorFrame = [anchor frame];
37 NSRect toMoveFrame = [toMove frame];
39 // At the time of this writing, OS X doesn't natively support BiDi UIs, but
40 // it doesn't hurt to be forward looking.
44 toMoveFrame.origin.x = NSMaxX(anchorFrame) + spacing;
46 // Place toMove to theleft of anchor.
47 toMoveFrame.origin.x = NSMinX(anchorFrame) -
48 spacing - NSWidth(toMoveFrame);
50 [toMove setFrame:toMoveFrame];
53 // Check that the control |before| is ordered visually before the |after|
54 // control. Also, check that there is space between them.
55 bool VerifyControlOrderAndSpacing(id before, id after) {
56 NSRect beforeFrame = [before frame];
57 NSRect afterFrame = [after frame];
58 return NSMinX(afterFrame) >= NSMaxX(beforeFrame);
61 // Vertically center |toMove| in its container.
62 void VerticallyCenterView(NSView* toMove) {
63 NSRect superViewFrame = [[toMove superview] frame];
64 NSRect viewFrame = [toMove frame];
65 // If the superview is the infobar view, then subtract out the anti-spoof
66 // height so that the content is centered in the content area of the infobar,
67 // rather than in the total height (which includes the bulge).
68 CGFloat superHeight = NSHeight(superViewFrame);
69 if ([[toMove superview] isKindOfClass:[InfoBarGradientView class]])
70 superHeight = InfoBarContainerDelegate::kDefaultBarTargetHeight;
72 floor((superHeight - NSHeight(viewFrame)) / 2.0);
73 [toMove setFrame:viewFrame];
76 // Creates a label control in the style we need for the infobar's labels
78 NSTextField* CreateLabel(NSRect bounds) {
79 NSTextField* ret = [[InfobarLabelTextField alloc] initWithFrame:bounds];
81 [ret setDrawsBackground:NO];
86 // Adds an item with the specified properties to |menu|.
87 void AddMenuItem(NSMenu *menu, id target, SEL selector, NSString* title,
88 int tag, bool enabled, bool checked) {
90 [menu addItem:[NSMenuItem separatorItem]];
92 base::scoped_nsobject<NSMenuItem> item(
93 [[NSMenuItem alloc] initWithTitle:title
98 [item setTarget:target];
100 [item setState:NSOnState];
102 [item setEnabled:NO];
106 } // namespace InfoBarUtilities