Fix it again

This commit is contained in:
Grishka 2023-06-04 04:50:40 +03:00
parent 148b8e9369
commit a78b0687f7
1 changed files with 22 additions and 62 deletions

View File

@ -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;i<l.getLineCount();i++){
view.getLineBounds(i, rect);
if(rect.contains((int)event.getX(), (int)event.getY())){
line=i;
break;
}
}
if(line==-1){
int padLeft=view.getTotalPaddingLeft(), padRight=view.getTotalPaddingRight(), padTop=view.getTotalPaddingTop(), padBottom=view.getTotalPaddingBottom();
float x=event.getX(), y=event.getY();
if(x<padLeft || y<padTop || x>view.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)) || (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()<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(dp(-2), dp(-2));
hlPath.addRect(new RectF(bounds), Path.Direction.CW);
}
hlPath.offset(view.getPaddingLeft(), 0);
view.invalidate();
return true;
}
for(LinkSpan span:spans){
int start=s.getSpanStart(span);
int end=s.getSpanEnd(span);
if(start<=position && end>position){
selectedSpan=span;
hlPath=new Path();
l.getSelectionPath(start, end, hlPath);
hlPaint.setColor((span.getColor() & 0x00FFFFFF) | 0x33000000);
view.invalidate();
return true;
}
}
}