Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Updates to LayoutRect API
Browse files Browse the repository at this point in the history
Updates to the LayoutRect API as suggested by kkhorimoto@ in comments to
https://codereview.chromium.org/1304793002/

- Adds LayoutRectMake constructor.

- Adds LayoutRectZero constant.

- Renames fields: 'contextWidth' -> 'boundingWidth',
                  'yOrigin' -> 'originY'

- Renames function LayoutRectForRectInRectUsingDirection() ->
		   LayoutRectForRectInBoundingRectUsingDirection()

- Renames function LayoutRectForRectInRect() ->
		   LayoutRectForRectInBoundingRect()

- Renames function LayoutRectGetTrailing() ->
		   LayoutRectGetTrailingEdge()

- Corrects parameter name errors in comments.

BUG=

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

Cr-Commit-Position: refs/heads/master@{#345587}
  • Loading branch information
marq authored and Commit bot committed Aug 26, 2015
1 parent b8f5eb4 commit c96804c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
38 changes: 27 additions & 11 deletions ios/chrome/browser/ui/ui_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,28 @@ base::i18n::TextDirection LayoutDirection();
// may not be flipped if positioned in RTL or LTR contexts. |leading| is the
// distance from the leading edge that the final rect's edge should be; in LTR
// this will be the x-origin, in RTL it will be used to compute the x-origin.
// |contextWidth| is the width of whatever element the rect will be used to
// frame a subview of. |yOrigin| will be origin.y of the rect, and |size| will
// |boundingWidth| is the width of whatever element the rect will be used to
// frame a subview of. |originY| will be origin.y of the rect, and |size| will
// be the size of the rect.
struct LayoutRect {
CGFloat leading;
CGFloat contextWidth;
CGFloat yOrigin;
CGFloat boundingWidth;
CGFloat originY;
CGSize size;
};

// The null LayoutRect, with leading, boundingWidth and originY of 0.0, and
// a size of CGSizeZero.
extern const LayoutRect LayoutRectZero;

// Returns a new LayoutRect; |height| and |width| are used to construct the
// |size| field.
LayoutRect LayoutRectMake(CGFloat leading,
CGFloat boundingWidth,
CGFloat originY,
CGFloat width,
CGFloat height);

// Given |layout|, returns the rect for that layout in text direction
// |direction|.
CGRect LayoutRectGetRectUsingDirection(LayoutRect layout,
Expand All @@ -60,19 +72,23 @@ CGPoint LayoutRectGetPositionForAnchorUsingDirection(
LayoutRect layout,
CGPoint anchor,
base::i18n::TextDirection direction);

// As above, using |direction| == RIGHT_TO_LEFT if UseRTLLayout(), LEFT_TO_RIGHT
// otherwise.
CGPoint LayoutRectGetPositionForAnchor(LayoutRect layout, CGPoint anchor);

// Given |layout|, a rect, and |contextRect|, a rect whose bounds are the
// context in which |layout|'s frame is interpreted, return the layout that
// defines |layout|.
LayoutRect LayoutRectForRectInRectUsingDirection(
// Given |rect|, a rect, and |boundingRect|, a rect whose bounds are the
// context in which |rect|'s frame is interpreted, return the layout that
// defines |rect|, assuming |direction| is the direction |rect| was positioned
// under.
LayoutRect LayoutRectForRectInBoundingRectUsingDirection(
CGRect rect,
CGRect contextRect,
CGRect boundingRect,
base::i18n::TextDirection direction);

// As above, using |direction| == RIGHT_TO_LEFT if UseRTLLayout(), LEFT_TO_RIGHT
// otherwise.
LayoutRect LayoutRectForRectInRect(CGRect rect, CGRect contextRect);
LayoutRect LayoutRectForRectInBoundingRect(CGRect rect, CGRect boundingRect);

// Given a layout |layout|, return the layout that defines the leading area up
// to |layout|.
Expand All @@ -83,7 +99,7 @@ LayoutRect LayoutRectGetLeadingLayout(LayoutRect layout);
LayoutRect LayoutRectGetTrailingLayout(LayoutRect layout);

// Return the trailing extent of |layout| (its leading plus its width).
CGFloat LayoutRectGetTrailing(LayoutRect layout);
CGFloat LayoutRectGetTrailingEdge(LayoutRect layout);

// Is the screen of the device a high resolution screen, i.e. Retina Display.
bool IsHighResScreen();
Expand Down
36 changes: 24 additions & 12 deletions ios/chrome/browser/ui/ui_util.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,29 @@ bool UseRTLLayout() {
return UseRTLLayout() ? base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
}

const LayoutRect LayoutRectZero = {0.0, 0.0, 0.0, CGSizeMake(0.0, 0.0)};

LayoutRect LayoutRectMake(CGFloat leading,
CGFloat boundingWidth,
CGFloat originY,
CGFloat width,
CGFloat height) {
LayoutRect layout = {leading, boundingWidth, originY,
CGSizeMake(width, height)};
return layout;
}

CGRect LayoutRectGetRectUsingDirection(LayoutRect layout,
base::i18n::TextDirection direction) {
CGRect rect;
if (direction == base::i18n::RIGHT_TO_LEFT) {
CGFloat trailing =
layout.contextWidth - (layout.leading + layout.size.width);
rect = CGRectMake(trailing, layout.yOrigin, layout.size.width,
layout.boundingWidth - (layout.leading + layout.size.width);
rect = CGRectMake(trailing, layout.originY, layout.size.width,
layout.size.height);
} else {
DCHECK_EQ(direction, base::i18n::LEFT_TO_RIGHT);
rect = CGRectMake(layout.leading, layout.yOrigin, layout.size.width,
rect = CGRectMake(layout.leading, layout.originY, layout.size.width,
layout.size.height);
}
return rect;
Expand Down Expand Up @@ -77,8 +89,8 @@ LayoutRect LayoutRectForRectInRectUsingDirection(
} else {
layout.leading = CGRectGetMinX(rect);
}
layout.contextWidth = contextRect.size.width;
layout.yOrigin = rect.origin.y;
layout.boundingWidth = contextRect.size.width;
layout.originY = rect.origin.y;
layout.size = rect.size;
return layout;
}
Expand All @@ -91,24 +103,24 @@ LayoutRect LayoutRectForRectInRect(CGRect rect, CGRect contextRect) {
LayoutRect LayoutRectGetLeadingLayout(LayoutRect layout) {
LayoutRect leadingLayout;
leadingLayout.leading = 0;
leadingLayout.contextWidth = layout.contextWidth;
leadingLayout.yOrigin = layout.yOrigin;
leadingLayout.boundingWidth = layout.boundingWidth;
leadingLayout.originY = leadingLayout.originY;
leadingLayout.size = CGSizeMake(layout.leading, layout.size.height);
return leadingLayout;
}

LayoutRect LayoutRectGetTrailingLayout(LayoutRect layout) {
LayoutRect leadingLayout;
CGFloat trailing = LayoutRectGetTrailing(layout);
CGFloat trailing = LayoutRectGetTrailingEdge(layout);
leadingLayout.leading = trailing;
leadingLayout.contextWidth = layout.contextWidth;
leadingLayout.yOrigin = layout.yOrigin;
leadingLayout.boundingWidth = layout.boundingWidth;
leadingLayout.originY = leadingLayout.originY;
leadingLayout.size =
CGSizeMake((layout.contextWidth - trailing), layout.size.height);
CGSizeMake((layout.boundingWidth - trailing), layout.size.height);
return leadingLayout;
}

CGFloat LayoutRectGetTrailing(LayoutRect layout) {
CGFloat LayoutRectGetTrailingEdge(LayoutRect layout) {
return layout.leading + layout.size.width;
}

Expand Down

0 comments on commit c96804c

Please sign in to comment.