Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / autofill / autofill_notification_container.mm
blob66561b2cd084387353f745124c46d4f2227a9701
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 @implementation AutofillNotificationContainer
15 - (id)initWithDelegate:(autofill::AutofillDialogViewDelegate*)delegate {
16   if (self = [super init]) {
17     delegate_ = delegate;
18     [self setView:[[[NSView alloc] initWithFrame:NSZeroRect] autorelease]];
19   }
20   return self;
23 // Just here to satisfy the protocol - not actually invoked.
24 - (NSSize)preferredSize {
25   NOTREACHED();
26   return NSZeroSize;
29 - (NSSize)preferredSizeForWidth:(CGFloat)width {
30   NSSize preferredSize = NSMakeSize(width, 0);
32   if ([notificationControllers_ count] == 0)
33     return preferredSize;
35   // A bit of padding above the arrow.
36   preferredSize.height += autofill::kDetailVerticalPadding;
38   for (AutofillNotificationController* controller in
39        notificationControllers_.get()) {
40     preferredSize.height += [controller preferredSizeForWidth:width].height;
41   }
43   preferredSize.height += autofill::kDetailVerticalPadding;
45   return preferredSize;
48 - (void)performLayout {
49   if ([notificationControllers_ count] == 0)
50     return;
52   NSRect remaining = [[self view] bounds];
53   remaining.origin.y += autofill::kDetailVerticalPadding;
54   remaining.size.height -= 2 * autofill::kDetailVerticalPadding;
56   for (AutofillNotificationController* controller in
57        notificationControllers_.get()) {
58     NSRect viewRect;
59     NSSize size = [controller preferredSizeForWidth:NSWidth(remaining)];
60     NSDivideRect(remaining, &viewRect, &remaining, size.height, NSMaxYEdge);
61     [[controller view ] setFrame:viewRect];
62     [controller performLayout];
63   }
64   DCHECK_EQ(0, NSHeight(remaining));
67 - (void)setNotifications:(const autofill::DialogNotifications&)notifications {
68   notificationControllers_.reset([[NSMutableArray alloc] init]);
69   [[self view] setSubviews:@[]];
71   for (size_t i = 0; i < notifications.size(); ++i) {
72     // Create basic notification view.
73     const autofill::DialogNotification& notification = notifications[i];
74     base::scoped_nsobject<AutofillNotificationController>
75         notificationController([[AutofillNotificationController alloc]
76                                     initWithNotification:&notification
77                                                 delegate:delegate_]);
79     [notificationControllers_ addObject:notificationController];
80     [[self view] addSubview:[notificationController view]];
81   }
84 @end