Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / autofill / form_suggestion_view.mm
blobd0cbffd9464c5fb8d890b04e056135ff6f2abbc7
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 "ios/chrome/browser/autofill/form_suggestion_view.h"
7 #include "base/i18n/rtl.h"
8 #include "base/mac/scoped_nsobject.h"
9 #import "ios/chrome/browser/autofill/form_suggestion_label.h"
10 #import "ios/chrome/browser/autofill/form_suggestion_view_client.h"
12 namespace {
14 // Vertical margin between suggestions and the edge of the suggestion content
15 // frame.
16 const CGFloat kSuggestionVerticalMargin = 6;
18 // Horizontal margin around suggestions (i.e. between suggestions, and between
19 // the end suggestions and the suggestion content frame).
20 const CGFloat kSuggestionHorizontalMargin = 6;
22 }  // namespace
24 @implementation FormSuggestionView {
25   // The FormSuggestions that are displayed by this view.
26   base::scoped_nsobject<NSArray> _suggestions;
29 - (instancetype)initWithFrame:(CGRect)frame
30                        client:(id<FormSuggestionViewClient>)client
31                   suggestions:(NSArray*)suggestions {
32   self = [super initWithFrame:frame];
33   if (self) {
34     _suggestions.reset([suggestions copy]);
36     self.showsVerticalScrollIndicator = NO;
37     self.showsHorizontalScrollIndicator = NO;
38     self.bounces = NO;
39     self.canCancelContentTouches = YES;
41     // Total height occupied by the label content, padding, border and margin.
42     const CGFloat labelHeight =
43         CGRectGetHeight(frame) - kSuggestionVerticalMargin * 2;
45     __block CGFloat currentX = kSuggestionHorizontalMargin;
46     void (^setupBlock)(FormSuggestion* suggestion, NSUInteger idx, BOOL* stop) =
47         ^(FormSuggestion* suggestion, NSUInteger idx, BOOL* stop) {
48           // FormSuggestionLabel will adjust the width, so here 0 is used for
49           // the width.
50           CGRect proposedFrame =
51               CGRectMake(currentX, kSuggestionVerticalMargin, 0, labelHeight);
52           base::scoped_nsobject<UIView> label(
53               [[FormSuggestionLabel alloc] initWithSuggestion:suggestion
54                                                 proposedFrame:proposedFrame
55                                                        client:client]);
56           [self addSubview:label];
57           currentX +=
58               CGRectGetWidth([label frame]) + kSuggestionHorizontalMargin;
59         };
60     [_suggestions
61         enumerateObjectsWithOptions:(base::i18n::IsRTL() ? NSEnumerationReverse
62                                                          : 0)
63                          usingBlock:setupBlock];
64   }
65   return self;
68 - (void)setFrame:(CGRect)frame {
69   [super setFrame:frame];
71   CGFloat contentwidth = kSuggestionHorizontalMargin * 2;
72   for (UIView* label in self.subviews) {
73     contentwidth += CGRectGetWidth([label frame]) + kSuggestionHorizontalMargin;
74   }
76   if (base::i18n::IsRTL()) {
77     if (contentwidth < CGRectGetWidth(frame)) {
78       self.contentSize = frame.size;
79       // Offsets labels for right alignment.
80       CGFloat offset = CGRectGetWidth(frame) - contentwidth;
81       for (UIView* label in self.subviews) {
82         label.frame = CGRectOffset(label.frame, offset, 0);
83       }
84     } else {
85       self.contentSize = CGSizeMake(contentwidth, CGRectGetHeight(frame));
86       // Sets the visible rectangle so suggestions at the right end are
87       // initially visible.
88       CGRect initRect = {{contentwidth - CGRectGetWidth(frame), 0}, frame.size};
89       [self scrollRectToVisible:initRect animated:NO];
90     }
91   } else {
92     self.contentSize = CGSizeMake(contentwidth, CGRectGetHeight(frame));
93   }
96 - (NSArray*)suggestions {
97   return _suggestions.get();
100 @end