Keyboard.mm 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. #include "Keyboard.h"
  2. #include "DisplayManager.h"
  3. #include "UnityAppController.h"
  4. #include "UnityForwardDecls.h"
  5. #include <string>
  6. #ifndef FILTER_EMOJIS_IOS_KEYBOARD
  7. #define FILTER_EMOJIS_IOS_KEYBOARD 1
  8. #endif
  9. static KeyboardDelegate* _keyboard = nil;
  10. static bool _shouldHideInput = false;
  11. static bool _shouldHideInputChanged = false;
  12. static const unsigned kToolBarHeight = 40;
  13. static const unsigned kSingleLineFontSize = 20;
  14. extern "C" void UnityKeyboard_StatusChanged(int status);
  15. extern "C" void UnityKeyboard_TextChanged(NSString* text);
  16. extern "C" void UnityKeyboard_LayoutChanged(NSString* layout);
  17. @implementation KeyboardDelegate
  18. {
  19. // UI handling
  20. // in case of single line we use UITextField inside UIToolbar
  21. // in case of multi-line input we use UITextView with UIToolbar as accessory view
  22. // tvOS does not support multiline input thus only UITextField option is implemented
  23. // tvOS does not support UIToolbar so we rely on tvOS default processing
  24. #if PLATFORM_IOS || PLATFORM_VISIONOS
  25. UITextView* textView;
  26. UIToolbar* viewToolbar;
  27. UIToolbar* fieldToolbar;
  28. // toolbar items are kept around to prevent releasing them
  29. UIBarButtonItem *multiLineDone, *multiLineCancel;
  30. UIBarButtonItem *singleLineDone, *singleLineCancel, *singleLineInputField;
  31. NSLayoutConstraint* widthConstraint;
  32. int singleLineSystemButtonsSpace;
  33. #endif
  34. UITextField* textField;
  35. // inputView is view used for actual input (it will be responder): UITextField [single-line] or UITextView [multi-line]
  36. // editView is the "root" view for keyboard: UIToolbar [single-line] or UITextView [multi-line]
  37. UIView* inputView;
  38. UIView* editView;
  39. KeyboardShowParam cachedKeyboardParam;
  40. CGRect _area;
  41. NSString* initialText;
  42. UIKeyboardType keyboardType;
  43. BOOL _multiline;
  44. BOOL _inputHidden;
  45. BOOL _active;
  46. KeyboardStatus _status;
  47. int _characterLimit;
  48. // not pretty but seems like easiest way to keep "we are rotating" status
  49. BOOL _rotating;
  50. NSRange _hiddenSelection;
  51. NSRange _selectionRequest;
  52. // used for < iOS 14 external keyboard
  53. CGFloat _heightOfKeyboard;
  54. }
  55. @synthesize area;
  56. @synthesize active = _active;
  57. @synthesize status = _status;
  58. @synthesize text;
  59. @synthesize selection;
  60. @synthesize hasUsedDictation;
  61. - (void)setPendingSelectionRequest
  62. {
  63. if (_selectionRequest.location != NSNotFound)
  64. {
  65. _keyboard.selection = _selectionRequest;
  66. _selectionRequest.location = NSNotFound;
  67. }
  68. }
  69. - (BOOL)textFieldShouldReturn:(UITextField*)textFieldObj
  70. {
  71. [self textInputDone: nil];
  72. return YES;
  73. }
  74. #if PLATFORM_IOS || PLATFORM_VISIONOS
  75. - (void)textInputModeDidChange:(NSNotification*)notification
  76. {
  77. [self setPendingSelectionRequest];
  78. // Apple reports back the primary language of the current keyboard text input mode using BCP 47 language code i.e "en-GB"
  79. // but this also (undocumented) will return "dictation" when using voice dictation and "emoji" when using the emoji keyboard.
  80. if ([_keyboard->inputView.textInputMode.primaryLanguage isEqualToString: @"dictation"])
  81. {
  82. hasUsedDictation = YES;
  83. }
  84. }
  85. #endif
  86. - (void)textInputDone:(id)sender
  87. {
  88. if (_status == Visible)
  89. {
  90. _status = Done;
  91. UnityKeyboard_StatusChanged(_status);
  92. }
  93. [self hide];
  94. }
  95. - (void)becomeFirstResponder
  96. {
  97. if (_status == Visible)
  98. {
  99. [_keyboard->inputView becomeFirstResponder];
  100. }
  101. }
  102. - (void)textInputCancel:(id)sender
  103. {
  104. _status = Canceled;
  105. UnityKeyboard_StatusChanged(_status);
  106. [self hide];
  107. }
  108. - (void)textInputLostFocus
  109. {
  110. if (_status == Visible)
  111. {
  112. _status = LostFocus;
  113. UnityKeyboard_StatusChanged(_status);
  114. }
  115. [self hide];
  116. }
  117. - (void)textViewDidChange:(UITextView *)textView
  118. {
  119. UnityKeyboard_TextChanged(textView.text);
  120. }
  121. - (void)textFieldDidChange:(UITextField*)textField
  122. {
  123. UnityKeyboard_TextChanged(textField.text);
  124. }
  125. - (BOOL)textViewShouldBeginEditing:(UITextView*)view
  126. {
  127. #if !PLATFORM_TVOS && !PLATFORM_VISIONOS
  128. view.inputAccessoryView = viewToolbar;
  129. #endif
  130. return YES;
  131. }
  132. #if PLATFORM_IOS || PLATFORM_VISIONOS
  133. - (void)keyboardWillShow:(NSNotification *)notification
  134. {
  135. if (notification.userInfo == nil || inputView == nil)
  136. return;
  137. [self setPendingSelectionRequest];
  138. CGRect srcRect = [[notification.userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
  139. CGRect rect = [UnityGetGLView() convertRect: srcRect fromView: nil];
  140. [self positionInput: rect x: rect.origin.x y: rect.origin.y];
  141. }
  142. - (void)keyboardDidShow:(NSNotification*)notification
  143. {
  144. _active = YES;
  145. UnityKeyboard_LayoutChanged(textField.textInputMode.primaryLanguage);
  146. // We only need to do this in < iOS 14
  147. // Used in keyboardDidShow as keyboardWillShow might not have the height ready yet as it's not on screen and
  148. // we're only interested in the height when it's fully on screen.
  149. if (@available(iOS 14, tvOS 14, *)) {}
  150. else
  151. {
  152. CGRect srcRect = [[notification.userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
  153. CGRect rect = [UnityGetGLView() convertRect: srcRect fromView: nil];
  154. _heightOfKeyboard = rect.size.height;
  155. }
  156. }
  157. - (void)keyboardWillHide:(NSNotification*)notification
  158. {
  159. if (_keyboard)
  160. {
  161. // Reset selection to avoid selection graphics staying on the screen
  162. if (_keyboard.selection.length > 0)
  163. {
  164. NSRange range = NSMakeRange(_keyboard.text.length, 0);
  165. _keyboard.selection = range;
  166. }
  167. }
  168. UnityKeyboard_LayoutChanged(nil);
  169. [self systemHideKeyboard];
  170. }
  171. - (void)keyboardDidHide:(NSNotification*)notification
  172. {
  173. // The audio engine starts and restarts by listening to AVAudioSessionInterruptionNotifications, However
  174. // Apple does *not* guarantee that the AVAudioSessionInterruptionTypeEnded will be sent, especially if
  175. // the app is in the foreground - This can happen when using the dictate function on the keyboard
  176. // so we send the notification ourselves to ensure the audio restarts.
  177. if (hasUsedDictation)
  178. {
  179. [[NSNotificationCenter defaultCenter] postNotificationName: AVAudioSessionInterruptionNotification
  180. object: [AVAudioSession sharedInstance]
  181. userInfo: @{AVAudioSessionInterruptionTypeKey: [NSNumber numberWithUnsignedInteger: AVAudioSessionInterruptionTypeEnded]}];
  182. }
  183. }
  184. - (void)keyboardDidChangeFrame:(NSNotification*)notification
  185. {
  186. _active = true;
  187. CGRect srcRect = [[notification.userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
  188. CGRect rect = [UnityGetGLView() convertRect: srcRect fromView: nil];
  189. // there are several ways to hide keyboard:
  190. // one, using the hide button on the keyboard, will move it outside view
  191. // another, for ipad floating keyboard, will "minimize" it (making its height/width zero)
  192. // if input field is multiline we need to account for the toolbar height
  193. float expectedHeight = _multiline ? kToolBarHeight : 1e-6;
  194. if (rect.origin.y + expectedHeight >= [UnityGetGLView() bounds].size.height || rect.size.width < 1e-6)
  195. {
  196. [self systemHideKeyboard];
  197. }
  198. else
  199. {
  200. [self positionInput: rect x: rect.origin.x y: rect.origin.y];
  201. }
  202. }
  203. #endif
  204. + (void)Initialize
  205. {
  206. NSAssert(_keyboard == nil, @"[KeyboardDelegate Initialize] called after creating keyboard");
  207. if (!_keyboard)
  208. _keyboard = [[KeyboardDelegate alloc] init];
  209. }
  210. + (KeyboardDelegate*)Instance
  211. {
  212. if (!_keyboard)
  213. _keyboard = [[KeyboardDelegate alloc] init];
  214. return _keyboard;
  215. }
  216. + (void)Destroy
  217. {
  218. _keyboard = nil;
  219. }
  220. #if PLATFORM_IOS || PLATFORM_VISIONOS
  221. - (UIToolbar*)createToolbarWithItems:(NSArray*)items
  222. {
  223. UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 840, 320, kToolBarHeight)];
  224. UnitySetViewTouchProcessing(toolbar, touchesIgnored);
  225. toolbar.hidden = NO;
  226. toolbar.items = items;
  227. return toolbar;
  228. }
  229. - (void)createToolbars
  230. {
  231. multiLineDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target: self action: @selector(textInputDone:)];
  232. multiLineCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel target: self action: @selector(textInputCancel:)];
  233. viewToolbar = [self createToolbarWithItems: @[multiLineDone, multiLineCancel]];
  234. singleLineInputField = [[UIBarButtonItem alloc] initWithCustomView: textField];
  235. singleLineDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target: self action: @selector(textInputDone:)];
  236. singleLineCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel target: self action: @selector(textInputCancel:)];
  237. fieldToolbar = [self createToolbarWithItems: @[singleLineInputField, singleLineDone, singleLineCancel]];
  238. // Gather round boys, let's hear the story of apple ingenious api.
  239. // Did you see UIBarButtonItem above? oh the marvel of design
  240. // Maybe you thought it will have some connection to UIView or something?
  241. // Yes, internally, in private members, hidden like dirty laundry in a room of a youngster
  242. // But, you may ask, why do we care? Oh, easy - sometimes you want to use non-english language
  243. // And in these languages, not good enough to be english, done/cancel items can have different sizes
  244. // And we insist on having input field size set because, yes, we cannot quite do a layout inside UIToolbar
  245. // [because there are no views we can actually touch, thanks for asking]
  246. // Obviously, localizing system strings is also well hidden, and what works now might stop working tomorrow
  247. // That's why we keep UIBarButtonSystemItemDone/UIBarButtonSystemItemCancel above
  248. // and try to translate "Done"/"Cancel" in a way that "should" work
  249. // if localization fails we will still have "some" values (coming from english)
  250. // and while this wont work with, say, asian languages - it should not regress the current behaviour
  251. UIFont* font = [UIFont systemFontOfSize: kSingleLineFontSize];
  252. NSBundle* uikitBundle = [NSBundle bundleForClass: UIApplication.class];
  253. NSString* doneStr = [uikitBundle localizedStringForKey: @"Done" value: nil table: nil];
  254. NSString* cancelStr = [uikitBundle localizedStringForKey: @"Cancel" value: nil table: nil];
  255. // mind you, all of that is highly empirical.
  256. // we assume space between items to be 18 [both betwen buttons and on the sides]
  257. // we also assume that button width would be more less title width exactly (it should be quite close though)
  258. const int doneW = (int)[doneStr sizeWithAttributes: @{NSFontAttributeName: font}].width;
  259. const int cancelW = (int)[cancelStr sizeWithAttributes: @{NSFontAttributeName: font}].width;
  260. singleLineSystemButtonsSpace = doneW + cancelW + 3 * 18;
  261. }
  262. #endif
  263. - (id)init
  264. {
  265. NSAssert(_keyboard == nil, @"You can have only one instance of KeyboardDelegate");
  266. self = [super init];
  267. if (self)
  268. {
  269. #if PLATFORM_IOS || PLATFORM_VISIONOS
  270. textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 840, 480, 30)];
  271. textView.delegate = self;
  272. textView.font = [UIFont systemFontOfSize: 18.0];
  273. textView.hidden = YES;
  274. // For some unknown reason, the `textView` has visual issues when
  275. // using Dark Mode (some parts of the view become transparent). See case 1367091.
  276. // However, setting alpha to a value different than 1 fixes the issue.
  277. if (@available(iOS 13, *))
  278. textView.alpha = 0.99;
  279. #endif
  280. textField = [[UITextField alloc] initWithFrame: CGRectMake(0, 0, 120, 30)];
  281. textField.delegate = self;
  282. textField.borderStyle = UITextBorderStyleRoundedRect;
  283. textField.font = [UIFont systemFontOfSize: kSingleLineFontSize];
  284. textField.clearButtonMode = UITextFieldViewModeWhileEditing;
  285. #if PLATFORM_IOS || PLATFORM_VISIONOS
  286. widthConstraint = [NSLayoutConstraint constraintWithItem: textField attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 constant: textField.frame.size.width];
  287. [textField addConstraint: widthConstraint];
  288. #endif
  289. [textField addTarget: self action: @selector(textFieldDidChange:) forControlEvents: UIControlEventEditingChanged];
  290. #if PLATFORM_IOS || PLATFORM_VISIONOS
  291. [self createToolbars];
  292. #endif
  293. #if PLATFORM_IOS || PLATFORM_VISIONOS
  294. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object: nil];
  295. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardDidShow:) name: UIKeyboardDidShowNotification object: nil];
  296. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object: nil];
  297. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardDidHide:) name: UIKeyboardDidHideNotification object: nil];
  298. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardDidChangeFrame:) name: UIKeyboardDidChangeFrameNotification object: nil];
  299. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(textInputModeDidChange:) name: UITextInputCurrentInputModeDidChangeNotification object: nil];
  300. #endif
  301. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(textInputDone:) name: UITextFieldTextDidEndEditingNotification object: nil];
  302. }
  303. return self;
  304. }
  305. - (void)setTextInputTraits:(id<UITextInputTraits>)traits
  306. withParam:(KeyboardShowParam)param
  307. {
  308. UITextAutocapitalizationType capitalization = [KeyboardDelegate capitalizationForKeyboardParam: param];
  309. if (!_inputHidden)
  310. traits.secureTextEntry = param.secure;
  311. if (param.secure)
  312. {
  313. traits.autocorrectionType = UITextAutocorrectionTypeNo;
  314. traits.spellCheckingType = UITextSpellCheckingTypeNo;
  315. traits.autocapitalizationType = UITextAutocapitalizationTypeNone;
  316. }
  317. else
  318. {
  319. traits.autocorrectionType = param.autocorrectionType;
  320. traits.spellCheckingType = param.spellcheckingType;
  321. traits.autocapitalizationType = capitalization;
  322. }
  323. traits.keyboardType = param.keyboardType;
  324. traits.keyboardAppearance = param.appearance;
  325. }
  326. + (UITextAutocapitalizationType)capitalizationForKeyboardParam:(KeyboardShowParam)param
  327. {
  328. if (param.secure)
  329. return UITextAutocapitalizationTypeNone;
  330. UITextAutocapitalizationType capitalization;
  331. switch (param.keyboardType)
  332. {
  333. case UIKeyboardTypeURL:
  334. case UIKeyboardTypeEmailAddress:
  335. case UIKeyboardTypeWebSearch:
  336. capitalization = UITextAutocapitalizationTypeNone;
  337. break;
  338. default:
  339. capitalization = UITextAutocapitalizationTypeSentences;
  340. }
  341. return capitalization;
  342. }
  343. - (void)setKeyboardParams:(KeyboardShowParam)param
  344. {
  345. [NSObject cancelPreviousPerformRequestsWithTarget: self];
  346. if (cachedKeyboardParam.multiline != param.multiline ||
  347. cachedKeyboardParam.secure != param.secure ||
  348. cachedKeyboardParam.keyboardType != param.keyboardType ||
  349. cachedKeyboardParam.autocorrectionType != param.autocorrectionType ||
  350. cachedKeyboardParam.appearance != param.appearance)
  351. {
  352. [self hideUIDelayed];
  353. }
  354. cachedKeyboardParam = param;
  355. if (_active)
  356. [self hide];
  357. initialText = param.text ? [[NSString alloc] initWithUTF8String: param.text] : @"";
  358. _characterLimit = param.characterLimit;
  359. #if PLATFORM_IOS || PLATFORM_VISIONOS
  360. _multiline = param.multiline;
  361. if (_multiline)
  362. {
  363. [self setTextInputTraits: textView withParam: param];
  364. }
  365. else
  366. {
  367. if (param.oneTimeCode)
  368. textField.textContentType = UITextContentTypeOneTimeCode;
  369. [self setTextInputTraits: textField withParam: param];
  370. textField.placeholder = [NSString stringWithUTF8String: param.placeholder];
  371. }
  372. inputView = _multiline ? textView : textField;
  373. editView = _multiline ? textView : fieldToolbar;
  374. // Initially hide input fields in case external keyboard is connected.
  375. // This is needed for certain cases where external keyboard is connected
  376. // and soft keyboard is reopened without closing it first.
  377. // If external keyboard does not exist, these values will be updated by keyboardWillShow
  378. editView.hidden = YES;
  379. viewToolbar.hidden = YES;
  380. inputView.hidden = YES;
  381. #else // PLATFORM_TVOS
  382. [self setTextInputTraits: textField withParam: param];
  383. textField.placeholder = [NSString stringWithUTF8String: param.placeholder];
  384. inputView = textField;
  385. editView = textField;
  386. #endif
  387. [self shouldHideInput: _shouldHideInput];
  388. [KeyboardDelegate Instance].text = initialText;
  389. _status = Visible;
  390. UnityKeyboard_StatusChanged(_status);
  391. _active = YES;
  392. _selectionRequest.location = NSNotFound;
  393. }
  394. // we need to show/hide keyboard to react to orientation too, so extract we extract UI fiddling
  395. - (void)showUI
  396. {
  397. // if we unhide everything now the input will be shown smaller then needed quickly (and resized later)
  398. // so unhide only when keyboard is actually shown (we will update it when reacting to ios notifications)
  399. [NSObject cancelPreviousPerformRequestsWithTarget: self];
  400. if (!inputView.isFirstResponder)
  401. {
  402. editView.hidden = YES;
  403. [UnityGetGLView() addSubview: editView];
  404. [inputView becomeFirstResponder];
  405. }
  406. // we need to reload input views when switching the keyboard type for already active keyboard
  407. // otherwise the changed traits may not be immediately applied
  408. [inputView reloadInputViews];
  409. }
  410. - (void)hideUI
  411. {
  412. [NSObject cancelPreviousPerformRequestsWithTarget: self];
  413. [self performSelector: @selector(hideUIDelayed) withObject: nil afterDelay: 0.05]; // to avoid unnecessary hiding
  414. }
  415. - (void)hideUIDelayed
  416. {
  417. [inputView resignFirstResponder];
  418. [editView removeFromSuperview];
  419. editView.hidden = YES;
  420. // Keyboard notifications are not supported on tvOS so keyboardWillHide: will never be called which would set _active to false.
  421. // To work around that limitation we will update _active from here.
  422. #if PLATFORM_TVOS
  423. _active = editView.isFirstResponder;
  424. #endif
  425. }
  426. - (void)systemHideKeyboard
  427. {
  428. // when we are rotating os will bombard us with keyboardWillHide: and keyboardDidChangeFrame:
  429. // ignore all of them (we do it here only to simplify code: we call systemHideKeyboard only from these notification handlers)
  430. if (_rotating)
  431. return;
  432. _active = editView.isFirstResponder;
  433. editView.hidden = YES;
  434. #if PLATFORM_IOS || PLATFORM_VISIONOS
  435. viewToolbar.hidden = YES;
  436. #endif
  437. _area = CGRectMake(0, 0, 0, 0);
  438. }
  439. - (void)show
  440. {
  441. [self showUI];
  442. }
  443. - (void)hide
  444. {
  445. [self hideUI];
  446. }
  447. - (void)updateInputHidden
  448. {
  449. if (_shouldHideInputChanged)
  450. {
  451. [self shouldHideInput: _shouldHideInput];
  452. _shouldHideInputChanged = false;
  453. }
  454. textField.returnKeyType = _inputHidden ? UIReturnKeyDone : UIReturnKeyDefault;
  455. #if PLATFORM_IOS || PLATFORM_VISIONOS
  456. viewToolbar.hidden = !_multiline || _inputHidden ? YES : NO;
  457. #endif
  458. editView.hidden = _inputHidden ? YES : NO;
  459. inputView.hidden = _inputHidden ? YES : NO;
  460. [self setTextInputTraits: textField withParam: cachedKeyboardParam];
  461. }
  462. #if PLATFORM_IOS || PLATFORM_VISIONOS
  463. - (void)positionInput:(CGRect)kbRect x:(float)x y:(float)y
  464. {
  465. const float safeAreaInsetLeft = [UnityGetGLView() safeAreaInsets].left;
  466. const float safeAreaInsetRight = [UnityGetGLView() safeAreaInsets].right;
  467. if (_multiline)
  468. {
  469. // use smaller area for iphones and bigger one for ipads
  470. int height = UnityDeviceDPI() > 300 ? 75 : 100;
  471. editView.frame = CGRectMake(safeAreaInsetLeft, y - height, kbRect.size.width - safeAreaInsetLeft - safeAreaInsetRight, height);
  472. }
  473. else
  474. {
  475. editView.frame = CGRectMake(0, y - kToolBarHeight, kbRect.size.width, kToolBarHeight);
  476. // old constraint must be removed, changing value while constraint is active causes conflict when changing inputView.frame
  477. [inputView removeConstraint: widthConstraint];
  478. inputView.frame = CGRectMake(inputView.frame.origin.x,
  479. inputView.frame.origin.y,
  480. kbRect.size.width - safeAreaInsetLeft - safeAreaInsetRight - self->singleLineSystemButtonsSpace,
  481. inputView.frame.size.height);
  482. // required to avoid auto-resizing on iOS 11 in case if input text is too long
  483. widthConstraint.constant = inputView.frame.size.width;
  484. [inputView addConstraint: widthConstraint];
  485. }
  486. _area = CGRectMake(x, y, kbRect.size.width, kbRect.size.height);
  487. [self updateInputHidden];
  488. }
  489. #endif
  490. - (CGRect)queryArea
  491. {
  492. return editView.hidden ? _area : CGRectUnion(_area, editView.frame);
  493. }
  494. - (NSRange)querySelection
  495. {
  496. UIView<UITextInput>* textInput;
  497. #if PLATFORM_TVOS
  498. textInput = textField;
  499. #else
  500. textInput = _multiline ? textView : textField;
  501. #endif
  502. UITextPosition* beginning = textInput.beginningOfDocument;
  503. UITextRange* selectedRange = textInput.selectedTextRange;
  504. UITextPosition* selectionStart = selectedRange.start;
  505. UITextPosition* selectionEnd = selectedRange.end;
  506. const NSInteger location = [textInput offsetFromPosition: beginning toPosition: selectionStart];
  507. const NSInteger length = [textInput offsetFromPosition: selectionStart toPosition: selectionEnd];
  508. return NSMakeRange(location, length);
  509. }
  510. - (void)assignSelection:(NSRange)range
  511. {
  512. UIView<UITextInput>* textInput;
  513. #if PLATFORM_TVOS
  514. textInput = textField;
  515. #else
  516. textInput = _multiline ? textView : textField;
  517. #endif
  518. UITextPosition* begin = [textInput beginningOfDocument];
  519. UITextPosition* caret = [textInput positionFromPosition: begin offset: range.location];
  520. UITextPosition* select = [textInput positionFromPosition: caret offset: range.length];
  521. UITextRange* textRange = [textInput textRangeFromPosition: caret toPosition: select];
  522. [textInput setSelectedTextRange: textRange];
  523. if (_inputHidden)
  524. _hiddenSelection = range;
  525. _selectionRequest = range;
  526. }
  527. + (void)StartReorientation
  528. {
  529. if (_keyboard && _keyboard.active)
  530. _keyboard->_rotating = YES;
  531. }
  532. + (void)FinishReorientation
  533. {
  534. if (_keyboard)
  535. _keyboard->_rotating = NO;
  536. }
  537. - (NSString*)getText
  538. {
  539. if (_status == Canceled)
  540. return initialText;
  541. else
  542. {
  543. #if PLATFORM_TVOS
  544. return [textField text];
  545. #else
  546. return _multiline ? [textView text] : [textField text];
  547. #endif
  548. }
  549. }
  550. - (void)setText:(NSString*)newText
  551. {
  552. #if PLATFORM_IOS || PLATFORM_VISIONOS
  553. if (_multiline)
  554. textView.text = newText;
  555. else
  556. textField.text = newText;
  557. #else
  558. textField.text = newText;
  559. #endif
  560. // for hidden selection place cursor at the end when text changes
  561. _hiddenSelection.location = newText.length;
  562. _hiddenSelection.length = 0;
  563. }
  564. - (void)shouldHideInput:(BOOL)hide
  565. {
  566. if (hide)
  567. {
  568. switch (keyboardType)
  569. {
  570. case UIKeyboardTypeDefault: hide = YES; break;
  571. case UIKeyboardTypeASCIICapable: hide = YES; break;
  572. case UIKeyboardTypeNumbersAndPunctuation: hide = YES; break;
  573. case UIKeyboardTypeURL: hide = YES; break;
  574. case UIKeyboardTypeNumberPad: hide = NO; break;
  575. case UIKeyboardTypePhonePad: hide = NO; break;
  576. case UIKeyboardTypeNamePhonePad: hide = NO; break;
  577. case UIKeyboardTypeEmailAddress: hide = YES; break;
  578. case UIKeyboardTypeTwitter: hide = YES; break;
  579. case UIKeyboardTypeWebSearch: hide = YES; break;
  580. case UIKeyboardTypeDecimalPad: hide = NO; break;
  581. default: hide = NO; break;
  582. }
  583. }
  584. _inputHidden = hide;
  585. }
  586. - (BOOL)hasExternalKeyboard
  587. {
  588. // iOS 14 and above has a public API in the GameController framework. If this is missing then this will return false
  589. if (@available(iOS 14, tvOS 14, *))
  590. return [NSClassFromString(@"GCKeyboard") valueForKey: @"coalescedKeyboard"] != nil;
  591. else // The minimum height a software keyboard will be on iOS is 160, A bluetooth keyboard just uses a toolbar which will be smaller than this.
  592. return _heightOfKeyboard < 160.0f;
  593. }
  594. - (UITextField*)getTextField
  595. {
  596. return textField;
  597. }
  598. static bool StringContainsEmoji(NSString *string);
  599. - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string_
  600. {
  601. BOOL stringContainsEmoji = NO;
  602. #if FILTER_EMOJIS_IOS_KEYBOARD
  603. stringContainsEmoji = StringContainsEmoji(string_);
  604. #endif
  605. if (range.length + range.location > textField.text.length)
  606. return NO;
  607. return [self currentText: textField.text shouldChangeInRange: range replacementText: string_] && !stringContainsEmoji;
  608. }
  609. - (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text_
  610. {
  611. BOOL stringContainsEmoji = NO;
  612. #if FILTER_EMOJIS_IOS_KEYBOARD
  613. stringContainsEmoji = StringContainsEmoji(text_);
  614. #endif
  615. if (range.length + range.location > textView.text.length)
  616. return NO;
  617. return [self currentText: textView.text shouldChangeInRange: range replacementText: text_] && !stringContainsEmoji;
  618. }
  619. - (BOOL)currentText:(NSString*)currentText shouldChangeInRange:(NSRange)range replacementText:(NSString*)text_
  620. {
  621. NSUInteger newLength = currentText.length + (text_.length - range.length);
  622. if (newLength > _characterLimit && _characterLimit != 0 && newLength >= currentText.length)
  623. {
  624. // If the user inserts any emoji that exceeds the character limit it should quickly reject it, else it'll crash. We need to check regardless of FILTER_EMOJIS_IOS_KEYBOARD status as sometimes this method gets called before we've filtered out an emoji.
  625. if (StringContainsEmoji(text_))
  626. return NO;
  627. NSString* newReplacementText = @"";
  628. if ((currentText.length - range.length) < _characterLimit)
  629. newReplacementText = [text_ substringWithRange: NSMakeRange(0, _characterLimit - (currentText.length - range.length))];
  630. NSString* newText = [currentText stringByReplacingCharactersInRange: range withString: newReplacementText];
  631. #if PLATFORM_IOS || PLATFORM_VISIONOS
  632. if (_multiline)
  633. [textView setText: newText];
  634. else
  635. [textField setText: newText];
  636. #else
  637. [textField setText: newText];
  638. #endif
  639. // If we're trying to exceed the max length of the field BUT the text can merge into
  640. // precomposed characters then we should allow the input.
  641. NSString* precomposedNewText = [currentText precomposedStringWithCompatibilityMapping];
  642. __block int count = 0;
  643. [precomposedNewText enumerateSubstringsInRange: NSMakeRange(0, [precomposedNewText length]) options: NSStringEnumerationByComposedCharacterSequences
  644. usingBlock: ^(NSString *inSubstring, NSRange inSubstringRange, NSRange inEnclosingRange, BOOL *outStop) {
  645. count++;
  646. }];
  647. // count of characters of precomposed string will equal the character limit
  648. // if there has been characters merged bringing us under the limit.
  649. return count <= _characterLimit;
  650. }
  651. else
  652. {
  653. if (_inputHidden && _hiddenSelection.length > 0)
  654. {
  655. NSString* newText = [currentText stringByReplacingCharactersInRange: _hiddenSelection withString: text_];
  656. #if PLATFORM_IOS || PLATFORM_VISIONOS
  657. if (_multiline)
  658. [textView setText: newText];
  659. else
  660. [textField setText: newText];
  661. #else
  662. [textField setText: newText];
  663. #endif
  664. _hiddenSelection.location = _hiddenSelection.location + text_.length;
  665. _hiddenSelection.length = 0;
  666. self.selection = _hiddenSelection;
  667. return NO;
  668. }
  669. _hiddenSelection.location = range.location + text_.length;
  670. _hiddenSelection.length = 0;
  671. return YES;
  672. }
  673. }
  674. @end
  675. //==============================================================================
  676. //
  677. // Unity Interface:
  678. extern "C" void UnityKeyboard_Create(unsigned keyboardType, int autocorrection, int multiline, int secure, int alert, const char* text, const char* placeholder, int characterLimit)
  679. {
  680. #if PLATFORM_TVOS
  681. // Not supported. The API for showing keyboard for editing multi-line text is not available on tvOS
  682. multiline = false;
  683. #endif
  684. static const UIKeyboardType keyboardTypes[] =
  685. {
  686. UIKeyboardTypeDefault,
  687. UIKeyboardTypeASCIICapable,
  688. UIKeyboardTypeNumbersAndPunctuation,
  689. UIKeyboardTypeURL,
  690. UIKeyboardTypeNumberPad,
  691. UIKeyboardTypePhonePad,
  692. UIKeyboardTypeNamePhonePad,
  693. UIKeyboardTypeEmailAddress,
  694. UIKeyboardTypeDefault, // Default is used in case Wii U specific NintendoNetworkAccount type is selected (indexed at 8 in UnityEngine.TouchScreenKeyboardType)
  695. UIKeyboardTypeTwitter,
  696. UIKeyboardTypeWebSearch,
  697. UIKeyboardTypeDecimalPad
  698. };
  699. // on iOS 15, QuickType bar was decoupled from autocorrection (so it still shows candidates)
  700. // for a principle of "the least surprise" we keep it coupled internally, so autocorrection == spellchecking
  701. // TODO: should we expose it the control of it?
  702. static const UITextAutocorrectionType autocorrectionTypes[] =
  703. {
  704. UITextAutocorrectionTypeNo,
  705. UITextAutocorrectionTypeDefault,
  706. };
  707. static const UITextSpellCheckingType spellcheckingTypes[] =
  708. {
  709. UITextSpellCheckingTypeNo,
  710. UITextSpellCheckingTypeDefault,
  711. };
  712. static const UIKeyboardAppearance keyboardAppearances[] =
  713. {
  714. UIKeyboardAppearanceDefault,
  715. UIKeyboardAppearanceAlert,
  716. };
  717. // Note: TouchScreenKeyboard with value 12 is OneTimeCode and does not directly translate to a UIKeyboardType.
  718. // We show a number pad but change the content type so that codes can be autofilled when received in Messages.
  719. KeyboardShowParam param =
  720. {
  721. text, placeholder,
  722. keyboardTypes[keyboardType == 12 ? UIKeyboardTypeNumberPad : keyboardType],
  723. autocorrectionTypes[autocorrection],
  724. spellcheckingTypes[autocorrection],
  725. keyboardAppearances[alert],
  726. (BOOL)multiline, (BOOL)secure,
  727. characterLimit,
  728. keyboardType == 12
  729. };
  730. [[KeyboardDelegate Instance] setKeyboardParams: param];
  731. }
  732. extern "C" void UnityKeyboard_Show()
  733. {
  734. // do not send hide if didnt create keyboard
  735. // TODO: probably assert?
  736. if (!_keyboard)
  737. return;
  738. [[KeyboardDelegate Instance] show];
  739. }
  740. extern "C" void UnityKeyboard_Hide()
  741. {
  742. // do not send hide if didnt create keyboard
  743. // TODO: probably assert?
  744. if (!_keyboard)
  745. return;
  746. [[KeyboardDelegate Instance] textInputLostFocus];
  747. }
  748. extern "C" void UnityKeyboard_SetText(const char* text)
  749. {
  750. [KeyboardDelegate Instance].text = [NSString stringWithUTF8String: text];
  751. }
  752. extern "C" NSString* UnityKeyboard_GetText()
  753. {
  754. return [KeyboardDelegate Instance].text;
  755. }
  756. extern "C" int UnityKeyboard_IsActive()
  757. {
  758. return (_keyboard && _keyboard.active) ? 1 : 0;
  759. }
  760. extern "C" int UnityKeyboard_Status()
  761. {
  762. return _keyboard ? _keyboard.status : Canceled;
  763. }
  764. extern "C" void UnityKeyboard_SetInputHidden(int hidden)
  765. {
  766. _shouldHideInput = hidden;
  767. _shouldHideInputChanged = true;
  768. // update hidden status only if keyboard is on screen to avoid showing input view out of nowhere
  769. if (_keyboard && _keyboard.active)
  770. [_keyboard updateInputHidden];
  771. }
  772. extern "C" int UnityKeyboard_IsInputHidden()
  773. {
  774. return _shouldHideInput ? 1 : 0;
  775. }
  776. extern "C" void UnityKeyboard_GetRect(float* x, float* y, float* w, float* h)
  777. {
  778. CGRect area = _keyboard ? _keyboard.area : CGRectMake(0, 0, 0, 0);
  779. // convert to unity coord system
  780. float multX = (float)GetMainDisplaySurface()->targetW / UnityGetGLView().bounds.size.width;
  781. float multY = (float)GetMainDisplaySurface()->targetH / UnityGetGLView().bounds.size.height;
  782. *x = 0;
  783. *y = area.origin.y * multY;
  784. *w = area.size.width * multX;
  785. *h = area.size.height * multY;
  786. }
  787. extern "C" void UnityKeyboard_SetCharacterLimit(unsigned characterLimit)
  788. {
  789. [KeyboardDelegate Instance].characterLimit = characterLimit;
  790. }
  791. extern "C" int UnityKeyboard_CanGetSelection()
  792. {
  793. return (_keyboard) ? 1 : 0;
  794. }
  795. extern "C" void UnityKeyboard_GetSelection(int* location, int* length)
  796. {
  797. if (_keyboard)
  798. {
  799. NSRange selection = _keyboard.selection;
  800. *location = (int)selection.location;
  801. *length = (int)selection.length;
  802. }
  803. else
  804. {
  805. *location = 0;
  806. *length = 0;
  807. }
  808. }
  809. extern "C" int UnityKeyboard_CanSetSelection()
  810. {
  811. return (_keyboard) ? 1 : 0;
  812. }
  813. extern "C" void UnityKeyboard_SetSelection(int location, int length)
  814. {
  815. if (_keyboard)
  816. {
  817. _keyboard.selection = NSMakeRange(location, length);
  818. }
  819. }
  820. //==============================================================================
  821. //
  822. // Emoji Filtering: unicode magic
  823. static bool StringContainsEmoji(NSString *string)
  824. {
  825. __block BOOL returnValue = NO;
  826. [string enumerateSubstringsInRange: NSMakeRange(0, string.length)
  827. options: NSStringEnumerationByComposedCharacterSequences
  828. usingBlock:^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop)
  829. {
  830. const unichar hs = [substring characterAtIndex: 0];
  831. const unichar ls = substring.length > 1 ? [substring characterAtIndex: 1] : 0;
  832. #define IS_IN(val, min, max) (((val) >= (min)) && ((val) <= (max)))
  833. if (IS_IN(hs, 0xD800, 0xDBFF))
  834. {
  835. if (substring.length > 1)
  836. {
  837. const int uc = ((hs - 0xD800) * 0x400) + (ls - 0xDC00) + 0x10000;
  838. // Musical: [U+1D000, U+1D24F]
  839. // Enclosed Alphanumeric Supplement: [U+1F100, U+1F1FF]
  840. // Enclosed Ideographic Supplement: [U+1F200, U+1F2FF]
  841. // Miscellaneous Symbols and Pictographs: [U+1F300, U+1F5FF]
  842. // Supplemental Symbols and Pictographs: [U+1F900, U+1F9FF]
  843. // Emoticons: [U+1F600, U+1F64F]
  844. // Transport and Map Symbols: [U+1F680, U+1F6FF]
  845. if (IS_IN(uc, 0x1D000, 0x1F9FF))
  846. returnValue = YES;
  847. }
  848. }
  849. else if (substring.length > 1 && ls == 0x20E3)
  850. {
  851. // emojis for numbers: number + modifier ls = U+20E3
  852. returnValue = YES;
  853. }
  854. else
  855. {
  856. if ( // Latin-1 Supplement
  857. hs == 0x00A9 || hs == 0x00AE
  858. // General Punctuation
  859. || hs == 0x203C || hs == 0x2049
  860. // Letterlike Symbols
  861. || hs == 0x2122 || hs == 0x2139
  862. // Arrows
  863. || IS_IN(hs, 0x2194, 0x2199) || IS_IN(hs, 0x21A9, 0x21AA)
  864. // Miscellaneous Technical
  865. || IS_IN(hs, 0x231A, 0x231B) || IS_IN(hs, 0x23E9, 0x23F3) || IS_IN(hs, 0x23F8, 0x23FA) || hs == 0x2328 || hs == 0x23CF
  866. // Geometric Shapes
  867. || IS_IN(hs, 0x25AA, 0x25AB) || IS_IN(hs, 0x25FB, 0x25FE) || hs == 0x25B6 || hs == 0x25C0
  868. // Miscellaneous Symbols
  869. || IS_IN(hs, 0x2600, 0x2604) || IS_IN(hs, 0x2614, 0x2615) || IS_IN(hs, 0x2622, 0x2623) || IS_IN(hs, 0x262E, 0x262F)
  870. || IS_IN(hs, 0x2638, 0x263A) || IS_IN(hs, 0x2648, 0x2653) || IS_IN(hs, 0x2665, 0x2666) || IS_IN(hs, 0x2692, 0x2694)
  871. || IS_IN(hs, 0x2696, 0x2697) || IS_IN(hs, 0x269B, 0x269C) || IS_IN(hs, 0x26A0, 0x26A1) || IS_IN(hs, 0x26AA, 0x26AB)
  872. || IS_IN(hs, 0x26B0, 0x26B1) || IS_IN(hs, 0x26BD, 0x26BE) || IS_IN(hs, 0x26C4, 0x26C5) || IS_IN(hs, 0x26CE, 0x26CF)
  873. || IS_IN(hs, 0x26D3, 0x26D4) || IS_IN(hs, 0x26D3, 0x26D4) || IS_IN(hs, 0x26E9, 0x26EA) || IS_IN(hs, 0x26F0, 0x26F5)
  874. || IS_IN(hs, 0x26F7, 0x26FA)
  875. || hs == 0x260E || hs == 0x2611 || hs == 0x2618 || hs == 0x261D || hs == 0x2620 || hs == 0x2626 || hs == 0x262A
  876. || hs == 0x2660 || hs == 0x2663 || hs == 0x2668 || hs == 0x267B || hs == 0x267F || hs == 0x2699 || hs == 0x26C8
  877. || hs == 0x26D1 || hs == 0x26FD
  878. // Dingbats
  879. || IS_IN(hs, 0x2708, 0x270D) || IS_IN(hs, 0x2733, 0x2734) || IS_IN(hs, 0x2753, 0x2755)
  880. || IS_IN(hs, 0x2763, 0x2764) || IS_IN(hs, 0x2795, 0x2797)
  881. || hs == 0x2702 || hs == 0x2705 || hs == 0x270F || hs == 0x2712 || hs == 0x2714 || hs == 0x2716 || hs == 0x271D
  882. || hs == 0x2721 || hs == 0x2728 || hs == 0x2744 || hs == 0x2747 || hs == 0x274C || hs == 0x274E || hs == 0x2757
  883. || hs == 0x27A1 || hs == 0x27B0 || hs == 0x27BF
  884. // CJK Symbols and Punctuation
  885. || hs == 0x3030 || hs == 0x303D
  886. // Enclosed CJK Letters and Months
  887. || hs == 0x3297 || hs == 0x3299
  888. // Supplemental Arrows-B
  889. || IS_IN(hs, 0x2934, 0x2935)
  890. // Miscellaneous Symbols and Arrows
  891. || IS_IN(hs, 0x2B05, 0x2B07) || IS_IN(hs, 0x2B1B, 0x2B1C) || hs == 0x2B50 || hs == 0x2B55
  892. )
  893. {
  894. returnValue = YES;
  895. }
  896. }
  897. #undef IS_IN
  898. }];
  899. return returnValue;
  900. }