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]) {
22 [self setView:[[[NSView alloc] initWithFrame:NSZeroRect] autorelease]];
27 // Just here to satisfy the protocol - not actually invoked.
28 - (NSSize)preferredSize {
33 - (NSSize)preferredSizeForWidth:(CGFloat)width {
34 NSSize preferredSize = NSMakeSize(width, 0);
36 if ([notificationControllers_ count] == 0)
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;
51 preferredSize.height += autofill::kDetailVerticalPadding;
56 - (void)performLayout {
57 if ([notificationControllers_ count] == 0)
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()) {
70 NSSize size = [controller preferredSizeForWidth:NSWidth(remaining)];
71 NSDivideRect(remaining, &viewRect, &remaining, size.height, NSMaxYEdge);
72 [[controller view ] setFrame:viewRect];
73 [controller performLayout];
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:¬ification
91 [notificationController setHasArrow:notification.HasArrow()
92 withAnchorView:anchorView_];
95 [notificationControllers_ addObject:notificationController];
96 [[self view] addSubview:[notificationController view]];
100 - (void)setAnchorView:(NSView*)anchorView {
101 anchorView_ = anchorView;