Commit 54d154eb authored by Vincent Boisselle's avatar Vincent Boisselle Committed by Commit Bot

Allow using a custom ViewRectProvider in the UserEducationHelper

We have a use case where we need our custom ViewRectProvider to call notifyRectHidden() when the rectangle that represents the anchor reaches a certain position, which dismisses the IPH.

Bug: 1085601
Change-Id: I687df3a1a24cf0e045b9ff39d127a60efbcc3802
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2212971Reviewed-by: default avatarJustin DeWitt <dewittj@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Vincent Boisselle <vincb@google.com>
Cr-Commit-Position: refs/heads/master@{#803325}
parent c951ed95
...@@ -107,8 +107,27 @@ public class SectionHeaderView extends LinearLayout implements View.OnClickListe ...@@ -107,8 +107,27 @@ public class SectionHeaderView extends LinearLayout implements View.OnClickListe
mHeader.isExpanded() ? 0 : R.drawable.hairline_border_card_background); mHeader.isExpanded() ? 0 : R.drawable.hairline_border_card_background);
} }
} }
/** Shows an IPH on the feed header menu button. */ /** Shows an IPH on the feed header menu button. */
public void showMenuIph(UserEducationHelper helper) { public void showMenuIph(UserEducationHelper helper) {
final ViewRectProvider rectProvider = new ViewRectProvider(mMenuView) {
// ViewTreeObserver.OnPreDrawListener implementation.
@Override
public boolean onPreDraw() {
boolean result = super.onPreDraw();
int minRectBottomPosPx =
getResources().getDimensionPixelSize(R.dimen.toolbar_height_no_shadow)
+ mMenuView.getHeight() / 2;
// Notify that the rectangle is hidden to dismiss the popup if the anchor is
// positioned too high.
if (getRect().bottom < minRectBottomPosPx) {
notifyRectHidden();
}
return result;
}
};
helper.requestShowIPH(new IPHCommandBuilder(mMenuView.getContext().getResources(), helper.requestShowIPH(new IPHCommandBuilder(mMenuView.getContext().getResources(),
FeatureConstants.FEED_HEADER_MENU_FEATURE, R.string.ntp_feed_menu_iph, FeatureConstants.FEED_HEADER_MENU_FEATURE, R.string.ntp_feed_menu_iph,
R.string.accessibility_ntp_feed_menu_iph) R.string.accessibility_ntp_feed_menu_iph)
...@@ -116,8 +135,9 @@ public class SectionHeaderView extends LinearLayout implements View.OnClickListe ...@@ -116,8 +135,9 @@ public class SectionHeaderView extends LinearLayout implements View.OnClickListe
.setCircleHighlight(true) .setCircleHighlight(true)
.setShouldHighlight(true) .setShouldHighlight(true)
.setDismissOnTouch(false) .setDismissOnTouch(false)
.setInsetRect(new Rect(0, 0, 0, 0)) .setInsetRect(new Rect(0, -1, 0, -1))
.setAutoDismissTimeout(5 * 1000) .setAutoDismissTimeout(5 * 1000)
.setViewRectProvider(rectProvider)
.build()); .build());
} }
......
...@@ -9,6 +9,8 @@ import android.view.View; ...@@ -9,6 +9,8 @@ import android.view.View;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import org.chromium.ui.widget.ViewRectProvider;
/** /**
* Class encapsulating the data needed to show in-product help (IPH). * Class encapsulating the data needed to show in-product help (IPH).
*/ */
...@@ -26,11 +28,12 @@ public class IPHCommand { ...@@ -26,11 +28,12 @@ public class IPHCommand {
public final Runnable onShowCallback; public final Runnable onShowCallback;
public final Rect insetRect; public final Rect insetRect;
public final long autoDismissTimeout; public final long autoDismissTimeout;
public final ViewRectProvider viewRectProvider;
IPHCommand(String featureName, String contentString, String accessibilityText, IPHCommand(String featureName, String contentString, String accessibilityText,
boolean circleHighlight, boolean shouldHighlight, boolean dismissOnTouch, boolean circleHighlight, boolean shouldHighlight, boolean dismissOnTouch,
View anchorView, Runnable onDismissCallback, Runnable onShowCallback, Rect insetRect, View anchorView, Runnable onDismissCallback, Runnable onShowCallback, Rect insetRect,
long autoDismissTimeout) { long autoDismissTimeout, ViewRectProvider viewRectProvider) {
this.featureName = featureName; this.featureName = featureName;
this.contentString = contentString; this.contentString = contentString;
this.accessibilityText = accessibilityText; this.accessibilityText = accessibilityText;
...@@ -42,5 +45,6 @@ public class IPHCommand { ...@@ -42,5 +45,6 @@ public class IPHCommand {
this.onShowCallback = onShowCallback; this.onShowCallback = onShowCallback;
this.insetRect = insetRect; this.insetRect = insetRect;
this.autoDismissTimeout = autoDismissTimeout; this.autoDismissTimeout = autoDismissTimeout;
this.viewRectProvider = viewRectProvider;
} }
} }
\ No newline at end of file
...@@ -12,6 +12,7 @@ import androidx.annotation.StringRes; ...@@ -12,6 +12,7 @@ import androidx.annotation.StringRes;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.components.browser_ui.widget.textbubble.TextBubble; import org.chromium.components.browser_ui.widget.textbubble.TextBubble;
import org.chromium.ui.widget.ViewRectProvider;
/** /**
* Builder for (@see IPHCommand.java). Use this instead of constructing an IPHCommand directly. * Builder for (@see IPHCommand.java). Use this instead of constructing an IPHCommand directly.
...@@ -35,6 +36,7 @@ public class IPHCommandBuilder { ...@@ -35,6 +36,7 @@ public class IPHCommandBuilder {
private Runnable mOnDismissCallback; private Runnable mOnDismissCallback;
private Rect mInsetRect; private Rect mInsetRect;
private long mAutoDismissTimeout = TextBubble.NO_TIMEOUT; private long mAutoDismissTimeout = TextBubble.NO_TIMEOUT;
private ViewRectProvider mViewRectProvider;
/** /**
* Constructor for IPHCommandBuilder when you would like your strings to be resolved for you. * Constructor for IPHCommandBuilder when you would like your strings to be resolved for you.
...@@ -140,6 +142,17 @@ public class IPHCommandBuilder { ...@@ -140,6 +142,17 @@ public class IPHCommandBuilder {
return this; return this;
} }
/**
*
* @param viewRectProvider Custom ViewRectProvider to replace the default one. Note that the
* provided insets will still be applied on the rectangle from the
* custom provider.
*/
public IPHCommandBuilder setViewRectProvider(ViewRectProvider viewRectProvider) {
mViewRectProvider = viewRectProvider;
return this;
}
/** /**
* *
* @return an (@see IPHCommand) containing the accumulated state of this builder. * @return an (@see IPHCommand) containing the accumulated state of this builder.
...@@ -170,6 +183,6 @@ public class IPHCommandBuilder { ...@@ -170,6 +183,6 @@ public class IPHCommandBuilder {
return new IPHCommand(mFeatureName, mContentString, mAccessibilityText, mCircleHighlight, return new IPHCommand(mFeatureName, mContentString, mAccessibilityText, mCircleHighlight,
mShouldHighlight, mDismissOnTouch, mAnchorView, mOnDismissCallback, mOnShowCallback, mShouldHighlight, mDismissOnTouch, mAnchorView, mOnDismissCallback, mOnShowCallback,
mInsetRect, mAutoDismissTimeout); mInsetRect, mAutoDismissTimeout, mViewRectProvider);
} }
} }
\ No newline at end of file
...@@ -74,7 +74,9 @@ public class UserEducationHelper { ...@@ -74,7 +74,9 @@ public class UserEducationHelper {
String accessibilityString = iphCommand.accessibilityText; String accessibilityString = iphCommand.accessibilityText;
assert (!contentString.isEmpty()); assert (!contentString.isEmpty());
assert (!accessibilityString.isEmpty()); assert (!accessibilityString.isEmpty());
ViewRectProvider rectProvider = new ViewRectProvider(anchorView); ViewRectProvider rectProvider = iphCommand.viewRectProvider != null
? iphCommand.viewRectProvider
: new ViewRectProvider(anchorView);
TextBubble textBubble = TextBubble textBubble =
new TextBubble(mActivity, anchorView, contentString, accessibilityString, true, new TextBubble(mActivity, anchorView, contentString, accessibilityString, true,
......
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