Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

center emoji properly

laurenspriem42e7ecc58b2f
diff --git a/mobile/apps/photos/lib/ui/rituals/all_rituals_screen.dart b/mobile/apps/photos/lib/ui/rituals/all_rituals_screen.dart
index 9896c549a0..08d5361a81 100644
--- a/mobile/apps/photos/lib/ui/rituals/all_rituals_screen.dart
+++ b/mobile/apps/photos/lib/ui/rituals/all_rituals_screen.dart
@@ -9,6 +9,7 @@ import "package:photos/ui/rituals/delete_ritual_confirmation_sheet.dart";
import "package:photos/ui/rituals/ritual_camera_page.dart";
import "package:photos/ui/rituals/ritual_day_thumbnail.dart";
import "package:photos/ui/rituals/ritual_editor_dialog.dart";
+import "package:photos/ui/rituals/ritual_emoji_icon.dart";
import "package:photos/ui/rituals/ritual_page.dart";
import "package:photos/ui/rituals/start_new_ritual_card.dart";
import "package:photos/utils/navigation_util.dart";
@@ -176,7 +177,7 @@ class _RitualOverviewCard extends StatelessWidget {
borderRadius: BorderRadius.circular(8),
),
child: Center(
- child: Text(
+ child: RitualEmojiIcon(
ritual.icon,
style: textTheme.bodyBold.copyWith(height: 1),
textHeightBehavior: _tightTextHeightBehavior,
diff --git a/mobile/apps/photos/lib/ui/rituals/ritual_emoji_icon.dart b/mobile/apps/photos/lib/ui/rituals/ritual_emoji_icon.dart
new file mode 100644
index 0000000000..66aa3b4f4e
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/rituals/ritual_emoji_icon.dart
@@ -0,0 +1,292 @@
+import "dart:collection";
+import "dart:typed_data";
+import "dart:ui" as ui;
+
+import "package:flutter/widgets.dart";
+
+class RitualEmojiIcon extends StatefulWidget {
+ const RitualEmojiIcon(
+ this.emoji, {
+ super.key,
+ required this.style,
+ this.textHeightBehavior,
+ this.textScaler,
+ });
+
+ final String emoji;
+ final TextStyle style;
+ final TextHeightBehavior? textHeightBehavior;
+ final TextScaler? textScaler;
+
+ @override
+ State<RitualEmojiIcon> createState() => _RitualEmojiIconState();
+}
+
+class _RitualEmojiIconState extends State<RitualEmojiIcon> {
+ static const int _cacheLimit = 256;
+ static final LinkedHashMap<_EmojiCacheKey, Offset> _offsetCache =
+ LinkedHashMap<_EmojiCacheKey, Offset>();
+ static final Map<_EmojiCacheKey, Future<Offset>> _inFlight =
+ <_EmojiCacheKey, Future<Offset>>{};
+
+ Offset _offset = Offset.zero;
+ _EmojiCacheKey? _activeKey;
+
+ @override
+ void didChangeDependencies() {
+ super.didChangeDependencies();
+ _resolveOffset();
+ }
+
+ @override
+ void didUpdateWidget(RitualEmojiIcon oldWidget) {
+ super.didUpdateWidget(oldWidget);
+ if (oldWidget.emoji != widget.emoji ||
+ oldWidget.style != widget.style ||
+ oldWidget.textHeightBehavior != widget.textHeightBehavior ||
+ oldWidget.textScaler != widget.textScaler) {
+ _resolveOffset(reset: true);
+ }
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final resolvedTextScaler =
+ widget.textScaler ?? MediaQuery.textScalerOf(context);
+ final resolvedStyle =
+ DefaultTextStyle.of(context).style.merge(widget.style);
+
+ return Transform.translate(
+ offset: _offset,
+ child: Text(
+ widget.emoji,
+ style: resolvedStyle,
+ textScaler: resolvedTextScaler,
+ textHeightBehavior: widget.textHeightBehavior,
+ ),
+ );
+ }
+
+ void _resolveOffset({bool reset = false}) {
+ if (widget.emoji.isEmpty) {
+ if (_offset != Offset.zero) {
+ setState(() {
+ _offset = Offset.zero;
+ });
+ }
+ return;
+ }
+
+ final resolvedTextScaler =
+ widget.textScaler ?? MediaQuery.textScalerOf(context);
+ final resolvedStyle =
+ DefaultTextStyle.of(context).style.merge(widget.style);
+ final textDirection = Directionality.of(context);
+ final measurementPixelRatio =
+ MediaQuery.devicePixelRatioOf(context).clamp(2.0, 3.5);
+
+ final key = _EmojiCacheKey(
+ emoji: widget.emoji,
+ style: resolvedStyle,
+ textHeightBehavior: widget.textHeightBehavior,
+ textDirection: textDirection,
+ scale: resolvedTextScaler.scale(1),
+ pixelRatio: measurementPixelRatio,
+ );
+
+ if (_activeKey == key && !reset) return;
+ _activeKey = key;
+
+ final cached = _getCachedOffset(key);
+ if (cached != null) {
+ if (_offset != cached) {
+ setState(() {
+ _offset = cached;
+ });
+ }
+ return;
+ }
+
+ if (reset && _offset != Offset.zero) {
+ setState(() {
+ _offset = Offset.zero;
+ });
+ }
+
+ _inFlight[key] ??= _computeVisualCenterOffset(
+ emoji: widget.emoji,
+ style: resolvedStyle,
+ textDirection: textDirection,
+ textScaler: resolvedTextScaler,
+ textHeightBehavior: widget.textHeightBehavior,
+ pixelRatio: measurementPixelRatio,
+ ).then((offset) {
+ _storeCachedOffset(key, offset);
+ return offset;
+ }).catchError((_) {
+ _storeCachedOffset(key, Offset.zero);
+ return Offset.zero;
+ }).whenComplete(() {
+ _inFlight.remove(key);
+ });
+
+ _inFlight[key]!.then((offset) {
+ if (!mounted) return;
+ if (_activeKey != key) return;
+ if (_offset == offset) return;
+ setState(() {
+ _offset = offset;
+ });
+ });
+ }
+
+ Offset? _getCachedOffset(_EmojiCacheKey key) {
+ final value = _offsetCache.remove(key);
+ if (value == null) return null;
+ _offsetCache[key] = value;
+ return value;
+ }
+
+ void _storeCachedOffset(_EmojiCacheKey key, Offset offset) {
+ _offsetCache.remove(key);
+ _offsetCache[key] = offset;
+ if (_offsetCache.length <= _cacheLimit) return;
+ _offsetCache.remove(_offsetCache.keys.first);
+ }
+}
+
+class _EmojiCacheKey {
+ const _EmojiCacheKey({
+ required this.emoji,
+ required this.style,
+ required this.textHeightBehavior,
+ required this.textDirection,
+ required this.scale,
+ required this.pixelRatio,
+ });
+
+ final String emoji;
+ final TextStyle style;
+ final TextHeightBehavior? textHeightBehavior;
+ final TextDirection textDirection;
+ final double scale;
+ final double pixelRatio;
+
+ @override
+ bool operator ==(Object other) {
+ return other is _EmojiCacheKey &&
+ emoji == other.emoji &&
+ style == other.style &&
+ textHeightBehavior == other.textHeightBehavior &&
+ textDirection == other.textDirection &&
+ scale == other.scale &&
+ pixelRatio == other.pixelRatio;
+ }
+
+ @override
+ int get hashCode => Object.hash(
+ emoji,
+ style,
+ textHeightBehavior,
+ textDirection,
+ scale,
+ pixelRatio,
+ );
+}
+
+Future<Offset> _computeVisualCenterOffset({
+ required String emoji,
+ required TextStyle style,
+ required TextDirection textDirection,
+ required TextScaler textScaler,
+ required TextHeightBehavior? textHeightBehavior,
+ required double pixelRatio,
+}) async {
+ if (emoji.isEmpty) return Offset.zero;
+
+ final painter = TextPainter(
+ text: TextSpan(text: emoji, style: style),
+ textDirection: textDirection,
+ textScaler: textScaler,
+ textHeightBehavior: textHeightBehavior,
+ )..layout();
+
+ final fontSize = style.fontSize ?? 14.0;
+ final padding = (fontSize * 1.2).clamp(8.0, 24.0);
+ final logicalSize = Size(
+ painter.width + (padding * 2),
+ painter.height + (padding * 2),
+ );
+
+ final scaledWidth = (logicalSize.width * pixelRatio).ceil();
+ final scaledHeight = (logicalSize.height * pixelRatio).ceil();
+
+ final recorder = ui.PictureRecorder();
+ final canvas = ui.Canvas(recorder);
+ canvas.scale(pixelRatio, pixelRatio);
+ painter.paint(canvas, Offset(padding, padding));
+ final picture = recorder.endRecording();
+
+ final image = await picture.toImage(scaledWidth, scaledHeight);
+ ByteData? byteData;
+ try {
+ byteData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
+ } finally {
+ image.dispose();
+ }
+ if (byteData == null) return Offset.zero;
+
+ final bounds = _opaquePixelBounds(
+ bytes: byteData.buffer.asUint8List(
+ byteData.offsetInBytes,
+ byteData.lengthInBytes,
+ ),
+ width: scaledWidth,
+ height: scaledHeight,
+ );
+ if (bounds == null) return Offset.zero;
+
+ final inkRect = Rect.fromLTRB(
+ (bounds.left / pixelRatio) - padding,
+ (bounds.top / pixelRatio) - padding,
+ (bounds.right / pixelRatio) - padding,
+ (bounds.bottom / pixelRatio) - padding,
+ );
+
+ final painterRect = Offset.zero & painter.size;
+ return painterRect.center - inkRect.center;
+}
+
+Rect? _opaquePixelBounds({
+ required Uint8List bytes,
+ required int width,
+ required int height,
+}) {
+ if (bytes.lengthInBytes < width * height * 4) return null;
+
+ var minX = width;
+ var minY = height;
+ var maxX = -1;
+ var maxY = -1;
+
+ for (var y = 0; y < height; y++) {
+ final rowStart = y * width * 4;
+ for (var x = 0; x < width; x++) {
+ final alpha = bytes[rowStart + (x * 4) + 3];
+ if (alpha == 0) continue;
+ if (x < minX) minX = x;
+ if (y < minY) minY = y;
+ if (x > maxX) maxX = x;
+ if (y > maxY) maxY = y;
+ }
+ }
+
+ if (maxX < 0 || maxY < 0) return null;
+
+ return Rect.fromLTRB(
+ minX.toDouble(),
+ minY.toDouble(),
+ (maxX + 1).toDouble(),
+ (maxY + 1).toDouble(),
+ );
+}
diff --git a/mobile/apps/photos/lib/ui/rituals/ritual_page.dart b/mobile/apps/photos/lib/ui/rituals/ritual_page.dart
index 1eedcb1b85..8328c6fddb 100644
--- a/mobile/apps/photos/lib/ui/rituals/ritual_page.dart
+++ b/mobile/apps/photos/lib/ui/rituals/ritual_page.dart
@@ -21,6 +21,7 @@ import "package:photos/ui/rituals/delete_ritual_confirmation_sheet.dart";
import "package:photos/ui/rituals/ritual_camera_page.dart";
import "package:photos/ui/rituals/ritual_day_thumbnail.dart";
import "package:photos/ui/rituals/ritual_editor_dialog.dart";
+import "package:photos/ui/rituals/ritual_emoji_icon.dart";
import "package:photos/ui/rituals/ritual_share_card.dart";
import "package:photos/ui/viewer/file/detail_page.dart";
import "package:photos/ui/viewer/gallery/collection_page.dart";
@@ -592,7 +593,7 @@ class _RitualHeader extends StatelessWidget {
child: Center(
child: FittedBox(
fit: BoxFit.scaleDown,
- child: Text(
+ child: RitualEmojiIcon(
ritual.icon,
style: textTheme.largeBold.copyWith(height: 1),
textHeightBehavior: _tightTextHeightBehavior,
diff --git a/mobile/apps/photos/lib/ui/rituals/rituals_banner.dart b/mobile/apps/photos/lib/ui/rituals/rituals_banner.dart
index c297a5edea..83e9145c9a 100644
--- a/mobile/apps/photos/lib/ui/rituals/rituals_banner.dart
+++ b/mobile/apps/photos/lib/ui/rituals/rituals_banner.dart
@@ -11,6 +11,7 @@ import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/rituals/all_rituals_screen.dart";
import "package:photos/ui/rituals/ritual_camera_page.dart";
import "package:photos/ui/rituals/ritual_editor_dialog.dart";
+import "package:photos/ui/rituals/ritual_emoji_icon.dart";
import "package:photos/ui/rituals/ritual_page.dart";
import "package:photos/ui/rituals/start_new_ritual_card.dart";
import "package:photos/utils/navigation_util.dart";
@@ -273,7 +274,7 @@ class _RitualIcon extends StatelessWidget {
borderRadius: BorderRadius.circular(10),
),
child: Center(
- child: Text(
+ child: RitualEmojiIcon(
ritual.icon,
style: textTheme.bodyBold.copyWith(height: 1),
textHeightBehavior: _tightTextHeightBehavior,