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/infobars/infobar.h"
9 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h"
10 #import "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h"
12 namespace InfoBarUtilities {
14 // Move the |toMove| view |spacing| pixels before/after the |anchor| view.
15 // |after| signifies the side of |anchor| on which to place |toMove|.
16 void MoveControl(NSView* anchor, NSView* toMove, int spacing, bool after) {
17 NSRect anchorFrame = [anchor frame];
18 NSRect toMoveFrame = [toMove frame];
20 // At the time of this writing, OS X doesn't natively support BiDi UIs, but
21 // it doesn't hurt to be forward looking.
25 toMoveFrame.origin.x = NSMaxX(anchorFrame) + spacing;
27 // Place toMove to theleft of anchor.
28 toMoveFrame.origin.x = NSMinX(anchorFrame) -
29 spacing - NSWidth(toMoveFrame);
31 [toMove setFrame:toMoveFrame];
34 // Check that the control |before| is ordered visually before the |after|
35 // control. Also, check that there is space between them.
36 bool VerifyControlOrderAndSpacing(id before, id after) {
37 NSRect beforeFrame = [before frame];
38 NSRect afterFrame = [after frame];
39 return NSMinX(afterFrame) >= NSMaxX(beforeFrame);
42 // Vertically center |toMove| in its container.
43 void VerticallyCenterView(NSView* toMove) {
44 NSRect superViewFrame = [[toMove superview] frame];
45 NSRect viewFrame = [toMove frame];
46 // If the superview is the infobar view, then subtract out the anti-spoof
47 // height so that the content is centered in the content area of the infobar,
48 // rather than in the total height (which includes the bulge).
49 CGFloat superHeight = NSHeight(superViewFrame);
50 if ([[toMove superview] isKindOfClass:[InfoBarGradientView class]])
51 superHeight = InfoBar::kDefaultBarTargetHeight;
53 floor((superHeight - NSHeight(viewFrame)) / 2.0);
54 [toMove setFrame:viewFrame];
57 // Creates a label control in the style we need for the infobar's labels
59 NSTextField* CreateLabel(NSRect bounds) {
60 NSTextField* ret = [[NSTextField alloc] initWithFrame:bounds];
62 [ret setDrawsBackground:NO];
67 // Adds an item with the specified properties to |menu|.
68 void AddMenuItem(NSMenu *menu, id target, SEL selector, NSString* title,
69 int tag, bool enabled, bool checked) {
71 [menu addItem:[NSMenuItem separatorItem]];
73 base::scoped_nsobject<NSMenuItem> item(
74 [[NSMenuItem alloc] initWithTitle:title
79 [item setTarget:target];
81 [item setState:NSOnState];
87 } // namespace InfoBarUtilities