Commit 1f9532b4 authored by erikchen@chromium.org's avatar erikchen@chromium.org

mac: Update layout of the autofill element to get contact permissions.

The icon should be left-aligned, to the left of the name. I refactored the
layout code of the autofill popup view to better support both image alignment,
as well as RTL alignment.

This CL depends on https://codereview.chromium.org/286243002/, which has not
yet landed.

See the associated bug for visual confirmation of the correctness of the layout
logic.

BUG=139154

Review URL: https://codereview.chromium.org/296603002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274185 0039d316-1c4b-4281-b951-d872f2087c98
parent b342daee
...@@ -28,12 +28,37 @@ using autofill::AutofillPopupView; ...@@ -28,12 +28,37 @@ using autofill::AutofillPopupView;
// Draws an Autofill suggestion in the given |bounds|, labeled with the given // Draws an Autofill suggestion in the given |bounds|, labeled with the given
// |name| and |subtext| hint. If the suggestion |isSelected|, then it is drawn // |name| and |subtext| hint. If the suggestion |isSelected|, then it is drawn
// with a highlight. |index| determines the font to use, as well as the icon, // with a highlight. |index| determines the font to use, as well as the icon,
// if the row requires it -- such as for credit cards. // if the row requires it -- such as for credit cards. |imageFirst| indicates
// whether the image should be drawn before the name, and with the same
// alignment, or whether it should be drawn afterwards, with the opposite
// alignment.
- (void)drawSuggestionWithName:(NSString*)name - (void)drawSuggestionWithName:(NSString*)name
subtext:(NSString*)subtext subtext:(NSString*)subtext
index:(size_t)index index:(size_t)index
bounds:(NSRect)bounds bounds:(NSRect)bounds
selected:(BOOL)isSelected; selected:(BOOL)isSelected
imageFirst:(BOOL)imageFirst;
// This comment block applies to all three draw* methods that follow.
// If |rightAlign| == YES.
// Draws the widget with right border aligned to |x|.
// Returns the x value of left border of the widget.
// If |rightAlign| == NO.
// Draws the widget with left border aligned to |x|.
// Returns the x value of right border of the widget.
- (CGFloat)drawName:(NSString*)name
atX:(CGFloat)x
index:(size_t)index
rightAlign:(BOOL)rightAlign
bounds:(NSRect)bounds;
- (CGFloat)drawIconAtIndex:(size_t)index
atX:(CGFloat)x
rightAlign:(BOOL)rightAlign
bounds:(NSRect)bounds;
- (CGFloat)drawSubtext:(NSString*)subtext
atX:(CGFloat)x
rightAlign:(BOOL)rightAlign
bounds:(NSRect)bounds;
// Returns the icon for the row with the given |index|, or |nil| if there is // Returns the icon for the row with the given |index|, or |nil| if there is
// none. // none.
...@@ -79,16 +104,20 @@ using autofill::AutofillPopupView; ...@@ -79,16 +104,20 @@ using autofill::AutofillPopupView;
if (controller_->identifiers()[i] == autofill::POPUP_ITEM_ID_SEPARATOR) { if (controller_->identifiers()[i] == autofill::POPUP_ITEM_ID_SEPARATOR) {
[self drawSeparatorWithBounds:rowBounds]; [self drawSeparatorWithBounds:rowBounds];
} else { continue;
NSString* name = SysUTF16ToNSString(controller_->names()[i]);
NSString* subtext = SysUTF16ToNSString(controller_->subtexts()[i]);
BOOL isSelected = static_cast<int>(i) == controller_->selected_line();
[self drawSuggestionWithName:name
subtext:subtext
index:i
bounds:rowBounds
selected:isSelected];
} }
BOOL imageFirst = (controller_->identifiers()[i] ==
autofill::POPUP_ITEM_ID_MAC_ACCESS_CONTACTS);
NSString* name = SysUTF16ToNSString(controller_->names()[i]);
NSString* subtext = SysUTF16ToNSString(controller_->subtexts()[i]);
BOOL isSelected = static_cast<int>(i) == controller_->selected_line();
[self drawSuggestionWithName:name
subtext:subtext
index:i
bounds:rowBounds
selected:isSelected
imageFirst:imageFirst];
} }
} }
...@@ -115,7 +144,8 @@ using autofill::AutofillPopupView; ...@@ -115,7 +144,8 @@ using autofill::AutofillPopupView;
subtext:(NSString*)subtext subtext:(NSString*)subtext
index:(size_t)index index:(size_t)index
bounds:(NSRect)bounds bounds:(NSRect)bounds
selected:(BOOL)isSelected { selected:(BOOL)isSelected
imageFirst:(BOOL)imageFirst {
// If this row is selected, highlight it. // If this row is selected, highlight it.
if (isSelected) { if (isSelected) {
[[self highlightColor] set]; [[self highlightColor] set];
...@@ -124,6 +154,28 @@ using autofill::AutofillPopupView; ...@@ -124,6 +154,28 @@ using autofill::AutofillPopupView;
BOOL isRTL = controller_->IsRTL(); BOOL isRTL = controller_->IsRTL();
// The X values of the left and right borders of the autofill widget.
CGFloat leftX = NSMinX(bounds) + AutofillPopupView::kEndPadding;
CGFloat rightX = NSMaxX(bounds) - AutofillPopupView::kEndPadding;
// Draw left side if isRTL == NO, right side if isRTL == YES.
CGFloat x = isRTL ? rightX : leftX;
if (imageFirst)
x = [self drawIconAtIndex:index atX:x rightAlign:isRTL bounds:bounds];
[self drawName:name atX:x index:index rightAlign:isRTL bounds:bounds];
// Draw right side if isRTL == NO, left side if isRTL == YES.
x = isRTL ? leftX : rightX;
if (!imageFirst)
x = [self drawIconAtIndex:index atX:x rightAlign:!isRTL bounds:bounds];
[self drawSubtext:subtext atX:x rightAlign:!isRTL bounds:bounds];
}
- (CGFloat)drawName:(NSString*)name
atX:(CGFloat)x
index:(size_t)index
rightAlign:(BOOL)rightAlign
bounds:(NSRect)bounds {
NSColor* nameColor = NSColor* nameColor =
controller_->IsWarning(index) ? [self warningColor] : [self nameColor]; controller_->IsWarning(index) ? [self warningColor] : [self nameColor];
NSDictionary* nameAttributes = NSDictionary* nameAttributes =
...@@ -133,26 +185,25 @@ using autofill::AutofillPopupView; ...@@ -133,26 +185,25 @@ using autofill::AutofillPopupView;
NSFontAttributeName, nameColor, NSForegroundColorAttributeName, NSFontAttributeName, nameColor, NSForegroundColorAttributeName,
nil]; nil];
NSSize nameSize = [name sizeWithAttributes:nameAttributes]; NSSize nameSize = [name sizeWithAttributes:nameAttributes];
CGFloat x = bounds.origin.x + x -= rightAlign ? nameSize.width : 0;
(isRTL ?
bounds.size.width - AutofillPopupView::kEndPadding - nameSize.width :
AutofillPopupView::kEndPadding);
CGFloat y = bounds.origin.y + (bounds.size.height - nameSize.height) / 2; CGFloat y = bounds.origin.y + (bounds.size.height - nameSize.height) / 2;
[name drawAtPoint:NSMakePoint(x, y) withAttributes:nameAttributes]; [name drawAtPoint:NSMakePoint(x, y) withAttributes:nameAttributes];
// The x-coordinate will be updated as each element is drawn. x += rightAlign ? 0 : nameSize.width;
x = bounds.origin.x + return x;
(isRTL ? }
AutofillPopupView::kEndPadding :
bounds.size.width - AutofillPopupView::kEndPadding);
// Draw the Autofill icon, if one exists. - (CGFloat)drawIconAtIndex:(size_t)index
atX:(CGFloat)x
rightAlign:(BOOL)rightAlign
bounds:(NSRect)bounds {
NSImage* icon = [self iconAtIndex:index]; NSImage* icon = [self iconAtIndex:index];
if (icon) { if (!icon)
NSSize iconSize = [icon size]; return x;
x += isRTL ? 0 : -iconSize.width; NSSize iconSize = [icon size];
y = bounds.origin.y + (bounds.size.height - iconSize.height) / 2; x -= rightAlign ? iconSize.width : 0;
CGFloat y = bounds.origin.y + (bounds.size.height - iconSize.height) / 2;
[icon drawInRect:NSMakeRect(x, y, iconSize.width, iconSize.height) [icon drawInRect:NSMakeRect(x, y, iconSize.width, iconSize.height)
fromRect:NSZeroRect fromRect:NSZeroRect
operation:NSCompositeSourceOver operation:NSCompositeSourceOver
...@@ -160,12 +211,15 @@ using autofill::AutofillPopupView; ...@@ -160,12 +211,15 @@ using autofill::AutofillPopupView;
respectFlipped:YES respectFlipped:YES
hints:nil]; hints:nil];
x += isRTL ? x += rightAlign ? -AutofillPopupView::kIconPadding
iconSize.width + AutofillPopupView::kIconPadding : : iconSize.width + AutofillPopupView::kIconPadding;
-AutofillPopupView::kIconPadding; return x;
} }
// Draw the subtext. - (CGFloat)drawSubtext:(NSString*)subtext
atX:(CGFloat)x
rightAlign:(BOOL)rightAlign
bounds:(NSRect)bounds {
NSDictionary* subtextAttributes = NSDictionary* subtextAttributes =
[NSDictionary dictionaryWithObjectsAndKeys: [NSDictionary dictionaryWithObjectsAndKeys:
controller_->subtext_font_list().GetPrimaryFont().GetNativeFont(), controller_->subtext_font_list().GetPrimaryFont().GetNativeFont(),
...@@ -174,10 +228,12 @@ using autofill::AutofillPopupView; ...@@ -174,10 +228,12 @@ using autofill::AutofillPopupView;
NSForegroundColorAttributeName, NSForegroundColorAttributeName,
nil]; nil];
NSSize subtextSize = [subtext sizeWithAttributes:subtextAttributes]; NSSize subtextSize = [subtext sizeWithAttributes:subtextAttributes];
x += isRTL ? 0 : -subtextSize.width; x -= rightAlign ? subtextSize.width : 0;
y = bounds.origin.y + (bounds.size.height - subtextSize.height) / 2; CGFloat y = bounds.origin.y + (bounds.size.height - subtextSize.height) / 2;
[subtext drawAtPoint:NSMakePoint(x, y) withAttributes:subtextAttributes]; [subtext drawAtPoint:NSMakePoint(x, y) withAttributes:subtextAttributes];
x += rightAlign ? 0 : subtextSize.width;
return x;
} }
- (NSImage*)iconAtIndex:(size_t)index { - (NSImage*)iconAtIndex:(size_t)index {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment