Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / autofill / autofill_notification_container.mm
blobafcbdaa7d0d115bcbb5f12522ac9f8b09c8400c2
1 // Copyright 2013 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/autofill/autofill_notification_container.h"
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "chrome/browser/ui/autofill/autofill_dialog_types.h"
10 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h"
11 #import "chrome/browser/ui/cocoa/autofill/autofill_notification_controller.h"
13 // Padding above the notifications section.
14 const CGFloat kTopPadding =
15     autofill::kDetailVerticalPadding - autofill::kArrowHeight;
17 @implementation AutofillNotificationContainer
19 - (id)initWithDelegate:(autofill::AutofillDialogViewDelegate*)delegate {
20   if (self = [super init]) {
21     delegate_ = delegate;
22     [self setView:[[[NSView alloc] initWithFrame:NSZeroRect] autorelease]];
23   }
24   return self;
27 // Just here to satisfy the protocol - not actually invoked.
28 - (NSSize)preferredSize {
29   NOTREACHED();
30   return NSZeroSize;
33 - (NSSize)preferredSizeForWidth:(CGFloat)width {
34   NSSize preferredSize = NSMakeSize(width, 0);
36   if ([notificationControllers_ count] == 0)
37     return preferredSize;
39   // A bit of padding above the arrow.
40   preferredSize.height += kTopPadding;
42   // If the first notification doesn't have an arrow, reserve empty space.
43   if (![[notificationControllers_ objectAtIndex:0] hasArrow])
44     preferredSize.height += autofill::kArrowHeight;
46   for (AutofillNotificationController* controller in
47        notificationControllers_.get()) {
48     preferredSize.height += [controller preferredSizeForWidth:width].height;
49   }
51   preferredSize.height += autofill::kDetailVerticalPadding;
53   return preferredSize;
56 - (void)performLayout {
57   if ([notificationControllers_ count] == 0)
58     return;
60   NSRect remaining = [[self view] bounds];
61   remaining.origin.y += autofill::kDetailVerticalPadding;
62   remaining.size.height -= kTopPadding + autofill::kDetailVerticalPadding;
64   if (![[notificationControllers_ objectAtIndex:0] hasArrow])
65     remaining.size.height -= autofill::kArrowHeight;
67   for (AutofillNotificationController* controller in
68        notificationControllers_.get()) {
69     NSRect viewRect;
70     NSSize size = [controller preferredSizeForWidth:NSWidth(remaining)];
71     NSDivideRect(remaining, &viewRect, &remaining, size.height, NSMaxYEdge);
72     [[controller view ] setFrame:viewRect];
73     [controller performLayout];
74   }
75   DCHECK_EQ(0, NSHeight(remaining));
78 - (void)setNotifications:(const autofill::DialogNotifications&)notifications {
79   notificationControllers_.reset([[NSMutableArray alloc] init]);
80   [[self view] setSubviews:@[]];
82   for (size_t i = 0; i < notifications.size(); ++i) {
83     // Create basic notification view.
84     const autofill::DialogNotification& notification = notifications[i];
85     base::scoped_nsobject<AutofillNotificationController>
86         notificationController([[AutofillNotificationController alloc]
87                                     initWithNotification:&notification
88                                                 delegate:delegate_]);
90     if (i == 0) {
91       [notificationController setHasArrow:notification.HasArrow()
92                            withAnchorView:anchorView_];
93     }
95     [notificationControllers_ addObject:notificationController];
96     [[self view] addSubview:[notificationController view]];
97   }
100 - (void)setAnchorView:(NSView*)anchorView {
101   anchorView_ = anchorView;
104 @end