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";
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];
}
......@@ -649,6 +656,17 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
[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.
- (void)paste:(id)sender {
id delegate = [self delegate];
......@@ -668,7 +686,7 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
#pragma mark Key Commands
- (NSArray<UIKeyCommand*>*)keyCommands {
- (NSArray<UIKeyCommand*>*)upDownCommands {
// 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 the text. Instead, the omnibox popup needs to highlight suggestions.
......@@ -684,6 +702,15 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
return @[ commandUp, commandDown ];
}
- (NSArray<UIKeyCommand*>*)keyCommands {
NSMutableArray<UIKeyCommand*>* commands = [[self upDownCommands] mutableCopy];
if ([self isPreEditing]) {
[commands addObjectsFromArray:[self leftRightCommands]];
}
return commands;
}
- (void)keyCommandUp {
[self.suggestionCommandsEndpoint highlightNextSuggestion];
}
......@@ -692,6 +719,40 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
[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
// Gets the bounds of the rect covering the URL.
......
......@@ -609,24 +609,31 @@ void OmniboxViewIOS::OnClear() {
bool OmniboxViewIOS::OnCopy() {
UIPasteboard* board = [UIPasteboard generalPasteboard];
UITextRange* selected_range = [field_ selectedTextRange];
base::string16 text =
base::SysNSStringToUTF16([field_ textInRange:selected_range]);
UITextPosition* start = [field_ beginningOfDocument];
UITextPosition* end = [field_ endOfDocument];
BOOL is_select_all = ([field_ comparePosition:[selected_range start]
toPosition:start] == NSOrderedSame) &&
([field_ comparePosition:[selected_range end]
toPosition:end] == NSOrderedSame);
// The following call to |-offsetFromPosition:toPosition:| gives the offset 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.
NSInteger start_location =
[field_ offsetFromPosition:start toPosition:[selected_range start]];
NSString* selectedText = nil;
BOOL is_select_all = NO;
NSInteger start_location = 0;
if ([field_ isPreEditing]) {
selectedText = [field_ preEditText];
is_select_all = YES;
start_location = 0;
} else {
UITextRange* selected_range = [field_ selectedTextRange];
selectedText = [field_ textInRange:selected_range];
UITextPosition* start = [field_ beginningOfDocument];
UITextPosition* end = [field_ endOfDocument];
is_select_all = ([field_ comparePosition:[selected_range start]
toPosition:start] == NSOrderedSame) &&
([field_ comparePosition:[selected_range end]
toPosition:end] == NSOrderedSame);
// The following call to |-offsetFromPosition:toPosition:| gives the offset
// 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;
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