Merge pull request #305 from cogwheel0/improve-cross-platform-image-paste

feat(chat): prevent duplicate paste image context menu options
This commit is contained in:
cogwheel
2025-12-21 08:57:04 +05:30
committed by GitHub

View File

@@ -319,10 +319,9 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
/// Builds a custom context menu with standard options plus "Paste Image".
///
/// The standard paste only works for text. This adds a "Paste Image"
/// option that uses the pasteboard package to read images from clipboard
/// on both iOS and Android. The option only appears when there's actually
/// an image in the clipboard.
/// Adds a "Paste Image" option when there's an image in the clipboard,
/// but only if the system hasn't already provided one (to avoid duplicates
/// on platforms like iOS that may include their own paste image option).
Widget _buildContextMenu(
BuildContext context,
EditableTextState editableTextState,
@@ -348,6 +347,19 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
final hasImage = imageData != null && imageData.isNotEmpty;
if (hasImage) {
// Check if the system already provides a paste image option
// (e.g., iOS may include one automatically). Look for any button
// with a label containing "image" (case-insensitive) to avoid
// adding a duplicate.
final pasteImageLabel =
AppLocalizations.of(context)?.pasteImage ?? 'Paste Image';
final alreadyHasPasteImage = buttonItems.any(
(item) =>
item.label != null &&
item.label!.toLowerCase().contains('image'),
);
if (!alreadyHasPasteImage) {
// Find the index of the standard Paste button to insert after it
final pasteIndex = buttonItems.indexWhere(
(item) => item.type == ContextMenuButtonType.paste,
@@ -355,7 +367,7 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
// Capture imageData in closure to avoid re-reading clipboard
final pasteImageItem = ContextMenuButtonItem(
label: AppLocalizations.of(context)?.pasteImage ?? 'Paste Image',
label: pasteImageLabel,
onPressed: () {
// Close the context menu first
ContextMenuController.removeAny();
@@ -371,6 +383,7 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
buttonItems.add(pasteImageItem);
}
}
}
return AdaptiveTextSelectionToolbar.buttonItems(
anchors: editableTextState.contextMenuAnchors,
@@ -1621,7 +1634,7 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
.toList(),
onContentInserted: _handleContentInserted,
),
// Custom context menu with "Paste Image" option for iOS
// Custom context menu with "Paste Image" option
contextMenuBuilder: (context, editableTextState) {
return _buildContextMenu(context, editableTextState);
},