Commit ebc5c1a8 authored by Ted Choc's avatar Ted Choc Committed by Commit Bot

Move more SuggestionView properties to MVC.

The suggestion view no longer has direct references to any
of the backend suggestion data.

BUG=898522

Change-Id: Iacb03cff130d659c845e134f593f87b8416c16ae
Reviewed-on: https://chromium-review.googlesource.com/c/1306350Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604471}
parent f9ce9d43
......@@ -521,7 +521,6 @@ public class AutocompleteCoordinator
public void updateSuggestionListLayoutDirection() {
if (mSuggestionList == null) return;
int layoutDirection = ViewCompat.getLayoutDirection(mParent);
mSuggestionList.updateSuggestionsLayoutDirection(layoutDirection);
mSuggestionListAdapter.setLayoutDirection(layoutDirection);
}
......
......@@ -8,7 +8,6 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
......@@ -27,6 +26,7 @@ import org.chromium.chrome.browser.modelutil.PropertyModelChangeProcessor;
import org.chromium.chrome.browser.omnibox.MatchClassificationStyle;
import org.chromium.chrome.browser.omnibox.OmniboxSuggestionType;
import org.chromium.chrome.browser.omnibox.suggestions.OmniboxSuggestion.MatchClassification;
import org.chromium.chrome.browser.omnibox.suggestions.SuggestionView.SuggestionViewDelegate;
import org.chromium.chrome.browser.omnibox.suggestions.SuggestionViewProperties.SuggestionIcon;
import org.chromium.chrome.browser.omnibox.suggestions.SuggestionViewProperties.SuggestionTextContainer;
import org.chromium.chrome.browser.toolbar.ToolbarDataProvider;
......@@ -94,11 +94,9 @@ public class OmniboxResultsAdapter extends BaseAdapter {
// Also, notifyDataSetChanged takes no effect if called within an update, so moving this
// after pushing the state results in cached icons not showing up.
maybeFetchAnswerIcon(item);
updateView(suggestionView, item);
// TODO(tedchoc): Remove the init function and push params to the model.
suggestionView.init(item, mSuggestionDelegate, position);
ViewCompat.setLayoutDirection(suggestionView, mLayoutDirection);
updateView(suggestionView, item, position);
// TODO(tedchoc): Investigate whether this is still needed.
suggestionView.jumpDrawablesToCurrentState();
return suggestionView;
}
......@@ -114,11 +112,14 @@ public class OmniboxResultsAdapter extends BaseAdapter {
return model;
}
private void updateView(SuggestionView view, OmniboxResultItem item) {
private void updateView(SuggestionView view, OmniboxResultItem item, int position) {
PropertyModel model = getModel(view);
Paint textLine1Paint = view.getTextLine1().getPaint();
Paint textLine2Paint = view.getTextLine2().getPaint();
model.set(SuggestionViewProperties.DELEGATE,
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();
......@@ -423,6 +424,7 @@ public class OmniboxResultsAdapter extends BaseAdapter {
*/
public void setLayoutDirection(int layoutDirection) {
mLayoutDirection = layoutDirection;
notifySuggestionsChanged();
}
/**
......@@ -441,6 +443,51 @@ public class OmniboxResultsAdapter extends BaseAdapter {
mUseDarkColors = useDarkColors;
}
private SuggestionViewDelegate createSuggestionViewDelegate(
OmniboxSuggestion suggestion, int position) {
return new SuggestionViewDelegate() {
@Override
public void onSetUrlToSuggestion() {
mSuggestionDelegate.onSetUrlToSuggestion(suggestion);
}
@Override
public void onSelection() {
mSuggestionDelegate.onSelection(suggestion, position);
}
@Override
public void onRefineSuggestion() {
mSuggestionDelegate.onRefineSuggestion(suggestion);
}
@Override
public void onLongPress() {
mSuggestionDelegate.onLongPress(suggestion, position);
}
@Override
public void onGestureUp(long timetamp) {
mSuggestionDelegate.onGestureUp(timetamp);
}
@Override
public void onGestureDown() {
mSuggestionDelegate.onGestureDown();
}
@Override
public float getMaxRequiredWidth() {
return mSuggestionDelegate.getMaxRequiredWidth();
}
@Override
public float getMaxMatchContentsWidth() {
return mSuggestionDelegate.getMaxMatchContentsWidth();
}
};
}
/**
* Handler for actions that happen on suggestion view.
*/
......
......@@ -172,19 +172,6 @@ public class OmniboxSuggestionsList extends ListView {
return new ColorDrawable(color);
}
/**
* Invalidates all of the suggestion views in the list. Only applicable when this
* is visible.
*/
void invalidateSuggestionViews() {
if (!isShown()) return;
for (int i = 0; i < getChildCount(); i++) {
if (getChildAt(i) instanceof SuggestionView) {
getChildAt(i).postInvalidateOnAnimation();
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View contentView =
......@@ -217,19 +204,6 @@ public class OmniboxSuggestionsList extends ListView {
}
}
/**
* Update the layout direction of the suggestions.
*/
void updateSuggestionsLayoutDirection(int layoutDirection) {
if (!isShown()) return;
for (int i = 0; i < getChildCount(); i++) {
View childView = getChildAt(i);
if (!(childView instanceof SuggestionView)) continue;
ViewCompat.setLayoutDirection(childView, layoutDirection);
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
......
......@@ -23,8 +23,6 @@ import android.widget.TextView;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.omnibox.suggestions.OmniboxResultsAdapter.OmniboxResultItem;
import org.chromium.chrome.browser.omnibox.suggestions.OmniboxResultsAdapter.OmniboxSuggestionDelegate;
import org.chromium.chrome.browser.widget.TintedDrawable;
import org.chromium.ui.base.DeviceFormFactor;
......@@ -52,10 +50,8 @@ public class SuggestionView extends ViewGroup {
private final int mSuggestionAnswerHeight;
@SuggestionLayoutType
private int mSuggestionLayoutType;
private SuggestionViewDelegate mSuggestionDelegate;
private OmniboxSuggestion mSuggestion;
private OmniboxSuggestionDelegate mSuggestionDelegate;
private int mPosition;
private int mRefineViewOffsetPx;
private final SuggestionContentsContainer mContentsView;
......@@ -68,6 +64,39 @@ public class SuggestionView extends ViewGroup {
private final int mSuggestionStartOffsetPx;
private final int mSuggestionIconWidthPx;
/**
* Handler for actions that happen on suggestion view.
*/
@VisibleForTesting
public static interface SuggestionViewDelegate {
/** Triggered when the user selects one of the omnibox suggestions to navigate to. */
void onSelection();
/** Triggered when the user selects to refine one of the omnibox suggestions. */
void onRefineSuggestion();
/** Triggered when the user long presses the omnibox suggestion. */
void onLongPress();
/** Triggered when the user navigates to one of the suggestions without clicking on it. */
void onSetUrlToSuggestion();
/** Triggered when the user touches the suggestion view. */
void onGestureDown();
/**
* Triggered when the user touch on the suggestion view finishes.
* @param timestamp the timestamp for the ACTION_UP event.
*/
void onGestureUp(long timestamp);
/** @return max required width for the suggestion. */
float getMaxRequiredWidth();
/** @return max match contents width for the suggestion. */
float getMaxMatchContentsWidth();
}
/**
* Constructs a new omnibox suggestion view.
*
......@@ -217,22 +246,9 @@ public class SuggestionView extends ViewGroup {
return super.dispatchTouchEvent(ev);
}
/**
* Sets the contents and state of the view for the given suggestion.
*
* @param suggestionItem The omnibox suggestion item this view represents.
* @param suggestionDelegate The suggestion delegate.
* @param position Position of the suggestion in the dropdown list.
*/
// TODO(tedchoc): Remove this.
public void init(OmniboxResultItem suggestionItem, OmniboxSuggestionDelegate suggestionDelegate,
int position) {
// Update the position unconditionally.
mPosition = position;
jumpDrawablesToCurrentState();
mSuggestion = suggestionItem.getSuggestion();
mSuggestionDelegate = suggestionDelegate;
/** Sets the delegate for the actions on the suggestion view. */
void setDelegate(SuggestionViewDelegate delegate) {
mSuggestionDelegate = delegate;
}
/** Set the type of layout this view is rendering. */
......@@ -333,7 +349,7 @@ public class SuggestionView extends ViewGroup {
public void setSelected(boolean selected) {
super.setSelected(selected);
if (selected && !isInTouchMode()) {
mSuggestionDelegate.onSetUrlToSuggestion(mSuggestion);
mSuggestionDelegate.onSetUrlToSuggestion();
}
}
......@@ -343,7 +359,7 @@ public class SuggestionView extends ViewGroup {
private class PerformSelectSuggestion implements Runnable {
@Override
public void run() {
mSuggestionDelegate.onSelection(mSuggestion, mPosition);
mSuggestionDelegate.onSelection();
}
}
......@@ -353,7 +369,7 @@ public class SuggestionView extends ViewGroup {
private class PerformRefineSuggestion implements Runnable {
@Override
public void run() {
mSuggestionDelegate.onRefineSuggestion(mSuggestion);
mSuggestionDelegate.onRefineSuggestion();
}
}
......@@ -362,7 +378,6 @@ public class SuggestionView extends ViewGroup {
* icon).
*/
private class SuggestionContentsContainer extends ViewGroup {
private int mTextStart = Integer.MIN_VALUE;
private TintedDrawable mSuggestionIcon;
private final TextView mTextLine1;
......@@ -396,7 +411,7 @@ public class SuggestionView extends ViewGroup {
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mSuggestionDelegate.onLongPress(mSuggestion, mPosition);
mSuggestionDelegate.onLongPress();
return true;
}
});
......@@ -446,7 +461,8 @@ public class SuggestionView extends ViewGroup {
// 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 ? mTextStart : (r - l - mTextStart);
int textWidth =
isRTL ? mSuggestionStartOffsetPx : (r - l - mSuggestionStartOffsetPx);
final float maxRequiredWidth = mSuggestionDelegate.getMaxRequiredWidth();
final float maxMatchContentsWidth = mSuggestionDelegate.getMaxMatchContentsWidth();
float paddingStart = (textWidth > maxRequiredWidth)
......@@ -512,30 +528,28 @@ public class SuggestionView extends ViewGroup {
final int answerImageBottom = answerImageTop + mAnswerImage.getMeasuredHeight();
if (isRTL) {
int rightStartPos = r - l - mTextStart;
int rightStartPos = r - l - mSuggestionStartOffsetPx;
mTextLine1.layout(0, line1Top, rightStartPos, line1Bottom);
mAnswerImage.layout(rightStartPos - imageWidth, answerImageTop, rightStartPos,
answerImageBottom);
mTextLine2.layout(
0, line2Top, rightStartPos - (imageWidth + imageSpacing), line2Bottom);
} else {
mTextLine1.layout(mTextStart, line1Top, r - l, line1Bottom);
mAnswerImage.layout(
mTextStart, answerImageTop, mTextStart + imageWidth, answerImageBottom);
mTextLine2.layout(
mTextStart + imageWidth + imageSpacing, line2Top, r - l, line2Bottom);
mTextLine1.layout(mSuggestionStartOffsetPx, line1Top, r - l, line1Bottom);
mAnswerImage.layout(mSuggestionStartOffsetPx, answerImageTop,
mSuggestionStartOffsetPx + imageWidth, answerImageBottom);
mTextLine2.layout(mSuggestionStartOffsetPx + imageWidth + imageSpacing, line2Top,
r - l, line2Bottom);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mTextStart = mSuggestionStartOffsetPx;
// TODO(tedchoc): Instead of comparing width/height, compare the last text (including
// style spans) measured and if that remains the same along with the
// height/width of this view, then we should be able to skip measure
// properly.
int maxWidth = MeasureSpec.getSize(widthMeasureSpec) - mTextStart;
int maxWidth = MeasureSpec.getSize(widthMeasureSpec) - mSuggestionStartOffsetPx;
mTextLine1.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(mSuggestionHeight, MeasureSpec.AT_MOST));
mTextLine2.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST),
......
......@@ -15,6 +15,7 @@ import org.chromium.chrome.browser.modelutil.PropertyKey;
import org.chromium.chrome.browser.modelutil.PropertyModel.WritableBooleanPropertyKey;
import org.chromium.chrome.browser.modelutil.PropertyModel.WritableIntPropertyKey;
import org.chromium.chrome.browser.modelutil.PropertyModel.WritableObjectPropertyKey;
import org.chromium.chrome.browser.omnibox.suggestions.SuggestionView.SuggestionViewDelegate;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
......@@ -85,9 +86,15 @@ class SuggestionViewProperties {
}
}
/** The delegate to handle actions on the suggestion view. */
public static final WritableObjectPropertyKey<SuggestionViewDelegate> DELEGATE =
new WritableObjectPropertyKey<>();
/** Whether dark colors should be applied to text, icons */
public static final WritableBooleanPropertyKey USE_DARK_COLORS =
new WritableBooleanPropertyKey();
/** The layout direction to be applied to the entire suggestion view. */
public static final WritableIntPropertyKey LAYOUT_DIRECTION = new WritableIntPropertyKey();
/** Whether the suggestion is for an answer. */
public static final WritableBooleanPropertyKey IS_ANSWER = new WritableBooleanPropertyKey();
......@@ -139,9 +146,9 @@ class SuggestionViewProperties {
public static final WritableObjectPropertyKey<SuggestionTextContainer> TEXT_LINE_2_TEXT =
new WritableObjectPropertyKey<>();
public static final PropertyKey[] ALL_KEYS =
new PropertyKey[] {USE_DARK_COLORS, 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_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};
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.omnibox.suggestions;
import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.v4.view.ViewCompat;
import android.text.Spannable;
import android.text.TextUtils;
import android.util.Pair;
......@@ -26,12 +27,17 @@ class SuggestionViewViewBinder {
* Object, Object)
*/
public static void bind(PropertyModel model, SuggestionView view, PropertyKey propertyKey) {
if (SuggestionViewProperties.USE_DARK_COLORS.equals(propertyKey)) {
if (SuggestionViewProperties.DELEGATE.equals(propertyKey)) {
view.setDelegate(model.get(SuggestionViewProperties.DELEGATE));
} else if (SuggestionViewProperties.USE_DARK_COLORS.equals(propertyKey)) {
boolean useDarkColors = model.get(SuggestionViewProperties.USE_DARK_COLORS);
view.updateRefineIconTint(useDarkColors);
view.updateSuggestionIconTint(useDarkColors);
view.getTextLine1().setTextColor(
getStandardFontColor(view.getContext(), useDarkColors));
} else if (SuggestionViewProperties.LAYOUT_DIRECTION.equals(propertyKey)) {
ViewCompat.setLayoutDirection(
view, model.get(SuggestionViewProperties.LAYOUT_DIRECTION));
} else if (SuggestionViewProperties.IS_ANSWER.equals(propertyKey)) {
updateSuggestionLayoutType(view, model);
} else if (SuggestionViewProperties.HAS_ANSWER_IMAGE.equals(propertyKey)) {
......
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