File: autofill_main_container.mm

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (424 lines) | stat: -rw-r--r-- 15,287 bytes parent folder | download
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// 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_main_container.h"

#include <algorithm>
#include <cmath>

#include "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/autofill/autofill_dialog_view_delegate.h"
#include "chrome/browser/ui/chrome_style.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_details_container.h"
#include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_notification_container.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_tooltip_controller.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h"
#import "chrome/browser/ui/cocoa/key_equivalent_constants.h"
#include "grit/theme_resources.h"
#include "skia/ext/skia_utils_mac.h"
#import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
#import "ui/base/cocoa/controls/blue_label_button.h"
#import "ui/base/cocoa/controls/hyperlink_text_view.h"
#include "ui/base/cocoa/window_size_constants.h"
#include "ui/gfx/range/range.h"

namespace {

// Padding between buttons and the last suggestion or details view. The mock
// has a total of 30px - but 10px are already provided by details/suggestions.
const CGFloat kButtonVerticalPadding = 20.0;

// Padding around the text for the legal documents.
const CGFloat kLegalDocumentsPadding = 20.0;

// The font color for the legal documents text. Set to match the Views
// implementation.
const SkColor kLegalDocumentsTextColor = SkColorSetRGB(102, 102, 102);

}  // namespace

@interface AutofillMainContainer (Private)
- (void)buildWindowButtons;
- (void)layoutButtons;
- (void)updateButtons;
- (NSSize)preferredLegalDocumentSizeForWidth:(CGFloat)width;
@end


@implementation AutofillMainContainer

@synthesize target = target_;

- (id)initWithDelegate:(autofill::AutofillDialogViewDelegate*)delegate {
  if (self = [super init]) {
    delegate_ = delegate;
  }
  return self;
}

- (void)loadView {
  [self buildWindowButtons];

  base::scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:NSZeroRect]);
  [view setAutoresizesSubviews:YES];
  [view setSubviews:@[buttonContainer_]];
  [self setView:view];

  // Set up Wallet icon.
  buttonStripImage_.reset([[NSImageView alloc] initWithFrame:NSZeroRect]);
  [self updateWalletIcon];
  [[self view] addSubview:buttonStripImage_];

  // Set up "Save in Chrome" checkbox.
  saveInChromeCheckbox_.reset([[NSButton alloc] initWithFrame:NSZeroRect]);
  [saveInChromeCheckbox_ setButtonType:NSSwitchButton];
  [saveInChromeCheckbox_ setTitle:
      base::SysUTF16ToNSString(delegate_->SaveLocallyText())];
  [saveInChromeCheckbox_ sizeToFit];
  [[self view] addSubview:saveInChromeCheckbox_];

  saveInChromeTooltip_.reset(
      [[AutofillTooltipController alloc]
            initWithArrowLocation:info_bubble::kTopCenter]);
  [saveInChromeTooltip_ setImage:
      ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
          IDR_AUTOFILL_TOOLTIP_ICON).ToNSImage()];
  [saveInChromeTooltip_ setMessage:
      base::SysUTF16ToNSString(delegate_->SaveLocallyTooltip())];
  [[self view] addSubview:[saveInChromeTooltip_ view]];
  [self updateSaveInChrome];

  detailsContainer_.reset(
      [[AutofillDetailsContainer alloc] initWithDelegate:delegate_]);
  NSSize frameSize = [[detailsContainer_ view] frame].size;
  [[detailsContainer_ view] setFrameOrigin:
      NSMakePoint(0, NSHeight([buttonContainer_ frame]))];
  frameSize.height += NSHeight([buttonContainer_ frame]);
  [[self view] setFrameSize:frameSize];
  [[self view] addSubview:[detailsContainer_ view]];

  legalDocumentsView_.reset(
      [[HyperlinkTextView alloc] initWithFrame:NSZeroRect]);
  [legalDocumentsView_ setEditable:NO];
  [legalDocumentsView_ setBackgroundColor:
      [NSColor colorWithCalibratedRed:0.96
                                green:0.96
                                 blue:0.96
                                alpha:1.0]];
  [legalDocumentsView_ setDrawsBackground:YES];
  [legalDocumentsView_ setTextContainerInset:
      NSMakeSize(kLegalDocumentsPadding, kLegalDocumentsPadding)];
  [legalDocumentsView_ setHidden:YES];
  [legalDocumentsView_ setDelegate:self];
  legalDocumentsSizeDirty_ = YES;
  [[self view] addSubview:legalDocumentsView_];

  notificationContainer_.reset(
      [[AutofillNotificationContainer alloc] initWithDelegate:delegate_]);
  [[self view] addSubview:[notificationContainer_ view]];
}

// Called when embedded links are clicked.
- (BOOL)textView:(NSTextView*)textView
    clickedOnLink:(id)link
          atIndex:(NSUInteger)charIndex {
  int index = [base::mac::ObjCCastStrict<NSNumber>(link) intValue];
  delegate_->LegalDocumentLinkClicked(
      delegate_->LegalDocumentLinks()[index]);
  return YES;
}

- (NSSize)decorationSizeForWidth:(CGFloat)width {
  NSSize buttonSize = [buttonContainer_ frame].size;
  NSSize buttonStripImageSize = [buttonStripImage_ frame].size;
  NSSize buttonStripSize =
      NSMakeSize(buttonSize.width + chrome_style::kHorizontalPadding +
                     buttonStripImageSize.width,
                 std::max(buttonSize.height + kButtonVerticalPadding,
                          buttonStripImageSize.height) +
                     chrome_style::kClientBottomPadding);

  NSSize size = NSMakeSize(std::max(buttonStripSize.width, width),
                           buttonStripSize.height);
  if (![legalDocumentsView_ isHidden]) {
    NSSize legalDocumentSize =
        [self preferredLegalDocumentSizeForWidth:width];
    size.height += legalDocumentSize.height + autofill::kVerticalSpacing;
  }

  NSSize notificationSize =
      [notificationContainer_ preferredSizeForWidth:width];
  size.height += notificationSize.height;

  return size;
}

- (NSSize)preferredSize {
  NSSize detailsSize = [detailsContainer_ preferredSize];
  NSSize decorationSize = [self decorationSizeForWidth:detailsSize.width];

  NSSize size = NSMakeSize(std::max(decorationSize.width, detailsSize.width),
                           decorationSize.height + detailsSize.height);
  size.height += autofill::kDetailVerticalPadding;

  return size;
}

- (void)performLayout {
  NSRect bounds = [[self view] bounds];

  CGFloat currentY = 0.0;
  if (![legalDocumentsView_ isHidden]) {
    [legalDocumentsView_ setFrameSize:
        [self preferredLegalDocumentSizeForWidth:NSWidth(bounds)]];
    currentY = NSMaxY([legalDocumentsView_ frame]) + autofill::kVerticalSpacing;
  }

  NSRect buttonFrame = [buttonContainer_ frame];
  buttonFrame.origin.y = currentY + chrome_style::kClientBottomPadding;
  [buttonContainer_ setFrameOrigin:buttonFrame.origin];
  currentY = NSMaxY(buttonFrame) + kButtonVerticalPadding;

  NSPoint walletIconOrigin =
      NSMakePoint(chrome_style::kHorizontalPadding, buttonFrame.origin.y);
  [buttonStripImage_ setFrameOrigin:walletIconOrigin];
  currentY = std::max(currentY, NSMaxY([buttonStripImage_ frame]));

  NSRect checkboxFrame = [saveInChromeCheckbox_ frame];
  [saveInChromeCheckbox_ setFrameOrigin:
      NSMakePoint(chrome_style::kHorizontalPadding,
                  NSMidY(buttonFrame) - NSHeight(checkboxFrame) / 2.0)];

  NSRect tooltipFrame = [[saveInChromeTooltip_ view] frame];
  [[saveInChromeTooltip_ view] setFrameOrigin:
      NSMakePoint(NSMaxX([saveInChromeCheckbox_ frame]) + autofill::kButtonGap,
                  NSMidY(buttonFrame) - (NSHeight(tooltipFrame) / 2.0))];

  NSRect notificationFrame = NSZeroRect;
  notificationFrame.size = [notificationContainer_ preferredSizeForWidth:
      NSWidth(bounds)];

  // Buttons/checkbox/legal take up lower part of view, notifications the
  // upper part. Adjust the detailsContainer to take up the remainder.
  CGFloat remainingHeight =
      NSHeight(bounds) - currentY - NSHeight(notificationFrame);
  NSRect containerFrame =
      NSMakeRect(0, currentY, NSWidth(bounds), remainingHeight);
  [[detailsContainer_ view] setFrame:containerFrame];
  [detailsContainer_ performLayout];

  notificationFrame.origin = NSMakePoint(0, NSMaxY(containerFrame));
  [[notificationContainer_ view] setFrame:notificationFrame];
  [notificationContainer_ performLayout];
}

- (void)buildWindowButtons {
  if (buttonContainer_.get())
    return;

  buttonContainer_.reset([[GTMWidthBasedTweaker alloc] initWithFrame:
      ui::kWindowSizeDeterminedLater]);
  [buttonContainer_
      setAutoresizingMask:(NSViewMinXMargin | NSViewMaxYMargin)];

  base::scoped_nsobject<NSButton> button(
      [[ConstrainedWindowButton alloc] initWithFrame:NSZeroRect]);
  [button setKeyEquivalent:kKeyEquivalentEscape];
  [button setTarget:target_];
  [button setAction:@selector(cancel:)];
  [button sizeToFit];
  [buttonContainer_ addSubview:button];

  CGFloat nextX = NSMaxX([button frame]) + autofill::kButtonGap;
  button.reset([[BlueLabelButton alloc] initWithFrame:NSZeroRect]);
  [button setFrameOrigin:NSMakePoint(nextX, 0)];
  [button setKeyEquivalent:kKeyEquivalentReturn];
  [button setTarget:target_];
  [button setAction:@selector(accept:)];
  [buttonContainer_ addSubview:button];
  [self updateButtons];

  NSRect frame = NSMakeRect(
      -NSMaxX([button frame]) - chrome_style::kHorizontalPadding, 0,
      NSMaxX([button frame]), NSHeight([button frame]));
  [buttonContainer_ setFrame:frame];
}

- (void)layoutButtons {
  base::scoped_nsobject<GTMUILocalizerAndLayoutTweaker> layoutTweaker(
      [[GTMUILocalizerAndLayoutTweaker alloc] init]);
  [layoutTweaker tweakUI:buttonContainer_];

  // Now ensure both buttons have the same height. The second button is
  // known to be the larger one.
  CGFloat buttonHeight =
      NSHeight([[[buttonContainer_ subviews] objectAtIndex:1] frame]);

  // Force first button to be the same height.
  NSView* button = [[buttonContainer_ subviews] objectAtIndex:0];
  NSSize buttonSize = [button frame].size;
  buttonSize.height = buttonHeight;
  [button setFrameSize:buttonSize];
}

- (void)updateButtons {
  NSButton* button = base::mac::ObjCCastStrict<NSButton>(
      [[buttonContainer_ subviews] objectAtIndex:0]);
  [button setTitle:base::SysUTF16ToNSString(delegate_->CancelButtonText())];
  button = base::mac::ObjCCastStrict<NSButton>(
    [[buttonContainer_ subviews] objectAtIndex:1]);
  [button setTitle:base::SysUTF16ToNSString(delegate_->ConfirmButtonText())];
  [self layoutButtons];
}

// Compute the preferred size for the legal documents text, given a width.
- (NSSize)preferredLegalDocumentSizeForWidth:(CGFloat)width {
  // Only recompute if necessary (On text or frame width change).
  if (!legalDocumentsSizeDirty_ &&
      std::abs(legalDocumentsSize_.width - width) < 1.0) {
    return legalDocumentsSize_;
  }

  // There's no direct API to compute desired sizes - use layouting instead.
  // Layout in a rect with fixed width and "infinite" height.
  NSRect currentFrame = [legalDocumentsView_ frame];
  [legalDocumentsView_ setFrame:NSMakeRect(0, 0, width, CGFLOAT_MAX)];

  // Now use the layout manager to compute layout.
  NSLayoutManager* layoutManager = [legalDocumentsView_ layoutManager];
  NSTextContainer* textContainer = [legalDocumentsView_ textContainer];
  [layoutManager ensureLayoutForTextContainer:textContainer];
  NSRect newFrame = [layoutManager usedRectForTextContainer:textContainer];

  // And finally, restore old frame.
  [legalDocumentsView_ setFrame:currentFrame];
  newFrame.size.width = width;

  // Account for the padding around the text.
  newFrame.size.height += 2 * kLegalDocumentsPadding;

  legalDocumentsSizeDirty_ = NO;
  legalDocumentsSize_ = newFrame.size;
  return legalDocumentsSize_;
}

- (AutofillSectionContainer*)sectionForId:(autofill::DialogSection)section {
  return [detailsContainer_ sectionForId:section];
}

- (void)modelChanged {
  [self updateSaveInChrome];
  [self updateWalletIcon];
  [self updateButtons];
  [detailsContainer_ modelChanged];
}

- (BOOL)saveDetailsLocally {
  return [saveInChromeCheckbox_ state] == NSOnState;
}

- (void)updateLegalDocuments {
  NSString* text = base::SysUTF16ToNSString(delegate_->LegalDocumentsText());

  if ([text length]) {
    NSFont* font =
        [NSFont labelFontOfSize:[[legalDocumentsView_ font] pointSize]];
    NSColor* color = gfx::SkColorToCalibratedNSColor(kLegalDocumentsTextColor);
    [legalDocumentsView_ setMessage:text withFont:font messageColor:color];

    const std::vector<gfx::Range>& link_ranges =
        delegate_->LegalDocumentLinks();
    for (size_t i = 0; i < link_ranges.size(); ++i) {
      NSRange range = link_ranges[i].ToNSRange();
      [legalDocumentsView_ addLinkRange:range
                               withName:@(i)
                              linkColor:[NSColor blueColor]];
    }
    legalDocumentsSizeDirty_ = YES;
  }
  [legalDocumentsView_ setHidden:[text length] == 0];

  // Always request re-layout on state change.
  id delegate = [[[self view] window] windowController];
  if ([delegate respondsToSelector:@selector(requestRelayout)])
    [delegate performSelector:@selector(requestRelayout)];
}

- (void)updateNotificationArea {
  [notificationContainer_ setNotifications:delegate_->CurrentNotifications()];
  id delegate = [[[self view] window] windowController];
  if ([delegate respondsToSelector:@selector(requestRelayout)])
    [delegate performSelector:@selector(requestRelayout)];
}

- (void)setAnchorView:(NSView*)anchorView {
  [notificationContainer_ setAnchorView:anchorView];
}

- (BOOL)validate {
  return [detailsContainer_ validate];
}

- (void)updateSaveInChrome {
  [saveInChromeCheckbox_ setHidden:!delegate_->ShouldOfferToSaveInChrome()];
  [[saveInChromeTooltip_ view] setHidden:[saveInChromeCheckbox_ isHidden]];
  [saveInChromeCheckbox_ setState:
      (delegate_->ShouldSaveInChrome() ? NSOnState : NSOffState)];
}

- (void)makeFirstInvalidInputFirstResponder {
  NSView* field = [detailsContainer_ firstInvalidField];
  if (!field)
    return;

  [detailsContainer_ scrollToView:field];
  [[[self view] window] makeFirstResponder:field];
}

- (void)updateWalletIcon {
  gfx::Image image = delegate_->ButtonStripImage();
  [buttonStripImage_ setHidden:image.IsEmpty()];
  if (![buttonStripImage_ isHidden]) {
    [buttonStripImage_ setImage:image.ToNSImage()];
    [buttonStripImage_ setFrameSize:[[buttonStripImage_ image] size]];
  }
}

- (void)scrollInitialEditorIntoViewAndMakeFirstResponder {
  // Try to focus on the first invalid field. If there isn't one, focus on the
  // first editable field instead.
  NSView* field = [detailsContainer_ firstInvalidField];
  if (!field)
    field = [detailsContainer_ firstVisibleField];
  if (!field)
    return;

  [detailsContainer_ scrollToView:field];
  [[[self view] window] makeFirstResponder:field];
}

- (void)updateErrorBubble {
  [detailsContainer_ updateErrorBubble];
}

@end


@implementation AutofillMainContainer (Testing)

- (NSButton*)saveInChromeCheckboxForTesting {
  return saveInChromeCheckbox_.get();
}

- (NSImageView*)buttonStripImageForTesting {
  return buttonStripImage_.get();
}

- (NSButton*)saveInChromeTooltipForTesting {
  return base::mac::ObjCCast<NSButton>([saveInChromeTooltip_ view]);
}

@end