From 41682d1147078a3986d3f247167cb8a913693cb8 Mon Sep 17 00:00:00 2001 From: Torge Rosendahl Date: Wed, 15 Feb 2023 17:30:31 -0500 Subject: [PATCH] added press-and-hold listener to ClickableLinks --- .../ui/text/ClickableLinksDelegate.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 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 482a7eacf..6b7573a5f 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 @@ -6,11 +6,14 @@ import android.graphics.Paint; import android.graphics.Path; import android.graphics.Rect; import android.graphics.RectF; +import android.os.Handler; import android.text.Layout; import android.text.Spanned; import android.view.MotionEvent; import android.view.SoundEffectConstants; +import android.view.ViewConfiguration; import android.widget.TextView; +import android.widget.Toast; import me.grishka.appkit.utils.V; @@ -20,6 +23,7 @@ public class ClickableLinksDelegate { private Path hlPath; private LinkSpan selectedSpan; private TextView view; + private final Handler longClickHandler = new Handler(); public ClickableLinksDelegate(TextView view) { this.view=view; @@ -63,6 +67,7 @@ public class ClickableLinksDelegate { } hlPath=new Path(); selectedSpan=span; + longClickHandler.postDelayed(copyTextToClipboard, ViewConfiguration.getLongPressTimeout()); hlPaint.setColor((span.getColor() & 0x00FFFFFF) | 0x33000000); //l.getSelectionPath(start, end, hlPath); for(int j=lstart;j<=lend;j++){ @@ -90,21 +95,31 @@ public class ClickableLinksDelegate { } } if(event.getAction()==MotionEvent.ACTION_UP && selectedSpan!=null){ + longClickHandler.removeCallbacks(copyTextToClipboard); view.playSoundEffect(SoundEffectConstants.CLICK); selectedSpan.onClick(view.getContext()); - hlPath=null; - selectedSpan=null; - view.invalidate(); + resetAndInvalidate(); return false; } if(event.getAction()==MotionEvent.ACTION_CANCEL){ - hlPath=null; - selectedSpan=null; - view.invalidate(); + resetAndInvalidate(); return false; } return false; } + + Runnable copyTextToClipboard = () -> { + //TODO actually copy to clipboard + //TODO think about removing toast, system > A12 (?) has a built-in popup + Toast.makeText(view.getContext(), "copied to clipboard", Toast.LENGTH_SHORT).show(); + resetAndInvalidate(); + }; + + private void resetAndInvalidate() { + hlPath=null; + selectedSpan=null; + view.invalidate(); + } public void onDraw(Canvas canvas){ if(hlPath!=null){