|
1 | 1 | import 'package:flutter/material.dart';
|
2 | 2 |
|
| 3 | +import '../api/exception.dart'; |
3 | 4 | import '../api/model/model.dart';
|
4 | 5 | import '../api/route/messages.dart';
|
5 | 6 | import '../model/emoji.dart';
|
6 | 7 | import 'color.dart';
|
| 8 | +import 'dialog.dart'; |
7 | 9 | import 'emoji.dart';
|
| 10 | +import 'inset_shadow.dart'; |
8 | 11 | import 'store.dart';
|
9 | 12 | import 'text.dart';
|
| 13 | +import 'theme.dart'; |
10 | 14 |
|
11 | 15 | /// Emoji-reaction styles that differ between light and dark themes.
|
12 | 16 | class EmojiReactionTheme extends ThemeExtension<EmojiReactionTheme> {
|
@@ -360,3 +364,234 @@ class _TextEmoji extends StatelessWidget {
|
360 | 364 | text);
|
361 | 365 | }
|
362 | 366 | }
|
| 367 | + |
| 368 | + |
| 369 | +void showEmojiPickerSheet({required BuildContext context, required Message message}) { |
| 370 | + final store = PerAccountStoreWidget.of(context); |
| 371 | + |
| 372 | + showModalBottomSheet<void>( |
| 373 | + context: context, |
| 374 | + // Clip.hardEdge looks bad; Clip.antiAliasWithSaveLayer looks pixel-perfect |
| 375 | + // on my iPhone 13 Pro but is marked as "much slower": |
| 376 | + // https://api.flutter.dev/flutter/dart-ui/Clip.html |
| 377 | + clipBehavior: Clip.antiAlias, |
| 378 | + useSafeArea: true, |
| 379 | + isScrollControlled: true, |
| 380 | + builder: (BuildContext _) { |
| 381 | + return SafeArea( |
| 382 | + minimum: const EdgeInsets.only(bottom: 16), |
| 383 | + // For _EmojiPickerItem, and RealmContentNetworkImage used in ImageEmojiWidget. |
| 384 | + child: PerAccountStoreWidget( |
| 385 | + accountId: store.accountId, |
| 386 | + child: EmojiPicker(pageContext: context, message: message))); |
| 387 | + }); |
| 388 | +} |
| 389 | + |
| 390 | +class EmojiPicker extends StatefulWidget { |
| 391 | + const EmojiPicker({ |
| 392 | + super.key, |
| 393 | + required this.pageContext, |
| 394 | + required this.message, |
| 395 | + }); |
| 396 | + |
| 397 | + final BuildContext pageContext; |
| 398 | + final Message message; |
| 399 | + |
| 400 | + @override |
| 401 | + State<EmojiPicker> createState() => _EmojiPickerState(); |
| 402 | +} |
| 403 | + |
| 404 | +class _EmojiPickerState extends State<EmojiPicker> with PerAccountStoreAwareStateMixin<EmojiPicker> { |
| 405 | + late List<EmojiCandidate> availableEmojiCandidates; |
| 406 | + List<EmojiCandidate>? searchResults; |
| 407 | + |
| 408 | + @override |
| 409 | + void onNewStore() { |
| 410 | + final store = PerAccountStoreWidget.of(context); |
| 411 | + final allEmojiCandidates = store.allEmojiCandidates(); |
| 412 | + |
| 413 | + bool isPopularUnicodeEmoji(EmojiDisplay candidateEmojiDisplay) { |
| 414 | + if (candidateEmojiDisplay is! UnicodeEmojiDisplay) return false; |
| 415 | + return popularUnicodeEmojis.any( |
| 416 | + (emoji) => emoji.emojiUnicode == candidateEmojiDisplay.emojiUnicode); |
| 417 | + } |
| 418 | + |
| 419 | + bool isSelfVoted(EmojiCandidate candidate) { |
| 420 | + return widget.message.reactions?.aggregated.any((reactionWithVotes) => |
| 421 | + reactionWithVotes.reactionType == candidate.emojiType |
| 422 | + && reactionWithVotes.emojiCode == candidate.emojiCode |
| 423 | + && reactionWithVotes.userIds.contains(store.selfUserId) |
| 424 | + ) ?? false; |
| 425 | + } |
| 426 | + |
| 427 | + availableEmojiCandidates = allEmojiCandidates |
| 428 | + .where((candidate) => |
| 429 | + !isSelfVoted(candidate) && !isPopularUnicodeEmoji(candidate.emojiDisplay)) |
| 430 | + .toList(growable: false); |
| 431 | + searchResults = null; |
| 432 | + } |
| 433 | + |
| 434 | + // Adapted from web/src/emoji_picker.ts |
| 435 | + void _filterEmojis(String query) { |
| 436 | + if (query.isEmpty) { |
| 437 | + setState(() { searchResults = null; }); |
| 438 | + return; |
| 439 | + } |
| 440 | + |
| 441 | + final results = <EmojiCandidate>[]; |
| 442 | + final searchTerms = query.toLowerCase().split(' '); |
| 443 | + for (final candidate in availableEmojiCandidates) { |
| 444 | + for (final alias in [candidate.emojiName, ...candidate.aliases]) { |
| 445 | + if (searchTerms.every((e) => alias.contains(e))) { |
| 446 | + results.add(candidate.copyWith( |
| 447 | + emojiName: alias, |
| 448 | + aliases: const [])); |
| 449 | + break; // We only need the first matching alias per emoji. |
| 450 | + } |
| 451 | + } |
| 452 | + |
| 453 | + // Using query instead of searchTerms because it's possible multiple |
| 454 | + // emojis were input without being separated by spaces. |
| 455 | + final emojiDisplay = candidate.emojiDisplay; |
| 456 | + if (emojiDisplay is UnicodeEmojiDisplay && query.contains(emojiDisplay.emojiUnicode)) { |
| 457 | + results.add(candidate.copyWith( |
| 458 | + emojiName: candidate.emojiName, |
| 459 | + aliases: const [])); |
| 460 | + } |
| 461 | + } |
| 462 | + |
| 463 | + // TODO sort emojis |
| 464 | + |
| 465 | + setState(() { searchResults = results; }); |
| 466 | + } |
| 467 | + |
| 468 | + @override |
| 469 | + Widget build(BuildContext context) { |
| 470 | + final designVariables = DesignVariables.of(context); |
| 471 | + |
| 472 | + final emojiList = searchResults ?? availableEmojiCandidates; |
| 473 | + |
| 474 | + return Column(children: [ |
| 475 | + Padding( |
| 476 | + padding: const EdgeInsets.only(left: 8, top: 8), |
| 477 | + child: Row(children: [ |
| 478 | + Flexible(child: TextField( |
| 479 | + onChanged: (query) => _filterEmojis(query), |
| 480 | + autofocus: false, |
| 481 | + decoration: InputDecoration( |
| 482 | + hintText: 'Search emoji', |
| 483 | + contentPadding: const EdgeInsets.only(left: 10, top: 6), |
| 484 | + filled: true, |
| 485 | + fillColor: designVariables.bgSearchInput, |
| 486 | + border: OutlineInputBorder( |
| 487 | + borderRadius: BorderRadius.circular(10), |
| 488 | + borderSide: BorderSide.none)), |
| 489 | + style: const TextStyle(fontSize: 19, height: 26 / 19) |
| 490 | + .merge(weightVariableTextStyle(context, wght: 400)))), |
| 491 | + TextButton( |
| 492 | + onPressed: () => Navigator.pop(context), |
| 493 | + style: TextButton.styleFrom( |
| 494 | + padding: EdgeInsets.zero, |
| 495 | + splashFactory: NoSplash.splashFactory, |
| 496 | + foregroundColor: designVariables.contextMenuItemText, |
| 497 | + ).copyWith(backgroundColor: WidgetStateColor.resolveWith((states) => |
| 498 | + states.contains(WidgetState.pressed) |
| 499 | + ? designVariables.contextMenuItemBg.withFadedAlpha(0.20) |
| 500 | + : Colors.transparent)), |
| 501 | + child: Text('Close', style: const TextStyle(fontSize: 20, height: 30 / 20) |
| 502 | + .merge(weightVariableTextStyle(context, wght: 400)))), |
| 503 | + ])), |
| 504 | + Expanded(child: InsetShadowBox( |
| 505 | + top: 8, bottom: 8, |
| 506 | + color: designVariables.bgContextMenu, |
| 507 | + child: ListView.builder( |
| 508 | + padding: const EdgeInsets.symmetric(vertical: 8), |
| 509 | + itemCount: emojiList.length, |
| 510 | + itemBuilder: (context, i) => EmojiPickerItem( |
| 511 | + pageContext: widget.pageContext, |
| 512 | + candidate: emojiList[i], |
| 513 | + message: widget.message), |
| 514 | + ), |
| 515 | + )), |
| 516 | + ]); |
| 517 | + } |
| 518 | +} |
| 519 | + |
| 520 | +@visibleForTesting |
| 521 | +class EmojiPickerItem extends StatelessWidget { |
| 522 | + const EmojiPickerItem({ |
| 523 | + super.key, |
| 524 | + required this.pageContext, |
| 525 | + required this.candidate, |
| 526 | + required this.message, |
| 527 | + }); |
| 528 | + |
| 529 | + final BuildContext pageContext; |
| 530 | + final EmojiCandidate candidate; |
| 531 | + final Message message; |
| 532 | + |
| 533 | + static const _size = 24.0; |
| 534 | + static const _notoColorEmojiTextSize = 24.0; |
| 535 | + |
| 536 | + void _onPressed() async { |
| 537 | + String? errorMessage; |
| 538 | + try { |
| 539 | + await addReaction( |
| 540 | + PerAccountStoreWidget.of(pageContext).connection, |
| 541 | + messageId: message.id, |
| 542 | + reactionType: candidate.emojiType, |
| 543 | + emojiCode: candidate.emojiCode, |
| 544 | + emojiName: candidate.emojiName, |
| 545 | + ); |
| 546 | + if (pageContext.mounted) Navigator.pop(pageContext); // Emoji picker |
| 547 | + if (pageContext.mounted) Navigator.pop(pageContext); // Context menu |
| 548 | + } catch (e) { |
| 549 | + if (!pageContext.mounted) return; |
| 550 | + |
| 551 | + switch (e) { |
| 552 | + case ZulipApiException(): |
| 553 | + errorMessage = e.message; |
| 554 | + // TODO(#741) specific messages for common errors, like network errors |
| 555 | + // (support with reusable code) |
| 556 | + default: |
| 557 | + } |
| 558 | + |
| 559 | + showErrorDialog(context: pageContext, |
| 560 | + title: 'Adding reaction failed', |
| 561 | + message: errorMessage); |
| 562 | + } |
| 563 | + } |
| 564 | + |
| 565 | + @override |
| 566 | + Widget build(BuildContext context) { |
| 567 | + final store = PerAccountStoreWidget.of(context); |
| 568 | + |
| 569 | + final emojiDisplay = candidate.emojiDisplay.resolve(store.userSettings); |
| 570 | + final Widget? glyph = switch (emojiDisplay) { |
| 571 | + ImageEmojiDisplay() => |
| 572 | + ImageEmojiWidget(size: _size, emojiDisplay: emojiDisplay), |
| 573 | + UnicodeEmojiDisplay() => |
| 574 | + UnicodeEmojiWidget( |
| 575 | + size: _size, notoColorEmojiTextSize: _notoColorEmojiTextSize, |
| 576 | + emojiDisplay: emojiDisplay), |
| 577 | + TextEmojiDisplay() => null, // The text is already shown separately. |
| 578 | + }; |
| 579 | + |
| 580 | + final label = candidate.aliases.isEmpty |
| 581 | + ? candidate.emojiName |
| 582 | + : [candidate.emojiName, ...candidate.aliases].join(", "); // TODO(#1080) |
| 583 | + |
| 584 | + return TextButton.icon( |
| 585 | + onPressed: _onPressed, |
| 586 | + icon: glyph, |
| 587 | + style: MenuItemButton.styleFrom( |
| 588 | + alignment: Alignment.centerLeft, |
| 589 | + shape: const RoundedRectangleBorder(), |
| 590 | + splashFactory: NoSplash.splashFactory), |
| 591 | + label: Text(label, |
| 592 | + maxLines: 2, |
| 593 | + overflow: TextOverflow.ellipsis, |
| 594 | + style: const TextStyle(fontSize: 17, height: 24 / 17) |
| 595 | + .merge(weightVariableTextStyle(context, wght: 400)))); |
| 596 | + } |
| 597 | +} |
0 commit comments