Files
alan 9bfd16c6e5 Chat: highlighted, clickable links in messages
Markdown links, autolinks and bare http(s)/www. URLs open externally through
url_launcher (http, https and mailto only) in assistant markdown and operator
messages; code is never linkified. Links use the host primary colour with an
underline that thickens on hover, and offer Copy link on long press and
right-click without breaking text selection.
2026-11-03 16:17:26 +00:00

42 lines
1.4 KiB
Dart

import 'package:flutter/widgets.dart';
import 'package:url_launcher/url_launcher.dart';
/// Opens one already-filtered link target; true when the platform accepted it.
typedef ChatLinkLauncher = Future<bool> Function(Uri uri);
/// Opens [uri] outside the app: the system browser or mail client natively, a
/// new tab on the web.
Future<bool> launchChatLinkExternally(Uri uri) =>
launchUrl(uri, mode: LaunchMode.externalApplication);
/// Supplies the launcher transcript links use; tests install a fake. Without a
/// scope, links open with [launchChatLinkExternally].
class ChatLinkScope extends InheritedWidget {
const ChatLinkScope({
super.key,
required this.launcher,
required super.child,
});
final ChatLinkLauncher launcher;
static ChatLinkLauncher of(BuildContext context) =>
context.getInheritedWidgetOfExactType<ChatLinkScope>()?.launcher ??
launchChatLinkExternally;
@override
bool updateShouldNotify(ChatLinkScope oldWidget) =>
launcher != oldWidget.launcher;
}
/// Launches [uri] with the scope's launcher, reporting a refusal or a failed
/// platform call instead of letting it escape as an unhandled error.
Future<void> openChatLink(BuildContext context, Uri uri) async {
final launcher = ChatLinkScope.of(context);
try {
if (!await launcher(uri)) debugPrint('Link not opened: $uri');
} on Object catch (error) {
debugPrint('Link not opened: $uri ($error)');
}
}