Fix it again

This commit is contained in:
Grishka 2023-06-04 04:50:40 +03:00 committed by sk
parent 6f25c8be0f
commit 5dcd6e5a0d
1 changed files with 23 additions and 63 deletions

View File

@ -4,8 +4,7 @@ import android.graphics.Canvas;
import android.graphics.CornerPathEffect; import android.graphics.CornerPathEffect;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Path; import android.graphics.Path;
import android.graphics.Rect; import android.os.Build;
import android.graphics.RectF;
import android.text.Layout; import android.text.Layout;
import android.text.Spanned; import android.text.Spanned;
import android.view.GestureDetector; import android.view.GestureDetector;
@ -33,7 +32,8 @@ public class ClickableLinksDelegate {
hlPaint=new Paint(); hlPaint=new Paint();
hlPaint.setAntiAlias(true); hlPaint.setAntiAlias(true);
hlPaint.setPathEffect(new CornerPathEffect(V.dp(3))); hlPaint.setPathEffect(new CornerPathEffect(V.dp(3)));
// view.setHighlightColor(view.getResources().getColor(android.R.color.holo_blue_light)); hlPaint.setStyle(Paint.Style.FILL_AND_STROKE);
hlPaint.setStrokeWidth(V.dp(4));
gestureDetector = new GestureDetector(view.getContext(), new LinkGestureListener(), view.getHandler()); gestureDetector = new GestureDetector(view.getContext(), new LinkGestureListener(), view.getHandler());
} }
@ -58,7 +58,7 @@ public class ClickableLinksDelegate {
public void onDraw(Canvas canvas){ public void onDraw(Canvas canvas){
if(hlPath!=null){ if(hlPath!=null){
canvas.save(); canvas.save();
canvas.translate(0, view.getPaddingTop()); canvas.translate(view.getTotalPaddingLeft(), view.getTotalPaddingTop());
canvas.drawPath(hlPath, hlPaint); canvas.drawPath(hlPath, hlPaint);
canvas.restore(); canvas.restore();
} }
@ -73,69 +73,29 @@ public class ClickableLinksDelegate {
private class LinkGestureListener extends GestureDetector.SimpleOnGestureListener { private class LinkGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override @Override
public boolean onDown(@NonNull MotionEvent event) { public boolean onDown(@NonNull MotionEvent event) {
int line=-1; int padLeft=view.getTotalPaddingLeft(), padRight=view.getTotalPaddingRight(), padTop=view.getTotalPaddingTop(), padBottom=view.getTotalPaddingBottom();
Rect rect=new Rect(); float x=event.getX(), y=event.getY();
Layout l=view.getLayout(); if(x<padLeft || y<padTop || x>view.getWidth()-padRight || y>view.getHeight()-padBottom)
for(int i=0;i<l.getLineCount();i++){
view.getLineBounds(i, rect);
if(rect.contains((int)event.getX(), (int)event.getY())){
line=i;
break;
}
}
if(line==-1){
return false; 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(); CharSequence text=view.getText();
if(text instanceof Spanned s){ if(text instanceof Spanned s){
LinkSpan[] spans=s.getSpans(0, s.length()-1, LinkSpan.class); LinkSpan[] spans=s.getSpans(0, s.length()-1, LinkSpan.class);
if(spans.length>0){ for(LinkSpan span:spans){
for(LinkSpan span:spans){ int start=s.getSpanStart(span);
int start=s.getSpanStart(span); int end=s.getSpanEnd(span);
int end=s.getSpanEnd(span); if(start<=position && end>position){
int lstart=l.getLineForOffset(start); selectedSpan=span;
int lend=l.getLineForOffset(end); hlPath=new Path();
if(line>=lstart && line<=lend){ l.getSelectionPath(start, end, hlPath);
boolean isRTL=l.getParagraphDirection(line)==-1; hlPaint.setColor((span.getColor() & 0x00FFFFFF) | 0x33000000);
if(line==lstart && ((!isRTL && event.getX()-view.getPaddingLeft()<l.getPrimaryHorizontal(start)) || (isRTL && event.getX()-view.getPaddingLeft()>l.getPrimaryHorizontal(start)))){ view.invalidate();
continue; return true;
}
if(line==lend && ((!isRTL && event.getX()-view.getPaddingLeft()>l.getPrimaryHorizontal(end)) || (isRTL && event.getX()-view.getPaddingLeft()<l.getPrimaryHorizontal(end)))){
continue;
}
hlPath=new Path();
selectedSpan=span;
hlPaint.setColor((span.getColor() & 0x00FFFFFF) | 0x33000000);
//l.getSelectionPath(start, end, hlPath);
for(int j=lstart;j<=lend;j++){
Rect bounds=new Rect();
l.getLineBounds(j, bounds);
isRTL=l.getParagraphDirection(line)==-1;
//bounds.left+=view.getPaddingLeft();
if(j==lstart){
int startOffset=Math.round(l.getPrimaryHorizontal(start));
if(isRTL)
bounds.right=startOffset;
else
bounds.left=startOffset;
}
if(j==lend){
int endOffset=Math.round(l.getPrimaryHorizontal(end));
if(isRTL)
bounds.left=endOffset;
else
bounds.right=endOffset;
}else{
CharSequence lineChars=view.getText().subSequence(l.getLineStart(j), l.getLineEnd(j));
bounds.right=Math.round(view.getPaint().measureText(lineChars.toString()))/*+view.getPaddingRight()*/;
}
bounds.inset(V.dp(-2), V.dp(-2));
hlPath.addRect(new RectF(bounds), Path.Direction.CW);
}
hlPath.offset(view.getPaddingLeft(), 0);
view.invalidate();
return true;
}
} }
} }
} }