Skip to content

Commit a5e9364

Browse files
committed
Added Copy Code button to short code blocks.
1 parent 495b025 commit a5e9364

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

‎TMessagesProj/src/main/java/org/telegram/messenger/MessageObject.java‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ public float textYOffset(ArrayList<TextLayoutBlock> blocks, ChatMessageCell.Tran
10641064
}
10651065

10661066
public void layoutCode(String lng, int codeLength, boolean noforwards) {
1067-
hasCodeCopyButton = codeLength >= 75 && !noforwards;
1067+
hasCodeCopyButton = codeLength >= 1 && !noforwards;
10681068
if (hasCodeCopyButton) {
10691069
copyText = new Text(getString(R.string.CopyCode).toUpperCase(), SharedConfig.fontSize - 3, AndroidUtilities.bold());
10701070
copyIcon = ApplicationLoader.applicationContext.getResources().getDrawable(R.drawable.msg_copy).mutate();
@@ -1105,9 +1105,12 @@ public void drawCopyCodeButton(Canvas canvas, RectF bounds, int textColor, int b
11051105
canvas.drawRect(bounds.left + dp(10), bounds.bottom - dp(38) - AndroidUtilities.getShadowHeight(), bounds.right - dp(6.66f), bounds.bottom - dp(38), copySeparator);
11061106

11071107
final float iconScale = .8f;
1108-
final float contentWidth = Math.min(bounds.width() - dp(12), copyIcon.getIntrinsicWidth() * iconScale + dp(5) + copyText.getCurrentWidth());
1109-
float x = bounds.centerX() - contentWidth / 2f;
1108+
final float iconWidth = copyIcon.getIntrinsicWidth() * iconScale;
11101109
final float cy = bounds.bottom - dp(38) / 2f;
1110+
final float fullWidth = iconWidth + dp(5) + copyText.getCurrentWidth();
1111+
final boolean iconOnly = fullWidth > bounds.width();
1112+
final float contentWidth = iconOnly ? iconWidth : fullWidth;
1113+
float x = bounds.centerX() - contentWidth / 2f;
11111114

11121115
if (copyIconColor != textColor) {
11131116
copyIcon.setColorFilter(new PorterDuffColorFilter(copyIconColor = textColor, PorterDuff.Mode.SRC_IN));
@@ -1116,15 +1119,17 @@ public void drawCopyCodeButton(Canvas canvas, RectF bounds, int textColor, int b
11161119
copyIcon.setBounds(
11171120
(int) x,
11181121
(int) (cy - copyIcon.getIntrinsicHeight() * iconScale / 2f),
1119-
(int) (x + copyIcon.getIntrinsicWidth() * iconScale),
1122+
(int) (x + iconWidth),
11201123
(int) (cy + copyIcon.getIntrinsicHeight() * iconScale / 2f)
11211124
);
11221125
copyIcon.draw(canvas);
11231126

1124-
x += copyIcon.getIntrinsicWidth() * iconScale + dp(5);
1125-
copyText
1126-
.ellipsize((int) (contentWidth - (copyIcon.getIntrinsicWidth() * iconScale + dp(5))) + dp(12))
1127-
.draw(canvas, x, cy, textColor, alpha);
1127+
if (iconOnly) {
1128+
return;
1129+
}
1130+
1131+
x += iconWidth + dp(5);
1132+
copyText.draw(canvas, x, cy, textColor, alpha);
11281133
}
11291134

11301135
public static String capitalizeLanguage(String lng) {

0 commit comments

Comments
 (0)