1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/autofill/autofill_details_container.h"
#include <algorithm>
#include "base/mac/foundation_util.h"
#include "chrome/browser/ui/autofill/autofill_dialog_view_delegate.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_bubble_controller.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_section_container.h"
#import "chrome/browser/ui/cocoa/info_bubble_view.h"
typedef BOOL (^FieldFilterBlock)(NSView<AutofillInputField>*);
@interface AutofillDetailsContainer ()
// Find the editable input field that is closest to the top of the dialog and
// matches the |predicateBlock|.
- (NSView*)firstEditableFieldMatchingBlock:(FieldFilterBlock)predicateBlock;
@end
@implementation AutofillDetailsContainer
- (id)initWithDelegate:(autofill::AutofillDialogViewDelegate*)delegate {
if (self = [super init]) {
delegate_ = delegate;
}
return self;
}
- (void)addSection:(autofill::DialogSection)section {
base::scoped_nsobject<AutofillSectionContainer> sectionContainer(
[[AutofillSectionContainer alloc] initWithDelegate:delegate_
forSection:section]);
[sectionContainer setValidationDelegate:self];
[details_ addObject:sectionContainer];
}
- (void)loadView {
details_.reset([[NSMutableArray alloc] init]);
[self addSection:autofill::SECTION_CC];
[self addSection:autofill::SECTION_BILLING];
[self addSection:autofill::SECTION_CC_BILLING];
[self addSection:autofill::SECTION_SHIPPING];
scrollView_.reset([[NSScrollView alloc] initWithFrame:NSZeroRect]);
[scrollView_ setHasVerticalScroller:YES];
[scrollView_ setHasHorizontalScroller:NO];
[scrollView_ setBorderType:NSNoBorder];
[scrollView_ setAutohidesScrollers:YES];
[self setView:scrollView_];
[scrollView_ setDocumentView:[[NSView alloc] initWithFrame:NSZeroRect]];
for (AutofillSectionContainer* container in details_.get())
[[scrollView_ documentView] addSubview:[container view]];
[self performLayout];
}
- (NSSize)preferredSize {
NSSize size = NSZeroSize;
for (AutofillSectionContainer* container in details_.get()) {
NSSize containerSize = [container preferredSize];
size.height += containerSize.height;
size.width = std::max(containerSize.width, size.width);
}
return size;
}
- (void)performLayout {
NSRect rect = NSZeroRect;
for (AutofillSectionContainer* container in
[details_ reverseObjectEnumerator]) {
if (![[container view] isHidden]) {
[container performLayout];
[[container view] setFrameOrigin:NSMakePoint(0, NSMaxY(rect))];
rect = NSUnionRect(rect, [[container view] frame]);
}
}
[[scrollView_ documentView] setFrameSize:[self preferredSize]];
}
- (AutofillSectionContainer*)sectionForId:(autofill::DialogSection)section {
for (AutofillSectionContainer* details in details_.get()) {
if ([details section] == section)
return details;
}
return nil;
}
- (void)modelChanged {
for (AutofillSectionContainer* details in details_.get())
[details modelChanged];
}
- (BOOL)validate {
// Account for a subtle timing issue. -validate is called from the dialog's
// -accept. -accept then hides the dialog. If the data does not validate the
// dialog is then reshown, focusing on the first invalid field. This happens
// without running the message loop, so windowWillClose has not fired when
// the dialog and error bubble is reshown, leading to a missing error bubble.
// Resetting the anchor view here forces the bubble to show.
errorBubbleAnchorView_ = nil;
bool allValid = true;
for (AutofillSectionContainer* details in details_.get()) {
if (![[details view] isHidden])
allValid = [details validateFor:autofill::VALIDATE_FINAL] && allValid;
}
return allValid;
}
- (NSView*)firstInvalidField {
return [self firstEditableFieldMatchingBlock:
^BOOL (NSView<AutofillInputField>* field) {
return [field invalid];
}];
}
- (NSView*)firstVisibleField {
return [self firstEditableFieldMatchingBlock:
^BOOL (NSView<AutofillInputField>* field) {
return YES;
}];
}
- (void)scrollToView:(NSView*)field {
const CGFloat bottomPadding = 5.0; // Padding below the visible field.
NSClipView* clipView = [scrollView_ contentView];
NSRect fieldRect = [field convertRect:[field bounds] toView:clipView];
// If the entire field is already visible, let's not scroll.
NSRect documentRect = [clipView documentVisibleRect];
documentRect = [[clipView documentView] convertRect:documentRect
toView:clipView];
if (NSContainsRect(documentRect, fieldRect))
return;
NSPoint scrollPoint = [clipView constrainScrollPoint:
NSMakePoint(0, NSMinY(fieldRect) - bottomPadding)];
[clipView scrollToPoint:scrollPoint];
[scrollView_ reflectScrolledClipView:clipView];
[self updateErrorBubble];
}
- (void)updateErrorBubble {
if (!delegate_->ShouldShowErrorBubble()) {
[errorBubbleController_ close];
}
}
- (void)errorBubbleWindowWillClose:(NSNotification*)notification {
DCHECK_EQ([notification object], [errorBubbleController_ window]);
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center removeObserver:self
name:NSWindowWillCloseNotification
object:[errorBubbleController_ window]];
errorBubbleController_ = nil;
errorBubbleAnchorView_ = nil;
}
- (void)showErrorBubbleForField:(NSControl<AutofillInputField>*)field {
// If there is already a bubble controller handling this field, reuse.
if (errorBubbleController_ && errorBubbleAnchorView_ == field) {
[errorBubbleController_ setMessage:[field validityMessage]];
return;
}
if (errorBubbleController_)
[errorBubbleController_ close];
DCHECK(!errorBubbleController_);
NSWindow* parentWindow = [field window];
DCHECK(parentWindow);
errorBubbleController_ =
[[AutofillBubbleController alloc]
initWithParentWindow:parentWindow
message:[field validityMessage]];
// Handle bubble self-deleting.
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(errorBubbleWindowWillClose:)
name:NSWindowWillCloseNotification
object:[errorBubbleController_ window]];
// Compute anchor point (in window coords - views might be flipped).
NSRect viewRect = [field convertRect:[field bounds] toView:nil];
// If a bubble at maximum size with a left-aligned edge would exceed the
// window width, align the right edge of bubble and view. In all other
// cases, align the left edge of the bubble and the view.
// Alignment is based on maximum width to avoid the arrow changing positions
// if the validation bubble stays on the same field but gets a message of
// differing length. (E.g. "Field is required"/"Invalid Zip Code. Please
// check and try again" if an empty zip field gets changed to a bad zip).
NSPoint anchorPoint;
if ((NSMinX(viewRect) + [errorBubbleController_ maxWidth]) >
NSWidth([parentWindow frame])) {
anchorPoint = NSMakePoint(NSMaxX(viewRect), NSMinY(viewRect));
[[errorBubbleController_ bubble] setArrowLocation:info_bubble::kTopRight];
[[errorBubbleController_ bubble] setAlignment:
info_bubble::kAlignRightEdgeToAnchorEdge];
} else {
anchorPoint = NSMakePoint(NSMinX(viewRect), NSMinY(viewRect));
[[errorBubbleController_ bubble] setArrowLocation:info_bubble::kTopLeft];
[[errorBubbleController_ bubble] setAlignment:
info_bubble::kAlignLeftEdgeToAnchorEdge];
}
[errorBubbleController_ setAnchorPoint:
[parentWindow convertBaseToScreen:anchorPoint]];
errorBubbleAnchorView_ = field;
[errorBubbleController_ showWindow:self];
}
- (void)hideErrorBubble {
[errorBubbleController_ close];
}
- (void)updateMessageForField:(NSControl<AutofillInputField>*)field {
// Ignore fields that are not first responder. Testing this is a bit
// convoluted, since for NSTextFields with firstResponder status, the
// firstResponder is a subview of the NSTextField, not the field itself.
NSView* firstResponderView =
base::mac::ObjCCast<NSView>([[field window] firstResponder]);
if (![firstResponderView isDescendantOf:field])
return;
if (!delegate_->ShouldShowErrorBubble()) {
DCHECK(!errorBubbleController_);
return;
}
if ([field invalid]) {
[self showErrorBubbleForField:field];
} else {
[errorBubbleController_ close];
}
}
- (NSView*)firstEditableFieldMatchingBlock:(FieldFilterBlock)predicateBlock {
base::scoped_nsobject<NSMutableArray> fields([[NSMutableArray alloc] init]);
for (AutofillSectionContainer* details in details_.get()) {
if (![[details view] isHidden])
[details addInputsToArray:fields];
}
NSPoint selectedFieldOrigin = NSZeroPoint;
NSView* selectedField = nil;
for (NSControl<AutofillInputField>* field in fields.get()) {
if (!base::mac::ObjCCast<NSControl>(field))
continue;
if (![field conformsToProtocol:@protocol(AutofillInputField)])
continue;
if ([field isHiddenOrHasHiddenAncestor])
continue;
if (![field isEnabled])
continue;
if (![field canBecomeKeyView])
continue;
if (!predicateBlock(field))
continue;
NSPoint fieldOrigin = [field convertPoint:[field bounds].origin toView:nil];
if (fieldOrigin.y < selectedFieldOrigin.y)
continue;
if (fieldOrigin.y == selectedFieldOrigin.y &&
fieldOrigin.x > selectedFieldOrigin.x) {
continue;
}
selectedField = field;
selectedFieldOrigin = fieldOrigin;
}
return selectedField;
}
@end
|