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_bubble_controller.h"
7 #import "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h"
8 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
9 #include "skia/ext/skia_utils_mac.h"
13 // Border inset for error label.
14 const CGFloat kLabelInset = 3.0;
16 const CGFloat kMaxLabelWidth =
17 2 * autofill::kFieldWidth + autofill::kHorizontalFieldPadding;
21 @interface AutofillBubbleController ()
23 // Update the current message, keeping arrow location in place.
24 - (void)updateMessage:(NSString*)message display:(BOOL)display;
28 @implementation AutofillBubbleController
30 - (id)initWithParentWindow:(NSWindow*)parentWindow
31 message:(NSString*)message {
32 return [self initWithParentWindow:parentWindow
34 inset:NSMakeSize(kLabelInset, kLabelInset)
35 arrowLocation:info_bubble::kTopCenter];
38 - (id)initWithParentWindow:(NSWindow*)parentWindow
39 message:(NSString*)message
41 arrowLocation:(info_bubble::BubbleArrowLocation)arrowLocation {
42 base::scoped_nsobject<InfoBubbleWindow> window(
43 [[InfoBubbleWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 100)
44 styleMask:NSBorderlessWindowMask
45 backing:NSBackingStoreBuffered
47 [window setAllowedAnimations:info_bubble::kAnimateNone];
48 if ((self = [super initWithWindow:window
49 parentWindow:parentWindow
50 anchoredAt:NSZeroPoint])) {
52 [self setShouldOpenAsKeyWindow:NO];
53 [[self bubble] setArrowLocation:arrowLocation];
54 [[self bubble] setAlignment:info_bubble::kAlignArrowToAnchor];
56 label_.reset([[NSTextField alloc] init]);
57 [label_ setEditable:NO];
58 [label_ setBordered:NO];
59 [label_ setDrawsBackground:NO];
60 [[self bubble] addSubview:label_];
62 [self updateMessage:message display:NO];
68 return kMaxLabelWidth + 2 * inset_.width;
71 - (void)setMessage:(NSString*)message {
72 if ([[label_ stringValue] isEqualToString:message])
74 [self updateMessage:message display:YES];
77 - (void)updateMessage:(NSString*)message display:(BOOL)display {
78 [label_ setStringValue:message];
80 NSRect labelFrame = NSMakeRect(inset_.width, inset_.height, 0, 0);
81 labelFrame.size = [[label_ cell] cellSizeForBounds:
82 NSMakeRect(0, 0, kMaxLabelWidth, CGFLOAT_MAX)];
83 [label_ setFrame:labelFrame];
85 // Update window's size, but ensure the origin is maintained so that the arrow
86 // still points at the same location.
87 NSRect windowFrame = [[self window] frame];
88 NSPoint origin = windowFrame.origin;
89 origin.y += NSHeight(windowFrame);
90 windowFrame.size = NSMakeSize(
91 NSMaxX([label_ frame]),
92 NSHeight([label_ frame]) + info_bubble::kBubbleArrowHeight);
93 windowFrame = NSInsetRect(windowFrame, -inset_.width, -inset_.height);
94 origin.y -= NSHeight(windowFrame);
95 windowFrame.origin = origin;
96 [[self window] setFrame:windowFrame display:display];