Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove UIPasteboard calls from Hakawai #161

Merged
merged 1 commit into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Hakawai/Core/AbstractionLayer/HKWAbstractionLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
@property (nonatomic) BOOL shouldIgnoreNextCharacterDeletion;

- (void)textViewDidProgrammaticallyUpdate;
- (BOOL)textViewShouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (BOOL)textViewShouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text wasPaste:(BOOL)wasPaste;
- (void)textViewDidChangeSelection;
- (void)textViewDidChange;

Expand Down
16 changes: 3 additions & 13 deletions Hakawai/Core/AbstractionLayer/HKWAbstractionLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ - (void)textViewDidProgrammaticallyUpdate {
}
}

- (BOOL)textViewShouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- (BOOL)textViewShouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text wasPaste:(BOOL)wasPaste {
// NSLog(@"textViewShouldChangeTextInRange:... called. Replacement: '%@', location: %ld, length: %ld; marked text: %@; text view text: '%@'",
// text, range.location, range.length, self.parentTextView.markedTextRange, self.parentTextView.text);
// NSLog(@" > The current input mode is %@", [HKWAbstractionLayer nameForMode:self.inputMode]);
Expand Down Expand Up @@ -250,12 +250,7 @@ - (BOOL)textViewShouldChangeTextInRange:(NSRange)range replacementText:(NSString
self.changeType = HKWAbstractionLayerChangeTypeDeletion;
self.changeRange = range;
self.changeString = [parentTextView.text substringWithRange:range];
NSString *const pasteboardString = [[UIPasteboard generalPasteboard] string];
if (pasteboardString) {
self.changeIsPaste = [text isEqualToString:pasteboardString];
} else {
self.changeIsPaste = NO;
}
self.changeIsPaste = wasPaste;
}
else if ([text length] > 0 && range.length == 0) {
// User inserted text
Expand All @@ -270,12 +265,7 @@ - (BOOL)textViewShouldChangeTextInRange:(NSRange)range replacementText:(NSString
self.changeType = HKWAbstractionLayerChangeTypeReplacement;
self.changeRange = range;
self.changeString = text;
NSString *const pasteboardString = [[UIPasteboard generalPasteboard] string];
if (pasteboardString) {
self.changeIsPaste = [text isEqualToString:pasteboardString];
} else {
self.changeIsPaste = NO;
}
self.changeIsPaste = wasPaste;
}
else {
// No change (e.g. user tapped backspace when there was no text)
Expand Down
21 changes: 7 additions & 14 deletions Hakawai/Core/HKWTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ @interface HKWTextView () <UITextViewDelegate, HKWAbstractionLayerDelegate, NSTe

@property (nonatomic, strong, readwrite) NSString *textStateBeforeDeletion;

@property (nonatomic, readwrite) BOOL wasPaste;

@end

static BOOL enableExperimentalDeadLockFix = NO;
Expand Down Expand Up @@ -226,6 +228,7 @@ - (NSLayoutConstraint *)translatedConstraintFor:(NSLayoutConstraint *)constraint

- (void)paste:(id)sender {
[super paste:sender];
self.wasPaste = YES;
__strong __auto_type externalDelegate = self.externalDelegate;
if ([externalDelegate respondsToSelector:@selector(textViewDidHaveTextPastedIn:)]) {
[externalDelegate textViewDidHaveTextPastedIn:self];
Expand Down Expand Up @@ -357,23 +360,13 @@ - (BOOL)shouldChangeTextInRange:(NSRange)range
textView:(UITextView *)textView {
// Note that the abstraction layer overrides all other behavior
if (self.abstractionLayerEnabled) {
return [self.abstractionLayer textViewShouldChangeTextInRange:range replacementText:replacementText];
return [self.abstractionLayer textViewShouldChangeTextInRange:range replacementText:replacementText wasPaste:self.wasPaste];
}

if (!isDictationText && self.shouldRejectAutocorrectInsertions && [replacementText length] > 1) {
NSString *const pasteboardString = [[UIPasteboard generalPasteboard] string];
if (!pasteboardString) {
return NO;
}
if (![replacementText isEqualToString:pasteboardString]) {
// PROVISIONAL FIX
// We need some way to distinguish autocorrect insertions from pasting in text. Since currently the only way
// that multiple characters can be inserted at a time is through pasting text from the pasteboard, we can check
// the text in the pasteboard against the string to be inserted to determine whether or not the request is
// coming from the autocorrect module
return NO;
}
if (!isDictationText && self.shouldRejectAutocorrectInsertions && [replacementText length] > 1 && !self.wasPaste) {
return NO;
}
self.wasPaste = NO;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this guaranteed to be invoked everytime after a paste action finishes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a pasting of text will trigger a text update, which will trigger this call

if (self.firstResponderIsCycling) {
return NO;
}
Expand Down