Home | History | Annotate | Download | only in infobars
      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.
      4 
      5 #import "chrome/browser/ui/cocoa/infobars/infobar_utilities.h"
      6 
      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 
     11 namespace InfoBarUtilities {
     12 
     13 // Move the |toMove| view |spacing| pixels before/after the |anchor| view.
     14 // |after| signifies the side of |anchor| on which to place |toMove|.
     15 void MoveControl(NSView* anchor, NSView* toMove, int spacing, bool after) {
     16   NSRect anchorFrame = [anchor frame];
     17   NSRect toMoveFrame = [toMove frame];
     18 
     19   // At the time of this writing, OS X doesn't natively support BiDi UIs, but
     20   // it doesn't hurt to be forward looking.
     21   bool toRight = after;
     22 
     23   if (toRight) {
     24     toMoveFrame.origin.x = NSMaxX(anchorFrame) + spacing;
     25   } else {
     26     // Place toMove to theleft of anchor.
     27     toMoveFrame.origin.x = NSMinX(anchorFrame) -
     28         spacing - NSWidth(toMoveFrame);
     29   }
     30   [toMove setFrame:toMoveFrame];
     31 }
     32 
     33 // Check that the control |before| is ordered visually before the |after|
     34 // control. Also, check that there is space between them.
     35 bool VerifyControlOrderAndSpacing(id before, id after) {
     36   NSRect beforeFrame = [before frame];
     37   NSRect afterFrame = [after frame];
     38   return NSMinX(afterFrame) >= NSMaxX(beforeFrame);
     39 }
     40 
     41 // Vertically center |toMove| in its container.
     42 void VerticallyCenterView(NSView* toMove) {
     43   NSRect superViewFrame = [[toMove superview] frame];
     44   NSRect viewFrame = [toMove frame];
     45   // If the superview is the infobar view, then subtract out the anti-spoof
     46   // height so that the content is centered in the content area of the infobar,
     47   // rather than in the total height (which includes the bulge).
     48   CGFloat superHeight = NSHeight(superViewFrame);
     49   if ([[toMove superview] isKindOfClass:[InfoBarGradientView class]])
     50     superHeight = infobars::kBaseHeight;
     51   viewFrame.origin.y =
     52       floor((superHeight - NSHeight(viewFrame)) / 2.0);
     53   [toMove setFrame:viewFrame];
     54 }
     55 
     56 // Creates a label control in the style we need for the infobar's labels
     57 // within |bounds|.
     58 NSTextField* CreateLabel(NSRect bounds) {
     59   NSTextField* ret = [[NSTextField alloc] initWithFrame:bounds];
     60   [ret setEditable:NO];
     61   [ret setDrawsBackground:NO];
     62   [ret setBordered:NO];
     63   return ret;
     64 }
     65 
     66 // Adds an item with the specified properties to |menu|.
     67 void AddMenuItem(NSMenu *menu, id target, SEL selector, NSString* title,
     68     int tag, bool enabled, bool checked) {
     69   if (tag == -1) {
     70     [menu addItem:[NSMenuItem separatorItem]];
     71   } else {
     72     base::scoped_nsobject<NSMenuItem> item(
     73         [[NSMenuItem alloc] initWithTitle:title
     74                                    action:selector
     75                             keyEquivalent:@""]);
     76     [item setTag:tag];
     77     [menu addItem:item];
     78     [item setTarget:target];
     79     if (checked)
     80       [item setState:NSOnState];
     81     if (!enabled)
     82       [item setEnabled:NO];
     83   }
     84 }
     85 
     86 }  // namespace InfoBarUtilities
     87