Commit dcc70767 authored by stkhapugin@chromium.org's avatar stkhapugin@chromium.org Committed by Commit Bot

[iOS] Support arrow keys when displaying inline autocomplete.

Adds support for arrows left/right when displaying inline autocomplete.
Arrow right accepts the input and moves the cursor to the end of the
textfield; arrow left accepts the input and keeps the cursor in the
same position - mirroring what desktop does.

Bug: 822307
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I58608e6cb41a904ffb12890e238a4ca000cf5b6f
Reviewed-on: https://chromium-review.googlesource.com/973525
Commit-Queue: Stepan Khapugin <stkhapugin@chromium.org>
Reviewed-by: default avatarJustin Cohen <justincohen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#545082}
parent 798a1075
...@@ -704,7 +704,7 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation"; ...@@ -704,7 +704,7 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
- (NSArray<UIKeyCommand*>*)keyCommands { - (NSArray<UIKeyCommand*>*)keyCommands {
NSMutableArray<UIKeyCommand*>* commands = [[self upDownCommands] mutableCopy]; NSMutableArray<UIKeyCommand*>* commands = [[self upDownCommands] mutableCopy];
if ([self isPreEditing]) { if ([self isPreEditing] || [self hasAutocompleteText]) {
[commands addObjectsFromArray:[self leftRightCommands]]; [commands addObjectsFromArray:[self leftRightCommands]];
} }
...@@ -719,10 +719,12 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation"; ...@@ -719,10 +719,12 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
[self.suggestionCommandsEndpoint highlightPreviousSuggestion]; [self.suggestionCommandsEndpoint highlightPreviousSuggestion];
} }
#pragma mark preedit-only key commands #pragma mark preedit and inline autocomplete key commands
// React to left and right keys when in preedit state to exit preedit and put // React to left and right keys when in preedit state to exit preedit and put
// cursor to the beginning/end of the textfield. // cursor to the beginning/end of the textfield; or if there is inline
// suggestion displayed, accept it and put the cursor before/after the
// suggested text.
- (NSArray<UIKeyCommand*>*)leftRightCommands { - (NSArray<UIKeyCommand*>*)leftRightCommands {
UIKeyCommand* commandLeft = UIKeyCommand* commandLeft =
[UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow [UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow
...@@ -737,22 +739,57 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation"; ...@@ -737,22 +739,57 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
} }
- (void)keyCommandLeft { - (void)keyCommandLeft {
[self exitPreEditState]; DCHECK([self isPreEditing] || [self hasAutocompleteText]);
// Cursor offset.
NSInteger offset = 0;
if ([self isPreEditing]) {
[self exitPreEditState];
}
if ([self hasAutocompleteText]) {
// The cursor should stay in the end of the user input.
offset = self.text.length;
// Accept autocomplete suggestion.
[self acceptAutocompleteText];
}
UITextPosition* beginning = self.beginningOfDocument; UITextPosition* beginning = self.beginningOfDocument;
UITextPosition* cursorPosition =
[self positionFromPosition:beginning offset:offset];
UITextRange* textRange = UITextRange* textRange =
[self textRangeFromPosition:beginning toPosition:beginning]; [self textRangeFromPosition:cursorPosition toPosition:cursorPosition];
self.selectedTextRange = textRange; self.selectedTextRange = textRange;
} }
- (void)keyCommandRight { - (void)keyCommandRight {
[self exitPreEditState]; DCHECK([self isPreEditing] || [self hasAutocompleteText]);
if ([self isPreEditing]) {
[self exitPreEditState];
}
if ([self hasAutocompleteText]) {
[self acceptAutocompleteText];
}
// Put the cursor to the end of the input.
UITextPosition* end = self.endOfDocument; UITextPosition* end = self.endOfDocument;
UITextRange* textRange = [self textRangeFromPosition:end toPosition:end]; UITextRange* textRange = [self textRangeFromPosition:end toPosition:end];
self.selectedTextRange = textRange; self.selectedTextRange = textRange;
} }
// A helper to accept the current autocomplete text.
- (void)acceptAutocompleteText {
DCHECK([self hasAutocompleteText]);
// Strip attributes and set text as if the user typed it.
NSAttributedString* string =
[[NSAttributedString alloc] initWithString:_selection.text];
[self setText:string userTextLength:string.length];
}
#pragma mark - helpers #pragma mark - helpers
// Gets the bounds of the rect covering the URL. // Gets the bounds of the rect covering the URL.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment