Add interactive GPS coordinates in messages
This commit is contained in:
1 parent
2f8fcb87d7
commit
05b47a037b
33 files changed
+1985
-84
No files matched your search
+311
-45
@@ -1,10 +1,16 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_linkify/flutter_linkify.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import '../l10n/l10n.dart';
|
||||
import '../utils/platform_info.dart';
|
||||
|
||||
import '../helpers/coordinate_parser.dart';
|
||||
import '../helpers/snack_bar_builder.dart';
|
||||
import '../l10n/l10n.dart';
|
||||
import '../screens/coordinate_map_screen.dart';
|
||||
import '../theme/mesh_theme.dart';
|
||||
import '../utils/platform_info.dart';
|
||||
import '../widgets/mesh_ui.dart';
|
||||
|
||||
class LinkHandler {
|
||||
static TextStyle defaultLinkStyle(BuildContext context, TextStyle base) {
|
||||
@@ -15,60 +21,153 @@ class LinkHandler {
|
||||
return base.copyWith(color: orange, decoration: TextDecoration.underline);
|
||||
}
|
||||
|
||||
/// The system text scale as a linear factor for [style]'s size.
|
||||
///
|
||||
/// flutter_linkify only takes a `textScaleFactor` (default 1.0, which
|
||||
/// ignores the system font size), so the ambient [TextScaler] is sampled
|
||||
/// at the style's font size to keep Android's nonlinear scaling. The
|
||||
/// in-app chat text scale is already part of `style.fontSize`.
|
||||
static double systemTextScaleFactor(BuildContext context, TextStyle style) {
|
||||
final fontSize =
|
||||
style.fontSize ?? DefaultTextStyle.of(context).style.fontSize ?? 14;
|
||||
if (fontSize <= 0) return 1.0;
|
||||
return MediaQuery.textScalerOf(context).scale(fontSize) / fontSize;
|
||||
static Color _inferBackground(BuildContext context, TextStyle base) {
|
||||
final textColor = base.color;
|
||||
if (textColor != null) {
|
||||
return textColor.computeLuminance() > 0.5
|
||||
? MeshPalette.bg2
|
||||
: Colors.white;
|
||||
}
|
||||
return Theme.of(context).colorScheme.surfaceContainerLow;
|
||||
}
|
||||
|
||||
/// Returns a [SelectableLinkify] on desktop or a [Linkify] on mobile.
|
||||
static TextStyle defaultCoordinateStyle(
|
||||
BuildContext context,
|
||||
TextStyle base, {
|
||||
Color? backgroundColor,
|
||||
}) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
final effectiveBg = backgroundColor ?? _inferBackground(context, base);
|
||||
final accent = readableAccent(
|
||||
scheme.primary,
|
||||
effectiveBg,
|
||||
minContrast: 4.5,
|
||||
);
|
||||
return base.copyWith(
|
||||
color: accent,
|
||||
decoration: TextDecoration.underline,
|
||||
decorationColor: accent,
|
||||
fontWeight: FontWeight.w600,
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns an interactive rich-text widget (selectable on desktop) with URL,
|
||||
/// email, and GPS coordinate detection.
|
||||
static Widget buildLinkifyText({
|
||||
required BuildContext context,
|
||||
required String text,
|
||||
required TextStyle style,
|
||||
TextStyle? linkStyle,
|
||||
TextStyle? coordinateStyle,
|
||||
Color? backgroundColor,
|
||||
VoidCallback? onSecondaryTap,
|
||||
}) {
|
||||
final effectiveLinkStyle = linkStyle ?? defaultLinkStyle(context, style);
|
||||
const options = LinkifyOptions(humanize: false, defaultToHttps: false);
|
||||
const linkifiers = [UrlLinkifier(), EmailLinkifier()];
|
||||
void onOpen(LinkableElement link) => handleLinkTap(context, link.url);
|
||||
final textScaleFactor = systemTextScaleFactor(context, style);
|
||||
|
||||
if (PlatformInfo.isDesktop) {
|
||||
final linkify = SelectableLinkify(
|
||||
text: text,
|
||||
style: style,
|
||||
linkStyle: effectiveLinkStyle,
|
||||
options: options,
|
||||
linkifiers: linkifiers,
|
||||
onOpen: onOpen,
|
||||
textScaleFactor: textScaleFactor,
|
||||
);
|
||||
if (onSecondaryTap == null) return linkify;
|
||||
return Listener(
|
||||
onPointerDown: (event) {
|
||||
if (event.buttons & kSecondaryMouseButton != 0) onSecondaryTap();
|
||||
},
|
||||
behavior: HitTestBehavior.translucent,
|
||||
child: linkify,
|
||||
);
|
||||
}
|
||||
return Linkify(
|
||||
return _InteractiveMessageText(
|
||||
text: text,
|
||||
style: style,
|
||||
linkStyle: effectiveLinkStyle,
|
||||
options: options,
|
||||
linkifiers: linkifiers,
|
||||
onOpen: onOpen,
|
||||
textScaleFactor: textScaleFactor,
|
||||
linkStyle: linkStyle,
|
||||
coordinateStyle: coordinateStyle,
|
||||
backgroundColor: backgroundColor,
|
||||
onSecondaryTap: onSecondaryTap,
|
||||
);
|
||||
}
|
||||
|
||||
static void openCoordinateMap(
|
||||
BuildContext context,
|
||||
CoordinateElement coordinate,
|
||||
) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (_) => CoordinateMapScreen(
|
||||
latitude: coordinate.latitude,
|
||||
longitude: coordinate.longitude,
|
||||
label: coordinate.text,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Future<void> copyCoordinateLocation(
|
||||
BuildContext context,
|
||||
String text,
|
||||
) async {
|
||||
await Clipboard.setData(ClipboardData(text: text));
|
||||
if (!context.mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(context.l10n.coordinate_copied),
|
||||
);
|
||||
}
|
||||
|
||||
static Future<void> openCoordinateInExternalMaps(
|
||||
BuildContext context,
|
||||
double latitude,
|
||||
double longitude,
|
||||
) async {
|
||||
final uri = buildExternalMapUri(latitude, longitude);
|
||||
try {
|
||||
if (!await launchUrl(uri, mode: LaunchMode.externalApplication)) {
|
||||
if (context.mounted) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(context.l10n.chat_couldNotOpenLink(uri.toString())),
|
||||
backgroundColor: Theme.of(context).colorScheme.error,
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (_) {
|
||||
if (context.mounted) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(context.l10n.chat_couldNotOpenLink(uri.toString())),
|
||||
backgroundColor: Theme.of(context).colorScheme.error,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> showCoordinateActionSheet(
|
||||
BuildContext context,
|
||||
CoordinateElement coordinate,
|
||||
) {
|
||||
return showMeshSheet<void>(
|
||||
context,
|
||||
builder: (sheetContext) {
|
||||
final l10n = sheetContext.l10n;
|
||||
return SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
BottomSheetHeader(
|
||||
title: coordinate.text,
|
||||
subtitle: l10n.chat_location,
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.copy),
|
||||
title: Text(l10n.coordinate_copyLocation),
|
||||
onTap: () {
|
||||
Navigator.pop(sheetContext);
|
||||
copyCoordinateLocation(context, coordinate.text);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.open_in_new),
|
||||
title: Text(l10n.coordinate_openInMaps),
|
||||
onTap: () {
|
||||
Navigator.pop(sheetContext);
|
||||
openCoordinateInExternalMaps(
|
||||
context,
|
||||
coordinate.latitude,
|
||||
coordinate.longitude,
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -139,3 +238,170 @@ class LinkHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extends [TapGestureRecognizer] so Flutter's paragraph/editable semantics
|
||||
/// recognize it as a tappable link while also forwarding pointers to an owned
|
||||
/// [LongPressGestureRecognizer] in the same gesture arena.
|
||||
class _CoordinateGestureRecognizer extends TapGestureRecognizer {
|
||||
final LongPressGestureRecognizer _longPress;
|
||||
|
||||
_CoordinateGestureRecognizer({
|
||||
required VoidCallback onTap,
|
||||
required VoidCallback onLongPress,
|
||||
}) : _longPress = LongPressGestureRecognizer()..onLongPress = onLongPress {
|
||||
this.onTap = onTap;
|
||||
}
|
||||
|
||||
@override
|
||||
void addPointer(PointerDownEvent event) {
|
||||
super.addPointer(event);
|
||||
_longPress.addPointer(event);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_longPress.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
class _InteractiveMessageText extends StatefulWidget {
|
||||
final String text;
|
||||
final TextStyle style;
|
||||
final TextStyle? linkStyle;
|
||||
final TextStyle? coordinateStyle;
|
||||
final Color? backgroundColor;
|
||||
final VoidCallback? onSecondaryTap;
|
||||
|
||||
const _InteractiveMessageText({
|
||||
required this.text,
|
||||
required this.style,
|
||||
this.linkStyle,
|
||||
this.coordinateStyle,
|
||||
this.backgroundColor,
|
||||
this.onSecondaryTap,
|
||||
});
|
||||
|
||||
@override
|
||||
State<_InteractiveMessageText> createState() =>
|
||||
_InteractiveMessageTextState();
|
||||
}
|
||||
|
||||
class _InteractiveMessageTextState extends State<_InteractiveMessageText> {
|
||||
late List<LinkifyElement> _elements;
|
||||
final List<GestureRecognizer> _recognizers = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_rebuildSpans();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant _InteractiveMessageText oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.text != widget.text) {
|
||||
_disposeRecognizers();
|
||||
_rebuildSpans();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposeRecognizers();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _disposeRecognizers() {
|
||||
for (final recognizer in _recognizers) {
|
||||
recognizer.dispose();
|
||||
}
|
||||
_recognizers.clear();
|
||||
}
|
||||
|
||||
void _rebuildSpans() {
|
||||
_elements = CoordinateParser.linkifyWithCoordinates(widget.text);
|
||||
}
|
||||
|
||||
InlineSpan _buildElementSpan(
|
||||
LinkifyElement element,
|
||||
TextStyle effectiveLinkStyle,
|
||||
TextStyle effectiveCoordinateStyle,
|
||||
) {
|
||||
if (element is CoordinateElement) {
|
||||
final recognizer = _CoordinateGestureRecognizer(
|
||||
onTap: () {
|
||||
if (!mounted) return;
|
||||
LinkHandler.openCoordinateMap(context, element);
|
||||
},
|
||||
onLongPress: () {
|
||||
if (!mounted) return;
|
||||
LinkHandler.showCoordinateActionSheet(context, element);
|
||||
},
|
||||
);
|
||||
_recognizers.add(recognizer);
|
||||
return TextSpan(
|
||||
text: element.text,
|
||||
style: effectiveCoordinateStyle,
|
||||
recognizer: recognizer,
|
||||
mouseCursor: SystemMouseCursors.click,
|
||||
);
|
||||
}
|
||||
if (element is LinkableElement) {
|
||||
final recognizer = TapGestureRecognizer()
|
||||
..onTap = () {
|
||||
if (!mounted) return;
|
||||
LinkHandler.handleLinkTap(context, element.url);
|
||||
};
|
||||
_recognizers.add(recognizer);
|
||||
return TextSpan(
|
||||
text: element.text,
|
||||
style: effectiveLinkStyle,
|
||||
recognizer: recognizer,
|
||||
mouseCursor: SystemMouseCursors.click,
|
||||
);
|
||||
}
|
||||
return TextSpan(text: element.text, style: widget.style);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_disposeRecognizers();
|
||||
final effectiveLinkStyle =
|
||||
widget.linkStyle ?? LinkHandler.defaultLinkStyle(context, widget.style);
|
||||
final effectiveCoordinateStyle =
|
||||
widget.coordinateStyle ??
|
||||
LinkHandler.defaultCoordinateStyle(
|
||||
context,
|
||||
widget.style,
|
||||
backgroundColor: widget.backgroundColor,
|
||||
);
|
||||
final span = TextSpan(
|
||||
style: widget.style,
|
||||
children: [
|
||||
for (final element in _elements)
|
||||
_buildElementSpan(
|
||||
element,
|
||||
effectiveLinkStyle,
|
||||
effectiveCoordinateStyle,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
if (PlatformInfo.isDesktop) {
|
||||
final selectable = SelectableText.rich(span);
|
||||
if (widget.onSecondaryTap == null) return selectable;
|
||||
return Listener(
|
||||
onPointerDown: (event) {
|
||||
if (event.buttons & kSecondaryMouseButton != 0) {
|
||||
widget.onSecondaryTap!();
|
||||
}
|
||||
},
|
||||
behavior: HitTestBehavior.translucent,
|
||||
child: selectable,
|
||||
);
|
||||
}
|
||||
|
||||
return Text.rich(span);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user