import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter_linkify/flutter_linkify.dart'; import '../helpers/link_handler.dart'; import '../l10n/l10n.dart'; import '../theme/mesh_theme.dart'; import '../utils/platform_info.dart'; /// One chat message as an IRC line for the terminal theme: /// /// 14:02 text that wraps under the whole width [2 hops] ✓ /// /// [trailing] widgets (route, delivery status) follow the text inline, so a /// plain message stays a single line. [text] may be null for messages whose /// body is not text (GIF, POI) — the line is then just the time and the nick. class TerminalMessageLine extends StatelessWidget { final String time; final String nick; final Color nickColor; final String? text; /// Text before translation, shown dimmed on its own line. final String? originalText; final Color textColor; final Color metaColor; final double textScale; final List trailing; final VoidCallback? onSecondaryTap; const TerminalMessageLine({ super.key, required this.time, required this.nick, required this.nickColor, required this.textColor, required this.metaColor, required this.textScale, this.text, this.originalText, this.trailing = const [], this.onSecondaryTap, }); static const _linkifyOptions = LinkifyOptions( humanize: false, defaultToHttps: false, ); static const _linkifiers = [UrlLinkifier(), EmailLinkifier()]; @override Widget build(BuildContext context) { final style = MeshTheme.mono( fontSize: 14 * textScale, color: textColor, letterSpacing: 0, ); final metaStyle = style.copyWith(color: metaColor); final body = text?.trim(); final original = originalText?.trim(); TextSpan linkified(String value, TextStyle base) { // What linkify() from package:linkify does; flutter_linkify does not // re-export the function, only the linkifiers. var elements = [TextElement(value)]; for (final linkifier in _linkifiers) { elements = linkifier.parse(elements, _linkifyOptions); } return buildTextSpan( elements, style: base, linkStyle: LinkHandler.defaultLinkStyle(context, base), onOpen: (link) => LinkHandler.handleLinkTap(context, link.url), ); } final line = TextSpan( children: [ TextSpan(text: '$time ', style: metaStyle), TextSpan( text: '<$nick>', style: style.copyWith(color: nickColor, fontWeight: FontWeight.w700), ), if (body != null && body.isNotEmpty) ...[ TextSpan(text: ' ', style: style), linkified(body, style), ], for (final widget in trailing) ...[ TextSpan(text: ' ', style: style), WidgetSpan(alignment: PlaceholderAlignment.middle, child: widget), ], ], ); final showOriginal = original != null && original.isNotEmpty && original != body; final rich = TextSpan( children: [ line, if (showOriginal) ...[ TextSpan(text: '\n', style: style), linkified(original, metaStyle.copyWith(fontStyle: FontStyle.italic)), ], ], ); if (!PlatformInfo.isDesktop) return Text.rich(rich); final selectable = SelectableText.rich(rich); if (onSecondaryTap == null) return selectable; return Listener( onPointerDown: (event) { if (event.buttons & kSecondaryMouseButton != 0) onSecondaryTap!(); }, behavior: HitTestBehavior.translucent, child: selectable, ); } } /// [RouteChip] as plain text for a terminal line, e.g. `[2 hops]`, `[flood]`. class TerminalRouteTag extends StatelessWidget { final bool isDirect; final int? hops; final Color color; final double textScale; const TerminalRouteTag({ super.key, required this.isDirect, required this.hops, required this.color, required this.textScale, }); @override Widget build(BuildContext context) { final l10n = context.l10n; final label = isDirect ? (hops == null || hops == 0 ? l10n.chat_direct : l10n.chat_hopsCount(hops!)) : l10n.routing_modeFlood; return Text( '[${label.toLowerCase()}]', style: MeshTheme.mono( fontSize: 12 * textScale, color: color, letterSpacing: 0, ), ); } }