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"
14 // Vertical margin between suggestions and the edge of the suggestion content
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;
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];
34 _suggestions.reset([suggestions copy]);
36 self.showsVerticalScrollIndicator = NO;
37 self.showsHorizontalScrollIndicator = 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
50 CGRect proposedFrame =
51 CGRectMake(currentX, kSuggestionVerticalMargin, 0, labelHeight);
52 base::scoped_nsobject<UIView> label(
53 [[FormSuggestionLabel alloc] initWithSuggestion:suggestion
54 proposedFrame:proposedFrame
56 [self addSubview:label];
58 CGRectGetWidth([label frame]) + kSuggestionHorizontalMargin;
61 enumerateObjectsWithOptions:(base::i18n::IsRTL() ? NSEnumerationReverse
63 usingBlock:setupBlock];
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;
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);
85 self.contentSize = CGSizeMake(contentwidth, CGRectGetHeight(frame));
86 // Sets the visible rectangle so suggestions at the right end are
88 CGRect initRect = {{contentwidth - CGRectGetWidth(frame), 0}, frame.size};
89 [self scrollRectToVisible:initRect animated:NO];
92 self.contentSize = CGSizeMake(contentwidth, CGRectGetHeight(frame));
96 - (NSArray*)suggestions {
97 return _suggestions.get();