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]) {
18 [self setView:[[[NSView alloc] initWithFrame:NSZeroRect] autorelease]];
23 // Just here to satisfy the protocol - not actually invoked.
24 - (NSSize)preferredSize {
29 - (NSSize)preferredSizeForWidth:(CGFloat)width {
30 NSSize preferredSize = NSMakeSize(width, 0);
32 if ([notificationControllers_ count] == 0)
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;
43 preferredSize.height += autofill::kDetailVerticalPadding;
48 - (void)performLayout {
49 if ([notificationControllers_ count] == 0)
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()) {
59 NSSize size = [controller preferredSizeForWidth:NSWidth(remaining)];
60 NSDivideRect(remaining, &viewRect, &remaining, size.height, NSMaxYEdge);
61 [[controller view ] setFrame:viewRect];
62 [controller performLayout];
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:¬ification
79 [notificationControllers_ addObject:notificationController];
80 [[self view] addSubview:[notificationController view]];