Modern Apps git

← Modern-Apps.git

web: add long-press link context menu with copy, share and open in new tab

Commit
abbf0f6b34c603d7f1a27031aeef1b29010574ff
Author
Vayun Mathur <[email protected]>
When
2026-08-13 19:54 UTC (3 months ago)
Parents
e4a3150

Diff

web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt b/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.ktindex ac0af14127..edb3d8efe2 100644--- a/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt@@ -124,6 +124,7 @@ fun BrowserPage(      var showMenu by remember { mutableStateOf(false) }     var showInstallDialog by remember { mutableStateOf(false) }+    var linkContextMenuUrl by remember { mutableStateOf<String?>(null) }     val searchFocusRequester = remember { FocusRequester() }      BackHandler(enabled = viewModel.showTabSwitcher) { viewModel.showTabSwitcher = false }@@ -405,6 +406,7 @@ fun BrowserPage(                                     viewModel = viewModel,                                     webViewPool = webViewPool,                                     onRequestNewTab = { url -> viewModel.newTab(url = url) },+                                    onLinkLongPress = { url -> linkContextMenuUrl = url },                                     modifier = Modifier.fillMaxSize()                                 )                             }@@ -501,6 +503,29 @@ fun BrowserPage(             )         } +        linkContextMenuUrl?.let { linkUrl ->+            LinkContextMenu(+                url = linkUrl,+                onDismiss = { linkContextMenuUrl = null },+                onCopyLink = {+                    com.vayunmathur.library.ui.ExternalIntents.copyToClipboard(+                        context,+                        linkUrl,+                        linkUrl,+                    )+                    com.vayunmathur.library.util.AppMessages.show(context.getString(R.string.link_copied))+                },+                onShareLink = {+                    com.vayunmathur.library.ui.ExternalIntents.shareText(+                        context,+                        linkUrl,+                        context.getString(R.string.share_link),+                    )+                },+                onOpenInNewTab = { viewModel.newTab(url = linkUrl) },+            )+        }+         if (showInstallDialog) {             val tabId = activeTab?.id             val url = activeTab?.url ?: ""
web/src/main/java/com/vayunmathur/web/ui/LinkContextMenu.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/LinkContextMenu.kt b/web/src/main/java/com/vayunmathur/web/ui/LinkContextMenu.ktnew file mode 100644index 0000000000..6c08e72700--- /dev/null+++ b/web/src/main/java/com/vayunmathur/web/ui/LinkContextMenu.kt@@ -0,0 +1,93 @@+package com.vayunmathur.web.ui++import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.navigationBarsPadding+import androidx.compose.foundation.layout.padding+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.text.style.TextOverflow+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.ExperimentalMaterial3Api+import com.vayunmathur.library.ui.HorizontalDivider+import com.vayunmathur.library.ui.IconAdd+import com.vayunmathur.library.ui.IconCopy+import com.vayunmathur.library.ui.IconShare+import com.vayunmathur.library.ui.ListItem+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.ModalBottomSheet+import com.vayunmathur.library.ui.Text+import com.vayunmathur.web.R+import com.vayunmathur.web.util.BrowserUtils++@OptIn(ExperimentalMaterial3Api::class)+@Composable+fun LinkContextMenu(+    url: String,+    onDismiss: () -> Unit,+    onCopyLink: () -> Unit,+    onShareLink: () -> Unit,+    onOpenInNewTab: () -> Unit,+) {+    ModalBottomSheet(onDismissRequest = onDismiss) {+        Column(+            Modifier+                .fillMaxWidth()+                .navigationBarsPadding()+                .padding(bottom = 16.dp),+        ) {+            Column(Modifier.padding(horizontal = 24.dp, vertical = 12.dp)) {+                Text(+                    text = BrowserUtils.prettyUrl(url).ifBlank { url },+                    style = MaterialTheme.typography.bodySmall,+                    color = MaterialTheme.colorScheme.onSurfaceVariant,+                    maxLines = 2,+                    overflow = TextOverflow.Ellipsis,+                )+                Spacer(Modifier.height(2.dp))+                Text(+                    text = url,+                    style = MaterialTheme.typography.bodySmall,+                    color = MaterialTheme.colorScheme.onSurfaceVariant,+                    maxLines = 3,+                    overflow = TextOverflow.Ellipsis,+                )+            }+            HorizontalDivider(Modifier.padding(vertical = 4.dp))+            ListItem(+                headlineContent = { Text(stringResource(R.string.copy_link)) },+                leadingContent = { IconCopy() },+                modifier = Modifier+                    .fillMaxWidth()+                    .clickable {+                        onCopyLink()+                        onDismiss()+                    },+            )+            ListItem(+                headlineContent = { Text(stringResource(R.string.share_link)) },+                leadingContent = { IconShare() },+                modifier = Modifier+                    .fillMaxWidth()+                    .clickable {+                        onShareLink()+                        onDismiss()+                    },+            )+            ListItem(+                headlineContent = { Text(stringResource(R.string.open_in_new_tab)) },+                leadingContent = { IconAdd() },+                modifier = Modifier+                    .fillMaxWidth()+                    .clickable {+                        onOpenInNewTab()+                        onDismiss()+                    },+            )+        }+    }+}
web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt b/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.ktindex e7140a8028..5a2237dc29 100644--- a/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt@@ -56,6 +56,7 @@ fun WebViewBrowser(     modifier: Modifier = Modifier,     onRequestNewTab: (String) -> Unit = {},     webViewPool: MutableMap<String, WebView>,+    onLinkLongPress: (String) -> Unit = {}, ) {     val context = LocalContext.current     val holder = remember(tabId) { WebViewHolder() }@@ -124,6 +125,19 @@ fun WebViewBrowser(             webViewPool[tabId]?.let { existing ->                 (existing.parent as? ViewGroup)?.removeView(existing)                 applySettings(existing, viewModel)+                existing.setOnLongClickListener {+                    val hit = existing.hitTestResult+                    val url: String? = when (hit?.type) {+                        WebView.HitTestResult.SRC_ANCHOR_TYPE,+                        WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE -> hit.extra+                        else -> null+                    }+                    if (!url.isNullOrBlank()) {+                        onLinkLongPress(url)+                        return@setOnLongClickListener true+                    }+                    false+                }                 return@AndroidView existing             } @@ -139,6 +153,23 @@ fun WebViewBrowser(                  applySettings(this, viewModel) +                // Long-press on a link: intercept before WebView's default tooltip/context menu+                setOnLongClickListener {+                    val hit = hitTestResult+                    val hitType = hit?.type+                    val url: String? = when (hitType) {+                        WebView.HitTestResult.SRC_ANCHOR_TYPE,+                        WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE -> hit.extra+                        else -> null+                    }+                    if (!url.isNullOrBlank()) {+                        onLinkLongPress(url)+                        return@setOnLongClickListener true+                    }+                    false+                }++                 setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimeType, contentLength ->                     val fileName = android.webkit.URLUtil.guessFileName(url, contentDisposition, mimeType)                     Log.d(TAG, "Download: $fileName $url")@@ -387,6 +418,20 @@ fun WebViewBrowser(             applySettings(webView, viewModel)             (webView.webViewClient as? ShieldsWebViewClient)                 ?.installFarbling(webView, farblingConfig)+            // Keep the long-press handler in sync after pool reuse / recomposition+            webView.setOnLongClickListener {+                val hit = webView.hitTestResult+                val url: String? = when (hit?.type) {+                    WebView.HitTestResult.SRC_ANCHOR_TYPE,+                    WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE -> hit.extra+                    else -> null+                }+                if (!url.isNullOrBlank()) {+                    onLinkLongPress(url)+                    return@setOnLongClickListener true+                }+                false+            }             holder.webView = webView         }     )
web/src/main/res/values/strings.xml
diff --git a/web/src/main/res/values/strings.xml b/web/src/main/res/values/strings.xmlindex 0db8debb61..4deeab7b4c 100644--- a/web/src/main/res/values/strings.xml+++ b/web/src/main/res/values/strings.xml@@ -115,4 +115,7 @@     <string name="shields_reset_site">Reset to defaults</string>     <string name="shields_clear_exceptions">Clear all site exceptions</string>     <string name="shields_engine_loading">Loading filter lists…</string>+    <string name="copy_link">Copy link</string>+    <string name="open_in_new_tab">Open in new tab</string>+    <string name="link_copied">Link copied</string> </resources>