summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/autofill/autofill_dialog_window_controller.mm
blob: 0f7bf68b49a19f6349b873513868c216524a74e2 (plain)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Copyright 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_dialog_window_controller.h"

#include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/autofill/autofill_dialog_view_delegate.h"
#include "chrome/browser/ui/cocoa/autofill/autofill_dialog_cocoa.h"
#include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_header.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_input_field.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_loading_shield_controller.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_main_container.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_overlay_controller.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_section_container.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_sign_in_container.h"
#import "chrome/browser/ui/cocoa/autofill/autofill_textfield.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
#include "content/public/browser/web_contents.h"
#import "ui/base/cocoa/flipped_view.h"
#include "ui/base/cocoa/window_size_constants.h"

// The minimum useful height of the contents area of the dialog.
const CGFloat kMinimumContentsHeight = 101;

#pragma mark AutofillDialogWindow

// Window class for the AutofillDialog. Its main purpose is the proper handling
// of layout requests - i.e. ensuring that layout is fully done before any
// updates of the display happen.
@interface AutofillDialogWindow : ConstrainedWindowCustomWindow {
 @private
  BOOL needsLayout_;  // Indicates that the subviews need to be laid out.
}

// Request a new layout for all subviews. Layout occurs right before -display
// or -displayIfNeeded are invoked.
- (void)requestRelayout;

// Layout the window's subviews. Delegates to the controller.
- (void)performLayout;

@end


@implementation AutofillDialogWindow

- (void)requestRelayout {
  needsLayout_ = YES;

  // Ensure displayIfNeeded: is sent on the next pass through the event loop.
  [self setViewsNeedDisplay:YES];
}

- (void)performLayout {
  if (needsLayout_) {
    needsLayout_ = NO;
    AutofillDialogWindowController* controller =
        base::mac::ObjCCastStrict<AutofillDialogWindowController>(
            [self windowController]);
    [controller performLayout];
  }
}

- (void)display {
  [self performLayout];
  [super display];
}

- (void)displayIfNeeded {
  [self performLayout];
  [super displayIfNeeded];
}

@end

#pragma mark Field Editor

@interface AutofillDialogFieldEditor : NSTextView
@end


@implementation AutofillDialogFieldEditor

- (void)mouseDown:(NSEvent*)event {
  // Delegate _must_ be notified before mouseDown is complete, since it needs
  // to distinguish between mouseDown for already focused fields, and fields
  // that will receive focus as part of the mouseDown.
  AutofillTextField* textfield =
      base::mac::ObjCCastStrict<AutofillTextField>([self delegate]);
  [textfield onEditorMouseDown:self];
  [super mouseDown:event];
}

// Intercept key down messages and forward them to the text fields delegate.
// This needs to happen in the field editor, since it handles all keyDown
// messages for NSTextField.
- (void)keyDown:(NSEvent*)event {
  AutofillTextField* textfield =
      base::mac::ObjCCastStrict<AutofillTextField>([self delegate]);
  if ([[textfield inputDelegate] keyEvent:event
                                 forInput:textfield] != kKeyEventHandled) {
    [super keyDown:event];
  }
}

@end


#pragma mark Window Controller

@interface AutofillDialogWindowController ()

// Compute maximum allowed height for the dialog.
- (CGFloat)maxHeight;

// Update size constraints on sign-in container.
- (void)updateSignInSizeConstraints;

// Notification that the WebContent's view frame has changed.
- (void)onContentViewFrameDidChange:(NSNotification*)notification;

// Update whether or not the main container is hidden.
- (void)updateMainContainerVisibility;

- (AutofillDialogWindow*)autofillWindow;

@end


@implementation AutofillDialogWindowController (NSWindowDelegate)

- (id)windowWillReturnFieldEditor:(NSWindow*)window toObject:(id)client {
  AutofillTextField* textfield = base::mac::ObjCCast<AutofillTextField>(client);
  if (!textfield)
    return nil;

  if (!fieldEditor_) {
    fieldEditor_.reset([[AutofillDialogFieldEditor alloc] init]);
    [fieldEditor_ setFieldEditor:YES];
  }
  return fieldEditor_.get();
}

@end


@implementation AutofillDialogWindowController

- (id)initWithWebContents:(content::WebContents*)webContents
                   dialog:(autofill::AutofillDialogCocoa*)dialog {
  DCHECK(webContents);

  base::scoped_nsobject<ConstrainedWindowCustomWindow> window(
      [[AutofillDialogWindow alloc]
          initWithContentRect:ui::kWindowSizeDeterminedLater]);

  if ((self = [super initWithWindow:window])) {
    [window setDelegate:self];
    webContents_ = webContents;
    dialog_ = dialog;

    header_.reset([[AutofillHeader alloc] initWithDelegate:dialog->delegate()]);

    mainContainer_.reset([[AutofillMainContainer alloc]
                             initWithDelegate:dialog->delegate()]);
    [mainContainer_ setTarget:self];

    signInContainer_.reset(
        [[AutofillSignInContainer alloc] initWithDialog:dialog]);
    [[signInContainer_ view] setHidden:YES];

    loadingShieldController_.reset(
        [[AutofillLoadingShieldController alloc] initWithDelegate:
            dialog->delegate()]);
    [[loadingShieldController_ view] setHidden:YES];

    overlayController_.reset(
        [[AutofillOverlayController alloc] initWithDelegate:
            dialog->delegate()]);
    [[overlayController_ view] setHidden:YES];

    // This needs a flipped content view because otherwise the size
    // animation looks odd. However, replacing the contentView for constrained
    // windows does not work - it does custom rendering.
    base::scoped_nsobject<NSView> flippedContentView(
        [[FlippedView alloc] initWithFrame:
            [[[self window] contentView] frame]]);
    [flippedContentView setSubviews:
        @[[header_ view],
          [mainContainer_ view],
          [signInContainer_ view],
          [loadingShieldController_ view],
          [overlayController_ view]]];
    [flippedContentView setAutoresizingMask:
        (NSViewWidthSizable | NSViewHeightSizable)];
    [[[self window] contentView] addSubview:flippedContentView];
    [mainContainer_ setAnchorView:[header_ anchorView]];
  }
  return self;
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  [super dealloc];
}

- (CGFloat)maxHeight {
  NSRect dialogFrameRect = [[self window] frame];
  NSRect browserFrameRect = [webContents_->GetTopLevelNativeWindow() frame];
  dialogFrameRect.size.height =
      NSMaxY(dialogFrameRect) - NSMinY(browserFrameRect);
  dialogFrameRect = [[self window] contentRectForFrameRect:dialogFrameRect];
  return NSHeight(dialogFrameRect);
}

- (void)updateSignInSizeConstraints {
  // For the minimum height, account for the size of the footer. Even though the
  // footer will not be visible when the sign-in view is showing, this prevents
  // the dialog's size from bouncing around.
  CGFloat width = NSWidth([[[self window] contentView] frame]);
  CGFloat minHeight =
      kMinimumContentsHeight +
      [mainContainer_ decorationSizeForWidth:width].height;

  // For the maximum size, factor in the size of the header.
  CGFloat headerHeight = [[header_ view] frame].size.height;
  CGFloat maxHeight = std::max([self maxHeight] - headerHeight, minHeight);

  [signInContainer_ constrainSizeToMinimum:NSMakeSize(width, minHeight)
                                   maximum:NSMakeSize(width, maxHeight)];
}

- (void)onContentViewFrameDidChange:(NSNotification*)notification {
  [self updateSignInSizeConstraints];
  if ([[signInContainer_ view] isHidden])
    [self requestRelayout];
}

- (void)updateMainContainerVisibility {
  BOOL visible =
      [[loadingShieldController_ view] isHidden] &&
      [[overlayController_ view] isHidden] &&
      [[signInContainer_ view] isHidden];
  BOOL wasVisible = ![[mainContainer_ view] isHidden];
  [[mainContainer_ view] setHidden:!visible];

  // Postpone [mainContainer_ didBecomeVisible] until layout is complete.
  if (visible && !wasVisible) {
    mainContainerBecameVisible_ = YES;
    [self requestRelayout];
  }
}

- (AutofillDialogWindow*)autofillWindow {
  return base::mac::ObjCCastStrict<AutofillDialogWindow>([self window]);
}

- (void)requestRelayout {
  [[self autofillWindow] requestRelayout];
}

- (NSSize)preferredSize {
  NSSize size;

  if (![[overlayController_ view] isHidden]) {
    // Overlay never changes window width.
    size.width = NSWidth([[[self window] contentView] frame]);
    size.height = [overlayController_ heightForWidth:size.width];
  } else {
    // Overall size is determined by either main container or sign in view.
    if ([[signInContainer_ view] isHidden])
      size = [mainContainer_ preferredSize];
    else
      size = [signInContainer_ preferredSize];

    // Always make room for the header.
    CGFloat headerHeight = [header_ preferredSize].height;
    size.height += headerHeight;

    // For the minimum height, account for both the header and the footer. Even
    // though the footer will not be visible when the sign-in view is showing,
    // this prevents the dialog's size from bouncing around.
    CGFloat minHeight = kMinimumContentsHeight;
    minHeight += [mainContainer_ decorationSizeForWidth:size.width].height;
    minHeight += headerHeight;

    // Show as much of the main view as is possible without going past the
    // bottom of the browser window, unless this would cause the dialog to be
    // less tall than the minimum height.
    size.height = std::min(size.height, [self maxHeight]);
    size.height = std::max(size.height, minHeight);
  }

  return size;
}

- (void)performLayout {
  NSRect contentRect = NSZeroRect;
  contentRect.size = [self preferredSize];

  CGFloat headerHeight = [header_ preferredSize].height;
  NSRect headerRect, mainRect;
  NSDivideRect(contentRect, &headerRect, &mainRect, headerHeight, NSMinYEdge);

  [[header_ view] setFrame:headerRect];
  [header_ performLayout];

  if ([[signInContainer_ view] isHidden]) {
    [[mainContainer_ view] setFrame:mainRect];
    [mainContainer_ performLayout];
  } else {
    [[signInContainer_ view] setFrame:mainRect];
  }

  [[loadingShieldController_ view] setFrame:contentRect];
  [loadingShieldController_ performLayout];

  [[overlayController_ view] setFrame:contentRect];
  [overlayController_ performLayout];

  NSRect frameRect = [[self window] frameRectForContentRect:contentRect];
  [[self window] setFrame:frameRect display:YES];

  [[self window] recalculateKeyViewLoop];

  if (mainContainerBecameVisible_) {
    [mainContainer_ scrollInitialEditorIntoViewAndMakeFirstResponder];
    mainContainerBecameVisible_ = NO;
  }
}

- (IBAction)accept:(id)sender {
  if ([mainContainer_ validate])
    dialog_->delegate()->OnAccept();
  else
    [mainContainer_ makeFirstInvalidInputFirstResponder];
}

- (IBAction)cancel:(id)sender {
  dialog_->delegate()->OnCancel();
  dialog_->PerformClose();
}

- (void)show {
  // Resizing the browser causes the ConstrainedWindow to move.
  // Observe that to allow resizes based on browser size.
  // NOTE: This MUST come last after all initial setup is done, because there
  // is an immediate notification post registration.
  DCHECK([self window]);
  [[NSNotificationCenter defaultCenter]
      addObserver:self
         selector:@selector(onContentViewFrameDidChange:)
             name:NSWindowDidMoveNotification
           object:[self window]];

  [self updateAccountChooser];
  [self updateNotificationArea];
  [self requestRelayout];
}

- (void)hide {
  dialog_->delegate()->OnCancel();
  dialog_->PerformClose();
}

- (void)updateNotificationArea {
  [mainContainer_ updateNotificationArea];
}

- (void)updateAccountChooser {
  [header_ update];
  [mainContainer_ updateLegalDocuments];
  [loadingShieldController_ update];
  [self updateMainContainerVisibility];
}

- (void)updateButtonStrip {
  // For the duration of the overlay, hide the main contents and the header.
  // This prevents the currently focused text field "shining through". No need
  // to remember previous state, because the overlay view is always the last
  // state of the dialog.
  [overlayController_ updateState];
  [[header_ view] setHidden:![[overlayController_ view] isHidden]];
  [self updateMainContainerVisibility];
}

- (void)updateSection:(autofill::DialogSection)section {
  [[mainContainer_ sectionForId:section] update];
  [mainContainer_ updateSaveInChrome];
}

- (void)fillSection:(autofill::DialogSection)section
            forType:(autofill::ServerFieldType)type {
  [[mainContainer_ sectionForId:section] fillForType:type];
  [mainContainer_ updateSaveInChrome];
}

- (void)updateForErrors {
  [mainContainer_ validate];
}

- (content::NavigationController*)showSignIn {
  [self updateSignInSizeConstraints];
  // Ensure |signInContainer_| is set to the same size as |mainContainer_|, to
  // force its minimum size so that there will not be a resize until the
  // contents are loaded.
  [[signInContainer_ view] setFrameSize:[[mainContainer_ view] frame].size];
  [signInContainer_ loadSignInPage];

  [[signInContainer_ view] setHidden:NO];
  [self updateMainContainerVisibility];
  [self requestRelayout];

  return [signInContainer_ navigationController];
}

- (void)getInputs:(autofill::FieldValueMap*)output
       forSection:(autofill::DialogSection)section {
  [[mainContainer_ sectionForId:section] getInputs:output];
}

- (NSString*)getCvc {
  autofill::DialogSection section = autofill::SECTION_CC;
  NSString* value = [[mainContainer_ sectionForId:section] suggestionText];
  if (!value) {
    section = autofill::SECTION_CC_BILLING;
    value = [[mainContainer_ sectionForId:section] suggestionText];
  }
  return value;
}

- (BOOL)saveDetailsLocally {
  return [mainContainer_ saveDetailsLocally];
}

- (void)hideSignIn {
  [[signInContainer_ view] setHidden:YES];
  [self updateMainContainerVisibility];
  [self requestRelayout];
}

- (void)modelChanged {
  [mainContainer_ modelChanged];
}

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

- (void)onSignInResize:(NSSize)size {
  [signInContainer_ setPreferredSize:size];
  [self requestRelayout];
}

- (void)validateSection:(autofill::DialogSection)section {
  [[mainContainer_ sectionForId:section] validateFor:autofill::VALIDATE_EDIT];
}

@end