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

[iOS] Support keyboard commands in pre-edit.

Allows using Cmd+C/Cmd+V/Cmd+X when the textfield is in pre-edit state.
Allows using arrow-left and arrow-right keys to go to the start/end of
the string and exit pre-edit, like on desktop.

Bug: 136291
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ibe71f4d2b6adba3df310051edf4ffa42832217c2
Reviewed-on: https://chromium-review.googlesource.com/951783
Commit-Queue: Stepan Khapugin <stkhapugin@chromium.org>
Reviewed-by: default avatarJustin Cohen <justincohen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543341}
parent d48c8369
...@@ -628,6 +628,13 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation"; ...@@ -628,6 +628,13 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
return NO; return NO;
} }
if ([self isPreEditing]) {
// Allow cut/copy/paste in preedit.
if ((action == @selector(copy:)) || (action == @selector(cut:))) {
return YES;
}
}
return [super canPerformAction:action withSender:sender]; return [super canPerformAction:action withSender:sender];
} }
...@@ -649,6 +656,17 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation"; ...@@ -649,6 +656,17 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
[super copy:sender]; [super copy:sender];
} }
- (void)cut:(id)sender {
if ([self isPreEditing]) {
[self copy:sender];
[self exitPreEditState];
NSAttributedString* emptyString = [[NSAttributedString alloc] init];
[self setText:emptyString userTextLength:0];
} else {
[super cut:sender];
}
}
// Overridden to notify the delegate that a paste is in progress. // Overridden to notify the delegate that a paste is in progress.
- (void)paste:(id)sender { - (void)paste:(id)sender {
id delegate = [self delegate]; id delegate = [self delegate];
...@@ -668,7 +686,7 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation"; ...@@ -668,7 +686,7 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
#pragma mark Key Commands #pragma mark Key Commands
- (NSArray<UIKeyCommand*>*)keyCommands { - (NSArray<UIKeyCommand*>*)upDownCommands {
// These up/down arrow key commands override the standard UITextInput handling // These up/down arrow key commands override the standard UITextInput handling
// of up/down arrow key. The standard behavior is to go to the beginning/end // of up/down arrow key. The standard behavior is to go to the beginning/end
// of the text. Instead, the omnibox popup needs to highlight suggestions. // of the text. Instead, the omnibox popup needs to highlight suggestions.
...@@ -684,6 +702,15 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation"; ...@@ -684,6 +702,15 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
return @[ commandUp, commandDown ]; return @[ commandUp, commandDown ];
} }
- (NSArray<UIKeyCommand*>*)keyCommands {
NSMutableArray<UIKeyCommand*>* commands = [[self upDownCommands] mutableCopy];
if ([self isPreEditing]) {
[commands addObjectsFromArray:[self leftRightCommands]];
}
return commands;
}
- (void)keyCommandUp { - (void)keyCommandUp {
[self.suggestionCommandsEndpoint highlightNextSuggestion]; [self.suggestionCommandsEndpoint highlightNextSuggestion];
} }
...@@ -692,6 +719,40 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation"; ...@@ -692,6 +719,40 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
[self.suggestionCommandsEndpoint highlightPreviousSuggestion]; [self.suggestionCommandsEndpoint highlightPreviousSuggestion];
} }
#pragma mark preedit-only key commands
// React to left and right keys when in preedit state to exit preedit and put
// cursor to the beginning/end of the textfield.
- (NSArray<UIKeyCommand*>*)leftRightCommands {
UIKeyCommand* commandLeft =
[UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow
modifierFlags:0
action:@selector(keyCommandLeft)];
UIKeyCommand* commandRight =
[UIKeyCommand keyCommandWithInput:UIKeyInputRightArrow
modifierFlags:0
action:@selector(keyCommandRight)];
return @[ commandLeft, commandRight ];
}
- (void)keyCommandLeft {
[self exitPreEditState];
UITextPosition* beginning = self.beginningOfDocument;
UITextRange* textRange =
[self textRangeFromPosition:beginning toPosition:beginning];
self.selectedTextRange = textRange;
}
- (void)keyCommandRight {
[self exitPreEditState];
UITextPosition* end = self.endOfDocument;
UITextRange* textRange = [self textRangeFromPosition:end toPosition:end];
self.selectedTextRange = textRange;
}
#pragma mark - helpers #pragma mark - helpers
// Gets the bounds of the rect covering the URL. // Gets the bounds of the rect covering the URL.
......
...@@ -609,24 +609,31 @@ void OmniboxViewIOS::OnClear() { ...@@ -609,24 +609,31 @@ void OmniboxViewIOS::OnClear() {
bool OmniboxViewIOS::OnCopy() { bool OmniboxViewIOS::OnCopy() {
UIPasteboard* board = [UIPasteboard generalPasteboard]; UIPasteboard* board = [UIPasteboard generalPasteboard];
UITextRange* selected_range = [field_ selectedTextRange]; NSString* selectedText = nil;
base::string16 text = BOOL is_select_all = NO;
base::SysNSStringToUTF16([field_ textInRange:selected_range]); NSInteger start_location = 0;
if ([field_ isPreEditing]) {
UITextPosition* start = [field_ beginningOfDocument]; selectedText = [field_ preEditText];
UITextPosition* end = [field_ endOfDocument]; is_select_all = YES;
BOOL is_select_all = ([field_ comparePosition:[selected_range start] start_location = 0;
toPosition:start] == NSOrderedSame) && } else {
([field_ comparePosition:[selected_range end] UITextRange* selected_range = [field_ selectedTextRange];
toPosition:end] == NSOrderedSame); selectedText = [field_ textInRange:selected_range];
UITextPosition* start = [field_ beginningOfDocument];
// The following call to |-offsetFromPosition:toPosition:| gives the offset in UITextPosition* end = [field_ endOfDocument];
// terms of the number of "visible characters." The documentation does not is_select_all = ([field_ comparePosition:[selected_range start]
// specify whether this means glyphs or UTF16 chars. This does not matter for toPosition:start] == NSOrderedSame) &&
// the current implementation of AdjustTextForCopy(), but it may become an ([field_ comparePosition:[selected_range end]
// issue at some point. toPosition:end] == NSOrderedSame);
NSInteger start_location = // The following call to |-offsetFromPosition:toPosition:| gives the offset
[field_ offsetFromPosition:start toPosition:[selected_range start]]; // in terms of the number of "visible characters." The documentation does
// not specify whether this means glyphs or UTF16 chars. This does not
// matter for the current implementation of AdjustTextForCopy(), but it may
// become an issue at some point.
start_location =
[field_ offsetFromPosition:start toPosition:[selected_range start]];
}
base::string16 text = base::SysNSStringToUTF16(selectedText);
GURL url; GURL url;
bool write_url = false; bool write_url = false;
......
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