import 'package:flutter/material.dart'; import 'package:flutter_linkify/flutter_linkify.dart'; import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; import '../l10n/l10n.dart'; import '../services/link_router.dart'; import '../utils/platform_info.dart'; import '../helpers/snack_bar_builder.dart'; /// Finds `meshcore://` share links in message text, which the stock URL /// linkifier does not, since it only knows http(s) and www. (#1289) class MeshcoreLinkifier extends Linkifier { const MeshcoreLinkifier(); static final _link = RegExp(r'meshcore://[^\s<>"]+', caseSensitive: false); static final _trailingPunctuation = RegExp(r'[.,;:!?]+$'); @override List parse( List elements, LinkifyOptions options, ) { final out = []; for (final element in elements) { if (element is! TextElement) { out.add(element); continue; } final text = element.text; var last = 0; for (final match in _link.allMatches(text)) { // A sentence ending right after a link keeps its full stop. final url = match[0]!.replaceFirst(_trailingPunctuation, ''); final end = match.start + url.length; if (match.start > last) { out.add(TextElement(text.substring(last, match.start))); } out.add(UrlElement(url)); last = end; } if (last < text.length) out.add(TextElement(text.substring(last))); } return out; } } class LinkHandler { static TextStyle defaultLinkStyle(BuildContext context, TextStyle base) { final brightness = Theme.of(context).brightness; final orange = brightness == Brightness.dark ? const Color(0xFFFFB74D) : const Color(0xFFE65100); return base.copyWith(color: orange, decoration: TextDecoration.underline); } static const _options = LinkifyOptions( humanize: false, defaultToHttps: false, ); static const _linkifiers = [ MeshcoreLinkifier(), UrlLinkifier(), EmailLinkifier(), ]; /// Returns a [SelectableLinkify] on desktop or a [Linkify] on mobile. static Widget buildLinkifyText({ required BuildContext context, required String text, required TextStyle style, TextStyle? linkStyle, }) { final effectiveLinkStyle = linkStyle ?? defaultLinkStyle(context, style); void onOpen(LinkableElement link) => handleLinkTap(context, link.url); if (PlatformInfo.isDesktop) { return SelectableLinkify( text: text, style: style, linkStyle: effectiveLinkStyle, options: _options, linkifiers: _linkifiers, onOpen: onOpen, ); } return Linkify( text: text, style: style, linkStyle: effectiveLinkStyle, options: _options, linkifiers: _linkifiers, onOpen: onOpen, ); } /// A span with the same links, style and confirm-on-tap as /// [buildLinkifyText], for text that sits between widget spans such as /// mention chips. (#1074) static InlineSpan buildLinkifySpan({ required BuildContext context, required String text, required TextStyle style, }) { return LinkifySpan( text: text, style: style, linkStyle: defaultLinkStyle(context, style), options: _options, linkifiers: _linkifiers, onOpen: (link) => handleLinkTap(context, link.url), ); } static Future handleLinkTap(BuildContext context, String url) async { // A share link opens Offband's own add confirmation, not the browser. if (url.toLowerCase().startsWith('meshcore://')) { context.read().handle(url); return; } // Show confirmation dialog final shouldOpen = await showDialog( context: context, builder: (context) => AlertDialog( title: Text(context.l10n.chat_openLink), content: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( context.l10n.chat_openLinkConfirmation, style: const TextStyle(fontSize: 14), ), const SizedBox(height: 16), Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: Theme.of(context).colorScheme.surfaceContainerHighest, borderRadius: BorderRadius.circular(8), ), child: SelectableText( url, style: const TextStyle(fontSize: 12, fontFamily: 'monospace'), ), ), ], ), actions: [ TextButton( onPressed: () => Navigator.pop(context, false), child: Text(context.l10n.common_cancel), ), FilledButton( onPressed: () => Navigator.pop(context, true), child: Text(context.l10n.chat_open), ), ], ), ); if (shouldOpen != true) return; // Launch URL try { final uri = Uri.parse(url); if (!await launchUrl(uri, mode: LaunchMode.externalApplication)) { if (context.mounted) { showDismissibleSnackBar( context, content: Text(context.l10n.chat_couldNotOpenLink(url)), backgroundColor: Colors.red, ); } } } catch (e) { if (context.mounted) { showDismissibleSnackBar( context, content: Text(context.l10n.chat_invalidLink), backgroundColor: Colors.red, ); } } } }