Commit 05f549ed authored by Ted Choc's avatar Ted Choc Committed by Commit Bot

Slight cleanup of tail suggest padding for Android.

The padding depends on the text size/paint metrics of the
textview itself, so it makes sense to calculate it based on
the state of the view.

This also fixes RTL and a bug introduced in a previous cleanup
that was applying tail suggest padding to all suggestions when
showing a single tail suggest item.

BUG=898522

Change-Id: I9b4a7be75b153948e6b53b8fcf3fcc804de1273a
Reviewed-on: https://chromium-review.googlesource.com/c/1310715
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604520}
parent 8dd3bf63
......@@ -17,6 +17,7 @@ import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting;
......@@ -120,7 +121,6 @@ public class OmniboxResultsAdapter extends BaseAdapter {
createSuggestionViewDelegate(item.getSuggestion(), position));
model.set(SuggestionViewProperties.USE_DARK_COLORS, mUseDarkColors);
model.set(SuggestionViewProperties.LAYOUT_DIRECTION, mLayoutDirection);
model.set(SuggestionViewProperties.TEXT_LINE_1_ALIGNMENT_CONSTRAINTS, Pair.create(0f, 0f));
OmniboxSuggestion suggestion = item.getSuggestion();
// Suggestions with attached answers are rendered with rich results regardless of which
......@@ -128,7 +128,7 @@ public class OmniboxResultsAdapter extends BaseAdapter {
if (suggestion.hasAnswer()) {
setStateForAnswerSuggestion(model, suggestion.getAnswer(), item.getAnswerImage());
} else {
setStateForTextSuggestion(model, item, textLine1Paint);
setStateForTextSuggestion(model, item);
}
}
......@@ -164,11 +164,10 @@ public class OmniboxResultsAdapter extends BaseAdapter {
* @param showDescriptionIfPresent Whether to show the description text of the suggestion if
* the item contains valid data.
* @param shouldHighlight Whether the query should be highlighted.
* @param textLine1Paint The paint used for the first text line.
* @return The first line of text.
*/
private Spannable getSuggestedQuery(PropertyModel model, OmniboxResultItem suggestionItem,
boolean showDescriptionIfPresent, boolean shouldHighlight, Paint textLine1Paint) {
private Spannable getSuggestedQuery(OmniboxResultItem suggestionItem,
boolean showDescriptionIfPresent, boolean shouldHighlight) {
String userQuery = suggestionItem.getMatchedQuery();
String suggestedQuery = null;
List<MatchClassification> classifications;
......@@ -205,19 +204,6 @@ public class OmniboxResultsAdapter extends BaseAdapter {
classifications.get(i).style));
}
classifications.add(0, new MatchClassification(0, MatchClassificationStyle.NONE));
if (DeviceFormFactor.isNonMultiDisplayContextOnTablet(mContext)) {
float requiredWidth =
textLine1Paint.measureText(fillIntoEdit, 0, fillIntoEdit.length());
float matchContentsWidth =
textLine1Paint.measureText(suggestedQuery, 0, suggestedQuery.length());
model.set(SuggestionViewProperties.TEXT_LINE_1_ALIGNMENT_CONSTRAINTS,
Pair.create(requiredWidth, matchContentsWidth));
// Update the max text widths values in SuggestionList. These will be passed to
// the contents view on layout.
mSuggestionDelegate.onTextWidthsUpdated(requiredWidth, matchContentsWidth);
}
}
}
......@@ -226,8 +212,7 @@ public class OmniboxResultsAdapter extends BaseAdapter {
return str;
}
private void setStateForTextSuggestion(
PropertyModel model, OmniboxResultItem suggestionItem, Paint textLine1Paint) {
private void setStateForTextSuggestion(PropertyModel model, OmniboxResultItem suggestionItem) {
OmniboxSuggestion suggestion = suggestionItem.getSuggestion();
int suggestionType = suggestion.getType();
@SuggestionIcon
......@@ -256,8 +241,7 @@ public class OmniboxResultsAdapter extends BaseAdapter {
} else {
textLine2 = null;
}
textLine1 =
getSuggestedQuery(model, suggestionItem, true, !urlHighlighted, textLine1Paint);
textLine1 = getSuggestedQuery(suggestionItem, true, !urlHighlighted);
} else {
suggestionIcon = SuggestionIcon.MAGNIFIER;
if (suggestionType == OmniboxSuggestionType.VOICE_SUGGEST) {
......@@ -267,7 +251,7 @@ public class OmniboxResultsAdapter extends BaseAdapter {
// Show history icon for suggestions based on user queries.
suggestionIcon = SuggestionIcon.HISTORY;
}
textLine1 = getSuggestedQuery(model, suggestionItem, false, true, textLine1Paint);
textLine1 = getSuggestedQuery(suggestionItem, false, true);
if ((suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_ENTITY)
|| (suggestionType == OmniboxSuggestionType.SEARCH_SUGGEST_PROFILE)) {
textLine2 = SpannableString.valueOf(suggestion.getDescription());
......@@ -474,13 +458,22 @@ public class OmniboxResultsAdapter extends BaseAdapter {
}
@Override
public float getMaxRequiredWidth() {
return mSuggestionDelegate.getMaxRequiredWidth();
}
@Override
public float getMaxMatchContentsWidth() {
return mSuggestionDelegate.getMaxMatchContentsWidth();
public int getAdditionalTextLine1StartPadding(TextView line1, int maxTextWidth) {
if (!DeviceFormFactor.isNonMultiDisplayContextOnTablet(mContext)) return 0;
if (suggestion.getType() != OmniboxSuggestionType.SEARCH_SUGGEST_TAIL) return 0;
String fillIntoEdit = suggestion.getFillIntoEdit();
float fullTextWidth =
line1.getPaint().measureText(fillIntoEdit, 0, fillIntoEdit.length());
String query = line1.getText().toString();
float abbreviatedTextWidth = line1.getPaint().measureText(query, 0, query.length());
mSuggestionDelegate.onTextWidthsUpdated(fullTextWidth, abbreviatedTextWidth);
final float maxRequiredWidth = mSuggestionDelegate.getMaxRequiredWidth();
final float maxMatchContentsWidth = mSuggestionDelegate.getMaxMatchContentsWidth();
return (int) ((maxTextWidth > maxRequiredWidth)
? (fullTextWidth - abbreviatedTextWidth)
: Math.max(maxTextWidth - maxMatchContentsWidth, 0));
}
};
}
......
......@@ -13,7 +13,6 @@ import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
import android.support.annotation.VisibleForTesting;
import android.support.v4.view.ViewCompat;
import android.support.v7.content.res.AppCompatResources;
import android.view.MotionEvent;
import android.view.View;
......@@ -24,7 +23,6 @@ import android.widget.TextView;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.widget.TintedDrawable;
import org.chromium.ui.base.DeviceFormFactor;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
......@@ -90,11 +88,12 @@ public class SuggestionView extends ViewGroup {
*/
void onGestureUp(long timestamp);
/** @return max required width for the suggestion. */
float getMaxRequiredWidth();
/** @return max match contents width for the suggestion. */
float getMaxMatchContentsWidth();
/**
* @param line1 The TextView containing the line 1 text whose padding is being calculated.
* @param maxTextWidth The maximum width the text can occupy.
* @return any additional padding to be applied to the start of the first line of text.
*/
int getAdditionalTextLine1StartPadding(TextView line1, int maxTextWidth);
}
/**
......@@ -337,14 +336,6 @@ public class SuggestionView extends ViewGroup {
mContentsView.invalidate();
}
/**
* Updates the text alignment constraints to be applied when positioning the text.
*/
void updateTextAlignmentConstraintWidths(float requiredWidth, float matchContentWidth) {
mContentsView.mRequiredWidth = requiredWidth;
mContentsView.mMatchContentsWidth = matchContentWidth;
}
@Override
public void setSelected(boolean selected) {
super.setSelected(selected);
......@@ -384,8 +375,6 @@ public class SuggestionView extends ViewGroup {
private final TextView mTextLine2;
private final ImageView mAnswerImage;
private float mRequiredWidth;
private float mMatchContentsWidth;
private boolean mForceIsFocused;
// TODO(crbug.com/635567): Fix this properly.
......@@ -458,21 +447,6 @@ public class SuggestionView extends ViewGroup {
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// Align the text to be pixel perfectly aligned with the text in the url bar.
boolean isRTL = ApiCompatibilityUtils.isLayoutRtl(this);
if (DeviceFormFactor.isNonMultiDisplayContextOnTablet(getContext())) {
int textWidth =
isRTL ? mSuggestionStartOffsetPx : (r - l - mSuggestionStartOffsetPx);
final float maxRequiredWidth = mSuggestionDelegate.getMaxRequiredWidth();
final float maxMatchContentsWidth = mSuggestionDelegate.getMaxMatchContentsWidth();
float paddingStart = (textWidth > maxRequiredWidth)
? (mRequiredWidth - mMatchContentsWidth)
: Math.max(textWidth - maxMatchContentsWidth, 0);
// TODO(skanuj) : Change to ViewCompat.getPaddingEnd(...).
ViewCompat.setPaddingRelative(mTextLine1, (int) paddingStart,
mTextLine1.getPaddingTop(), 0, mTextLine1.getPaddingBottom());
}
int imageWidth = 0;
int imageSpacing = 0;
if (mAnswerImage.getVisibility() == VISIBLE) {
......@@ -526,16 +500,21 @@ public class SuggestionView extends ViewGroup {
final int line2Bottom = line2Top + line2Height;
final int answerImageTop = t + answerVerticalOffset;
final int answerImageBottom = answerImageTop + mAnswerImage.getMeasuredHeight();
final int line1AdditionalStartPadding =
mSuggestionDelegate.getAdditionalTextLine1StartPadding(
mTextLine1, r - l - mSuggestionStartOffsetPx);
if (isRTL) {
if (ApiCompatibilityUtils.isLayoutRtl(this)) {
int rightStartPos = r - l - mSuggestionStartOffsetPx;
mTextLine1.layout(0, line1Top, rightStartPos, line1Bottom);
mTextLine1.layout(
0, line1Top, rightStartPos - line1AdditionalStartPadding, line1Bottom);
mAnswerImage.layout(rightStartPos - imageWidth, answerImageTop, rightStartPos,
answerImageBottom);
mTextLine2.layout(
0, line2Top, rightStartPos - (imageWidth + imageSpacing), line2Bottom);
} else {
mTextLine1.layout(mSuggestionStartOffsetPx, line1Top, r - l, line1Bottom);
mTextLine1.layout(mSuggestionStartOffsetPx + line1AdditionalStartPadding, line1Top,
r - l, line1Bottom);
mAnswerImage.layout(mSuggestionStartOffsetPx, answerImageTop,
mSuggestionStartOffsetPx + imageWidth, answerImageBottom);
mTextLine2.layout(mSuggestionStartOffsetPx + imageWidth + imageSpacing, line2Top,
......
......@@ -122,9 +122,6 @@ class SuggestionViewProperties {
/** The actual text content for the first line of text. */
public static final WritableObjectPropertyKey<SuggestionTextContainer> TEXT_LINE_1_TEXT =
new WritableObjectPropertyKey<>();
/** The alignment constraints for positioning the first line of text. */
public static final WritableObjectPropertyKey<Pair<Float, Float>>
TEXT_LINE_1_ALIGNMENT_CONSTRAINTS = new WritableObjectPropertyKey<>();
/**
* The sizing information for the second line of text.
......@@ -146,9 +143,9 @@ class SuggestionViewProperties {
public static final WritableObjectPropertyKey<SuggestionTextContainer> TEXT_LINE_2_TEXT =
new WritableObjectPropertyKey<>();
public static final PropertyKey[] ALL_KEYS = new PropertyKey[] {DELEGATE, USE_DARK_COLORS,
LAYOUT_DIRECTION, IS_ANSWER, HAS_ANSWER_IMAGE, ANSWER_IMAGE, REFINABLE,
SUGGESTION_ICON_TYPE, TEXT_LINE_1_SIZING, TEXT_LINE_1_TEXT,
TEXT_LINE_1_ALIGNMENT_CONSTRAINTS, TEXT_LINE_2_SIZING, TEXT_LINE_2_MAX_LINES,
TEXT_LINE_2_TEXT_COLOR, TEXT_LINE_2_TEXT_DIRECTION, TEXT_LINE_2_TEXT};
public static final PropertyKey[] ALL_KEYS =
new PropertyKey[] {DELEGATE, USE_DARK_COLORS, LAYOUT_DIRECTION, IS_ANSWER,
HAS_ANSWER_IMAGE, ANSWER_IMAGE, REFINABLE, SUGGESTION_ICON_TYPE,
TEXT_LINE_1_SIZING, TEXT_LINE_1_TEXT, TEXT_LINE_2_SIZING, TEXT_LINE_2_MAX_LINES,
TEXT_LINE_2_TEXT_COLOR, TEXT_LINE_2_TEXT_DIRECTION, TEXT_LINE_2_TEXT};
}
\ No newline at end of file
......@@ -83,10 +83,6 @@ class SuggestionViewViewBinder {
view.getTextLine1().setTextSize(sizing.first, sizing.second);
} else if (SuggestionViewProperties.TEXT_LINE_1_TEXT.equals(propertyKey)) {
view.getTextLine1().setText(model.get(SuggestionViewProperties.TEXT_LINE_1_TEXT).text);
} else if (SuggestionViewProperties.TEXT_LINE_1_ALIGNMENT_CONSTRAINTS.equals(propertyKey)) {
Pair<Float, Float> constraints =
model.get(SuggestionViewProperties.TEXT_LINE_1_ALIGNMENT_CONSTRAINTS);
view.updateTextAlignmentConstraintWidths(constraints.first, constraints.second);
} else if (SuggestionViewProperties.TEXT_LINE_2_SIZING.equals(propertyKey)) {
Pair<Integer, Float> sizing = model.get(SuggestionViewProperties.TEXT_LINE_2_SIZING);
view.getTextLine2().setTextSize(sizing.first, sizing.second);
......
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