1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-06-27 15:43:21 +00:00

Improve ellipsizing comments

This commit is contained in:
Stypox 2023-01-15 14:57:34 +01:00
parent 489df0ed7d
commit 6e73c489de
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23

View File

@ -48,6 +48,8 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder {
private final int commentHorizontalPadding; private final int commentHorizontalPadding;
private final int commentVerticalPadding; private final int commentVerticalPadding;
private final Paint paintAtContentSize;
private final float ellipsisWidthPx; private final float ellipsisWidthPx;
private final RelativeLayout itemRoot; private final RelativeLayout itemRoot;
@ -76,9 +78,9 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder {
commentVerticalPadding = (int) infoItemBuilder.getContext() commentVerticalPadding = (int) infoItemBuilder.getContext()
.getResources().getDimension(R.dimen.comments_vertical_padding); .getResources().getDimension(R.dimen.comments_vertical_padding);
final Paint paint = new Paint(); paintAtContentSize = new Paint();
paint.setTextSize(itemContentView.getTextSize()); paintAtContentSize.setTextSize(itemContentView.getTextSize());
ellipsisWidthPx = paint.measureText(ELLIPSIS); ellipsisWidthPx = paintAtContentSize.measureText(ELLIPSIS);
} }
public CommentsMiniInfoItemHolder(final InfoItemBuilder infoItemBuilder, public CommentsMiniInfoItemHolder(final InfoItemBuilder infoItemBuilder,
@ -201,18 +203,40 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder {
} }
private void ellipsize() { private void ellipsize() {
itemContentView.setMaxLines(COMMENT_EXPANDED_LINES);
linkifyCommentContentView(v -> { linkifyCommentContentView(v -> {
boolean hasEllipsis = false; boolean hasEllipsis = false;
if (itemContentView.getLineCount() > COMMENT_DEFAULT_LINES) { if (itemContentView.getLineCount() > COMMENT_DEFAULT_LINES) {
final int endOfLastLine = itemContentView // Note that converting to String removes spans (i.e. links), but that's something
.getLayout() // we actually want since when the text is ellipsized we want all clicks on the
.getLineEnd(COMMENT_DEFAULT_LINES - 1); // comment to expand the comment, not to open links.
int end = itemContentView.getText().toString().lastIndexOf(' ', endOfLastLine - 2); final String text = itemContentView.getText().toString();
if (end == -1) {
end = Math.max(endOfLastLine - 2, 0); final Layout layout = itemContentView.getLayout();
final float lineWidth = layout.getLineWidth(COMMENT_DEFAULT_LINES - 1);
final float layoutWidth = layout.getWidth();
final int lineStart = layout.getLineStart(COMMENT_DEFAULT_LINES - 1);
final int lineEnd = layout.getLineEnd(COMMENT_DEFAULT_LINES - 1);
// remove characters up until there is enough space for the ellipsis
// (also summing 2 more pixels, just to be sure to avoid float rounding errors)
int end = lineEnd;
float removedCharactersWidth = 0.0f;
while (lineWidth - removedCharactersWidth + ellipsisWidthPx + 2.0f > layoutWidth
&& end >= lineStart) {
end -= 1;
// recalculate each time to account for ligatures or other similar things
removedCharactersWidth = paintAtContentSize.measureText(
text.substring(end, lineEnd));
} }
final String newVal = itemContentView.getText().subSequence(0, end) + "";
// remove trailing spaces and newlines
while (end > 0 && Character.isWhitespace(text.charAt(end - 1))) {
end -= 1;
}
final String newVal = text.substring(0, end) + ELLIPSIS;
itemContentView.setText(newVal); itemContentView.setText(newVal);
hasEllipsis = true; hasEllipsis = true;
} }