client_keyboard.mm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #if !defined(__has_feature) || !__has_feature(objc_arc)
  5. #error "This file requires ARC support."
  6. #endif
  7. #import "remoting/ios/client_keyboard.h"
  8. #include "remoting/client/input/keycode_map.h"
  9. // TODO(nicholss): Look into inputAccessoryView to get the top bar for sending
  10. // special keys.
  11. // TODO(nicholss): Look into inputView - The custom input view to display when
  12. // the receiver becomes the first responder
  13. @interface ClientKeyboard () {
  14. UIView* _inputView;
  15. }
  16. @end
  17. @implementation ClientKeyboard
  18. @synthesize autocapitalizationType = _autocapitalizationType;
  19. @synthesize autocorrectionType = _autocorrectionType;
  20. @synthesize keyboardAppearance = _keyboardAppearance;
  21. @synthesize keyboardType = _keyboardType;
  22. @synthesize spellCheckingType = _spellCheckingType;
  23. @synthesize selectedTextRange = _selectedTextRange;
  24. @synthesize delegate = _delegate;
  25. // TODO(nicholss): For physical keyboard, look at UIKeyCommand
  26. // https://developer.apple.com/reference/uikit/uikeycommand?language=objc
  27. - (instancetype)init {
  28. self = [super init];
  29. if (self) {
  30. _autocapitalizationType = UITextAutocapitalizationTypeNone;
  31. _autocorrectionType = UITextAutocorrectionTypeNo;
  32. _keyboardAppearance = UIKeyboardAppearanceDefault;
  33. _keyboardType = UIKeyboardTypeASCIICapable;
  34. _spellCheckingType = UITextSpellCheckingTypeNo;
  35. self.showsSoftKeyboard = NO;
  36. }
  37. return self;
  38. }
  39. #pragma mark - UIKeyInput
  40. - (void)insertText:(NSString*)text {
  41. if (text.length == 1) {
  42. // TODO(yuweih): KeyboardLayout should be configurable.
  43. remoting::KeypressInfo keypress =
  44. remoting::KeypressFromUnicode([text characterAtIndex:0]);
  45. if (keypress.dom_code != ui::DomCode::NONE) {
  46. [_delegate clientKeyboardShouldSendKey:keypress];
  47. return;
  48. }
  49. }
  50. // Fallback to text injection.
  51. [_delegate clientKeyboardShouldSend:text];
  52. }
  53. - (void)deleteBackward {
  54. [_delegate clientKeyboardShouldDelete];
  55. }
  56. - (BOOL)hasText {
  57. return NO;
  58. }
  59. #pragma mark - UIResponder
  60. - (BOOL)canBecomeFirstResponder {
  61. return YES;
  62. }
  63. - (BOOL)resignFirstResponder {
  64. if (self.showsSoftKeyboard) {
  65. // This translates the action of resigning first responder when the keyboard
  66. // is showing into hiding the soft keyboard while keeping the view first
  67. // responder. This is to allow the hide keyboard button on the soft keyboard
  68. // to work properly with ClientKeyboard's soft keyboard logic, which calls
  69. // resignFirstResponder.
  70. // This may cause weird behavior if the superview has multiple responders
  71. // (text views).
  72. self.showsSoftKeyboard = NO;
  73. return NO;
  74. }
  75. return [super resignFirstResponder];
  76. }
  77. - (UIView*)inputAccessoryView {
  78. return nil;
  79. }
  80. - (UIView*)inputView {
  81. return _inputView;
  82. }
  83. #pragma mark - UITextInputTraits
  84. #pragma mark - Properties
  85. - (void)setShowsSoftKeyboard:(BOOL)showsSoftKeyboard {
  86. if (self.showsSoftKeyboard == showsSoftKeyboard) {
  87. return;
  88. }
  89. // Returning nil for inputView will fallback to the system soft keyboard.
  90. // Returning an empty view will effectively hide it.
  91. _inputView =
  92. showsSoftKeyboard ? nil : [[UIView alloc] initWithFrame:CGRectZero];
  93. [self reloadInputViews];
  94. }
  95. - (BOOL)showsSoftKeyboard {
  96. return _inputView == nil;
  97. }
  98. @end