From a78b0687f74fafbe45a5387360184748c62076d7 Mon Sep 17 00:00:00 2001 From: Grishka Date: Sun, 4 Jun 2023 04:50:40 +0300 Subject: [PATCH] Fix it again --- .../ui/text/ClickableLinksDelegate.java | 84 +++++-------------- 1 file changed, 22 insertions(+), 62 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/text/ClickableLinksDelegate.java b/mastodon/src/main/java/org/joinmastodon/android/ui/text/ClickableLinksDelegate.java index 3dc82907f..68b54fb35 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/text/ClickableLinksDelegate.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/text/ClickableLinksDelegate.java @@ -8,8 +8,6 @@ import android.graphics.Canvas; import android.graphics.CornerPathEffect; import android.graphics.Paint; import android.graphics.Path; -import android.graphics.Rect; -import android.graphics.RectF; import android.os.Build; import android.text.Layout; import android.text.Spanned; @@ -38,6 +36,8 @@ public class ClickableLinksDelegate implements CustomViewHelper{ hlPaint=new Paint(); hlPaint.setAntiAlias(true); hlPaint.setPathEffect(new CornerPathEffect(dp(3))); + hlPaint.setStyle(Paint.Style.FILL_AND_STROKE); + hlPaint.setStrokeWidth(dp(4)); gestureDetector = new GestureDetector(view.getContext(), new LinkGestureListener(), view.getHandler()); } @@ -62,7 +62,7 @@ public class ClickableLinksDelegate implements CustomViewHelper{ public void onDraw(Canvas canvas){ if(hlPath!=null){ canvas.save(); - canvas.translate(0, view.getPaddingTop()); + canvas.translate(view.getTotalPaddingLeft(), view.getTotalPaddingTop()); canvas.drawPath(hlPath, hlPaint); canvas.restore(); } @@ -82,69 +82,29 @@ public class ClickableLinksDelegate implements CustomViewHelper{ private class LinkGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onDown(@NonNull MotionEvent event) { - int line=-1; - Rect rect=new Rect(); - Layout l=view.getLayout(); - for(int i=0;iview.getWidth()-padRight || y>view.getHeight()-padBottom) return false; - } + x-=padLeft; + y-=padTop; + Layout l=view.getLayout(); + int line=l.getLineForVertical(Math.round(y)); + int position=l.getOffsetForHorizontal(line, x); + CharSequence text=view.getText(); if(text instanceof Spanned s){ LinkSpan[] spans=s.getSpans(0, s.length()-1, LinkSpan.class); - if(spans.length>0){ - for(LinkSpan span:spans){ - int start=s.getSpanStart(span); - int end=s.getSpanEnd(span); - int lstart=l.getLineForOffset(start); - int lend=l.getLineForOffset(end); - if(line>=lstart && line<=lend){ - boolean isRTL=l.getParagraphDirection(line)==-1; - if(line==lstart && ((!isRTL && event.getX()-view.getPaddingLeft()l.getPrimaryHorizontal(start)))){ - continue; - } - if(line==lend && ((!isRTL && event.getX()-view.getPaddingLeft()>l.getPrimaryHorizontal(end)) || (isRTL && event.getX()-view.getPaddingLeft()position){ + selectedSpan=span; + hlPath=new Path(); + l.getSelectionPath(start, end, hlPath); + hlPaint.setColor((span.getColor() & 0x00FFFFFF) | 0x33000000); + view.invalidate(); + return true; } } }