SDL: Avoid textinput events when pasting from clipboard on iOS. (c67ff)

From c67ff82377c0acbf1d9b32c91f22e52718a27f0b Mon Sep 17 00:00:00 2001
From: Deve <[EMAIL REDACTED]>
Date: Wed, 4 Jan 2023 22:11:01 +0100
Subject: [PATCH] Avoid textinput events when pasting from clipboard on iOS.

I handle command+C and command+V shortcuts for copy/paste from clipboard using
SDL_GetClipboardText/SDL_SetClipboardText. But on iOS command+V shortcut is
also handled by system, so that I also get textinput event with that clipboard
text. And thus the application gets this clipboard text twice (from
SDL_GetClipboardText and from textinput event).

I assume that intended behavior is that command+V shouldn't generate textinput
events. At least as far as I know ctrl+V on other platforms does nothing. This
commit disables paste action for UITextField, so that textinput event isn't
generated anymore.

(cherry picked from commit eddaf870f5b57bbdb7f1cf08bacc4f997806d9a7)
---
 src/video/uikit/SDL_uikitviewcontroller.h |  4 ++++
 src/video/uikit/SDL_uikitviewcontroller.m | 15 +++++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/video/uikit/SDL_uikitviewcontroller.h b/src/video/uikit/SDL_uikitviewcontroller.h
index e9f346a22198b..925f0b2711121 100644
--- a/src/video/uikit/SDL_uikitviewcontroller.h
+++ b/src/video/uikit/SDL_uikitviewcontroller.h
@@ -33,6 +33,10 @@
 #define SDLRootViewController UIViewController
 #endif
 
+@interface SDLUITextField : UITextField
+- (BOOL)canPerformAction:(SEL)action withSender:(id)sender;
+@end
+
 #ifdef SDL_IPHONE_KEYBOARD
 @interface SDL_uikitviewcontroller : SDLRootViewController <UITextFieldDelegate>
 #else
diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m
index 1d79c7a40ddc0..46fbcdeff344f 100644
--- a/src/video/uikit/SDL_uikitviewcontroller.m
+++ b/src/video/uikit/SDL_uikitviewcontroller.m
@@ -64,6 +64,17 @@
 }
 #endif
 
+@implementation SDLUITextField : UITextField
+- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
+{
+	if (action == @selector(paste:)) {
+		return NO;
+	}
+
+	return [super canPerformAction:action withSender:sender];
+}
+@end
+
 @implementation SDL_uikitviewcontroller {
     CADisplayLink *displayLink;
     int animationInterval;
@@ -71,7 +82,7 @@ @implementation SDL_uikitviewcontroller {
     void *animationCallbackParam;
 
 #ifdef SDL_IPHONE_KEYBOARD
-    UITextField *textField;
+    SDLUITextField *textField;
     BOOL hardwareKeyboard;
     BOOL showingKeyboard;
     BOOL hidingKeyboard;
@@ -259,7 +270,7 @@ - (BOOL)prefersPointerLocked
 - (void)initKeyboard
 {
     obligateForBackspace = @"                                                                "; /* 64 space */
-    textField = [[UITextField alloc] initWithFrame:CGRectZero];
+    textField = [[SDLUITextField alloc] initWithFrame:CGRectZero];
     textField.delegate = self;
     /* placeholder so there is something to delete! */
     textField.text = obligateForBackspace;