Skip to content

Commit ecd3138

Browse files
committed
emoji [nfc]: Factor out ImageEmojiWidget
1 parent 54b1add commit ecd3138

File tree

2 files changed

+61
-24
lines changed

2 files changed

+61
-24
lines changed

lib/widgets/emoji.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter/widgets.dart';
3+
4+
import '../model/emoji.dart';
5+
import 'content.dart';
6+
7+
class ImageEmojiWidget extends StatelessWidget {
8+
const ImageEmojiWidget({
9+
super.key,
10+
required this.emojiDisplay,
11+
required this.size,
12+
this.textScaler = TextScaler.noScaling,
13+
this.errorBuilder,
14+
});
15+
16+
final ImageEmojiDisplay emojiDisplay;
17+
18+
/// The base width and height to use for the emoji.
19+
///
20+
/// This will be scaled by [textScaler].
21+
final double size;
22+
23+
/// The text scaler to apply to [size].
24+
///
25+
/// Defaults to [TextScaler.noScaling].
26+
final TextScaler textScaler;
27+
28+
final ImageErrorWidgetBuilder? errorBuilder;
29+
30+
@override
31+
Widget build(BuildContext context) {
32+
// Some people really dislike animated emoji.
33+
final doNotAnimate =
34+
// From reading code, this doesn't actually get set on iOS:
35+
// https://github.com/zulip/zulip-flutter/pull/410#discussion_r1408522293
36+
MediaQuery.disableAnimationsOf(context)
37+
|| (defaultTargetPlatform == TargetPlatform.iOS
38+
// TODO(upstream) On iOS 17+ (new in 2023), there's a more closely
39+
// relevant setting than "reduce motion". It's called "auto-play
40+
// animated images", and we should file an issue to expose it.
41+
// See GitHub comment linked above.
42+
&& WidgetsBinding.instance.platformDispatcher.accessibilityFeatures.reduceMotion);
43+
44+
final size = textScaler.scale(this.size);
45+
46+
final resolvedUrl = doNotAnimate
47+
? (emojiDisplay.resolvedStillUrl ?? emojiDisplay.resolvedUrl)
48+
: emojiDisplay.resolvedUrl;
49+
50+
return RealmContentNetworkImage(
51+
width: size, height: size,
52+
errorBuilder: errorBuilder,
53+
resolvedUrl);
54+
}
55+
}

lib/widgets/emoji_reaction.dart

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import '../api/model/model.dart';
55
import '../api/route/messages.dart';
66
import '../model/emoji.dart';
77
import 'color.dart';
8-
import 'content.dart';
8+
import 'emoji.dart';
99
import 'store.dart';
1010
import 'text.dart';
1111

@@ -361,29 +361,11 @@ class _ImageEmoji extends StatelessWidget {
361361

362362
@override
363363
Widget build(BuildContext context) {
364-
// Some people really dislike animated emoji.
365-
final doNotAnimate =
366-
// From reading code, this doesn't actually get set on iOS:
367-
// https://github.com/zulip/zulip-flutter/pull/410#discussion_r1408522293
368-
MediaQuery.disableAnimationsOf(context)
369-
|| (defaultTargetPlatform == TargetPlatform.iOS
370-
// TODO(upstream) On iOS 17+ (new in 2023), there's a more closely
371-
// relevant setting than "reduce motion". It's called "auto-play
372-
// animated images", and we should file an issue to expose it.
373-
// See GitHub comment linked above.
374-
&& WidgetsBinding.instance.platformDispatcher.accessibilityFeatures.reduceMotion);
375-
376-
final resolvedUrl = doNotAnimate
377-
? (emojiDisplay.resolvedStillUrl ?? emojiDisplay.resolvedUrl)
378-
: emojiDisplay.resolvedUrl;
379-
380-
// Unicode and text emoji get scaled; it would look weird if image emoji didn't.
381-
final size = _squareEmojiScalerClamped(context).scale(_squareEmojiSize);
382-
383-
return RealmContentNetworkImage(
384-
resolvedUrl,
385-
width: size,
386-
height: size,
364+
return ImageEmojiWidget(
365+
size: _squareEmojiSize,
366+
// Unicode and text emoji get scaled; it would look weird if image emoji didn't.
367+
textScaler: _squareEmojiScalerClamped(context),
368+
emojiDisplay: emojiDisplay,
387369
errorBuilder: (context, _, __) => _TextEmoji(
388370
emojiDisplay: TextEmojiDisplay(emojiName: emojiName), selected: selected),
389371
);

0 commit comments

Comments
 (0)