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

Fix omnibox suggestion sizing pre-M.

BUG=900150

Change-Id: Ia14a0b1c0f165da71e752922f1e1f776a294605c
Reviewed-on: https://chromium-review.googlesource.com/c/1308081Reviewed-by: default avatarTheresa <twellington@chromium.org>
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604063}
parent bf7c3813
...@@ -72,9 +72,7 @@ ...@@ -72,9 +72,7 @@
android:id="@+id/omnibox_results_container_stub" android:id="@+id/omnibox_results_container_stub"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/control_container_height"
app:layout_anchor="@id/control_container" app:layout_anchor="@id/control_container"
app:layout_anchorGravity="start|bottom"
android:inflatedId="@+id/omnibox_results_container" android:inflatedId="@+id/omnibox_results_container"
android:layout="@layout/omnibox_results_container" /> android:layout="@layout/omnibox_results_container" />
......
...@@ -14,9 +14,7 @@ ...@@ -14,9 +14,7 @@
android:id="@+id/omnibox_results_container_stub" android:id="@+id/omnibox_results_container_stub"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/toolbar_height_no_shadow"
app:layout_anchor="@id/toolbar" app:layout_anchor="@id/toolbar"
app:layout_anchorGravity="start|bottom"
android:background="@android:color/white" android:background="@android:color/white"
android:layout="@layout/omnibox_results_container" /> android:layout="@layout/omnibox_results_container" />
......
...@@ -12,6 +12,9 @@ import android.graphics.drawable.Drawable; ...@@ -12,6 +12,9 @@ import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewCompat;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ListView; import android.widget.ListView;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
...@@ -36,6 +39,9 @@ public class OmniboxSuggestionsList extends ListView { ...@@ -36,6 +39,9 @@ public class OmniboxSuggestionsList extends ListView {
private final int[] mTempPosition = new int[2]; private final int[] mTempPosition = new int[2];
private final Rect mTempRect = new Rect(); private final Rect mTempRect = new Rect();
private final OnGlobalLayoutListener mAnchorViewLayoutListener;
private final OnLayoutChangeListener mAlignmentViewLayoutListener;
/** /**
* Provides the capabilities required to embed the omnibox suggestion list into the UI. * Provides the capabilities required to embed the omnibox suggestion list into the UI.
*/ */
...@@ -81,16 +87,40 @@ public class OmniboxSuggestionsList extends ListView { ...@@ -81,16 +87,40 @@ public class OmniboxSuggestionsList extends ListView {
refreshPopupBackground(); refreshPopupBackground();
mAnchorView = mEmbedder.getAnchorView(); mAnchorView = mEmbedder.getAnchorView();
// Prior to Android M, the contextual actions associated with the omnibox were anchored to
// the top of the screen and not a floating copy/paste menu like on newer versions. As a
// result of this, the toolbar is pushed down in these Android versions and we need to
// montior those changes to update the positioning of the list.
mAnchorViewLayoutListener = new OnGlobalLayoutListener() {
private int mOffsetInWindow;
@Override
public void onGlobalLayout() {
int offsetInWindow = 0;
View currentView = mAnchorView;
while (true) {
offsetInWindow += currentView.getTop();
ViewParent parent = currentView.getParent();
if (parent == null || !(parent instanceof View)) break;
currentView = (View) parent;
}
if (mOffsetInWindow == offsetInWindow) return;
mOffsetInWindow = offsetInWindow;
requestLayout();
}
};
mAlignmentView = mEmbedder.getAlignmentView(); mAlignmentView = mEmbedder.getAlignmentView();
if (mAlignmentView != null) { if (mAlignmentView != null) {
adjustSidePadding(); mAlignmentViewLayoutListener = new OnLayoutChangeListener() {
mAlignmentView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override @Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) { int oldLeft, int oldTop, int oldRight, int oldBottom) {
adjustSidePadding(); adjustSidePadding();
} }
}); };
} else {
mAlignmentViewLayoutListener = null;
} }
} }
...@@ -163,6 +193,12 @@ public class OmniboxSuggestionsList extends ListView { ...@@ -163,6 +193,12 @@ public class OmniboxSuggestionsList extends ListView {
int anchorY = mTempPosition[1]; int anchorY = mTempPosition[1];
int anchorBottomRelativeToContent = anchorY + mAnchorView.getMeasuredHeight(); int anchorBottomRelativeToContent = anchorY + mAnchorView.getMeasuredHeight();
// Update the layout params to ensure the parent correctly positions the suggestions under
// the anchor view.
ViewGroup.LayoutParams layoutParams = getLayoutParams();
if (layoutParams != null && layoutParams instanceof MarginLayoutParams) {
((MarginLayoutParams) layoutParams).topMargin = anchorBottomRelativeToContent;
}
mEmbedder.getWindowDelegate().getWindowVisibleDisplayFrame(mTempRect); mEmbedder.getWindowDelegate().getWindowVisibleDisplayFrame(mTempRect);
int availableViewportHeight = mTempRect.height() - anchorBottomRelativeToContent; int availableViewportHeight = mTempRect.height() - anchorBottomRelativeToContent;
super.onMeasure( super.onMeasure(
...@@ -194,11 +230,25 @@ public class OmniboxSuggestionsList extends ListView { ...@@ -194,11 +230,25 @@ public class OmniboxSuggestionsList extends ListView {
} }
} }
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mAnchorView.getViewTreeObserver().addOnGlobalLayoutListener(mAnchorViewLayoutListener);
if (mAlignmentView != null) {
adjustSidePadding();
mAlignmentView.addOnLayoutChangeListener(mAlignmentViewLayoutListener);
}
}
@Override @Override
protected void onDetachedFromWindow() { protected void onDetachedFromWindow() {
super.onDetachedFromWindow(); super.onDetachedFromWindow();
// Ensure none of the views are reused when re-attaching as the TextViews in the suggestions // Ensure none of the views are reused when re-attaching as the TextViews in the suggestions
// do not handle it in all cases. https://crbug.com/851839 // do not handle it in all cases. https://crbug.com/851839
reclaimViews(new ArrayList<>()); reclaimViews(new ArrayList<>());
mAnchorView.getViewTreeObserver().removeOnGlobalLayoutListener(mAnchorViewLayoutListener);
if (mAlignmentView != null) {
mAlignmentView.removeOnLayoutChangeListener(mAlignmentViewLayoutListener);
}
} }
} }
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