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.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;
import android.view.GestureDetector;
@ -33,7 +32,8 @@ public class ClickableLinksDelegate {
hlPaint=new Paint();
hlPaint.setAntiAlias(true);
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());
}
@ -58,7 +58,7 @@ public class ClickableLinksDelegate {
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();
}
@ -73,69 +73,29 @@ public class ClickableLinksDelegate {
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(V.dp(-2), V.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;
}
}
}