Skip to content

Commit

Permalink
Introduce ChromeBulletSpan and use on incognito NTP, sad tab
Browse files Browse the repository at this point in the history
Move the existing SadTabBulletSpan to a new
ui/widget/../ChromeBulletSpan.java and re-use for the material design
incognito NTP. This fixes a text wrapping issues caused by the bullet
previously being included in the text string.

BUG=740619,740619

Change-Id: I172ee3e388962efc90d0defe01525828491883cf
Reviewed-on: https://chromium-review.googlesource.com/c/1277376
Reviewed-by: Ted Choc <tedchoc@chromium.org>
Commit-Queue: Theresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#598870}
  • Loading branch information
Theresa authored and Commit Bot committed Oct 11, 2018
1 parent 8bc49ae commit 91645e3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 50 deletions.
2 changes: 0 additions & 2 deletions chrome/android/java/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,6 @@
<dimen name="action_bar_shadow_height">10dp</dimen>
<dimen name="clear_text_button_end_padding">10dp</dimen>
<dimen name="sync_promo_view_padding">16dp</dimen>
<dimen name="sad_tab_bullet_gap">12dp</dimen>
<dimen name="sad_tab_bullet_leading_offset">1dp</dimen>
<dimen name="open_new_tab_animation_y_translation">-20dp</dimen>
<dimen name="bottom_sheet_peek_height">56dp</dimen>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.text.Layout;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.BulletSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.Gravity;
Expand All @@ -28,6 +24,7 @@
import org.chromium.chrome.R;
import org.chromium.ui.text.NoUnderlineClickableSpan;
import org.chromium.ui.text.SpanApplier;
import org.chromium.ui.widget.ChromeBulletSpan;

/**
* The Material Design New Tab Page for use in the Incognito profile. This is an extension
Expand All @@ -50,19 +47,6 @@ public class IncognitoNewTabPageViewMD extends IncognitoNewTabPageView {
private static final int CONTENT_WIDTH_DP = 600;
private static final int WIDE_LAYOUT_THRESHOLD_DP = 720;

private static class IncognitoBulletSpan extends BulletSpan {
public IncognitoBulletSpan() {
super(0 /* gapWidth */);
}

@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline,
int bottom, CharSequence text, int start, int end, boolean first, Layout l) {
// Do not draw the standard bullet point. We will include the Unicode bullet point
// symbol in the text instead.
}
}

/** Default constructor needed to inflate via XML. */
public IncognitoNewTabPageViewMD(Context context, AttributeSet attrs) {
super(context, attrs);
Expand Down Expand Up @@ -132,12 +116,11 @@ private void populateBulletpoints(@IdRes int element, @StringRes int content) {

// Format the bulletpoints:
// - Disambiguate the <li></li> spans for SpanApplier.
// - Add the bulletpoint symbols (Unicode BULLET U+2022)
// - Remove leading whitespace (caused by formatting in the .grdp file)
// - Remove the trailing newline after the last bulletpoint.
text = text.replaceFirst(" +<li>([^<]*)</li>", "<li1> \u2022 $1</li1>");
text = text.replaceFirst(" +<li>([^<]*)</li>", "<li2> \u2022 $1</li2>");
text = text.replaceFirst(" +<li>([^<]*)</li>\n", "<li3> \u2022 $1</li3>");
text = text.replaceFirst(" +<li>([^<]*)</li>", "<li1>$1</li1>");
text = text.replaceFirst(" +<li>([^<]*)</li>", "<li2>$1</li2>");
text = text.replaceFirst(" +<li>([^<]*)</li>\n", "<li3>$1</li3>");

// Remove the <ul></ul> tags which serve no purpose here, including the whitespace around
// them.
Expand All @@ -147,9 +130,9 @@ private void populateBulletpoints(@IdRes int element, @StringRes int content) {
new SpanApplier.SpanInfo("<em>", "</em>",
new ForegroundColorSpan(ApiCompatibilityUtils.getColor(
mContext.getResources(), R.color.incognito_emphasis))),
new SpanApplier.SpanInfo("<li1>", "</li1>", new IncognitoBulletSpan()),
new SpanApplier.SpanInfo("<li2>", "</li2>", new IncognitoBulletSpan()),
new SpanApplier.SpanInfo("<li3>", "</li3>", new IncognitoBulletSpan())));
new SpanApplier.SpanInfo("<li1>", "</li1>", new ChromeBulletSpan(mContext)),
new SpanApplier.SpanInfo("<li2>", "</li2>", new ChromeBulletSpan(mContext)),
new SpanApplier.SpanInfo("<li3>", "</li3>", new ChromeBulletSpan(mContext))));
}

/** Adjusts the paddings, margins, and the orientation of bulletpoints. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.BulletSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
Expand All @@ -33,6 +29,7 @@
import org.chromium.ui.text.NoUnderlineClickableSpan;
import org.chromium.ui.text.SpanApplier;
import org.chromium.ui.text.SpanApplier.SpanInfo;
import org.chromium.ui.widget.ChromeBulletSpan;

/**
* Represent the sad tab displayed in place of a crashed renderer. Instantiated on the first
Expand Down Expand Up @@ -279,7 +276,7 @@ private static void intializeSuggestionsViews(
*/
private static SpannableString generateBulletedString(Context context, int stringResId) {
SpannableString bullet = new SpannableString(context.getString(stringResId));
bullet.setSpan(new SadTabBulletSpan(context), 0, bullet.length(), 0);
bullet.setSpan(new ChromeBulletSpan(context), 0, bullet.length(), 0);
return bullet;
}

Expand All @@ -298,25 +295,6 @@ private static void recordEvent(boolean sendFeedbackView, int event) {
}
}

private static class SadTabBulletSpan extends BulletSpan {
private int mXOffset;

public SadTabBulletSpan(Context context) {
super(context.getResources().getDimensionPixelSize(R.dimen.sad_tab_bullet_gap));
mXOffset = context.getResources().getDimensionPixelSize(
R.dimen.sad_tab_bullet_leading_offset);
}

@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline,
int bottom, CharSequence text, int start, int end, boolean first, Layout l) {
// Android cuts off the bullet points. Adjust the x-position so that the bullets aren't
// cut off.
super.drawLeadingMargin(
c, p, x + mXOffset, dir, top, baseline, bottom, text, start, end, first, l);
}
}

// Bare minimum set up so |isShowing| returns true.
@VisibleForTesting
public static void initForTesting(Tab tab, SadTab sadTab) {
Expand Down
1 change: 1 addition & 0 deletions ui/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ android_library("ui_full_java") {
"java/src/org/chromium/ui/widget/AnchoredPopupWindow.java",
"java/src/org/chromium/ui/widget/ButtonCompat.java",
"java/src/org/chromium/ui/widget/CheckableImageView.java",
"java/src/org/chromium/ui/widget/ChromeBulletSpan.java",
"java/src/org/chromium/ui/widget/OptimizedFrameLayout.java",
"java/src/org/chromium/ui/widget/RectProvider.java",
"java/src/org/chromium/ui/widget/RoundedCornerImageView.java",
Expand Down
3 changes: 3 additions & 0 deletions ui/android/java/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

<dimen name="button_compat_corner_radius">4dp</dimen>

<dimen name="chrome_bullet_gap">12dp</dimen>
<dimen name="chrome_bullet_leading_offset">1dp</dimen>

<!-- Divider Dimensions -->
<dimen name="divider_height">1dp</dimen>

Expand Down
40 changes: 40 additions & 0 deletions ui/android/java/src/org/chromium/ui/widget/ChromeBulletSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout;
import android.text.style.BulletSpan;

import org.chromium.ui.R;

/**
* A wrapper around Android's BulletSpan that provides default styling and adjusts the bullet
* positioning to prevent it from being cut off.
*/
public class ChromeBulletSpan extends BulletSpan {
private int mXOffset;

/**
* Construts a new ChromeBuletSpan.
* @param context The context of the containing view, used to retrieve dimensions.
*/
public ChromeBulletSpan(Context context) {
super(context.getResources().getDimensionPixelSize(R.dimen.chrome_bullet_gap));
mXOffset =
context.getResources().getDimensionPixelSize(R.dimen.chrome_bullet_leading_offset);
}

@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline,
int bottom, CharSequence text, int start, int end, boolean first, Layout l) {
// Android cuts off the bullet points. Adjust the x-position so that the bullets aren't
// cut off.
super.drawLeadingMargin(
c, p, x + mXOffset, dir, top, baseline, bottom, text, start, end, first, l);
}
}

0 comments on commit 91645e3

Please sign in to comment.