Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
fix: launch review issues for non-search-bar emoji picker (#7654)
Mornb52f681e3cec
diff --git a/frontend/appflowy_flutter/lib/plugins/emoji/emoji_handler.dart b/frontend/appflowy_flutter/lib/plugins/emoji/emoji_handler.dartindex ec86de7e8d..2109414ff4 100644--- a/frontend/appflowy_flutter/lib/plugins/emoji/emoji_handler.dart+++ b/frontend/appflowy_flutter/lib/plugins/emoji/emoji_handler.dart@@ -2,14 +2,16 @@ import 'dart:async';import 'dart:math';import 'package:appflowy/plugins/base/emoji/emoji_picker.dart';+import 'package:appflowy/shared/icon_emoji_picker/emoji_skin_tone.dart';import 'package:appflowy_editor/appflowy_editor.dart';+import 'package:flowy_infra/size.dart';import 'package:flowy_infra_ui/style_widget/button.dart';import 'package:flowy_infra_ui/style_widget/text.dart';+import 'package:flowy_infra_ui/widget/flowy_tooltip.dart';import 'package:flutter/material.dart';import 'package:flutter/services.dart';import 'package:flutter_emoji_mart/flutter_emoji_mart.dart';-import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';import 'emoji_menu.dart';@@ -38,27 +40,31 @@ class EmojiHandler extends StatefulWidget {}class _EmojiHandlerState extends State<EmojiHandler> {- final _focusNode = FocusNode(debugLabel: 'emoji_menu_handler');- final ItemScrollController controller = ItemScrollController();+ final focusNode = FocusNode(debugLabel: 'emoji_menu_handler');+ final scrollController = ScrollController();late EmojiData emojiData;final List<Emoji> searchedEmojis = [];bool loaded = false;int invalidCounter = 0;late int startOffset;String _search = '';+ double emojiHeight = 36.0;+ final configuration = EmojiPickerConfiguration(+ defaultSkinTone: lastSelectedEmojiSkinTone ?? EmojiSkinTone.none,+ );set search(String search) {_search = search;_doSearch();}- final ValueNotifier<int> _selectedIndexNotifier = ValueNotifier(0);+ final ValueNotifier<int> selectedIndexNotifier = ValueNotifier(0);@overridevoid initState() {super.initState();WidgetsBinding.instance.addPostFrameCallback(- (_) => _focusNode.requestFocus(),+ (_) => focusNode.requestFocus(),);startOffset = widget.editorState.selection?.endIndex ?? 0;@@ -77,8 +83,9 @@ class _EmojiHandlerState extends State<EmojiHandler> {@overridevoid dispose() {- _focusNode.dispose();- _selectedIndexNotifier.dispose();+ focusNode.dispose();+ selectedIndexNotifier.dispose();+ scrollController.dispose();super.dispose();}@@ -86,10 +93,11 @@ class _EmojiHandlerState extends State<EmojiHandler> {Widget build(BuildContext context) {final noEmojis = searchedEmojis.isEmpty;return Focus(- focusNode: _focusNode,+ focusNode: focusNode,onKeyEvent: onKeyEvent,child: Container(- constraints: const BoxConstraints(maxHeight: 400, maxWidth: 300),+ constraints: const BoxConstraints(maxHeight: 392, maxWidth: 360),+ padding: const EdgeInsets.symmetric(vertical: 16),decoration: BoxDecoration(borderRadius: BorderRadius.circular(6.0),color: Theme.of(context).cardColor,@@ -101,52 +109,73 @@ class _EmojiHandlerState extends State<EmojiHandler> {),],),- child: noEmojis- ? SizedBox(- width: 400,- height: 40,- child: Center(- child: SizedBox.square(- dimension: 20,- child: CircularProgressIndicator(),+ child: noEmojis ? buildLoading() : buildEmojis(),+ ),+ );+ }++ Widget buildLoading() {+ return SizedBox(+ width: 400,+ height: 40,+ child: Center(+ child: SizedBox.square(+ dimension: 20,+ child: CircularProgressIndicator(),+ ),+ ),+ );+ }++ Widget buildEmojis() {+ return SizedBox(+ height:+ (searchedEmojis.length / configuration.perLine).ceil() * emojiHeight,+ child: GridView.builder(+ controller: scrollController,+ itemCount: searchedEmojis.length,+ padding: const EdgeInsets.symmetric(horizontal: 16),+ gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(+ crossAxisCount: configuration.perLine,+ ),+ itemBuilder: (context, index) {+ final currentEmoji = searchedEmojis[index];+ final emojiId = currentEmoji.id;+ final emoji = emojiData.getEmojiById(+ emojiId,+ skinTone: configuration.defaultSkinTone,+ );+ return ValueListenableBuilder(+ valueListenable: selectedIndexNotifier,+ builder: (context, value, child) {+ final isSelected = value == index;+ return SizedBox.square(+ dimension: emojiHeight,+ child: FlowyButton(+ isSelected: isSelected,+ margin: EdgeInsets.zero,+ radius: Corners.s8Border,+ text: ManualTooltip(+ key: ValueKey('$emojiId-$isSelected'),+ message: currentEmoji.name,+ showAutomaticlly: isSelected,+ preferBelow: false,+ child: FlowyText.emoji(+ emoji,+ fontSize: configuration.emojiSize,+ ),),+ onTap: () => onSelect(index),),- )- : ScrollablePositionedList.builder(- itemCount: searchedEmojis.length,- itemScrollController: controller,- padding: EdgeInsets.all(8),- shrinkWrap: true,- physics: const ClampingScrollPhysics(),- itemBuilder: (ctx, index) {- return ValueListenableBuilder(- valueListenable: _selectedIndexNotifier,- builder: (context, value, __) {- final selectedEmoji = searchedEmojis[index];- final displayedEmoji =- emojiData.getEmojiById(selectedEmoji.id);- final isSelected = value == index;- return SizedBox(- height: 32,- child: FlowyButton(- text: FlowyText.medium(- '$displayedEmoji ${selectedEmoji.name}',- lineHeight: 1.0,- overflow: TextOverflow.ellipsis,- ),- isSelected: isSelected,- onTap: () => onSelect(index),- ),- );- },- );- },- ),+ );+ },+ );+ },),);}- void changeSelectedIndex(int index) => _selectedIndexNotifier.value = index;+ void changeSelectedIndex(int index) => selectedIndexNotifier.value = index;void loadEmojis(EmojiData data) {emojiData = data;@@ -170,6 +199,7 @@ class _EmojiHandlerState extends State<EmojiHandler> {searchedEmojis.clear();searchedEmojis.addAll(searchEmojiData.emojis.values);changeSelectedIndex(0);+ _scrollToItem();});if (searchedEmojis.isEmpty) {widget.onDismiss.call();@@ -177,17 +207,19 @@ class _EmojiHandlerState extends State<EmojiHandler> {}KeyEventResult onKeyEvent(focus, KeyEvent event) {- if (event is! KeyDownEvent) {+ if (event is! KeyDownEvent && event is! KeyRepeatEvent) {return KeyEventResult.ignored;}const moveKeys = [LogicalKeyboardKey.arrowUp,LogicalKeyboardKey.arrowDown,+ LogicalKeyboardKey.arrowLeft,+ LogicalKeyboardKey.arrowRight,];if (event.logicalKey == LogicalKeyboardKey.enter) {- onSelect(_selectedIndexNotifier.value);+ onSelect(selectedIndexNotifier.value);return KeyEventResult.handled;} else if (event.logicalKey == LogicalKeyboardKey.escape) {// Workaround to bring focus back to editor@@ -214,11 +246,7 @@ class _EmojiHandlerState extends State<EmojiHandler> {return KeyEventResult.handled;} else if (event.character != null &&- ![- ...moveKeys,- LogicalKeyboardKey.arrowLeft,- LogicalKeyboardKey.arrowRight,- ].contains(event.logicalKey)) {+ !moveKeys.contains(event.logicalKey)) {/// Prevents dismissal of context menu by notifying the parent/// that the selection change occurred from the handler.widget.onSelectionUpdate();@@ -259,7 +287,7 @@ class _EmojiHandlerState extends State<EmojiHandler> {/// focus goes back to the editor, this makes sure this handler/// receives the next keypress.///- _focusNode.requestFocus();+ focusNode.requestFocus();return KeyEventResult.handled;}@@ -304,41 +332,63 @@ class _EmojiHandlerState extends State<EmojiHandler> {}void _moveSelection(LogicalKeyboardKey key) {- bool didChange = false;- final index = _selectedIndexNotifier.value;- if (key == LogicalKeyboardKey.arrowUp ||- (key == LogicalKeyboardKey.tab &&- HardwareKeyboard.instance.isShiftPressed)) {+ final index = selectedIndexNotifier.value,+ perLine = configuration.perLine,+ remainder = index % perLine,+ length = searchedEmojis.length,+ currentLine = index ~/ perLine,+ maxLine = (length / perLine).ceil();++ final heightBefore = currentLine * emojiHeight;+ if (key == LogicalKeyboardKey.arrowUp) {+ if (currentLine == 0) {+ final exceptLine = max(0, maxLine - 1);+ changeSelectedIndex(min(exceptLine * perLine + remainder, length - 1));+ } else if (currentLine > 0) {+ changeSelectedIndex(index - perLine);+ }+ } else if (key == LogicalKeyboardKey.arrowDown) {+ if (currentLine == maxLine - 1) {+ changeSelectedIndex(remainder);+ } else if (currentLine < maxLine - 1) {+ changeSelectedIndex(min(index + perLine, length - 1));+ }+ } else if (key == LogicalKeyboardKey.arrowLeft) {if (index == 0) {- changeSelectedIndex(max(0, searchedEmojis.length - 1));- didChange = true;+ changeSelectedIndex(length - 1);} else if (index > 0) {changeSelectedIndex(index - 1);- didChange = true;}- } else if ([LogicalKeyboardKey.arrowDown, LogicalKeyboardKey.tab]- .contains(key)) {- if (index < searchedEmojis.length - 1) {- changeSelectedIndex(index + 1);- didChange = true;- } else if (index == searchedEmojis.length - 1) {+ } else if (key == LogicalKeyboardKey.arrowRight) {+ if (index == length - 1) {changeSelectedIndex(0);- didChange = true;+ } else if (index < length - 1) {+ changeSelectedIndex(index + 1);}}+ final heightAfter =+ (selectedIndexNotifier.value ~/ configuration.perLine) * emojiHeight;- if (mounted && didChange) {- _scrollToItem();+ if (mounted && (heightAfter != heightBefore)) {+ WidgetsBinding.instance.addPostFrameCallback((_) {+ _scrollToItem();+ });}}void _scrollToItem() {final noEmojis = searchedEmojis.isEmpty;- if (noEmojis) return;- controller.scrollTo(- index: _selectedIndexNotifier.value,- duration: const Duration(milliseconds: 200),- alignment: 0.5,+ if (noEmojis || !mounted) return;+ final currentItem = selectedIndexNotifier.value;+ final exceptHeight = (currentItem ~/ configuration.perLine) * emojiHeight;+ final maxExtent = scrollController.position.maxScrollExtent;+ final jumpTo = (exceptHeight - maxExtent > 10 * emojiHeight)+ ? exceptHeight+ : min(exceptHeight, maxExtent);+ scrollController.animateTo(+ jumpTo,+ duration: Duration(milliseconds: 300),+ curve: Curves.linear,);}diff --git a/frontend/appflowy_flutter/lib/plugins/emoji/emoji_menu.dart b/frontend/appflowy_flutter/lib/plugins/emoji/emoji_menu.dartindex 58269a32d5..8a9ab472a6 100644--- a/frontend/appflowy_flutter/lib/plugins/emoji/emoji_menu.dart+++ b/frontend/appflowy_flutter/lib/plugins/emoji/emoji_menu.dart@@ -16,14 +16,19 @@ class EmojiMenu extends EmojiMenuService {required this.editorState,this.startCharAmount = 1,this.cancelBySpaceHandler,+ this.menuHeight = 400,+ this.menuWidth = 300,});final BuildContext context;final EditorState editorState;+ final double menuHeight;+ final double menuWidth;final bool Function()? cancelBySpaceHandler;final int startCharAmount;-+ Offset _offset = Offset.zero;+ Alignment _alignment = Alignment.topLeft;OverlayEntry? _menuEntry;bool selectionChangedByMenu = false;@@ -62,41 +67,11 @@ class EmojiMenu extends EmojiMenuService {return;}- const menuHeight = 400.0, menuWidth = 300.0;- const Offset menuOffset = Offset(0, 10);- final Offset editorOffset =- editorState.renderBox?.localToGlobal(Offset.zero) ?? Offset.zero;final Size editorSize = editorState.renderBox!.size;- final editorHeight = editorSize.height, editorWidth = editorSize.width;- // Default to opening the overlay below- Alignment alignment = Alignment.topLeft;-- final firstRect = selectionRects.first;- Offset offset = firstRect.bottomRight + menuOffset;-- // Show above- if (offset.dy + menuHeight >= editorOffset.dy + editorSize.height) {- offset = firstRect.topRight - menuOffset;- alignment = Alignment.bottomLeft;- offset = Offset(- offset.dx,- editorHeight - offset.dy,- );- }- // Show on the left- if (offset.dx > (editorWidth - menuWidth)) {- alignment = alignment == Alignment.topLeft- ? Alignment.topRight- : Alignment.bottomRight;+ calculateSelectionMenuOffset(selectionRects.first);- offset = Offset(- editorWidth - offset.dx,- offset.dy,- );- }-- final (left, top, right, bottom) = _getPosition(alignment, offset);+ final (left, top, right, bottom) = _getPosition();_menuEntry = OverlayEntry(builder: (context) => SizedBox(@@ -175,30 +150,77 @@ class EmojiMenu extends EmojiMenuService {selectionChangedByMenu = false;}- (double? left, double? top, double? right, double? bottom) _getPosition(- Alignment alignment,- Offset offset,- ) {+ (double? left, double? top, double? right, double? bottom) _getPosition() {double? left, top, right, bottom;- switch (alignment) {+ switch (_alignment) {case Alignment.topLeft:- left = offset.dx;- top = offset.dy;+ left = _offset.dx;+ top = _offset.dy;break;case Alignment.bottomLeft:- left = offset.dx;- bottom = offset.dy;+ left = _offset.dx;+ bottom = _offset.dy;break;case Alignment.topRight:- right = offset.dx;- top = offset.dy;+ right = _offset.dx;+ top = _offset.dy;break;case Alignment.bottomRight:- right = offset.dx;- bottom = offset.dy;+ right = _offset.dx;+ bottom = _offset.dy;break;}return (left, top, right, bottom);}++ void calculateSelectionMenuOffset(Rect rect) {+ // Workaround: We can customize the padding through the [EditorStyle],+ // but the coordinates of overlay are not properly converted currently.+ // Just subtract the padding here as a result.+ const menuOffset = Offset(0, 10);+ final editorOffset =+ editorState.renderBox?.localToGlobal(Offset.zero) ?? Offset.zero;+ final editorHeight = editorState.renderBox!.size.height;+ final editorWidth = editorState.renderBox!.size.width;++ // show below default+ _alignment = Alignment.topLeft;+ final bottomRight = rect.bottomRight;+ final topRight = rect.topRight;+ var offset = bottomRight + menuOffset;+ _offset = Offset(+ offset.dx,+ offset.dy,+ );++ // show above+ if (offset.dy + menuHeight >= editorOffset.dy + editorHeight) {+ offset = topRight - menuOffset;+ _alignment = Alignment.bottomLeft;++ _offset = Offset(+ offset.dx,+ editorHeight + editorOffset.dy - offset.dy,+ );+ }++ // show on right+ if (_offset.dx + menuWidth < editorOffset.dx + editorWidth) {+ _offset = Offset(+ _offset.dx,+ _offset.dy,+ );+ } else if (offset.dx - editorOffset.dx > menuWidth) {+ // show on left+ _alignment = _alignment == Alignment.topLeft+ ? Alignment.topRight+ : Alignment.bottomRight;++ _offset = Offset(+ editorWidth - _offset.dx + editorOffset.dx,+ _offset.dy,+ );+ }+ }}diff --git a/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/widget/flowy_tooltip.dart b/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/widget/flowy_tooltip.dartindex c770452fcd..8813cad09e 100644--- a/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/widget/flowy_tooltip.dart+++ b/frontend/appflowy_flutter/packages/flowy_infra_ui/lib/widget/flowy_tooltip.dart@@ -50,6 +50,72 @@ class FlowyTooltip extends StatelessWidget {}}+class ManualTooltip extends StatefulWidget {+ const ManualTooltip({+ super.key,+ this.message,+ this.richMessage,+ this.preferBelow,+ this.margin,+ this.verticalOffset,+ this.padding,+ this.showAutomaticlly = false,+ this.child,+ });++ final String? message;+ final InlineSpan? richMessage;+ final bool? preferBelow;+ final EdgeInsetsGeometry? margin;+ final Widget? child;+ final double? verticalOffset;+ final EdgeInsets? padding;+ final bool showAutomaticlly;++ @override+ State<ManualTooltip> createState() => _ManualTooltipState();+}++class _ManualTooltipState extends State<ManualTooltip> {+ final key = GlobalKey<TooltipState>();++ @override+ void initState() {+ if (widget.showAutomaticlly) {+ WidgetsBinding.instance.addPostFrameCallback((_) {+ if (mounted) key.currentState?.ensureTooltipVisible();+ });+ }+ super.initState();+ }+++ @override+ Widget build(BuildContext context) {+ return Tooltip(+ key: key,+ margin: widget.margin,+ verticalOffset: widget.verticalOffset ?? 16.0,+ triggerMode: widget.showAutomaticlly ? TooltipTriggerMode.manual : null,+ padding: widget.padding ??+ const EdgeInsets.symmetric(+ horizontal: 12.0,+ vertical: 8.0,+ ),+ decoration: BoxDecoration(+ color: context.tooltipBackgroundColor(),+ borderRadius: BorderRadius.circular(10.0),+ ),+ waitDuration: _tooltipWaitDuration,+ message: widget.message,+ textStyle: widget.message != null ? context.tooltipTextStyle() : null,+ richMessage: widget.richMessage,+ preferBelow: widget.preferBelow,+ child: widget.child,+ );+ }+}+extension FlowyToolTipExtension on BuildContext {double tooltipFontSize() => 14.0;